Add support for inverted sections in the parser

This commit is contained in:
wycats
2010-12-03 00:54:18 -05:00
parent 87b526df27
commit 8f6f3051e6
5 changed files with 27 additions and 5 deletions
+2 -1
View File
@@ -21,10 +21,11 @@ Handlebars.AST.PartialNode = function(id, context) {
this.context = context;
};
Handlebars.AST.BlockNode = function(mustache, program) {
Handlebars.AST.BlockNode = function(mustache, program, inverted) {
this.type = "block";
this.mustache = mustache;
this.program = program;
this.inverted = inverted;
};
Handlebars.AST.ContentNode = function(string) {
+1 -1
View File
@@ -50,7 +50,7 @@ Handlebars.PrintVisitor.prototype.program = function(program) {
Handlebars.PrintVisitor.prototype.block = function(block) {
var out = "";
out = out + this.pad("BLOCK:");
out = out + (block.inverted ? this.pad("INVERSE:") : this.pad("BLOCK:"));
this.padding++;
out = out + this.accept(block.mustache);
out = out + this.accept(block.program);
+14 -2
View File
@@ -55,8 +55,8 @@ describe "Parser" do
with_padding { yield }
end
def block
pad("BLOCK:")
def block(inverse = false)
inverse ? pad("INVERSE:") : pad("BLOCK:")
with_padding { yield }
end
@@ -148,6 +148,18 @@ describe "Parser" do
end
end
it "parses a standalone inverse section" do
ast_for("{{^foo}}bar{{/baz}}").should == program do
block true do
mustache id("foo")
program do
content "bar"
end
end
end
end
it "raises if there's a Parse error" do
lambda { ast_for("{{foo}") }.should raise_error(V8::JSError, /Parse error on line 1/)
lambda { ast_for("{{foo &}}")}.should raise_error(V8::JSError, /Parse error on line 1/)
+4
View File
@@ -42,6 +42,10 @@ module Handlebars
context.load('lib/handlebars/runtime.js')
context.load('lib/handlebars/utils.js')
context.load('lib/handlebars.js')
context["p"] = proc do |val|
p val
end
end
end
end
+6 -1
View File
@@ -18,7 +18,8 @@ statements
;
statement
: openBlock program closeBlock { $$ = new yy.BlockNode($1, $2) }
: openInverse program closeBlock { $$ = new yy.BlockNode($1, $2, true) }
| openBlock program closeBlock { $$ = new yy.BlockNode($1, $2, false) }
| mustache { $$ = $1 }
| partial { $$ = $1 }
| CONTENT { $$ = new yy.ContentNode($1) }
@@ -29,6 +30,10 @@ openBlock
: OPEN_BLOCK inMustache CLOSE { $$ = new yy.MustacheNode($2) }
;
openInverse
: OPEN_INVERSE inMustache CLOSE { $$ = new yy.MustacheNode($2) }
;
closeBlock
: OPEN_ENDBLOCK path CLOSE { }
;