summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRyan Grove <ryan@wonko.com>2011-04-28 22:30:21 -0700
committerRyan Grove <ryan@wonko.com>2011-04-28 22:30:21 -0700
commitb324d002ca42a7a148b6b7d756319c8becc328fb (patch)
tree23a6e1a47d42c37c6a97890d4dca7790bdb18493
parent038d9b3feed51bc12db95af0e0ef8b69776b42c9 (diff)
downloadhandlebars.js-b324d002ca42a7a148b6b7d756319c8becc328fb.zip
handlebars.js-b324d002ca42a7a148b6b7d756319c8becc328fb.tar.gz
handlebars.js-b324d002ca42a7a148b6b7d756319c8becc328fb.tar.bz2
Allow ids to contain hyphens. Fixes #36.
Previously, a mustache like {{foo-bar}} would generate a parse error, which was a departure from the behavior of other Mustache implementations.
-rw-r--r--lib/handlebars/vm.js2
-rw-r--r--spec/parser_spec.rb4
-rw-r--r--spec/qunit_spec.js4
-rw-r--r--src/handlebars.l2
4 files changed, 10 insertions, 2 deletions
diff --git a/lib/handlebars/vm.js b/lib/handlebars/vm.js
index 3dbd8f4..ec070e0 100644
--- a/lib/handlebars/vm.js
+++ b/lib/handlebars/vm.js
@@ -273,7 +273,7 @@ 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]) {
+ if(JavaScriptCompiler.RESERVED_WORDS[name] || name.indexOf('-') !== -1) {
return parent + "['" + name + "']";
} else {
return parent + "." + name;
diff --git a/spec/parser_spec.rb b/spec/parser_spec.rb
index 3c6250a..997f4ed 100644
--- a/spec/parser_spec.rb
+++ b/spec/parser_spec.rb
@@ -113,6 +113,10 @@ describe "Parser" do
ast_for("{{this/foo}}").should == program { mustache id("foo") }
end
+ it "parses mustaches with - in a path" do
+ ast_for("{{foo-bar}}").should == program { mustache id("foo-bar") }
+ end
+
it "parses mustaches with parameters" do
ast_for("{{foo bar}}").should == program { mustache id("foo"), [id("bar")] }
end
diff --git a/spec/qunit_spec.js b/spec/qunit_spec.js
index b8191ff..0dabd0f 100644
--- a/spec/qunit_spec.js
+++ b/spec/qunit_spec.js
@@ -106,6 +106,10 @@ test("functions with context argument", function() {
"Frank", "functions are called with context arguments");
});
+test("paths with hyphens", function() {
+ shouldCompileTo("{{foo-bar}}", {"foo-bar": "baz"}, "baz", "Paths can contain hyphens (-)");
+});
+
test("nested paths", function() {
shouldCompileTo("Goodbye {{alan/expression}} world!", {alan: {expression: "beautiful"}},
"Goodbye beautiful world!", "Nested paths access nested objects");
diff --git a/src/handlebars.l b/src/handlebars.l
index e24e871..cb0d88b 100644
--- a/src/handlebars.l
+++ b/src/handlebars.l
@@ -24,7 +24,7 @@
<mu>"}}}" { this.begin("INITIAL"); return 'CLOSE'; }
<mu>"}}" { this.begin("INITIAL"); return 'CLOSE'; }
<mu>'"'("\\"["]|[^"])*'"' { yytext = yytext.substr(1,yyleng-2).replace(/\\"/g,'"'); return 'STRING'; }
-<mu>[a-zA-Z0-9_]+/[=} /.] { return 'ID'; }
+<mu>[a-zA-Z0-9_-]+/[=} /.] { return 'ID'; }
<mu>. { return 'INVALID'; }
<INITIAL,mu><<EOF>> { return 'EOF'; }