raw block helpers

This commit is contained in:
Jesse Ezell
2013-07-11 14:50:14 -07:00
committed by kpdecker
parent 954253305a
commit 9b14dc40a5
5 changed files with 40 additions and 10 deletions
+9
View File
@@ -131,6 +131,15 @@ var AST = {
}
},
RawBlockNode: function(mustache, content, locInfo) {
LocationInfo.call(this, locInfo);
this.type = 'block';
this.mustache = mustache;
mustache.sexpr.isHelper = mustache.isHelper = true;
mustache.params.splice(0, 0, new AST.StringNode(content, locInfo));
},
ContentNode: function(string, locInfo) {
LocationInfo.call(this, locInfo);
this.type = "content";
-8
View File
@@ -20,14 +20,6 @@ describe('blocks', function() {
equal(result, "0. goodbye! 1. Goodbye! 2. GOODBYE! cruel world!", "The @index variable is used");
});
it("raw block", function() {
var string = "{{{{ {{test}} }}}}";
var hash = { test: "hello" };
shouldCompileTo(string, hash, " {{test}} ",
"raw block ignores blocks");
});
it("empty block", function() {
var string = "{{#goodbyes}}{{/goodbyes}}cruel {{world}}!";
+20
View File
@@ -9,6 +9,26 @@ describe('helpers', function() {
shouldCompileTo(string, [hash, helpers], "<a href='/root/goodbye'>Goodbye</a>");
});
it("helper for raw block gets raw content", function() {
var string = "{{{{raw}}}} {{test}} {{{{/raw}}}}";
var hash = { test: "hello" };
var helpers = { raw: function(content) {
return content;
} };
shouldCompileTo(string, [hash, helpers], " {{test}} ",
"raw block helper gets raw content");
});
it("helper for raw block gets parameters", function() {
var string = "{{{{raw 1 2 3}}}} {{test}} {{{{/raw}}}}";
var hash = { test: "hello" };
var helpers = { raw: function(content, a, b, c) {
return content + a + b + c;
} };
shouldCompileTo(string, [hash, helpers], " {{test}} 123",
"raw block helper gets raw content");
});
it("helper block with complex lookup expression", function() {
var string = "{{#goodbyes}}{{../name}}{{/goodbyes}}";
var hash = {name: "Alan"};
+6 -1
View File
@@ -1,5 +1,5 @@
%x mu emu com
%x mu emu com raw
%{
@@ -49,11 +49,16 @@ ID [^\s!"#%-,\.\/;->@\[-\^`\{-~]+/{LOOKAHEAD}
return 'CONTENT';
}
<raw>"{{{{/"[^\s!"#%-,\.\/;->@\[-\^`\{-~]+/[=}\s\/.]"}}}}" { yytext = yytext.substr(5, yyleng-9); this.popState(); return 'END_RAW_BLOCK'; }
<raw>[^\x00]*?/("{{{{/") { return 'CONTENT'; }
<com>[\s\S]*?"--}}" strip(0,4); this.popState(); return 'COMMENT';
<mu>"(" return 'OPEN_SEXPR';
<mu>")" return 'CLOSE_SEXPR';
<mu>"{{{{" { return 'OPEN_RAW_BLOCK'; }
<mu>"}}}}" { this.popState(); this.begin('raw'); return 'CLOSE_RAW_BLOCK'; }
<mu>"{{{{"[^\x00]*"}}}}" {
yytext = yytext.substr(4, yyleng-8);
this.popState();
+5 -1
View File
@@ -35,7 +35,7 @@ statements
;
statement
: RAW_BLOCK { $$ = new yy.ContentNode($1, @$); }
: openRawBlock CONTENT END_RAW_BLOCK -> new yy.RawBlockNode($1, $2, @$)
| openInverse program closeBlock -> new yy.BlockNode($1, $2.inverse, $2, $3, @$)
| openBlock program closeBlock -> new yy.BlockNode($1, $2, $2.inverse, $3, @$)
| mustache -> $1
@@ -44,6 +44,10 @@ statement
| COMMENT -> new yy.CommentNode($1, @$)
;
openRawBlock
: OPEN_RAW_BLOCK sexpr CLOSE_RAW_BLOCK -> new yy.MustacheNode($2, null, '', '', @$)
;
openBlock
: OPEN_BLOCK sexpr CLOSE -> new yy.MustacheNode($2, null, $1, stripFlags($1, $3), @$)
;