Skip to content

Commit

Permalink
Merge pull request #557 from dmarcotte/fix-escapes
Browse files Browse the repository at this point in the history
`\\{{foo}}` escaping only works in some situations
  • Loading branch information
kpdecker committed Oct 13, 2013
2 parents e9350f2 + 468fa8b commit dd777c9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
2 changes: 2 additions & 0 deletions spec/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ describe("basic context", function() {

it("escaping", function() {
shouldCompileTo("\\{{foo}}", { foo: "food" }, "{{foo}}");
shouldCompileTo("content \\{{foo}}", { foo: "food" }, "content {{foo}}");
shouldCompileTo("\\\\{{foo}}", { foo: "food" }, "\\food");
shouldCompileTo("content \\\\{{foo}}", { foo: "food" }, "content \\food");
shouldCompileTo("\\\\ {{foo}}", { foo: "food" }, "\\\\ food");
});

Expand Down
38 changes: 38 additions & 0 deletions spec/tokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ describe('Tokenizer', function() {
var result = tokenize("{{foo}} \\{{bar}} {{baz}}");
result.should.match_tokens(['OPEN', 'ID', 'CLOSE', 'CONTENT', 'CONTENT', 'OPEN', 'ID', 'CLOSE']);

result[3].should.be_token("CONTENT", " ");
result[4].should.be_token("CONTENT", "{{bar}} ");
});

Expand All @@ -77,6 +78,43 @@ describe('Tokenizer', function() {
result[4].should.be_token("CONTENT", "{{{bar}}} ");
});

it('supports escaping escape character', function() {
var result = tokenize("{{foo}} \\\\{{bar}} {{baz}}");
result.should.match_tokens(['OPEN', 'ID', 'CLOSE', 'CONTENT', 'OPEN', 'ID', 'CLOSE', 'CONTENT', 'OPEN', 'ID', 'CLOSE']);

result[3].should.be_token("CONTENT", " \\");
result[5].should.be_token("ID", "bar");
});

it('supports escaping multiple escape characters', function() {
var result = tokenize("{{foo}} \\\\{{bar}} \\\\{{baz}}");
result.should.match_tokens(['OPEN', 'ID', 'CLOSE', 'CONTENT', 'OPEN', 'ID', 'CLOSE', 'CONTENT', 'OPEN', 'ID', 'CLOSE']);

result[3].should.be_token("CONTENT", " \\");
result[5].should.be_token("ID", "bar");
result[7].should.be_token("CONTENT", " \\");
result[9].should.be_token("ID", "baz");
});

it('supports mixed escaped delimiters and escaped escape characters', function() {
var result = tokenize("{{foo}} \\\\{{bar}} \\{{baz}}");
result.should.match_tokens(['OPEN', 'ID', 'CLOSE', 'CONTENT', 'OPEN', 'ID', 'CLOSE', 'CONTENT', 'CONTENT', 'CONTENT']);

result[3].should.be_token("CONTENT", " \\");
result[4].should.be_token("OPEN", "{{");
result[5].should.be_token("ID", "bar");
result[7].should.be_token("CONTENT", " ");
result[8].should.be_token("CONTENT", "{{baz}}");
});

it('supports escaped escape character on a triple stash', function() {
var result = tokenize("{{foo}} \\\\{{{bar}}} {{baz}}");
result.should.match_tokens(['OPEN', 'ID', 'CLOSE', 'CONTENT', 'OPEN_UNESCAPED', 'ID', 'CLOSE_UNESCAPED', 'CONTENT', 'OPEN', 'ID', 'CLOSE']);

result[3].should.be_token("CONTENT", " \\");
result[5].should.be_token("ID", "bar");
});

it('tokenizes a simple path', function() {
var result = tokenize("{{foo/bar}}");
result.should.match_tokens(['OPEN', 'ID', 'SEP', 'ID', 'CLOSE']);
Expand Down
12 changes: 9 additions & 3 deletions src/handlebars.l
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@ ID [^\s!"#%-,\.\/;->@\[-\^`\{-~]+/[=}\s\/.]

%%

"\\\\"/("{{") yytext = "\\"; return 'CONTENT';
[^\x00]*?/("{{") {
if(yytext.slice(-1) !== "\\") this.begin("mu");
if(yytext.slice(-1) === "\\") strip(0,1), this.begin("emu");
if(yytext.slice(-2) === "\\\\") {
strip(0,1);
this.begin("mu");
} else if(yytext.slice(-1) === "\\") {
strip(0,1);
this.begin("emu");
} else {
this.begin("mu");
}
if(yytext) return 'CONTENT';
}

Expand Down

0 comments on commit dd777c9

Please sign in to comment.