Skip to content

Commit

Permalink
Add legacy override to turn on trimming for notes and expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
jasmith-hs committed Nov 2, 2023
1 parent cda07c0 commit eafc139
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
20 changes: 19 additions & 1 deletion src/main/java/com/hubspot/jinjava/LegacyOverrides.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class LegacyOverrides {
.withUseNaturalOperatorPrecedence(true)
.withParseWhitespaceControlStrictly(true)
.withAllowAdjacentTextNodes(true)
.withUseTrimmingForNotesAndExpressions(true)
.build();
private final boolean evaluateMapKeys;
private final boolean iterateOverMapKeys;
Expand All @@ -25,6 +26,7 @@ public class LegacyOverrides {
private final boolean useNaturalOperatorPrecedence;
private final boolean parseWhitespaceControlStrictly;
private final boolean allowAdjacentTextNodes;
private final boolean useTrimmingForNotesAndExpressions;

private LegacyOverrides(Builder builder) {
evaluateMapKeys = builder.evaluateMapKeys;
Expand All @@ -35,6 +37,7 @@ private LegacyOverrides(Builder builder) {
useNaturalOperatorPrecedence = builder.useNaturalOperatorPrecedence;
parseWhitespaceControlStrictly = builder.parseWhitespaceControlStrictly;
allowAdjacentTextNodes = builder.allowAdjacentTextNodes;
useTrimmingForNotesAndExpressions = builder.useTrimmingForNotesAndExpressions;
}

public static Builder newBuilder() {
Expand Down Expand Up @@ -73,6 +76,10 @@ public boolean isAllowAdjacentTextNodes() {
return allowAdjacentTextNodes;
}

public boolean isUseTrimmingForNotesAndExpressions() {
return useTrimmingForNotesAndExpressions;
}

public static class Builder {
private boolean evaluateMapKeys = false;
private boolean iterateOverMapKeys = false;
Expand All @@ -82,6 +89,7 @@ public static class Builder {
private boolean useNaturalOperatorPrecedence = false;
private boolean parseWhitespaceControlStrictly = false;
private boolean allowAdjacentTextNodes = false;
private boolean useTrimmingForNotesAndExpressions = false;

private Builder() {}

Expand All @@ -102,7 +110,10 @@ public static Builder from(LegacyOverrides legacyOverrides) {
.withParseWhitespaceControlStrictly(
legacyOverrides.parseWhitespaceControlStrictly
)
.withAllowAdjacentTextNodes(legacyOverrides.allowAdjacentTextNodes);
.withAllowAdjacentTextNodes(legacyOverrides.allowAdjacentTextNodes)
.withUseTrimmingForNotesAndExpressions(
legacyOverrides.useTrimmingForNotesAndExpressions
);
}

public Builder withEvaluateMapKeys(boolean evaluateMapKeys) {
Expand Down Expand Up @@ -150,5 +161,12 @@ public Builder withAllowAdjacentTextNodes(boolean allowAdjacentTextNodes) {
this.allowAdjacentTextNodes = allowAdjacentTextNodes;
return this;
}

public Builder withUseTrimmingForNotesAndExpressions(
boolean useTrimmingForNotesAndExpressions
) {
this.useTrimmingForNotesAndExpressions = useTrimmingForNotesAndExpressions;
return this;
}
}
}
16 changes: 14 additions & 2 deletions src/main/java/com/hubspot/jinjava/tree/TreeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.google.common.collect.Iterators;
import com.google.common.collect.PeekingIterator;
import com.hubspot.jinjava.JinjavaConfig;
import com.hubspot.jinjava.interpret.DisabledException;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.MissingEndTagException;
Expand Down Expand Up @@ -102,7 +103,7 @@ public Node buildTree() {

private Node nextNode() {
Token token = scanner.next();
if (token.isLeftTrim()) {
if (token.isLeftTrim() && isTrimmingEnabledForToken(token, interpreter.getConfig())) {
final Node lastSibling = getLastSibling();
if (lastSibling instanceof TextNode) {
lastSibling.getMaster().setRightTrim(true);
Expand Down Expand Up @@ -182,7 +183,11 @@ private Node text(TextToken textToken) {
final Node lastSibling = getLastSibling();

// if last sibling was a tag and has rightTrimAfterEnd, strip whitespace
if (lastSibling != null && isRightTrim(lastSibling)) {
if (
lastSibling != null &&
isRightTrim(lastSibling) &&
isTrimmingEnabledForToken(lastSibling.getMaster(), interpreter.getConfig())
) {
textToken.setLeftTrim(true);
}

Expand Down Expand Up @@ -315,4 +320,11 @@ private void endTag(Tag tag, TagToken tagToken) {
);
}
}

private boolean isTrimmingEnabledForToken(Token token, JinjavaConfig jinjavaConfig) {
if (token instanceof TagToken || token instanceof TextToken) {
return true;
}
return jinjavaConfig.getLegacyOverrides().isUseTrimmingForNotesAndExpressions();
}
}
34 changes: 32 additions & 2 deletions src/test/java/com/hubspot/jinjava/tree/TreeParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,22 @@ public void itWarnsTwiceAgainstUnclosedBlockTag() {
public void itTrimsNotes() {
String expression = "A\n{#- note -#}\nB";
final Node tree = new TreeParser(interpreter, expression).buildTree();
assertThat(interpreter.render(tree)).isEqualTo("AB");
assertThat(interpreter.render(tree)).isEqualTo("A\n\nB");
interpreter =
new Jinjava(
JinjavaConfig
.newBuilder()
.withLegacyOverrides(
LegacyOverrides
.newBuilder()
.withUseTrimmingForNotesAndExpressions(true)
.build()
)
.build()
)
.newInterpreter();
final Node newTree = new TreeParser(interpreter, expression).buildTree();
assertThat(interpreter.render(newTree)).isEqualTo("AB");
}

@Test
Expand All @@ -243,7 +258,22 @@ public void itMergesTextNodesWhileRespectingTrim() {
public void itTrimsExpressions() {
String expression = "A\n{{- 'B' -}}\nC";
final Node tree = new TreeParser(interpreter, expression).buildTree();
assertThat(interpreter.render(tree)).isEqualTo("ABC");
assertThat(interpreter.render(tree)).isEqualTo("A\nB\nC");
interpreter =
new Jinjava(
JinjavaConfig
.newBuilder()
.withLegacyOverrides(
LegacyOverrides
.newBuilder()
.withUseTrimmingForNotesAndExpressions(true)
.build()
)
.build()
)
.newInterpreter();
final Node newTree = new TreeParser(interpreter, expression).buildTree();
assertThat(interpreter.render(newTree)).isEqualTo("ABC");
}

@Test
Expand Down

0 comments on commit eafc139

Please sign in to comment.