diff --git a/compiler/TemplateBuilder.js b/compiler/TemplateBuilder.js index 84fbf9abd..63601bb17 100644 --- a/compiler/TemplateBuilder.js +++ b/compiler/TemplateBuilder.js @@ -82,7 +82,7 @@ CodeWriter.prototype = { this._code.append(this._indent + '__helpers.' + methodName + '(context'); if (args.length) { - this._code.append(', '); + this._code.append(', '); } writeArgs(this, args); @@ -162,7 +162,12 @@ CodeWriter.prototype = { code.append(' +\n' + this.indentStr()); } + // The expression might be a ternary operator + // so we need to surround it with parentheses. + // Minification will remove unnecessary parentheses. + code.append('('); writeArg(_this, expression); + code.append(')'); if (i !== 0) { _this.decIndent(); @@ -203,7 +208,7 @@ CodeWriter.prototype = { this.firstStatement = false; this._bufferedWrites = null; if (this.concatWrites) { - concat(); + concat(); } else { chain(); } diff --git a/test-render.sh b/test-render-rhtml.sh similarity index 100% rename from test-render.sh rename to test-render-rhtml.sh diff --git a/test-render-rxml.sh b/test-render-rxml.sh new file mode 100755 index 000000000..67a4d1a29 --- /dev/null +++ b/test-render-rxml.sh @@ -0,0 +1 @@ +node_modules/.bin/mocha --ui bdd --reporter spec ./test/render-rxml-tests.js \ No newline at end of file diff --git a/test/render-rxml-tests.js b/test/render-rxml-tests.js index 005b70add..192a78601 100644 --- a/test/render-rxml-tests.js +++ b/test/render-rxml-tests.js @@ -206,7 +206,7 @@ describe('raptor-templates/rxml' , function() { testRender("test-project/rxml-templates/include.rxml", {}, done); }); - it("should allow for ", function(done) { + it("should allow for ", function(done) { testRender("test-project/rxml-templates/invoke.rxml", {}, done); }); @@ -362,5 +362,9 @@ describe('raptor-templates/rxml' , function() { testRender("test-project/rxml-templates/layout-use.rxml", {}, done); }); + it("should add parentheses around each expression when using string concatenation to handle ternary operator", function(done) { + testRender("test-project/rxml-templates/string-concat-with-ternary-operator.rxml", {}, done); + }); + }); diff --git a/test/test-project/rhtml-templates/simple.rhtml.js b/test/test-project/rhtml-templates/simple.rhtml.js index 61893af3b..f083df1ff 100644 --- a/test/test-project/rhtml-templates/simple.rhtml.js +++ b/test/test-project/rhtml-templates/simple.rhtml.js @@ -12,26 +12,26 @@ module.exports = function create(__helpers) { var message=data.message; - context.w('
' + - escapeXml(message) + - '
'); + context.w(('
') + + (escapeXml(message)) + + ('
')); if (notEmpty(colors)) { - context.w('')); } if (empty(colors)) { - context.w('
No colors!
'); + context.w(('
No colors!
')); } }; } \ No newline at end of file diff --git a/test/test-project/rxml-templates/simple.rxml.js b/test/test-project/rxml-templates/simple.rxml.js index d030bf5ca..11bab6a12 100644 --- a/test/test-project/rxml-templates/simple.rxml.js +++ b/test/test-project/rxml-templates/simple.rxml.js @@ -17,26 +17,26 @@ module.exports = function create(__helpers) { "name": "World" }); - context.w('
' + - escapeXml(message) + - '
'); + context.w(('
') + + (escapeXml(message)) + + ('
')); if (notEmpty(colors)) { - context.w('')); } if (empty(colors)) { - context.w('
No colors!
'); + context.w(('
No colors!
')); } }; } \ No newline at end of file diff --git a/test/test-project/rxml-templates/string-concat-with-ternary-operator.rxml b/test/test-project/rxml-templates/string-concat-with-ternary-operator.rxml new file mode 100644 index 000000000..ff0594ab7 --- /dev/null +++ b/test/test-project/rxml-templates/string-concat-with-ternary-operator.rxml @@ -0,0 +1,4 @@ + +A: $!{true ? 'ABC' : ''} +B: This should be outputted as well. + \ No newline at end of file diff --git a/test/test-project/rxml-templates/string-concat-with-ternary-operator.rxml.expected.html b/test/test-project/rxml-templates/string-concat-with-ternary-operator.rxml.expected.html new file mode 100644 index 000000000..dfac8d5c1 --- /dev/null +++ b/test/test-project/rxml-templates/string-concat-with-ternary-operator.rxml.expected.html @@ -0,0 +1,3 @@ + +A: ABC +B: This should be outputted as well. diff --git a/test/test-project/rxml-templates/string-concat-with-ternary-operator.rxml.expected.js b/test/test-project/rxml-templates/string-concat-with-ternary-operator.rxml.expected.js new file mode 100644 index 000000000..cadbc9965 --- /dev/null +++ b/test/test-project/rxml-templates/string-concat-with-ternary-operator.rxml.expected.js @@ -0,0 +1,10 @@ +module.exports = function create(__helpers) { + var empty = __helpers.e, + notEmpty = __helpers.ne; + + return function render(data, context) { + context.w(('\nA: ') + + (true ? 'ABC' : '') + + ('\nB: This should be outputted as well.\n')); + }; +} \ No newline at end of file diff --git a/test/test-project/simple.rhtml.expected.js b/test/test-project/simple.rhtml.expected.js index acd884784..15e20d5c5 100644 --- a/test/test-project/simple.rhtml.expected.js +++ b/test/test-project/simple.rhtml.expected.js @@ -3,6 +3,6 @@ module.exports = function create(__helpers) { notEmpty = __helpers.ne; return function render(data, context) { - context.w('Hello John'); + context.w(('Hello John')); }; } \ No newline at end of file