Skip to content

Commit

Permalink
escape HTML - fixes #1066
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Jan 5, 2018
1 parent d280d1d commit 7026222
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/generators/nodes/Element.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import deindent from '../../utils/deindent';
import { stringify } from '../../utils/stringify';
import { stringify, escapeHTML } from '../../utils/stringify';
import flattenReference from '../../utils/flattenReference';
import isVoidElementName from '../../utils/isVoidElementName';
import validCalleeObjects from '../../utils/validCalleeObjects';
Expand Down Expand Up @@ -414,7 +414,7 @@ export default class Element extends Node {
}

function toHTML(node: Element | Text) {
if (node.type === 'Text') return node.data;
if (node.type === 'Text') return escapeHTML(node.data);

let open = `<${node.name}`;

Expand Down
4 changes: 2 additions & 2 deletions src/generators/server-side-rendering/visitors/Text.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { SsrGenerator } from '../index';
import Block from '../Block';
import { escape } from '../../../utils/stringify';
import { escape, escapeHTML } from '../../../utils/stringify';
import { Node } from '../../../interfaces';

export default function visitText(
generator: SsrGenerator,
block: Block,
node: Node
) {
generator.append(escape(node.data).replace(/(\${|`|\\)/g, '\\$1'));
generator.append(escapeHTML(escape(node.data).replace(/(\${|`|\\)/g, '\\$1')));
}
12 changes: 12 additions & 0 deletions src/utils/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,15 @@ export function escape(data: string, { onlyEscapeAtSymbol = false } = {}) {
return match + match[0];
});
}

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

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

0 comments on commit 7026222

Please sign in to comment.