Skip to content

Increase the performance of some xml and html helpers #1476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions lib/utils/under-dash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const {toString} = Object.prototype;

const escapeHtmlRegex = /["&<>]/;
const _ = {
each: function each(obj, cb) {
if (obj) {
Expand Down Expand Up @@ -84,12 +84,39 @@ const _ = {
},

escapeHtml(html) {
return html
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#039;');
const regexResult = escapeHtmlRegex.exec(html);
if (!regexResult) return html;

let result = '';
let escape = '';
let lastIndex = 0;
let i = regexResult.index;
for (; i < html.length; i++) {
switch (html.charAt(i)) {
case '"':
escape = '&quot;';
break;
case '&':
escape = '&amp;';
break;
case "'":
escape = '&apos;';
break;
case '<':
escape = '&lt;';
break;
case '>':
escape = '&gt;';
break;
default:
continue;
}
if (lastIndex !== i) result += html.substring(lastIndex, i);
lastIndex = i + 1;
result += escape;
}
if (lastIndex !== i) return result + html.substring(lastIndex, i);
return result;
},

strcmp(a, b) {
Expand Down
61 changes: 44 additions & 17 deletions lib/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const inherits = function(cls, superCtor, statics, prototype) {
cls.prototype = Object.create(superCtor.prototype, properties);
};

// eslint-disable-next-line no-control-regex
const xmlDecodeRegex = /[<>&'"\x7F\x00-\x08\x0B-\x0C\x0E-\x1F]/;
const utils = {
nop() {},
promiseImmediate(value) {
Expand Down Expand Up @@ -69,26 +71,51 @@ const utils = {
return `${path.path}/_rels/${path.name}.rels`;
},
xmlEncode(text) {
// eslint-disable-next-line no-control-regex
return text.replace(/[<>&'"\x7F\x00-\x08\x0B-\x0C\x0E-\x1F]/g, c => {
switch (c) {
case '<':
return '&lt;';
case '>':
return '&gt;';
case '&':
return '&amp;';
case '\'':
return '&apos;';
case '"':
return '&quot;';
default:
return '';
const regexResult = xmlDecodeRegex.exec(text);
if (!regexResult) return text;

let result = '';
let escape = '';
let lastIndex = 0;
let i = regexResult.index;
for (; i < text.length; i++) {
const charCode = text.charCodeAt(i);
switch (charCode) {
case 34: // "
escape = '&quot;';
break;
case 38: // &
escape = '&amp;';
break;
case 39: // '
escape = '&apos;';
break;
case 60: // <
escape = '&lt;';
break;
case 62: // >
escape = '&gt;';
break;
case 127:
escape = '';
break;
default: {
if (charCode <= 31 && (charCode <= 8 || (charCode >= 11 && charCode !== 13))) {
escape = '';
break;
}
continue;
}
}
});
if (lastIndex !== i) result += text.substring(lastIndex, i);
lastIndex = i + 1;
if (escape) result += escape;
}
if (lastIndex !== i) return result + text.substring(lastIndex, i);
return result;
},
xmlDecode(text) {
return text.replace(/&([a-z]*);/, c => {
return text.replace(/&([a-z]*);/g, c => {
switch (c) {
case '&lt;':
return '<';
Expand Down