summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/handlebars/compiler/compiler.js14
-rw-r--r--spec/basic.js35
-rw-r--r--src/handlebars.yy10
3 files changed, 35 insertions, 24 deletions
diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js
index 605a54b..21de99c 100644
--- a/lib/handlebars/compiler/compiler.js
+++ b/lib/handlebars/compiler/compiler.js
@@ -110,6 +110,8 @@ Compiler.prototype = {
},
BlockStatement: function(block) {
+ transformLiteralToPath(block);
+
var program = block.program,
inverse = block.inverse;
@@ -172,7 +174,6 @@ Compiler.prototype = {
},
MustacheStatement: function(mustache) {
- transformLiteralToPath(mustache);
this.SubExpression(mustache);
if(mustache.escaped && !this.options.noEscape) {
@@ -191,6 +192,7 @@ Compiler.prototype = {
CommentStatement: function() {},
SubExpression: function(sexpr) {
+ transformLiteralToPath(sexpr);
var type = this.classifySexpr(sexpr);
if (type === 'simple') {
@@ -487,9 +489,11 @@ function argEquals(a, b) {
}
}
-function transformLiteralToPath(mustache) {
- if (mustache.path.type.match(/Literal$/)) {
- var literal = mustache.path;
- mustache.path = { type: 'PathExpression', original: String(literal.original), parts: [String(literal.original)], depth: 0, data: false };
+function transformLiteralToPath(sexpr) {
+ if (!sexpr.path.parts) {
+ var literal = sexpr.path;
+ // Casting to string here to make false and 0 literal values play nicely with the rest
+ // of the system.
+ sexpr.path = new AST.PathExpression(false, 0, [literal.original+''], literal.original+'', literal.log);
}
}
diff --git a/spec/basic.js b/spec/basic.js
index 765e41d..9b6678a 100644
--- a/spec/basic.js
+++ b/spec/basic.js
@@ -230,21 +230,32 @@ describe("basic context", function() {
}, Error);
});
- it("pass string literals", function() {
- shouldCompileTo('{{"foo"}}', {}, "");
- shouldCompileTo('{{"foo"}}', { foo: "bar" }, "bar");
+ it('pass string literals', function() {
+ shouldCompileTo('{{"foo"}}', {}, '');
+ shouldCompileTo('{{"foo"}}', { foo: 'bar' }, 'bar');
+ shouldCompileTo('{{#"foo"}}{{.}}{{/"foo"}}', { foo: ['bar', 'baz'] }, 'barbaz');
});
- it("pass number literals", function() {
- shouldCompileTo("{{12}}", {}, "");
- shouldCompileTo("{{12}}", { "12": "bar" }, "bar");
- shouldCompileTo("{{12.34}}", {}, "");
- shouldCompileTo("{{12.34}}", { "12.34": "bar" }, "bar");
+ it('pass number literals', function() {
+ shouldCompileTo('{{12}}', {}, '');
+ shouldCompileTo('{{12}}', { '12': 'bar' }, 'bar');
+ shouldCompileTo('{{12.34}}', {}, '');
+ shouldCompileTo('{{12.34}}', { '12.34': 'bar' }, 'bar');
+ shouldCompileTo('{{12.34 1}}', { '12.34': function(arg) { return 'bar' + arg; } }, 'bar1');
});
- it("pass boolean literals", function() {
- shouldCompileTo("{{true}}", {}, "");
- shouldCompileTo("{{true}}", { "": "foo" }, "");
- shouldCompileTo("{{false}}", { "false": "foo" }, "foo");
+ it('pass boolean literals', function() {
+ shouldCompileTo('{{true}}', {}, '');
+ shouldCompileTo('{{true}}', { '': 'foo' }, '');
+ shouldCompileTo('{{false}}', { 'false': 'foo' }, 'foo');
+ });
+
+ it('should handle literals in subexpression', function() {
+ var helpers = {
+ foo: function(arg) {
+ return arg;
+ }
+ };
+ shouldCompileTo('{{foo (false)}}', [{ 'false': function() { return 'bar'; } }, helpers], 'bar');
});
});
diff --git a/src/handlebars.yy b/src/handlebars.yy
index 7de04dd..18f03b9 100644
--- a/src/handlebars.yy
+++ b/src/handlebars.yy
@@ -66,7 +66,7 @@ inverseChain
;
closeBlock
- : OPEN_ENDBLOCK path CLOSE -> {path: $2, strip: yy.stripFlags($1, $3)}
+ : OPEN_ENDBLOCK helperName CLOSE -> {path: $2, strip: yy.stripFlags($1, $3)}
;
mustache
@@ -81,11 +81,7 @@ partial
;
param
- : path -> $1
- | STRING -> new yy.StringLiteral($1, yy.locInfo(@$))
- | NUMBER -> new yy.NumberLiteral($1, yy.locInfo(@$))
- | BOOLEAN -> new yy.BooleanLiteral($1, yy.locInfo(@$))
- | dataName -> $1
+ : helperName -> $1
| sexpr -> $1
;
@@ -108,7 +104,7 @@ blockParams
helperName
: path -> $1
| dataName -> $1
- | STRING -> new yy.StringLiteral($1, yy.locInfo(@$)), yy.locInfo(@$)
+ | STRING -> new yy.StringLiteral($1, yy.locInfo(@$))
| NUMBER -> new yy.NumberLiteral($1, yy.locInfo(@$))
| BOOLEAN -> new yy.BooleanLiteral($1, yy.locInfo(@$))
;