-
Notifications
You must be signed in to change notification settings - Fork 170
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
Conversation
…tags/notes when PREVENT_ACCIDENTAL_EXPRESSIONS is enabled
String separator = new StringBuilder() | ||
.append('\n') | ||
.append(tokenScannerSymbols.getPrefixChar()) | ||
.append(tokenScannerSymbols.getNoteChar()) | ||
.append(tokenScannerSymbols.getTrimChar()) | ||
.append(' ') | ||
.append(tokenScannerSymbols.getNoteChar()) | ||
.append(tokenScannerSymbols.getExprEndChar()) | ||
.toString(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
cur.charAt(0) == tokenScannerSymbols.getTag() || | ||
cur.charAt(0) == tokenScannerSymbols.getExprStartChar() || | ||
cur.charAt(0) == tokenScannerSymbols.getNoteChar() |
There was a problem hiding this comment.
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?
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:
Would normally output as:
But with this feature enabled, it will output as:
Which will prevent a
PrintTag
from being in it, if it were to be passed back into the interpreter.