escape HTML - fixes #1066

pull/1073/head
Rich Harris 7 years ago
parent d280d1d458
commit 7026222792

@ -1,5 +1,5 @@
import deindent from '../../utils/deindent'; import deindent from '../../utils/deindent';
import { stringify } from '../../utils/stringify'; import { stringify, escapeHTML } from '../../utils/stringify';
import flattenReference from '../../utils/flattenReference'; import flattenReference from '../../utils/flattenReference';
import isVoidElementName from '../../utils/isVoidElementName'; import isVoidElementName from '../../utils/isVoidElementName';
import validCalleeObjects from '../../utils/validCalleeObjects'; import validCalleeObjects from '../../utils/validCalleeObjects';
@ -414,7 +414,7 @@ export default class Element extends Node {
} }
function toHTML(node: Element | Text) { function toHTML(node: Element | Text) {
if (node.type === 'Text') return node.data; if (node.type === 'Text') return escapeHTML(node.data);
let open = `<${node.name}`; let open = `<${node.name}`;

@ -1,6 +1,6 @@
import { SsrGenerator } from '../index'; import { SsrGenerator } from '../index';
import Block from '../Block'; import Block from '../Block';
import { escape } from '../../../utils/stringify'; import { escape, escapeHTML } from '../../../utils/stringify';
import { Node } from '../../../interfaces'; import { Node } from '../../../interfaces';
export default function visitText( export default function visitText(
@ -8,5 +8,5 @@ export default function visitText(
block: Block, block: Block,
node: Node node: Node
) { ) {
generator.append(escape(node.data).replace(/(\${|`|\\)/g, '\\$1')); generator.append(escapeHTML(escape(node.data).replace(/(\${|`|\\)/g, '\\$1')));
} }

@ -7,3 +7,15 @@ export function escape(data: string, { onlyEscapeAtSymbol = false } = {}) {
return match + match[0]; return match + match[0];
}); });
} }
const escaped = {
'"': '&quot;',
"'": '&##39;',
'&': '&amp;',
'<': '&lt;',
'>': '&gt;'
};
export function escapeHTML(html) {
return String(html).replace(/["'&<>]/g, match => escaped[match]);
}

@ -1,3 +1,3 @@
<div> <div>
<p>foo: ''</p> <p>foo: &#39;&#39;</p>
</div> </div>
Loading…
Cancel
Save