Created
February 18, 2019 23:02
-
-
Save pixelsoul/6c62776bb33c1b7d9044904b02dfd4fd to your computer and use it in GitHub Desktop.
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
(function(window){ | |
window.htmlentities = { | |
/** | |
* Converts a string to its html characters completely. | |
* | |
* @param {String} str String with unescaped HTML characters | |
**/ | |
encode : function(str) { | |
var buf = []; | |
for (var i=str.length-1;i>=0;i--) { | |
buf.unshift(['&#', str[i].charCodeAt(), ';'].join('')); | |
} | |
return buf.join(''); | |
}, | |
/** | |
* Converts an html characterSet into its original character. | |
* | |
* @param {String} str htmlSet entities | |
**/ | |
decode : function(str) { | |
return str.replace(/&#(\d+);/g, function(match, dec) { | |
return String.fromCharCode(dec); | |
}); | |
} | |
}; | |
})(window); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment