Skip to content
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

[WIP] Fix HTML escaping and non-top-level <script> and <style> issues #1086

Merged
merged 7 commits into from
Jan 9, 2018
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
6 changes: 5 additions & 1 deletion src/generators/nodes/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,10 @@ export default class Element extends Node {

if (isVoidElementName(node.name)) return open + '>';

if (node.name === 'script' || node.name === 'style') {
return `${open}>${node.data}</${node.name}>`;
}

return `${open}>${node.children.map(toHTML).join('')}</${node.name}>`;
}
}
Expand Down Expand Up @@ -756,4 +760,4 @@ const events = [
node.isMediaNode() &&
(name === 'buffered' || name === 'seekable')
}
];
];
2 changes: 2 additions & 0 deletions src/generators/server-side-rendering/visitors/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export default function visitElement(

if (node.name === 'textarea' && textareaContents !== undefined) {
generator.append(textareaContents);
} else if (node.name === 'script' || node.name === 'style') {
generator.append(node.data);
} else {
node.children.forEach((child: Node) => {
visit(generator, block, child);
Expand Down
7 changes: 6 additions & 1 deletion src/parse/state/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ export default function tag(parser: Parser) {
parser.eat('>', true);

if (selfClosing) {
// don't push self-closing elements onto the stack
element.end = parser.index;
} else if (name === 'textarea') {
// special case
Expand All @@ -223,8 +224,12 @@ export default function tag(parser: Parser) {
);
parser.read(/<\/textarea>/);
element.end = parser.index;
} else if (name === 'script' || name === 'style') {
// special case
element.data = parser.readUntil(new RegExp(`</${name}>`));
parser.eat(`</${name}>`, true);
element.end = parser.index;
} else {
// don't push self-closing elements onto the stack
parser.stack.push(element);
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/utils/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ export function escape(data: string, { onlyEscapeAtSymbol = false } = {}) {
}

const escaped = {
'"': '&quot;',
"'": '&##39;',
'&': '&amp;',
'<': '&lt;',
'>': '&gt;'
'>': '&gt;',
};

export function escapeHTML(html) {
return String(html).replace(/["'&<>]/g, match => escaped[match]);
}
return String(html).replace(/[&<>]/g, match => escaped[match]);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
html: `
<div>'foo'<span/></div>
`
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>'foo'<span/></div>
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<div>
<p>foo: &#39;&#39;</p>
<p>foo: ''</p>
</div>