Fixing bug that occurs when string concatenation is being used and one of the parts is a ternary operator expression. Each part needs to be surrounded with parentheses.

This commit is contained in:
Phil Gates-Idem 2014-05-01 16:19:45 -04:00
parent 0dad9a6d78
commit e1872eeaaa
10 changed files with 53 additions and 26 deletions

View File

@ -82,7 +82,7 @@ CodeWriter.prototype = {
this._code.append(this._indent + '__helpers.' + methodName + '(context'); this._code.append(this._indent + '__helpers.' + methodName + '(context');
if (args.length) { if (args.length) {
this._code.append(', '); this._code.append(', ');
} }
writeArgs(this, args); writeArgs(this, args);
@ -162,7 +162,12 @@ CodeWriter.prototype = {
code.append(' +\n' + this.indentStr()); 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); writeArg(_this, expression);
code.append(')');
if (i !== 0) { if (i !== 0) {
_this.decIndent(); _this.decIndent();
@ -203,7 +208,7 @@ CodeWriter.prototype = {
this.firstStatement = false; this.firstStatement = false;
this._bufferedWrites = null; this._bufferedWrites = null;
if (this.concatWrites) { if (this.concatWrites) {
concat(); concat();
} else { } else {
chain(); chain();
} }

1
test-render-rxml.sh Executable file
View File

@ -0,0 +1 @@
node_modules/.bin/mocha --ui bdd --reporter spec ./test/render-rxml-tests.js

View File

@ -206,7 +206,7 @@ describe('raptor-templates/rxml' , function() {
testRender("test-project/rxml-templates/include.rxml", {}, done); testRender("test-project/rxml-templates/include.rxml", {}, done);
}); });
it("should allow for <c:invoke function... />", function(done) { it("should allow for <c:invoke function... />", function(done) {
testRender("test-project/rxml-templates/invoke.rxml", {}, 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); 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);
});
}); });

View File

@ -12,26 +12,26 @@ module.exports = function create(__helpers) {
var message=data.message; var message=data.message;
context.w('<div class="hello-world ' + context.w(('<div class="hello-world ') +
escapeXmlAttr(rootClass) + (escapeXmlAttr(rootClass)) +
'">' + ('">') +
escapeXml(message) + (escapeXml(message)) +
'</div>'); ('</div>'));
if (notEmpty(colors)) { if (notEmpty(colors)) {
context.w('<ul>'); context.w(('<ul>'));
forEach(colors, function(color) { forEach(colors, function(color) {
context.w('<li class="color">' + context.w(('<li class="color">') +
escapeXml(color) + (escapeXml(color)) +
'</li>'); ('</li>'));
}); });
context.w('</ul>'); context.w(('</ul>'));
} }
if (empty(colors)) { if (empty(colors)) {
context.w('<div>No colors!</div>'); context.w(('<div>No colors!</div>'));
} }
}; };
} }

View File

@ -17,26 +17,26 @@ module.exports = function create(__helpers) {
"name": "World" "name": "World"
}); });
context.w('<div class="hello-world ' + context.w(('<div class="hello-world ') +
escapeXmlAttr(rootClass) + (escapeXmlAttr(rootClass)) +
'">' + ('">') +
escapeXml(message) + (escapeXml(message)) +
'</div>'); ('</div>'));
if (notEmpty(colors)) { if (notEmpty(colors)) {
context.w('<ul>'); context.w(('<ul>'));
forEach(colors, function(color) { forEach(colors, function(color) {
context.w('<li class="color">' + context.w(('<li class="color">') +
escapeXml(color) + (escapeXml(color)) +
'</li>'); ('</li>'));
}); });
context.w('</ul>'); context.w(('</ul>'));
} }
if (empty(colors)) { if (empty(colors)) {
context.w('<div>No colors!</div>'); context.w(('<div>No colors!</div>'));
} }
}; };
} }

View File

@ -0,0 +1,4 @@
<c-template c-whitespace="preserve">
A: $!{true ? 'ABC' : ''}
B: This should be outputted as well.
</c-template>

View File

@ -0,0 +1,3 @@
A: ABC
B: This should be outputted as well.

View File

@ -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'));
};
}

View File

@ -3,6 +3,6 @@ module.exports = function create(__helpers) {
notEmpty = __helpers.ne; notEmpty = __helpers.ne;
return function render(data, context) { return function render(data, context) {
context.w('Hello John'); context.w(('Hello John'));
}; };
} }