0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-12-01 17:30:59 +01:00

implement triples in SSR compiler, and escape HTML for regular tags

This commit is contained in:
Rich Harris 2016-12-15 10:55:39 -05:00
parent 23b7759160
commit c8b232f431
8 changed files with 36 additions and 0 deletions

View File

@ -219,6 +219,11 @@ export default function compile ( parsed, source, { filename }) {
},
MustacheTag ( node ) {
const { snippet } = contextualise( node.expression ); // TODO use snippet, for sourcemap support
return '${__escape( String( ' + snippet + ') )}';
},
RawMustacheTag ( node ) {
const { snippet } = contextualise( node.expression ); // TODO use snippet, for sourcemap support
return '${' + snippet + '}';
},
@ -381,6 +386,18 @@ export default function compile ( parsed, source, { filename }) {
exports.renderCss = function () {
${renderCssStatements.join( '\n\n' )}
};
var escaped = {
'"': '"',
"'": '&39;',
'&': '&',
'<': '&lt;',
'>': '&gt;'
};
function __escape ( html ) {
return html.replace( /["'&<>]/g, match => escaped[ match ] );
}
` );
const rendered = topLevelStatements.join( '\n\n' );

View File

@ -0,0 +1 @@
&lt;p&gt;this should be &lt;em&gt;escaped&lt;/em&gt; &amp; so should &39;this&39;&lt;/p&gt;

View File

@ -0,0 +1 @@
&lt;p&gt;this should be &lt;em&gt;escaped&lt;/em&gt; &amp; so should &39;this&39;&lt;/p&gt;

View File

@ -0,0 +1,3 @@
{
"foo": "<p>this should be <em>escaped</em> & so should 'this'</p>"
}

View File

@ -0,0 +1 @@
{{foo}}

View File

@ -0,0 +1 @@
<div><p>html</p></div>

View File

@ -0,0 +1 @@
<div><p>html</p></div>

View File

@ -0,0 +1,11 @@
<div>{{{triple}}}</div>
<script>
export default {
data () {
return {
triple: '<p>html</p>'
};
}
};
</script>