Created
September 15, 2010 09:49
-
-
Save anonymous/580493 to your computer and use it in GitHub Desktop.
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 mgDecodeHTMLEntities(str) | |
{ | |
//substitutes html numeric entities with the corresponding unicode character in one pass | |
var currPos = 0; | |
var d = str; | |
var finalStr = ''; | |
while (true) | |
{ | |
var start = d.indexOf('&#', currPos); | |
if (start == -1) | |
break; | |
var end = d.indexOf(';', start); | |
if (end == -1) | |
break; | |
var expr = d.substr(start + 2, end - start - 2); | |
//handle hexadecimal values | |
if (expr.substr(0, 1) == 'x') | |
{ | |
expr = parseInt(expr.substr(1), 16); | |
} | |
else | |
{ | |
expr = parseInt(expr); | |
} | |
if(expr >= -32768 && expr <= 65535) | |
{ | |
expr = String.fromCharCode(expr); | |
} | |
else | |
{ | |
expr = ''; | |
} | |
finalStr = finalStr + d.substr(currPos, start - currPos) + expr; | |
currPos = end + 1; | |
} | |
finalStr = finalStr + d.substr(currPos); | |
return finalStr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment