Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent accidental expressions from being output #1123

Merged
merged 3 commits into from
Oct 28, 2023

Conversation

jasmith-hs
Copy link
Contributor

This adds a feature flag for PREVENT_ACCIDENTAL_EXPRESSIONS where, when combining output, it will check if the last character of the previous node and the first character of the current node create a tag/expression/note start string. If so, it will add a \n{#- #} spacer, which, if the entire output is passed back to jinjava, will tokensise properly, and maintain the correct whitespace due to the left-trim of the note.

Note/expression trimming is added in #1122

For example:

if (true) {
{%- print '% print 123 %' -%}
}

Would normally output as:

if (true) {% print 123 %}

But with this feature enabled, it will output as:

if (true) {
{#- #}% print 123 %}

Which will prevent a PrintTag from being in it, if it were to be passed back into the interpreter.

Comment on lines 75 to 83
String separator = new StringBuilder()
.append('\n')
.append(tokenScannerSymbols.getPrefixChar())
.append(tokenScannerSymbols.getNoteChar())
.append(tokenScannerSymbols.getTrimChar())
.append(' ')
.append(tokenScannerSymbols.getNoteChar())
.append(tokenScannerSymbols.getExprEndChar())
.toString();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could create this statically.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean. Do you mean memoize it inside of TokenScannerSymbols? The result is different depending on what symbols are used

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking that TokenScannerSymbols were static. Doesn't seem that this will be called too often. If it does, then I think memoization would be helpful.

Comment on lines 97 to 99
cur.charAt(0) == tokenScannerSymbols.getTag() ||
cur.charAt(0) == tokenScannerSymbols.getExprStartChar() ||
cur.charAt(0) == tokenScannerSymbols.getNoteChar()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this could be extracted to a useful function for checking if it's the start of something?

Base automatically changed from note-trim to master October 27, 2023 18:28
@jasmith-hs jasmith-hs merged commit 264ac2b into master Oct 28, 2023
4 checks passed
@jasmith-hs jasmith-hs deleted the prevent-accidental-expressions branch October 28, 2023 01:56
jasmith-hs added a commit that referenced this pull request Oct 31, 2023
…essions"

This reverts commit 264ac2b, reversing
changes made to 7a60dca.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants