summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/handlebars/compiler/ast.js14
-rw-r--r--spec/blocks.js2
-rw-r--r--spec/helpers.js20
-rw-r--r--spec/parser.js4
-rw-r--r--src/handlebars.l20
-rw-r--r--src/handlebars.yy7
6 files changed, 64 insertions, 3 deletions
diff --git a/lib/handlebars/compiler/ast.js b/lib/handlebars/compiler/ast.js
index 5a5f514..286917d 100644
--- a/lib/handlebars/compiler/ast.js
+++ b/lib/handlebars/compiler/ast.js
@@ -131,6 +131,20 @@ var AST = {
}
},
+ RawBlockNode: function(mustache, content, close, locInfo) {
+ LocationInfo.call(this, locInfo);
+
+ if (mustache.sexpr.id.original !== close) {
+ throw new Exception(mustache.sexpr.id.original + " doesn't match " + close, this);
+ }
+
+ content = new AST.ContentNode(content, locInfo);
+
+ this.type = 'block';
+ this.mustache = mustache;
+ this.program = new AST.ProgramNode([content], locInfo);
+ },
+
ContentNode: function(string, locInfo) {
LocationInfo.call(this, locInfo);
this.type = "content";
diff --git a/spec/blocks.js b/spec/blocks.js
index d72a44e..8f7c242 100644
--- a/spec/blocks.js
+++ b/spec/blocks.js
@@ -20,7 +20,7 @@ describe('blocks', function() {
equal(result, "0. goodbye! 1. Goodbye! 2. GOODBYE! cruel world!", "The @index variable is used");
});
-
+
it("empty block", function() {
var string = "{{#goodbyes}}{{/goodbyes}}cruel {{world}}!";
var hash = {goodbyes: [{text: "goodbye"}, {text: "Goodbye"}, {text: "GOODBYE"}], world: "world"};
diff --git a/spec/helpers.js b/spec/helpers.js
index 904f56a..ccde982 100644
--- a/spec/helpers.js
+++ b/spec/helpers.js
@@ -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(options) {
+ return options.fn();
+ } };
+ 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(a, b, c, options) {
+ return options.fn() + 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"};
diff --git a/spec/parser.js b/spec/parser.js
index 5b6dc8a..097bb15 100644
--- a/spec/parser.js
+++ b/spec/parser.js
@@ -157,6 +157,10 @@ describe('parser', function() {
shouldThrow(function() {
ast_for("{{#goodbyes}}{{/hellos}}");
}, Error, /goodbyes doesn't match hellos/);
+
+ shouldThrow(function() {
+ ast_for("{{{{goodbyes}}}} {{{{/hellos}}}}");
+ }, Error, /goodbyes doesn't match hellos/);
});
it('knows how to report the correct line number in errors', function() {
diff --git a/src/handlebars.l b/src/handlebars.l
index 630840e..cafdd72 100644
--- a/src/handlebars.l
+++ b/src/handlebars.l
@@ -1,5 +1,5 @@
-%x mu emu com
+%x mu emu com raw
%{
@@ -49,11 +49,29 @@ 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();
+ return 'RAW_BLOCK';
+ }
<mu>"{{"{LEFT_STRIP}?">" return 'OPEN_PARTIAL';
<mu>"{{"{LEFT_STRIP}?"#" return 'OPEN_BLOCK';
<mu>"{{"{LEFT_STRIP}?"/" return 'OPEN_ENDBLOCK';
diff --git a/src/handlebars.yy b/src/handlebars.yy
index 40f68ce..51796ec 100644
--- a/src/handlebars.yy
+++ b/src/handlebars.yy
@@ -35,7 +35,8 @@ statements
;
statement
- : openInverse program closeBlock -> new yy.BlockNode($1, $2.inverse, $2, $3, @$)
+ : openRawBlock CONTENT END_RAW_BLOCK -> new yy.RawBlockNode($1, $2, $3, @$)
+ | openInverse program closeBlock -> new yy.BlockNode($1, $2.inverse, $2, $3, @$)
| openBlock program closeBlock -> new yy.BlockNode($1, $2, $2.inverse, $3, @$)
| mustache -> $1
| partial -> $1
@@ -43,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), @$)
;