From ca98de925868e072234f03a512e0c891ed41426a Mon Sep 17 00:00:00 2001 From: Jack Smith Date: Tue, 31 Oct 2023 11:07:32 -0400 Subject: [PATCH] Revert "Merge pull request #1122 from HubSpot/note-trim" This reverts commit 7a60dca9580a929c7fd515429d5f20094202ac4e, reversing changes made to 23695ab534b517842d0225dffdd9df9f632f7c3d. --- .../com/hubspot/jinjava/tree/TreeParser.java | 58 ++++++++++++------- .../hubspot/jinjava/tree/parse/NoteToken.java | 5 -- .../com/hubspot/jinjava/tree/parse/Token.java | 5 ++ .../hubspot/jinjava/tree/TreeParserTest.java | 14 ----- 4 files changed, 41 insertions(+), 41 deletions(-) diff --git a/src/main/java/com/hubspot/jinjava/tree/TreeParser.java b/src/main/java/com/hubspot/jinjava/tree/TreeParser.java index bc6f62557..07fbbb142 100644 --- a/src/main/java/com/hubspot/jinjava/tree/TreeParser.java +++ b/src/main/java/com/hubspot/jinjava/tree/TreeParser.java @@ -62,7 +62,12 @@ public Node buildTree() { Node node = nextNode(); if (node != null) { - parent.getChildren().add(node); + if (node instanceof TextNode && getLastSibling() instanceof TextNode) { + // merge adjacent text nodes so whitespace control properly applies + getLastSibling().getMaster().mergeImageAndContent(node.getMaster()); + } else { + parent.getChildren().add(node); + } } } @@ -91,12 +96,6 @@ public Node buildTree() { private Node nextNode() { Token token = scanner.next(); - if (token.isLeftTrim()) { - final Node lastSibling = getLastSibling(); - if (lastSibling instanceof TextNode) { - lastSibling.getMaster().setRightTrim(true); - } - } if (token.getType() == symbols.getFixed()) { if (token instanceof UnclosedToken) { @@ -171,7 +170,7 @@ 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 instanceof TagNode && isRightTrim((TagNode) lastSibling)) { textToken.setLeftTrim(true); } @@ -187,21 +186,18 @@ private Node text(TextToken textToken) { return n; } - private boolean isRightTrim(Node lastSibling) { - if (lastSibling instanceof TagNode) { - return ( - ((TagNode) lastSibling).getEndName() == null || - ( - ((TagNode) lastSibling).getTag() instanceof FlexibleTag && - !((FlexibleTag) ((TagNode) lastSibling).getTag()).hasEndTag( - (TagToken) lastSibling.getMaster() - ) - ) + 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(); - } - return lastSibling.getMaster().isRightTrim(); + ) + ? lastSibling.getMaster().isRightTrim() + : lastSibling.getMaster().isRightTrimAfterEnd(); } private Node expression(ExpressionToken expressionToken) { @@ -246,6 +242,14 @@ private Node tag(TagToken tagToken) { if (tag instanceof EndTag) { endTag(tag, tagToken); return null; + } else { + // if a tag has left trim, mark the last sibling to trim right whitespace + if (tagToken.isLeftTrim()) { + final Node lastSibling = getLastSibling(); + if (lastSibling instanceof TextNode) { + lastSibling.getMaster().setRightTrim(true); + } + } } TagNode node = new TagNode(tag, tagToken, symbols); @@ -264,6 +268,16 @@ private Node tag(TagToken tagToken) { } private void endTag(Tag tag, TagToken tagToken) { + final Node lastSibling = getLastSibling(); + + if ( + parent instanceof TagNode && + tagToken.isLeftTrim() && + lastSibling instanceof TextNode + ) { + lastSibling.getMaster().setRightTrim(true); + } + if (parent.getMaster() != null) { // root node parent.getMaster().setRightTrimAfterEnd(tagToken.isRightTrim()); } diff --git a/src/main/java/com/hubspot/jinjava/tree/parse/NoteToken.java b/src/main/java/com/hubspot/jinjava/tree/parse/NoteToken.java index c57f8e124..fe98c4dda 100644 --- a/src/main/java/com/hubspot/jinjava/tree/parse/NoteToken.java +++ b/src/main/java/com/hubspot/jinjava/tree/parse/NoteToken.java @@ -15,8 +15,6 @@ **********************************************************************/ package com.hubspot.jinjava.tree.parse; -import org.apache.commons.lang3.StringUtils; - public class NoteToken extends Token { private static final long serialVersionUID = -3859011447900311329L; @@ -39,9 +37,6 @@ public int getType() { */ @Override protected void parse() { - if (StringUtils.isNotEmpty(image)) { - handleTrim(image.substring(2, image.length() - 2)); - } content = ""; } diff --git a/src/main/java/com/hubspot/jinjava/tree/parse/Token.java b/src/main/java/com/hubspot/jinjava/tree/parse/Token.java index 487987626..dda5bd63b 100644 --- a/src/main/java/com/hubspot/jinjava/tree/parse/Token.java +++ b/src/main/java/com/hubspot/jinjava/tree/parse/Token.java @@ -53,6 +53,11 @@ 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; } diff --git a/src/test/java/com/hubspot/jinjava/tree/TreeParserTest.java b/src/test/java/com/hubspot/jinjava/tree/TreeParserTest.java index b31e82cc3..6ba9065ca 100644 --- a/src/test/java/com/hubspot/jinjava/tree/TreeParserTest.java +++ b/src/test/java/com/hubspot/jinjava/tree/TreeParserTest.java @@ -224,20 +224,6 @@ public void itWarnsTwiceAgainstUnclosedBlockTag() { assertThat(interpreter.getErrors().get(1).getLineno()).isEqualTo(1); } - @Test - public void itTrimsNotes() { - String expression = "A\n{#- note -#}\nB"; - final Node tree = new TreeParser(interpreter, expression).buildTree(); - assertThat(interpreter.render(tree)).isEqualTo("AB"); - } - - @Test - public void itTrimsExpressions() { - String expression = "A\n{{- 'B' -}}\nC"; - final Node tree = new TreeParser(interpreter, expression).buildTree(); - assertThat(interpreter.render(tree)).isEqualTo("ABC"); - } - Node parse(String fixture) { try { return new TreeParser(