Skip to content

Instantly share code, notes, and snippets.

@jpiccari
Last active August 29, 2015 14:07
Show Gist options
  • Save jpiccari/3206c3717dd847fd06a3 to your computer and use it in GitHub Desktop.
Save jpiccari/3206c3717dd847fd06a3 to your computer and use it in GitHub Desktop.
RequireJS module to convert strings containing HTML entities to unicode text.
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, '&lt;');
return el.textContent;
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment