Skip to content

Instantly share code, notes, and snippets.

@euforic
Created February 14, 2014 00:31
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 euforic/8987018 to your computer and use it in GitHub Desktop.
Save euforic/8987018 to your computer and use it in GitHub Desktop.
Simple xml entity encoder / decoder
function decodeEntity(str) {
var names = {
'nbsp': 160,
'lt': 60,
'gt': 62,
'amp': 38,
'cent': 162,
'pound': 163,
'yen': 164,
'euro': 8364,
'copy': 169,
'reg:': 174
};
return ('' + str).replace(/&#?([\w\d]+);?/g, function(s, entity) {
entity = (isNaN(entity)) ? names[entity] : entity;
return String.fromCharCode(encodeURI(entity).replace('%'));
});
};
function encodeEntity(str) {
return '&#' + encodeURI(str.charCodeAt(0)).replace('%') + ';';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment