diff options
-rw-r--r-- | lib/handlebars/compiler/compiler.js | 18 | ||||
-rw-r--r-- | spec/qunit_spec.js | 8 | ||||
-rw-r--r-- | src/handlebars.l | 1 |
3 files changed, 20 insertions, 7 deletions
diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js index a3fd120..86b7361 100644 --- a/lib/handlebars/compiler/compiler.js +++ b/lib/handlebars/compiler/compiler.js @@ -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) { diff --git a/spec/qunit_spec.js b/spec/qunit_spec.js index 1774bbf..fe4866a 100644 --- a/spec/qunit_spec.js +++ b/spec/qunit_spec.js @@ -130,6 +130,11 @@ test("nested paths with empty string value", function() { "Goodbye world!", "Nested paths access nested objects with empty string"); }); +test("literal paths", function() { + shouldCompileTo("Goodbye {{[@alan]/expression}} world!", {"@alan": {expression: "beautiful"}}, + "Goodbye beautiful world!", "Literal paths can be used"); +}); + test("--- TODO --- bad idea nested paths", function() { return; var hash = {goodbyes: [{text: "goodbye"}, {text: "Goodbye"}, {text: "GOODBYE"}], world: "world"}; @@ -940,5 +945,4 @@ test("when inside a block in String mode, .. passes the appropriate context in t }, {helpers: helpers}); equals(result, "STOP ME FROM READING HACKER NEWS I need-a dad.joke wot", "Proper context variable output"); -}); - +});
\ No newline at end of file diff --git a/src/handlebars.l b/src/handlebars.l index 0d3cdf0..57ebb90 100644 --- a/src/handlebars.l +++ b/src/handlebars.l @@ -28,6 +28,7 @@ <mu>"false"/[}\s] { return 'BOOLEAN'; } <mu>[0-9]+/[}\s] { return 'INTEGER'; } <mu>[a-zA-Z0-9_$-]+/[=}\s/.] { return 'ID'; } +<mu>\[.*\] { yytext = yytext.substr(1, yyleng-2); return 'ID'; } <mu>. { return 'INVALID'; } <INITIAL,mu><<EOF>> { return 'EOF'; } |