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

Fix right-side inline whitespace trimming #885

Merged
merged 4 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/main/java/com/hubspot/jinjava/tree/TreeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private Node text(TextToken textToken) {
final Node lastSibling = getLastSibling();

// if last sibling was a tag and has rightTrimAfterEnd, strip whitespace
if (lastSibling instanceof TagNode && lastSibling.getMaster().isRightTrimAfterEnd()) {
if (lastSibling instanceof TagNode && isRightTrim((TagNode) lastSibling)) {
textToken.setLeftTrim(true);
}

Expand All @@ -171,6 +171,20 @@ private Node text(TextToken textToken) {
return n;
}

private boolean isRightTrim(TagNode lastSibling) {
return (
lastSibling.getEndName() == null ||
(
lastSibling.getTag() instanceof FlexibleTag &&
!((FlexibleTag) lastSibling.getTag()).hasEndTag(
(TagToken) lastSibling.getMaster()
)
)
)
? lastSibling.getMaster().isRightTrim()
: lastSibling.getMaster().isRightTrimAfterEnd();
}

private Node expression(ExpressionToken expressionToken) {
ExpressionNode n = createExpressionNode(expressionToken);
n.setParent(parent);
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/com/hubspot/jinjava/EagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1073,4 +1073,9 @@ public void itReconstructsMapNodeSecondPass() {
"reconstructs-map-node.expected"
);
}

@Test
public void itHasProperLineStripping() {
expectedTemplateInterpreter.assertExpectedOutput("has-proper-line-stripping");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -636,15 +636,15 @@ public void itHandlesVarFromImportedMacro() {
);
assertThat(result.trim())
.isEqualTo(
"{% set var = [] %}{% do var.append('a' ~ deferred) %}\n" +
"{% set var = [] %}{% do var.append('a' ~ deferred) %}" +
"a\n" +
"{% do var.append('b' ~ deferred) %}\n" +
"{% do var.append('b' ~ deferred) %}" +
"b\n" +
"c{{ var }}"
);
context.put("deferred", "resolved");
assertThat(interpreter.render(result).trim())
.isEqualTo("a\n" + "\n" + "b\n" + "c['aresolved', 'bresolved']");
.isEqualTo("a\n" + "b\n" + "c['aresolved', 'bresolved']");
}

@Test
Expand All @@ -656,7 +656,7 @@ public void itPutsDeferredInOtherSpotValuesOnContext() {
"{{ apply('foo') }}\n" +
"{{ val }}"
);
assertThat(result.trim()).isEqualTo("{% set val = deferred %}\n5foo\n{{ val }}");
assertThat(result.trim()).isEqualTo("{% set val = deferred %}5foo\n{{ val }}");
context.put("deferred", "resolved");
assertThat(interpreter.render(result).trim()).isEqualTo("5foo\nresolved");
}
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/com/hubspot/jinjava/tree/TreeParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ public void itStripsAllOuterWhiteSpace() throws Exception {
assertThat(interpreter.render(tree)).isEqualTo(".123.");
}

@Test
public void itStripsAllOuterWhiteSpaceForInlineTags() throws Exception {
String expression = "1\n\n{%- print 2 -%}\n\n3\n\n{%- set x = 1 -%}\n\n4";
final Node tree = new TreeParser(interpreter, expression).buildTree();
assertThat(interpreter.render(tree)).isEqualTo("1234");
}

@Test
public void itStripsAllOuterWhiteSpaceWithComment() throws Exception {
String expression =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
Is ({{ my_list }}),
{% print macro_append(first) %}
Is ({{ my_list }}),
{%- do macro_append(deferred) -%}
{%- do macro_append(deferred) %}
Is ({{ my_list }}),
{% print macro_append(deferred2) %}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
I am foo



No more foo
10 changes: 3 additions & 7 deletions src/test/resources/eager/eagerly-defers-macro.expected.jinja
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
{% macro big_guy() %}
{% if deferred %}I am foo
{% else %}I am bar
{% endif %}
{% if deferred %}I am foo{% else %}I am bar{% endif %}
{% endmacro %}{% print big_guy() %}
{% macro big_guy() %}
{% if deferred %}No more foo
{% else %}I am bar
{% endif %}
{% endmacro %}{% print big_guy() %}
{% if deferred %}No more foo{% else %}I am bar{% endif %}
{% endmacro %}{% print big_guy() %}
2 changes: 1 addition & 1 deletion src/test/resources/eager/eagerly-defers-macro.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
{% endif %}
{% endmacro %}
{% print big_guy() %}
{%- set foo = 'No more foo' -%}
{% set foo = 'No more foo' -%}
{% print big_guy() %}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{% set myname = deferred + 7 %}{% set __ignored__ %}
{% set bar = myname + 19 %}
Hello {{ myname }}
{% set from_bar = bar %}{% endset %}
from_foo: Hello {{ myname }}
{% set from_bar = bar %}{% endset %}from_foo: Hello {{ myname }}
from_bar: {{ from_bar }}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{% set myname = deferred + 3 %}{% set __ignored__ %}
{% set bar = myname + 19 %}
Hello {{ myname }}
{% endset %}
foo: Hello {{ myname }}
{% endset %}foo: Hello {{ myname }}
bar: {{ bar }}
---{% set myname = deferred + 7 %}{% set __ignored__ %}{% set current_path = 'macro-and-set.jinja' %}{% set simple = {} %}
---
{% set myname = deferred + 7 %}{% set __ignored__ %}{% set current_path = 'macro-and-set.jinja' %}{% set simple = {} %}
{% set bar = myname + 19 %}{% set simple = {} %}{% do simple.update({'bar': bar}) %}
Hello {{ myname }}
{% do simple.update({'import_resource_path': 'macro-and-set.jinja'}) %}{% set current_path = '' %}{% endset %}
simple.foo: {% set deferred_import_resource_path = 'macro-and-set.jinja' %}{% macro simple.foo() %}Hello {{ myname }}{% endmacro %}{% set deferred_import_resource_path = null %}{{ simple.foo() }}
{% do simple.update({'import_resource_path': 'macro-and-set.jinja'}) %}{% set current_path = '' %}{% endset %}simple.foo: {% set deferred_import_resource_path = 'macro-and-set.jinja' %}{% macro simple.foo() %}Hello {{ myname }}{% endmacro %}{% set deferred_import_resource_path = null %}{{ simple.foo() }}
simple.bar: {{ simple.bar }}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
foo: {{ foo() }}
bar: {{ bar }}
---
{%- set myname = deferred + (3 + 4) -%}
{% set myname = deferred + (3 + 4) -%}
{%- import "macro-and-set.jinja" as simple -%}
simple.foo: {{ simple.foo() }}
simple.bar: {{ simple.bar }}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
{% if deferred %}{% set __ignored__ %}{% set current_path = 'macro-and-set.jinja' %}{% set simple = {} %}
{% set bar = 'person19' %}{% do simple.update({'bar': bar}) %}
Hello person
{% do simple.update({'bar': 'person19','import_resource_path': 'macro-and-set.jinja'}) %}{% set current_path = '' %}{% endset %}
{% else %}{% set __ignored__ %}{% set current_path = 'macro-and-set.jinja' %}{% set simple = {} %}
{% do simple.update({'bar': 'person19','import_resource_path': 'macro-and-set.jinja'}) %}{% set current_path = '' %}{% endset %}{% else %}{% set __ignored__ %}{% set current_path = 'macro-and-set.jinja' %}{% set simple = {} %}
{% set bar = 'person19' %}{% do simple.update({'bar': bar}) %}
Hello person
{% do simple.update({'bar': 'person19','import_resource_path': 'macro-and-set.jinja'}) %}{% set current_path = '' %}{% endset %}
{% endif %}
{% do simple.update({'bar': 'person19','import_resource_path': 'macro-and-set.jinja'}) %}{% set current_path = '' %}{% endset %}{% endif %}
simple.foo: {{ simple.foo() }}
simple.bar: {{ simple.bar }}
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
item1 resolved.
item1resolved
item2 resolved.
item2resolved
item3 resolved.
item3resolved
item1 resolved.item1resolved
item2 resolved.item2resolved
item3 resolved.item3resolved
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
item1 {{ deferred }}.{% set temp = 'item1' ~ deferred %}
{{ temp }}
item2 {{ deferred }}.{% set temp = 'item2' ~ deferred %}
{{ temp }}
item3 {{ deferred }}.{% set temp = 'item3' ~ deferred %}
{{ temp }}
item1 {{ deferred }}.{% set temp = 'item1' ~ deferred %}{{ temp }}
item2 {{ deferred }}.{% set temp = 'item2' ~ deferred %}{{ temp }}
item3 {{ deferred }}.{% set temp = 'item3' ~ deferred %}{{ temp }}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
foo: {{ foo() }}
bar: {{ bar }}
---
{%- set myname = (3 + 4) -%}
{% set myname = (3 + 4) -%}
{%- import "macro-and-set.jinja" as simple -%}
simple.foo: {{ simple.foo() }}
simple.bar: {{ simple.bar }}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
start: {% cycle 'start' %}
foo: {% cycle foo %}
one/two: {% cycle 'one','two' %}
{%- cycle 'a','b','c' as letter -%}
{% cycle 'a','b','c' as letter -%}
letter: {% cycle letter %}
{%- endfor -%}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Some ({{ unknown(null) }}) expression
{%- set foo = unknown(null) -%}
{% set foo = unknown(null) -%}
Nothing: ({{ foo }})
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1
2
3{% if deferred > 0 %}{{ deferred }}{% elif deferred == 0 %}null{% else %}{{ deferred }}{% endif %}
10 changes: 10 additions & 0 deletions src/test/resources/eager/has-proper-line-stripping.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
1
{% do 1 -%}
2
3{% if deferred > 0 -%}
{{ deferred }}
{%- elif deferred == 0 -%}
null
{%- else -%}
{{ deferred }}
{%- endif %}
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
{% set myname = deferred + 3 %}
{% set deferred_import_resource_path = 'simple-with-call.jinja' %}{% macro getPath() %}Hello {{ myname }}{% endmacro %}{% set deferred_import_resource_path = null %}{% print getPath() %}
{% set myname = deferred + 3 %}{% set deferred_import_resource_path = 'simple-with-call.jinja' %}{% macro getPath() %}Hello {{ myname }}{% endmacro %}{% set deferred_import_resource_path = null %}{% print getPath() %}
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
{% set myname = deferred + 3 %}
{% set deferred_import_resource_path = 'simple-with-call.jinja' %}{% macro simple.getPath() %}Hello {{ myname }}{% endmacro %}{% set deferred_import_resource_path = null %}{% print simple.getPath() %}
{% set myname = deferred + 3 %}{% set deferred_import_resource_path = 'simple-with-call.jinja' %}{% macro simple.getPath() %}Hello {{ myname }}{% endmacro %}{% set deferred_import_resource_path = null %}{% print simple.getPath() %}