Skip to content

Commit f1752fe

Browse files
committed
fix: prevent zero length tokens in raw-blocks (#1577)
This comment ensures that the lexer is not stuck on zero-length tokens, in raw-blocks like {{{{a}}}} {{{{/a}}}}
1 parent 0b593bf commit f1752fe

3 files changed

Lines changed: 30 additions & 9 deletions

File tree

spec/helpers.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,35 @@ 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) {
31+
describe('raw block parsing (with identity helper-function)', function() {
32+
33+
function runWithIdentityHelper(template, expected) {
34+
var helpers = {
35+
identity: function(options) {
3536
return options.fn();
36-
}
37-
};
38-
shouldCompileTo(string, [{}, helpers], ' {{{{b}}}} {{{{/b}}}} ', 'raw block helper should get nested raw block as raw content');
37+
}
38+
};
39+
shouldCompileTo(template, [{}, helpers], expected);
40+
}
41+
42+
it('helper for nested raw block gets raw content', function() {
43+
runWithIdentityHelper('{{{{identity}}}} {{{{b}}}} {{{{/b}}}} {{{{/identity}}}}', ' {{{{b}}}} {{{{/b}}}} ');
44+
});
45+
46+
it('helper for nested raw block works with empty content', function() {
47+
runWithIdentityHelper('{{{{identity}}}}{{{{/identity}}}}', '');
48+
});
49+
50+
it('helper for nested raw block works if nested raw blocks are broken', function() {
51+
runWithIdentityHelper('{{{{identity}}}} {{{{a}}}} {{{{ {{{{/ }}}} }}}} {{{{/identity}}}}', ' {{{{a}}}} {{{{ {{{{/ }}}} }}}} ');
52+
});
53+
54+
it('helper for nested raw block throw exception when with missing closing braces', function() {
55+
var string = '{{{{a}}}} {{{{/a';
56+
shouldThrow(function() {
57+
Handlebars.compile(string)();
58+
});
59+
});
3960
});
4061

4162
it('helper block with identical context', function() {

src/handlebars.l

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ ID [^\s!"#%-,\.\/;->@\[-\^`\{-~]+/{LOOKAHEAD}
6363
return 'END_RAW_BLOCK';
6464
}
6565
}
66-
<raw>[^\x00]*?/("{{{{") { return 'CONTENT'; }
66+
<raw>[^\x00]+/("{{{{") { return 'CONTENT'; }
6767

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

src/handlebars.yy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ content
3939
};
4040

4141
rawBlock
42-
: openRawBlock content+ END_RAW_BLOCK -> yy.prepareRawBlock($1, $2, $3, @$)
42+
: openRawBlock content* END_RAW_BLOCK -> yy.prepareRawBlock($1, $2, $3, @$)
4343
;
4444

4545
openRawBlock

0 commit comments

Comments
 (0)