mirror of
https://github.com/jellyfin/jellyfin-web
synced 2025-03-30 19:56:21 +00:00
update javascript encoding
This commit is contained in:
parent
aa333d4a60
commit
c125dff9e7
1 changed files with 38 additions and 6 deletions
|
@ -1,11 +1,43 @@
|
||||||
function htmlEncode(value) {
|
// Regular Expressions for parsing tags and attributes
|
||||||
//create a in-memory div, set it's inner text(which jQuery automatically encodes)
|
var SURROGATE_PAIR_REGEXP = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
|
||||||
//then grab the encoded contents back out. The div never exists on the page.
|
// Match everything outside of normal chars and " (quote character)
|
||||||
return $('<div/>').text(value).html();
|
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(/</g, "<");
|
||||||
|
// innerText depends on styling as it doesn't display hidden elements.
|
||||||
|
// Therefore, it's better to use textContent not to cause unnecessary reflows.
|
||||||
|
return hiddenPre.textContent;
|
||||||
}
|
}
|
||||||
|
|
||||||
function htmlDecode(value) {
|
/**
|
||||||
return $('<div/>').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, '<').
|
||||||
|
replace(/>/g, '>');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Array Remove - By John Resig (MIT Licensed)
|
// Array Remove - By John Resig (MIT Licensed)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue