Skip to content

Commit c83c363

Browse files
committed
Merge pull request #1060 from ericbn/issue-1056
#1056 Fixed grammar for nested raw blocks
2 parents a458fe2 + aa7a45b commit c83c363

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

lib/handlebars/compiler/helpers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ export function prepareMustache(path, params, hash, open, strip, locInfo) {
7070
return new this.MustacheStatement(path, params, hash, escaped, strip, this.locInfo(locInfo));
7171
}
7272

73-
export function prepareRawBlock(openRawBlock, content, close, locInfo) {
73+
export function prepareRawBlock(openRawBlock, contents, close, locInfo) {
7474
if (openRawBlock.path.original !== close) {
7575
let errorNode = {loc: openRawBlock.path.loc};
7676

7777
throw new Exception(openRawBlock.path.original + " doesn't match " + close, errorNode);
7878
}
7979

8080
locInfo = this.locInfo(locInfo);
81-
let program = new this.Program([content], null, {}, locInfo);
81+
let program = new this.Program(contents, null, {}, locInfo);
8282

8383
return new this.BlockStatement(
8484
openRawBlock.path, openRawBlock.params, openRawBlock.hash,

spec/helpers.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ describe('helpers', function() {
2828
'raw block helper gets raw content');
2929
});
3030

31+
it('helper for nested raw block gets raw content', function() {
32+
var string = '{{{{a}}}} {{{{b}}}} {{{{/b}}}} {{{{/a}}}}';
33+
var helpers = {
34+
a: function(options) {
35+
return options.fn();
36+
}
37+
};
38+
shouldCompileTo(string, [{}, helpers], ' {{{{b}}}} {{{{/b}}}} ', 'raw block helper should get nested raw block as raw content');
39+
});
40+
3141
it('helper block with complex lookup expression', function() {
3242
var string = '{{#goodbyes}}{{../name}}{{/goodbyes}}';
3343
var hash = {name: 'Alan'};

src/handlebars.l

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,21 @@ ID [^\s!"#%-,\.\/;->@\[-\^`\{-~]+/{LOOKAHEAD}
4949
return 'CONTENT';
5050
}
5151

52+
// nested raw block will create stacked 'raw' condition
53+
<raw>"{{{{"/[^/] this.begin('raw'); return 'CONTENT';
5254
<raw>"{{{{/"[^\s!"#%-,\.\/;->@\[-\^`\{-~]+/[=}\s\/.]"}}}}" {
53-
yytext = yytext.substr(5, yyleng-9);
5455
this.popState();
55-
return 'END_RAW_BLOCK';
56+
// Should be using `this.topState()` below, but it currently
57+
// returns the second top instead of the first top. Opened an
58+
// issue about it at https://github.com/zaach/jison/issues/291
59+
if (this.conditionStack[this.conditionStack.length-1] === 'raw') {
60+
return 'CONTENT';
61+
} else {
62+
yytext = yytext.substr(5, yyleng-9);
63+
return 'END_RAW_BLOCK';
64+
}
5665
}
57-
<raw>[^\x00]*?/("{{{{/") { return 'CONTENT'; }
66+
<raw>[^\x00]*?/("{{{{") { return 'CONTENT'; }
5867

5968
<com>[\s\S]*?"--"{RIGHT_STRIP}?"}}" {
6069
this.popState();

src/handlebars.yy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ content
2626
;
2727

2828
rawBlock
29-
: openRawBlock content END_RAW_BLOCK -> yy.prepareRawBlock($1, $2, $3, @$)
29+
: openRawBlock content+ END_RAW_BLOCK -> yy.prepareRawBlock($1, $2, $3, @$)
3030
;
3131

3232
openRawBlock

0 commit comments

Comments
 (0)