Created
January 13, 2012 00:09
-
-
Save rgrove/1603932 to your computer and use it in GitHub Desktop.
Decode HTML entities
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
var utils = {}, | |
decodeEl; | |
/** | |
* Returns a version of the specified string with all HTML entities | |
* decoded. DO NOT use the output of this function with innerHTML if you're | |
* working with user-supplied content! | |
* | |
* @method decodeEntities | |
* @param {String} string string to decode | |
* @return {String} decoded string | |
*/ | |
if (Y.UA.ie) { | |
// IE strips whitespace from innerHTML assignments, so we have to use a | |
// hack for our hack. | |
utils.decodeEntities = function (string) { | |
if (!decodeEl) { | |
decodeEl = document.createElement('textarea'); | |
} | |
var chars = [' ', "\t", "\n"], | |
len = chars.length, | |
html = string, | |
c, i, value; | |
for (i = 0; i < len; ++i) { | |
c = chars[i]; | |
html = html.replace(new RegExp(c, 'g'), '__#' + | |
c.charCodeAt(0) + '__;'); | |
} | |
decodeEl.innerHTML = ''; | |
decodeEl.innerHTML = html; | |
value = decodeEl.value; | |
for (i = 0; i < len; ++i) { | |
c = chars[i]; | |
value = value.replace(new RegExp('__#' + | |
c.charCodeAt(0) + '__;', 'g'), c); | |
} | |
return value; | |
}; | |
} else { | |
utils.decodeEntities = function (string) { | |
if (!decodeEl) { | |
decodeEl = document.createElement('textarea'); | |
} | |
decodeEl.innerHTML = ''; | |
decodeEl.innerHTML = string; | |
return decodeEl.value; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment