Skip to content

Commit d7831b1

Browse files
committed
Handle invalid escaped quotes in tags more gracefully
Don't break the entire parsing of the template afterwards by treating escaped quotes which aren't already in quotes as not being quotes
1 parent 69379e5 commit d7831b1

3 files changed

Lines changed: 30 additions & 8 deletions

File tree

src/main/java/com/hubspot/jinjava/tree/parse/TokenScanner.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,15 @@ private Token getNextToken() {
6868
}
6969

7070
if (inBlock > 0) {
71-
if (inQuote != 0) {
71+
if (c == '\\') {
72+
++currPost;
73+
continue;
74+
} else if (inQuote != 0) {
7275
if (inQuote == c) {
7376
inQuote = 0;
74-
continue;
75-
} else if (c == '\\') {
76-
++currPost;
77-
continue;
78-
} else {
79-
continue;
8077
}
81-
} else if (inQuote == 0 && (c == '\'' || c == '"')) {
78+
continue;
79+
} else if (c == '\'' || c == '"') {
8280
inQuote = c;
8381
continue;
8482
}

src/test/java/com/hubspot/jinjava/tree/parse/TokenScannerTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,23 @@ public void testLstripBlocks() {
305305
assertThat(tokens).isNotEmpty();
306306
}
307307

308+
@Test
309+
public void itTreatsEscapedQuotesSameWhenNotInQuotes() {
310+
List<Token> tokens = tokens("tag-with-all-escaped-quotes");
311+
assertThat(tokens).hasSize(8);
312+
assertThat(tokens.stream().map(Token::getType).collect(Collectors.toList()))
313+
.containsExactly(
314+
symbols.getNote(),
315+
symbols.getFixed(),
316+
symbols.getTag(),
317+
symbols.getFixed(),
318+
symbols.getTag(),
319+
symbols.getFixed(),
320+
symbols.getTag(),
321+
symbols.getFixed()
322+
);
323+
}
324+
308325
private List<Token> tokens(String fixture) {
309326
TokenScanner t = fixture(fixture);
310327
return Lists.newArrayList(t);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{# This print tag is invalid, but it should not cause the rest of the template to break #}
2+
{% print \"foo\" %}
3+
Start
4+
{% if true %}
5+
The dog says: "Woof!"
6+
{% endif %}
7+
End

0 commit comments

Comments
 (0)