Got simple literal expressions added into paths.

This commit is contained in:
Alan Johnson
2011-09-02 09:04:41 -04:00
parent 91bbc4fd2c
commit fc84308cc9
3 changed files with 20 additions and 7 deletions
+13 -5
View File
@@ -314,12 +314,13 @@ Handlebars.JavaScriptCompiler = function() {};
// PUBLIC API: You can override these methods in a subclass to provide
// alternative compiled forms for name lookup and buffering semantics
nameLookup: function(parent, name, type) {
if(JavaScriptCompiler.RESERVED_WORDS[name] || name.indexOf('-') !== -1 || !isNaN(name)) {
return parent + "['" + name + "']";
} else if (/^[0-9]+$/.test(name)) {
if (/^[0-9]+$/.test(name)) {
return parent + "[" + name + "]";
} else {
return parent + "." + name;
} else if (JavaScriptCompiler.isValidJavaScriptVariableName(name)) {
return parent + "." + name;
}
else {
return parent + "['" + name + "']";
}
},
@@ -734,6 +735,13 @@ Handlebars.JavaScriptCompiler = function() {};
compilerWords[reservedWords[i]] = true;
}
JavaScriptCompiler.isValidJavaScriptVariableName = function(name) {
if(!JavaScriptCompiler.RESERVED_WORDS[name] && /^[a-zA-Z_$][0-9a-zA-Z_$]+$/.test(name)) {
return true;
}
return false;
}
})(Handlebars.Compiler, Handlebars.JavaScriptCompiler);
Handlebars.precompile = function(string, options) {