Fixed unnecessary parentheses

This commit is contained in:
Patrick Steele-Idem 2014-05-05 22:00:18 -06:00
parent f1ce8a4e95
commit 04b96556c7
7 changed files with 55 additions and 41 deletions

View File

@ -30,7 +30,7 @@ function writeArg(writer, arg) {
} else if (typeof arg === 'function') {
arg();
} else if (arg instanceof Expression) {
writer._code.append(arg.toString());
writer._code.append( arg.toString() );
} else if (arg) {
writer._code.append(arg.toString());
} else {
@ -162,12 +162,7 @@ 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();

View File

@ -223,12 +223,23 @@ ExpressionParserHelper.prototype = {
this.prevEscapeXml = escapeXml;
}
},
addXmlExpression: function (expression, escapeXml) {
addUnescapedExpression: function (expression, escapeXml) {
this.addExpression(expression, false);
},
addExpression: function (expression, escapeXml) {
this._endText();
escapeXml = escapeXml !== false;
if (!(expression instanceof Expression)) {
if (!escapeXml) {
// The expression might be a ternary operator
// so we need to surround it with parentheses.
// Minification will remove unnecessary parentheses.
// We don't need to surround with parentheses if
// the expression will be escaped since the expression
// is an argument to a function call
expression = '(' + expression + ')';
}
expression = new Expression(expression);
}
this._invokeCallback('expression', expression, escapeXml !== false);
@ -339,7 +350,7 @@ parse = function (str, listeners, options) {
}
var varName = variableMatches[1];
if (startToken === '$!') {
helper.addXmlExpression(varName); //Add the variable as an expression
helper.addUnescapedExpression(varName); //Add the variable as an expression
} else {
helper.addExpression(varName); //Add the variable as an expression
}
@ -406,7 +417,7 @@ parse = function (str, listeners, options) {
expression = processNestedStrings(expression, foundStrings);
}
if (startToken === '$!{') {
helper.addXmlExpression(expression);
helper.addUnescapedExpression(expression);
} else {
helper.addExpression(expression);
}
@ -429,7 +440,7 @@ parse = function (str, listeners, options) {
exports.parse = parse;
exports.custom = {
'xml': function (expression, helper) {
helper.addXmlExpression(new Expression(expression));
helper.addUnescapedExpression(new Expression(expression));
},
'entity': function (expression, helper) {
helper.addXmlText('&' + expression + ';');

View File

@ -28,20 +28,23 @@ WriteNode.prototype = {
doGenerateCode: function (template) {
var expression = this.getExpression();
var escapeXml;
var options = {};
if (this.hasProperty('escapeXml')) {
escapeXml = this.getProperty('escapeXml') !== false;
} else {
escapeXml = this.getProperty('escape-xml') !== false;
}
if (escapeXml === true) {
if (this.getEscapeXmlContext() === 'ATTRIBUTE') {
expression = template.getStaticHelperFunction('escapeXmlAttr', 'xa') + '(' + expression + ')';
options.escapeXmlAttr = true;
} else {
expression = template.getStaticHelperFunction('escapeXml', 'x') + '(' + expression + ')';
options.escapeXml = true;
}
}
if (expression) {
template.write(expression);
template.write(expression, options);
}
},
getExpression: function () {

View File

@ -7,13 +7,18 @@ var nodePath = require('path');
var raptorTemplates = require('../');
var through = require('through');
describe('raptor-templates/rhtml' , function() {
describe('raptor-templates/api' , function() {
before(function() {
require('../compiler').defaultOptions.checkUpToDate = false;
});
beforeEach(function(done) {
done();
});
it('should allow a template to be loaded and rendered using a callback', function(done) {
it('should allow a template to be rendered using a callback', function(done) {
raptorTemplates.render(
nodePath.join(__dirname, 'test-project/hello.rhtml'),
{
@ -29,7 +34,7 @@ describe('raptor-templates/rhtml' , function() {
});
});
it('should allow a template to be loaded and rendered to a context wrapping a string builder', function(done) {
it('should allow a template to be rendered to a context wrapping a string builder', function(done) {
var context = raptorTemplates.createContext();
context
.on('end', function() {
@ -50,7 +55,7 @@ describe('raptor-templates/rhtml' , function() {
context.end();
});
it('should allow a template to be loaded and rendered to a context wrapping a stream', function(done) {
it('should allow a template to be rendered to a context wrapping a stream', function(done) {
var output = '';
var stream = through(function write(data) {
@ -77,7 +82,7 @@ describe('raptor-templates/rhtml' , function() {
context.end();
});
it('should allow a template to be loaded and rendered to a stream', function(done) {
it('should allow a template to be rendered to a stream', function(done) {
var output = '';

View File

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

View File

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