Transform literals to path expressions in mustache nodes

This commit is contained in:
Marcio Junior
2015-02-08 19:31:27 -02:00
parent 5cc326d425
commit 07f27843dc
2 changed files with 18 additions and 12 deletions
+9 -5
View File
@@ -172,11 +172,8 @@ Compiler.prototype = {
},
MustacheStatement: function(mustache) {
if (!mustache.path.type.match(/Literal$/)) {
this.SubExpression(mustache);
} else {
this.accept(mustache.path);
}
transformLiteralToPath(mustache);
this.SubExpression(mustache);
if(mustache.escaped && !this.options.noEscape) {
this.opcode('appendEscaped');
@@ -489,3 +486,10 @@ function argEquals(a, b) {
return true;
}
}
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 };
}
}
+9 -7
View File
@@ -231,18 +231,20 @@ describe("basic context", function() {
});
it("pass string literals", function() {
shouldCompileTo('{{"foo"}}', {}, "foo", "works with strings");
shouldCompileTo('{{"foo"}}', { foo: "bar" }, "foo", "uses the provided string instead of lookup in the context object");
shouldCompileTo('{{"foo"}}', {}, "");
shouldCompileTo('{{"foo"}}', { foo: "bar" }, "bar");
});
it("pass number literals", function() {
shouldCompileTo("{{12}}", {}, "12", "works with numbers");
shouldCompileTo("{{12}}", { "12": "bar" }, "12", "uses the provided number instead of lookup in the context object");
shouldCompileTo("{{12}}", {}, "");
shouldCompileTo("{{12}}", { "12": "bar" }, "bar");
shouldCompileTo("{{12.34}}", {}, "");
shouldCompileTo("{{12.34}}", { "12.34": "bar" }, "bar");
});
it("pass boolean literals", function() {
shouldCompileTo("{{true}}", {}, "true", "works with true");
shouldCompileTo("{{true}}", { "true": "foo" }, "true", "uses the primitive true instead of lookup in the context object");
shouldCompileTo("{{false}}", { "false": "foo" }, "false", "uses the primitive false instead of lookup in the context object");
shouldCompileTo("{{true}}", {}, "");
shouldCompileTo("{{true}}", { "": "foo" }, "");
shouldCompileTo("{{false}}", { "false": "foo" }, "foo");
});
});