Skip to content

Commit

Permalink
fix text nodes in .innerHTML-optimized output
Browse files Browse the repository at this point in the history
- collapse whitespace to single space when appropriate (#2745)
- escape template string characters in script and style tags
  • Loading branch information
Conduitry committed Jun 16, 2019
1 parent 5e81280 commit cb6309b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/compiler/compile/render-dom/wrappers/Element/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,16 +335,18 @@ export default class ElementWrapper extends Wrapper {

function to_html(wrapper: ElementWrapper | TextWrapper) {
if (wrapper.node.type === 'Text') {
if (wrapper.node.use_space) return ' ';

const parent = wrapper.node.parent as Element;

const raw = parent && (
parent.name === 'script' ||
parent.name === 'style'
);

return raw
return (raw
? wrapper.node.data
: escape_html(wrapper.node.data)
: escape_html(wrapper.node.data))
.replace(/\\/g, '\\\\')
.replace(/`/g, '\\`')
.replace(/\$/g, '\\$');
Expand Down
2 changes: 1 addition & 1 deletion test/runtime/samples/script-style-non-top-level/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export default {
html: `
<div>
<style>div { color: red; }</style>
<script>alert('<>');</script>
<script>alert(\`<>\`);</script>
</div>
`
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div>
<style>div { color: red; }</style>
<script>alert('<>');</script>
<script>alert(`<>`);</script>
</div>

0 comments on commit cb6309b

Please sign in to comment.