Created
February 14, 2014 00:31
-
-
Save euforic/8987018 to your computer and use it in GitHub Desktop.
Simple xml entity encoder / decoder
This file contains hidden or 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 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