Skip to content

Instantly share code, notes, and snippets.

@hkfoster
Last active May 15, 2019 11:56
Show Gist options
  • Save hkfoster/86a186ace266963adfc7 to your computer and use it in GitHub Desktop.
Save hkfoster/86a186ace266963adfc7 to your computer and use it in GitHub Desktop.
Wrap ampersands with <span class="amp"></span> for styling
/**
* Fancysands v0.0.1
* Wrap ampersands with <span class="amp"></span> for styling
* @author Kyle Foster (@hkfoster)
* @license MIT (http://www.opensource.org/licenses/mit-license.php/)
*/
( function( window ) {
'use strict';
// Define fancysands function
var fancysands = function( node ) {
for ( var i = 0; i < node.length; i++ ) {
var trueNode = bypassLinks( node[ i ] ),
nodeText = trueNode.textContent,
ampWrap = nodeText.replace( /&/gi, '<span class="amp">&amp;</span>' );
trueNode.innerHTML = ampWrap;
}
// Prevent stripped hrefs
function bypassLinks( node ) {
if ( node.firstChild.nodeType === 3 ) {
return node;
} else {
return node.firstChild;
}
}
};
// Transport
if ( typeof define === 'function' && define.amd ) {
define( fancysands );
} else if ( typeof exports === 'object' ) {
module.exports = fancysands;
} else {
window.fancysands = fancysands;
}
})( window );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment