Last active
August 29, 2015 14:07
-
-
Save jpiccari/3206c3717dd847fd06a3 to your computer and use it in GitHub Desktop.
RequireJS module to convert strings containing HTML entities to unicode text.
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
define( | |
'EntityToUnicode', | |
function() { | |
var el = document.createElement('p'); | |
/** | |
* HTML Entities to unicode text | |
* @param {string} str - String which contains HTML entities to decode | |
* @returns {string} A string of the equivalent unicode text | |
*/ | |
return function(str) { | |
/** | |
* XSS counter measure. | |
* Replacing all less-than signs with their entity ensures that there | |
* are no valid HTML tags in the .innerHTML of el. This simple change | |
* prevents possible XSS issues. | |
*/ | |
el.innerHTML = str.replace(/</g, '<'); | |
return el.textContent; | |
} | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment