Refactoring the old escape() function improved performance on 30-40%
This commit is contained in:
parent
da942fd82f
commit
eb9f08cbb3
@ -1274,14 +1274,32 @@ Parser.prototype.tok = function() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
function escape(html, encode) {
|
function escape(html, encode) {
|
||||||
return html
|
if (encode) {
|
||||||
.replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&')
|
if (escape.escapeTest.test(html)) {
|
||||||
.replace(/</g, '<')
|
return html.replace(escape.escapeReplace, function (ch) { return escape.replacements[ch] });
|
||||||
.replace(/>/g, '>')
|
}
|
||||||
.replace(/"/g, '"')
|
} else {
|
||||||
.replace(/'/g, ''');
|
if (escape.escapeTestNoEncode.test(html)) {
|
||||||
|
return html.replace(escape.escapeReplaceNoEncode, function (ch) { return escape.replacements[ch] });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return html;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
escape.escapeTest = /[&<>"']/;
|
||||||
|
escape.escapeReplace = /[&<>"']/g;
|
||||||
|
escape.replacements = {
|
||||||
|
'&': '&',
|
||||||
|
'<': '<',
|
||||||
|
'>': '>',
|
||||||
|
'"': '"',
|
||||||
|
"'": '''
|
||||||
|
};
|
||||||
|
|
||||||
|
escape.escapeTestNoEncode = /[<>"']|&(?!#?\w+;)/;
|
||||||
|
escape.escapeReplaceNoEncode = /[<>"']|&(?!#?\w+;)/g;
|
||||||
|
|
||||||
function unescape(html) {
|
function unescape(html) {
|
||||||
// explicitly match decimal, hex, and named HTML entities
|
// explicitly match decimal, hex, and named HTML entities
|
||||||
return html.replace(/&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/ig, function(_, n) {
|
return html.replace(/&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/ig, function(_, n) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user