diff --git a/src/main/java/com/hubspot/jinjava/tree/parse/TokenScanner.java b/src/main/java/com/hubspot/jinjava/tree/parse/TokenScanner.java index 78d3ee2cc..9f4475db5 100644 --- a/src/main/java/com/hubspot/jinjava/tree/parse/TokenScanner.java +++ b/src/main/java/com/hubspot/jinjava/tree/parse/TokenScanner.java @@ -68,17 +68,15 @@ private Token getNextToken() { } if (inBlock > 0) { - if (inQuote != 0) { + if (c == '\\') { + ++currPost; + continue; + } else if (inQuote != 0) { if (inQuote == c) { inQuote = 0; - continue; - } else if (c == '\\') { - ++currPost; - continue; - } else { - continue; } - } else if (inQuote == 0 && (c == '\'' || c == '"')) { + continue; + } else if (c == '\'' || c == '"') { inQuote = c; continue; } diff --git a/src/test/java/com/hubspot/jinjava/tree/parse/TokenScannerTest.java b/src/test/java/com/hubspot/jinjava/tree/parse/TokenScannerTest.java index 030e87fc9..8ba2955e7 100644 --- a/src/test/java/com/hubspot/jinjava/tree/parse/TokenScannerTest.java +++ b/src/test/java/com/hubspot/jinjava/tree/parse/TokenScannerTest.java @@ -305,6 +305,23 @@ public void testLstripBlocks() { assertThat(tokens).isNotEmpty(); } + @Test + public void itTreatsEscapedQuotesSameWhenNotInQuotes() { + List tokens = tokens("tag-with-all-escaped-quotes"); + assertThat(tokens).hasSize(8); + assertThat(tokens.stream().map(Token::getType).collect(Collectors.toList())) + .containsExactly( + symbols.getNote(), + symbols.getFixed(), + symbols.getTag(), + symbols.getFixed(), + symbols.getTag(), + symbols.getFixed(), + symbols.getTag(), + symbols.getFixed() + ); + } + private List tokens(String fixture) { TokenScanner t = fixture(fixture); return Lists.newArrayList(t); diff --git a/src/test/resources/parse/tokenizer/tag-with-all-escaped-quotes.jinja b/src/test/resources/parse/tokenizer/tag-with-all-escaped-quotes.jinja new file mode 100644 index 000000000..5f9a709a9 --- /dev/null +++ b/src/test/resources/parse/tokenizer/tag-with-all-escaped-quotes.jinja @@ -0,0 +1,7 @@ +{# This print tag is invalid, but it should not cause the rest of the template to break #} +{% print \"foo\" %} +Start +{% if true %} +The dog says: "Woof!" +{% endif %} +End