Skip to content

Instantly share code, notes, and snippets.

@pixelsoul
Created February 18, 2019 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pixelsoul/6c62776bb33c1b7d9044904b02dfd4fd to your computer and use it in GitHub Desktop.
Save pixelsoul/6c62776bb33c1b7d9044904b02dfd4fd to your computer and use it in GitHub Desktop.
(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