-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
fix(v2): render issue #6610
fix(v2): render issue #6610
Conversation
let lastIdx = 0; | ||
let openAngleBracketIdx: number = -1; | ||
while ((openAngleBracketIdx = text.indexOf('<', openAngleBracketIdx + 1)) !== -1) { | ||
this.write(text.substring(lastIdx, openAngleBracketIdx)); | ||
this.write('<'); | ||
lastIdx = openAngleBracketIdx + 1; | ||
} | ||
this.write(lastIdx === 0 ? text : text.substring(lastIdx)); | ||
this.write(escapeContent(text)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mhevery can we replace this code with just escapeContent
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but the previous code was way more efficient. RegExp are a bit expensive. So it would be better for streaming if we could fix up the old code to take this into account (OR to rewrite the escapeContent
to not use RegExp) Normally I would not care but invoking the RegExp for each text node will add up. Do you need help rewriting this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wrote this code:
const escapeCharacterMap = new Map([
['<', '<'],
['>', '>'],
['&', '&'],
['"', '"'],
["'", '''],
]);
let data = '';
for (const character of text) {
const escapeCharacter = escapeCharacterMap.get(character);
if (escapeCharacter) {
this.write(data);
this.write(escapeCharacter);
data = '';
} else {
data += character;
}
}
if (data.length) {
this.write(data);
}
Do you have another idea how to do this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Map look ups are also expensive. You want to do a scan.
export function escapeHTML(html: string): string {
let escapedHTML = "";
const length = html.length;
let idx = 0;
let lastIdx = idx;
for (; idx < length; idx++) {
// We get the charCode NOT string. String would allocate memory.
const ch = html.charCodeAt(idx);
// Every time we cancat a string we allocate memory. We want to minimize that.
if (ch === 60 /* < */) {
escapedHTML += html.substring(lastIdx, idx) + '<'
} else if (ch === 62 /* > */) {
escapedHTML += html.substring(lastIdx, idx) + '>'
} else if (ch === 38 /* & */) {
escapedHTML += html.substring(lastIdx, idx) + '&'
} else if (ch === 34 /* " */) {
escapedHTML += html.substring(lastIdx, idx) + '"'
} else if (ch === 39 /* ' */) {
escapedHTML += html.substring(lastIdx, idx) + '''
} else {
continue;
}
lastIdx = idx + 1;
}
if (lastIdx === 0) {
// This is most common case, just return previous string no memory allocation.
return html;
} else {
// Add the tail of replacement.
return escapedHTML + html.substring(lastIdx);
}
}
it('escapeHTML', () => {
expect(escapeHTML("text")).toEqual("text");
expect(escapeHTML("<div a='b' c=\"d\">text")).toEqual("<div a='b' c="d">text");
});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this is MORE code, but it will run way faster at runtime.
let lastIdx = 0; | ||
let openAngleBracketIdx: number = -1; | ||
while ((openAngleBracketIdx = text.indexOf('<', openAngleBracketIdx + 1)) !== -1) { | ||
this.write(text.substring(lastIdx, openAngleBracketIdx)); | ||
this.write('<'); | ||
lastIdx = openAngleBracketIdx + 1; | ||
} | ||
this.write(lastIdx === 0 ? text : text.substring(lastIdx)); | ||
this.write(escapeContent(text)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but the previous code was way more efficient. RegExp are a bit expensive. So it would be better for streaming if we could fix up the old code to take this into account (OR to rewrite the escapeContent
to not use RegExp) Normally I would not care but invoking the RegExp for each text node will add up. Do you need help rewriting this?
--------- Co-authored-by: Miško Hevery <misko@hevery.com>
This PR fixes render issue