diff --git a/src/server-side-rendering/compile.js b/src/server-side-rendering/compile.js index 47ccae587a..e34156745a 100644 --- a/src/server-side-rendering/compile.js +++ b/src/server-side-rendering/compile.js @@ -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;', + '&': '&', + '<': '<', + '>': '>' + }; + + function __escape ( html ) { + return html.replace( /["'&<>]/g, match => escaped[ match ] ); + } ` ); const rendered = topLevelStatements.join( '\n\n' ); diff --git a/test/server-side-rendering/dynamic-text-escaped/_actual.html b/test/server-side-rendering/dynamic-text-escaped/_actual.html new file mode 100644 index 0000000000..467e5de3d1 --- /dev/null +++ b/test/server-side-rendering/dynamic-text-escaped/_actual.html @@ -0,0 +1 @@ +<p>this should be <em>escaped</em> & so should &39;this&39;</p> \ No newline at end of file diff --git a/test/server-side-rendering/dynamic-text-escaped/_expected.html b/test/server-side-rendering/dynamic-text-escaped/_expected.html new file mode 100644 index 0000000000..080992cfd2 --- /dev/null +++ b/test/server-side-rendering/dynamic-text-escaped/_expected.html @@ -0,0 +1 @@ +<p>this should be <em>escaped</em> & so should &39;this&39;</p> diff --git a/test/server-side-rendering/dynamic-text-escaped/data.json b/test/server-side-rendering/dynamic-text-escaped/data.json new file mode 100644 index 0000000000..ece574c8bd --- /dev/null +++ b/test/server-side-rendering/dynamic-text-escaped/data.json @@ -0,0 +1,3 @@ +{ + "foo": "

this should be escaped & so should 'this'

" +} diff --git a/test/server-side-rendering/dynamic-text-escaped/main.html b/test/server-side-rendering/dynamic-text-escaped/main.html new file mode 100644 index 0000000000..054e96cb81 --- /dev/null +++ b/test/server-side-rendering/dynamic-text-escaped/main.html @@ -0,0 +1 @@ +{{foo}} diff --git a/test/server-side-rendering/triple/_actual.html b/test/server-side-rendering/triple/_actual.html new file mode 100644 index 0000000000..924d26dd6a --- /dev/null +++ b/test/server-side-rendering/triple/_actual.html @@ -0,0 +1 @@ +

html

\ No newline at end of file diff --git a/test/server-side-rendering/triple/_expected.html b/test/server-side-rendering/triple/_expected.html new file mode 100644 index 0000000000..138c8a30cd --- /dev/null +++ b/test/server-side-rendering/triple/_expected.html @@ -0,0 +1 @@ +

html

diff --git a/test/server-side-rendering/triple/main.html b/test/server-side-rendering/triple/main.html new file mode 100644 index 0000000000..80be85ccdc --- /dev/null +++ b/test/server-side-rendering/triple/main.html @@ -0,0 +1,11 @@ +
{{{triple}}}
+ +