Skip to content

Commit 0878179

Browse files
committed
Allow for escaped ] characters in [] IDs
Allows for ] literal characters to be used within [] IDs by prefixing them with the \ character. `\` literal at the end of the may be referenced by the `\\` sequence if conflicting. Under most circumstances the `\\` sequence will continue to work. Potentially breaking change for users of [] ids that have `\\` anywhere in the id or `\` at the end of the id. Fixes #1092
1 parent 641fe33 commit 0878179

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

spec/parser.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ describe('parser', function() {
3939
it('parses mustaches with - in a path', function() {
4040
equals(astFor('{{foo-bar}}'), '{{ PATH:foo-bar [] }}\n');
4141
});
42+
it('parses mustaches with escaped [] in a path', function() {
43+
equals(astFor('{{[foo[\\]]}}'), '{{ PATH:foo[] [] }}\n');
44+
});
45+
it('parses escaped \\\\ in path', function() {
46+
equals(astFor('{{[foo\\\\]}}'), '{{ PATH:foo\\ [] }}\n');
47+
});
4248

4349
it('parses mustaches with parameters', function() {
4450
equals(astFor('{{foo bar}}'), '{{ PATH:foo [PATH:bar] }}\n');

spec/tokenizer.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ describe('Tokenizer', function() {
146146
shouldMatchTokens(result, ['OPEN', 'ID', 'SEP', 'ID', 'CLOSE', 'OPEN', 'ID', 'SEP', 'ID', 'CLOSE']);
147147
});
148148

149+
it('allows escaped literals in []', function() {
150+
var result = tokenize('{{foo.[bar\\]]}}');
151+
shouldMatchTokens(result, ['OPEN', 'ID', 'SEP', 'ID', 'CLOSE']);
152+
});
153+
149154
it('tokenizes {{.}} as OPEN ID CLOSE', function() {
150155
var result = tokenize('{{.}}');
151156
shouldMatchTokens(result, ['OPEN', 'ID', 'CLOSE']);

src/handlebars.l

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ ID [^\s!"#%-,\.\/;->@\[-\^`\{-~]+/{LOOKAHEAD}
120120
121121
<mu>{ID} return 'ID';
122122
123-
<mu>'['[^\]]*']' return 'ID';
123+
<mu>'['('\\]'|[^\]])*']' yytext = yytext.replace(/\\([\\\]])/g,'$1'); return 'ID';
124124
<mu>. return 'INVALID';
125125
126126
<INITIAL,mu><<EOF>> return 'EOF';

0 commit comments

Comments
 (0)