Inverse should really be its own node, as it has totally different runtime semantics

This commit is contained in:
wycats
2010-12-03 01:10:14 -05:00
parent 8f6f3051e6
commit c89ecf80a5
4 changed files with 31 additions and 8 deletions
+7 -2
View File
@@ -21,11 +21,16 @@ Handlebars.AST.PartialNode = function(id, context) {
this.context = context; this.context = context;
}; };
Handlebars.AST.BlockNode = function(mustache, program, inverted) { Handlebars.AST.BlockNode = function(mustache, program) {
this.type = "block"; this.type = "block";
this.mustache = mustache; this.mustache = mustache;
this.program = program; this.program = program;
this.inverted = inverted; };
Handlebars.AST.InverseNode = function(mustache, program) {
this.type = "inverse";
this.mustache = mustache;
this.program = program;
}; };
Handlebars.AST.ContentNode = function(string) { Handlebars.AST.ContentNode = function(string) {
+14 -1
View File
@@ -50,7 +50,7 @@ Handlebars.PrintVisitor.prototype.program = function(program) {
Handlebars.PrintVisitor.prototype.block = function(block) { Handlebars.PrintVisitor.prototype.block = function(block) {
var out = ""; var out = "";
out = out + (block.inverted ? this.pad("INVERSE:") : this.pad("BLOCK:")); out = out + this.pad("BLOCK:");
this.padding++; this.padding++;
out = out + this.accept(block.mustache); out = out + this.accept(block.mustache);
out = out + this.accept(block.program); out = out + this.accept(block.program);
@@ -59,6 +59,19 @@ Handlebars.PrintVisitor.prototype.block = function(block) {
return out; return out;
}; };
Handlebars.PrintVisitor.prototype.inverse = function(block) {
var out = "";
out = out + this.pad("INVERSE:");
this.padding++;
out = out + this.accept(block.mustache);
out = out + this.accept(block.program);
this.padding--;
return out;
};
Handlebars.PrintVisitor.prototype.mustache = function(mustache) { Handlebars.PrintVisitor.prototype.mustache = function(mustache) {
var params = mustache.params, paramStrings = []; var params = mustache.params, paramStrings = [];
+8 -3
View File
@@ -55,8 +55,13 @@ describe "Parser" do
with_padding { yield } with_padding { yield }
end end
def block(inverse = false) def block
inverse ? pad("INVERSE:") : pad("BLOCK:") pad("BLOCK:")
with_padding { yield }
end
def inverted_block
pad("INVERSE:")
with_padding { yield } with_padding { yield }
end end
@@ -150,7 +155,7 @@ describe "Parser" do
it "parses a standalone inverse section" do it "parses a standalone inverse section" do
ast_for("{{^foo}}bar{{/baz}}").should == program do ast_for("{{^foo}}bar{{/baz}}").should == program do
block true do inverted_block do
mustache id("foo") mustache id("foo")
program do program do
+2 -2
View File
@@ -18,8 +18,8 @@ statements
; ;
statement statement
: openInverse program closeBlock { $$ = new yy.BlockNode($1, $2, true) } : openInverse program closeBlock { $$ = new yy.InverseNode($1, $2) }
| openBlock program closeBlock { $$ = new yy.BlockNode($1, $2, false) } | openBlock program closeBlock { $$ = new yy.BlockNode($1, $2) }
| mustache { $$ = $1 } | mustache { $$ = $1 }
| partial { $$ = $1 } | partial { $$ = $1 }
| CONTENT { $$ = new yy.ContentNode($1) } | CONTENT { $$ = new yy.ContentNode($1) }