When compiling templates that use string concatenation, treat null and undefined as empty strings so that the the literal "null" and "undefined" don't get written

This commit is contained in:
Phil Gates-Idem 2014-08-13 00:37:18 -04:00
parent 8a25b7eab9
commit a49554b4cb
3 changed files with 16 additions and 9 deletions

View File

@ -250,9 +250,12 @@ function TemplateBuilder(compiler, path, rootNode) {
this.helperFunctionsAdded = {};
this.vars = [];
this.varsLookup = {};
this.getStaticHelperFunction('toString', 's');
this.getStaticHelperFunction('empty', 'e');
this.getStaticHelperFunction('notEmpty', 'ne');
}
TemplateBuilder.prototype = {
captureCode: function (func, thisObj) {
@ -283,7 +286,7 @@ TemplateBuilder.prototype = {
addStaticVar: function (name, expression) {
name = safeVarName(name);
if (!this.staticVarsLookup[name]) {
if (!this.staticVarsLookup.hasOwnProperty(name)) {
this.staticVarsLookup[name] = true;
this.staticVars.push({
name: name,

View File

@ -238,7 +238,7 @@ ExpressionParserHelper.prototype = {
// 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 = 'toString(' + expression + ')';
}
expression = new Expression(expression);
}

View File

@ -14,6 +14,10 @@ function notEmpty(o) {
}
module.exports = {
s: function(str) {
return (str == null) ? '' : str;
},
fv: function (array, callback) {
if (!array) {
return;