Skip to content

Commit

Permalink
Make legacy merging compatible with note trimming
Browse files Browse the repository at this point in the history
  • Loading branch information
jasmith-hs committed Oct 30, 2023
1 parent 3874722 commit a66b0ab
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/main/java/com/hubspot/jinjava/tree/TreeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ public Node buildTree() {
!interpreter.getConfig().getLegacyOverrides().isAllowAdjacentTextNodes()
) {
// merge adjacent text nodes so whitespace control properly applies
getLastSibling().getMaster().mergeImageAndContent(node.getMaster());
((TextToken) getLastSibling().getMaster()).mergeImageAndContent(
(TextToken) node.getMaster()
);
} else {
parent.getChildren().add(node);
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/hubspot/jinjava/tree/parse/TextToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public TextToken(
super(image, lineNumber, startPosition, symbols);
}

public void mergeImageAndContent(TextToken otherToken) {
String thisOutput = output();
String otherTokenOutput = otherToken.output();
this.image = thisOutput + otherTokenOutput;
this.content = image;
}

@Override
public int getType() {
return getSymbols().getFixed();
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/com/hubspot/jinjava/tree/parse/Token.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ public String getImage() {
return image;
}

public void mergeImageAndContent(Token otherToken) {
this.image = image + otherToken.image;
this.content = content + otherToken.content;
}

public int getLineNumber() {
return lineNumber;
}
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 @@ -232,6 +232,13 @@ public void itTrimsNotes() {
assertThat(interpreter.render(tree)).isEqualTo("AB");
}

@Test
public void itMergesTextNodesWhileRespectingTrim() {
String expression = "{% print 'A' -%}\n{#- note -#}\nB\n{%- print 'C' %}";
final Node tree = new TreeParser(interpreter, expression).buildTree();
assertThat(interpreter.render(tree)).isEqualTo("ABC");
}

@Test
public void itTrimsExpressions() {
String expression = "A\n{{- 'B' -}}\nC";
Expand Down

0 comments on commit a66b0ab

Please sign in to comment.