diff --git a/compiler/CompileContext.js b/compiler/CompileContext.js index 967848bca..067ff6385 100644 --- a/compiler/CompileContext.js +++ b/compiler/CompileContext.js @@ -68,6 +68,7 @@ const helpers = { 'escapeXml': 'x', 'escapeXmlAttr': 'xa', 'escapeScript': 'xs', + 'escapeStyle': 'xc', 'forEach': 'f', 'forEachProp': { module: 'marko/runtime/helper-forEachProperty' }, 'forEachPropStatusVar': { module: 'marko/runtime/helper-forEachPropStatusVar' }, diff --git a/compiler/ast/HtmlElement/index.js b/compiler/ast/HtmlElement/index.js index bccd93eaa..1bd224bc2 100644 --- a/compiler/ast/HtmlElement/index.js +++ b/compiler/ast/HtmlElement/index.js @@ -11,12 +11,18 @@ function beforeGenerateCode(event) { if (event.node.tagName === 'script') { event.context.pushFlag('SCRIPT_BODY'); } + if (event.node.tagName === 'style') { + event.context.pushFlag('STYLE_BODY'); + } } function afterGenerateCode(event) { if (event.node.tagName === 'script') { event.context.popFlag('SCRIPT_BODY'); } + if (event.node.tagName === 'style') { + event.context.popFlag('STYLE_BODY'); + } } class HtmlElement extends Node { diff --git a/compiler/ast/Text/html/generateCode.js b/compiler/ast/Text/html/generateCode.js index 9feb57a5e..cb06e9aeb 100644 --- a/compiler/ast/Text/html/generateCode.js +++ b/compiler/ast/Text/html/generateCode.js @@ -1,7 +1,7 @@ 'use strict'; var escapeXml = require('../../../../runtime/html/helpers').x; -var Literal = require('../..//Literal'); +var Literal = require('../../Literal'); module.exports = function(node, codegen) { var context = codegen.context; @@ -16,7 +16,7 @@ module.exports = function(node, codegen) { return; } - if (context.isFlagSet('SCRIPT_BODY')) { + if (context.isFlagSet('SCRIPT_BODY') || context.isFlagSet('STYLE_BODY')) { escape = false; } @@ -35,6 +35,10 @@ module.exports = function(node, codegen) { escapeIdentifier = context.helper('escapeScript'); } + if (context.isFlagSet('STYLE_BODY')) { + escapeIdentifier = context.helper('escapeStyle'); + } + // TODO Only escape the parts that need to be escaped if it is a compound expression with static // text parts argument = builder.functionCall( diff --git a/runtime/html/helpers.js b/runtime/html/helpers.js index c7427651c..f107a8981 100644 --- a/runtime/html/helpers.js +++ b/runtime/html/helpers.js @@ -3,7 +3,6 @@ var extend = require('raptor-util/extend'); var STYLE_ATTR = 'style'; var CLASS_ATTR = 'class'; -var escapeEndingScriptTagRegExp = /<\//g; var escape = require('./escape'); var escapeXml = escape.escapeXml; @@ -44,8 +43,27 @@ exports.xa = escapeXmlAttr; * prematurely ended and a new script tag could then be started that could then execute * arbitrary code. */ +var escapeEndingScriptTagRegExp = /<\/script/g; exports.xs = function escapeScriptHelper(val) { - return (typeof val === 'string') ? val.replace(escapeEndingScriptTagRegExp, '\\u003C/') : val; + return (typeof val === 'string') ? val.replace(escapeEndingScriptTagRegExp, '\\u003C/script') : val; +}; + +/** + * Escapes the ' body to avoid the `'; + * + * + * + * Without escaping the ending '' sequence the opening \ No newline at end of file diff --git a/test/autotests/render/escape-style/template.marko b/test/autotests/render/escape-style/template.marko new file mode 100644 index 000000000..37c1ff9d5 --- /dev/null +++ b/test/autotests/render/escape-style/template.marko @@ -0,0 +1,5 @@ + \ No newline at end of file diff --git a/test/autotests/render/escape-style/test.js b/test/autotests/render/escape-style/test.js new file mode 100644 index 000000000..e6b5be57a --- /dev/null +++ b/test/autotests/render/escape-style/test.js @@ -0,0 +1,5 @@ +exports.templateData = { + color: '' +}; + +exports.vdomSkip = true; \ No newline at end of file