Last active
May 15, 2019 11:56
-
-
Save hkfoster/86a186ace266963adfc7 to your computer and use it in GitHub Desktop.
Wrap ampersands with <span class="amp"></span> for styling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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">&</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