diff options
author | kpdecker <kpdecker@gmail.com> | 2015-02-09 23:54:46 -0600 |
---|---|---|
committer | kpdecker <kpdecker@gmail.com> | 2015-02-09 23:54:46 -0600 |
commit | 39121cf8f50ec02b5a979f4911caefef8030161a (patch) | |
tree | d73b514d22c9a66dc3d01b232d1bda8176f56d7e | |
parent | 07f27843dc01058103d084c18a5db297b150d9cb (diff) | |
download | handlebars.js-39121cf8f50ec02b5a979f4911caefef8030161a.zip handlebars.js-39121cf8f50ec02b5a979f4911caefef8030161a.tar.gz handlebars.js-39121cf8f50ec02b5a979f4911caefef8030161a.tar.bz2 |
Handle all potential literal values
Adds support for literal helper names in a few missing cases such as block expressions and subexpressions.
-rw-r--r-- | lib/handlebars/compiler/compiler.js | 14 | ||||
-rw-r--r-- | spec/basic.js | 35 | ||||
-rw-r--r-- | src/handlebars.yy | 10 |
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(@$)) ; |