HubSpot / jinjava

Jinja template engine for Java
Apache License 2.0
701 stars 169 forks source link

Prevent accidental expressions from being output #1123

Closed jasmith-hs closed 1 year ago

jasmith-hs commented 1 year ago

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 https://github.com/HubSpot/jinjava/pull/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.