diff --git a/dashboard-ui/scripts/extensions.js b/dashboard-ui/scripts/extensions.js index 1ad1ff29ee..f8467a9cd9 100644 --- a/dashboard-ui/scripts/extensions.js +++ b/dashboard-ui/scripts/extensions.js @@ -1,11 +1,43 @@ -function htmlEncode(value) { - //create a in-memory div, set it's inner text(which jQuery automatically encodes) - //then grab the encoded contents back out. The div never exists on the page. - return $('
').text(value).html(); +// Regular Expressions for parsing tags and attributes +var SURROGATE_PAIR_REGEXP = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g, + // Match everything outside of normal chars and " (quote character) + NON_ALPHANUMERIC_REGEXP = /([^\#-~| |!])/g; + +var hiddenPre = document.createElement("pre"); +/** + * decodes all entities into regular string + * @param value + * @returns {string} A string with decoded entities. + */ +function htmlDecode(value) { + if (!value) { return ''; } + + hiddenPre.innerHTML = value.replace(/').html(value).text(); +/** + * Escapes all potentially dangerous characters, so that the + * resulting string can be safely inserted into attribute or + * element text. + * @param value + * @returns {string} escaped text + */ +function htmlEncode(value) { + return value. + replace(/&/g, '&'). + replace(SURROGATE_PAIR_REGEXP, function (value) { + var hi = value.charCodeAt(0); + var low = value.charCodeAt(1); + return '&#' + (((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000) + ';'; + }). + replace(NON_ALPHANUMERIC_REGEXP, function (value) { + return '&#' + value.charCodeAt(0) + ';'; + }). + replace(//g, '>'); } // Array Remove - By John Resig (MIT Licensed)