fix: treat backslash as escape character only inside quoted strings, matching Jinja2 behaviour (fixes #1304)#1306
Open
jmoraleda wants to merge 4 commits intoHubSpot:masterfrom
Open
Conversation
6dd04ff to
303ccda
Compare
303ccda to
7c6964a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR implements the fix discussed in #1304 , and builds on the scanner infrastructure introduced in #1305 (configurable multi-character delimiters).
Background
In Python's Jinja2, the template scanner is not responsible for backslash interpretation. It locates delimiters, extracts the raw content between them, and passes it to the expression parser. Backslash handling inside string literals is the expression parser's concern. A bare backslash outside a string literal is invalid Python, so Jinja2 never needs to handle it at the scanner level.
Jinjava's
TokenScannerhas historically handled backslash unconditionally in both the char-based and string-based scanning paths:This has two consequences:
\followed by a closing delimiter (e.g.\}}) prevents the delimiter from being recognized. The block runs to the next valid closer, swallowing surrounding text. The resulting expression content is a JUEL lexical error.What this PR adds
A new
LegacyOverridesflaghandleBackslashInQuotesOnly. Whentrue, backslash is treated as an escape character only inside quoted string literals — matching Jinja2 behaviour — and is left untouched outside quotes for the expression parser to handle. Both the char-based and string-based scanning paths respect this flag.LegacyOverridespreset behaviourLegacyOverrides.NONE— flag isfalse(legacy behaviour, default).LegacyOverrides.THREE_POINT_0— flag isfalse. Although this represents the "correct" behaviour, it is not included here because existing templates in production may rely on\}}preventing delimiter recognition, andTHREE_POINT_0is the current default config in Jinjava. Changing it would be a silent breaking change.LegacyOverrides.ALL— flag istrue.ALLis explicitly documented as opting into all new correct behaviours, and the existing test suite passes with this setting.If the maintainers wish to include this in
THREE_POINT_0, the only required change is to update the existing testTokenScannerTest.itTreatsEscapedQuotesSameWhenNotInQuotesto build its config withhandleBackslashInQuotesOnlyexplicitly set tofalse, isolating it from the preset value. That is a one-line change and we are happy to include it in this PR if desired.Tests
BackslashHandlingTestcovers all four combinations of char-based / string-based scanner × legacy / new flag:TokenScanner(rather thanrender(), which throws aFatalTemplateErrorsExceptionin both modes because the expressionx \is a JUEL lexical error). In legacy mode,"prefix {{ x \}} suffix }}"produces two tokens —TEXT "prefix "andEXPR "{{ x \}} suffix }}". In new mode it produces three —TEXT "prefix ",EXPR "{{ x \}}",TEXT " suffix }}".LegacyOverridespreset values are explicitly asserted with comments documenting the rationale.