Skip to content

Commit ca98de9

Browse files
committed
Revert "Merge pull request #1122 from HubSpot/note-trim"
This reverts commit 7a60dca, reversing changes made to 23695ab.
1 parent c0992b7 commit ca98de9

4 files changed

Lines changed: 41 additions & 41 deletions

File tree

src/main/java/com/hubspot/jinjava/tree/TreeParser.java

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,12 @@ public Node buildTree() {
6262
Node node = nextNode();
6363

6464
if (node != null) {
65-
parent.getChildren().add(node);
65+
if (node instanceof TextNode && getLastSibling() instanceof TextNode) {
66+
// merge adjacent text nodes so whitespace control properly applies
67+
getLastSibling().getMaster().mergeImageAndContent(node.getMaster());
68+
} else {
69+
parent.getChildren().add(node);
70+
}
6671
}
6772
}
6873

@@ -91,12 +96,6 @@ public Node buildTree() {
9196

9297
private Node nextNode() {
9398
Token token = scanner.next();
94-
if (token.isLeftTrim()) {
95-
final Node lastSibling = getLastSibling();
96-
if (lastSibling instanceof TextNode) {
97-
lastSibling.getMaster().setRightTrim(true);
98-
}
99-
}
10099

101100
if (token.getType() == symbols.getFixed()) {
102101
if (token instanceof UnclosedToken) {
@@ -171,7 +170,7 @@ private Node text(TextToken textToken) {
171170
final Node lastSibling = getLastSibling();
172171

173172
// if last sibling was a tag and has rightTrimAfterEnd, strip whitespace
174-
if (lastSibling != null && isRightTrim(lastSibling)) {
173+
if (lastSibling instanceof TagNode && isRightTrim((TagNode) lastSibling)) {
175174
textToken.setLeftTrim(true);
176175
}
177176

@@ -187,21 +186,18 @@ private Node text(TextToken textToken) {
187186
return n;
188187
}
189188

190-
private boolean isRightTrim(Node lastSibling) {
191-
if (lastSibling instanceof TagNode) {
192-
return (
193-
((TagNode) lastSibling).getEndName() == null ||
194-
(
195-
((TagNode) lastSibling).getTag() instanceof FlexibleTag &&
196-
!((FlexibleTag) ((TagNode) lastSibling).getTag()).hasEndTag(
197-
(TagToken) lastSibling.getMaster()
198-
)
199-
)
189+
private boolean isRightTrim(TagNode lastSibling) {
190+
return (
191+
lastSibling.getEndName() == null ||
192+
(
193+
lastSibling.getTag() instanceof FlexibleTag &&
194+
!((FlexibleTag) lastSibling.getTag()).hasEndTag(
195+
(TagToken) lastSibling.getMaster()
196+
)
200197
)
201-
? lastSibling.getMaster().isRightTrim()
202-
: lastSibling.getMaster().isRightTrimAfterEnd();
203-
}
204-
return lastSibling.getMaster().isRightTrim();
198+
)
199+
? lastSibling.getMaster().isRightTrim()
200+
: lastSibling.getMaster().isRightTrimAfterEnd();
205201
}
206202

207203
private Node expression(ExpressionToken expressionToken) {
@@ -246,6 +242,14 @@ private Node tag(TagToken tagToken) {
246242
if (tag instanceof EndTag) {
247243
endTag(tag, tagToken);
248244
return null;
245+
} else {
246+
// if a tag has left trim, mark the last sibling to trim right whitespace
247+
if (tagToken.isLeftTrim()) {
248+
final Node lastSibling = getLastSibling();
249+
if (lastSibling instanceof TextNode) {
250+
lastSibling.getMaster().setRightTrim(true);
251+
}
252+
}
249253
}
250254

251255
TagNode node = new TagNode(tag, tagToken, symbols);
@@ -264,6 +268,16 @@ private Node tag(TagToken tagToken) {
264268
}
265269

266270
private void endTag(Tag tag, TagToken tagToken) {
271+
final Node lastSibling = getLastSibling();
272+
273+
if (
274+
parent instanceof TagNode &&
275+
tagToken.isLeftTrim() &&
276+
lastSibling instanceof TextNode
277+
) {
278+
lastSibling.getMaster().setRightTrim(true);
279+
}
280+
267281
if (parent.getMaster() != null) { // root node
268282
parent.getMaster().setRightTrimAfterEnd(tagToken.isRightTrim());
269283
}

src/main/java/com/hubspot/jinjava/tree/parse/NoteToken.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
**********************************************************************/
1616
package com.hubspot.jinjava.tree.parse;
1717

18-
import org.apache.commons.lang3.StringUtils;
19-
2018
public class NoteToken extends Token {
2119
private static final long serialVersionUID = -3859011447900311329L;
2220

@@ -39,9 +37,6 @@ public int getType() {
3937
*/
4038
@Override
4139
protected void parse() {
42-
if (StringUtils.isNotEmpty(image)) {
43-
handleTrim(image.substring(2, image.length() - 2));
44-
}
4540
content = "";
4641
}
4742

src/main/java/com/hubspot/jinjava/tree/parse/Token.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ public String getImage() {
5353
return image;
5454
}
5555

56+
public void mergeImageAndContent(Token otherToken) {
57+
this.image = image + otherToken.image;
58+
this.content = content + otherToken.content;
59+
}
60+
5661
public int getLineNumber() {
5762
return lineNumber;
5863
}

src/test/java/com/hubspot/jinjava/tree/TreeParserTest.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -224,20 +224,6 @@ public void itWarnsTwiceAgainstUnclosedBlockTag() {
224224
assertThat(interpreter.getErrors().get(1).getLineno()).isEqualTo(1);
225225
}
226226

227-
@Test
228-
public void itTrimsNotes() {
229-
String expression = "A\n{#- note -#}\nB";
230-
final Node tree = new TreeParser(interpreter, expression).buildTree();
231-
assertThat(interpreter.render(tree)).isEqualTo("AB");
232-
}
233-
234-
@Test
235-
public void itTrimsExpressions() {
236-
String expression = "A\n{{- 'B' -}}\nC";
237-
final Node tree = new TreeParser(interpreter, expression).buildTree();
238-
assertThat(interpreter.render(tree)).isEqualTo("ABC");
239-
}
240-
241227
Node parse(String fixture) {
242228
try {
243229
return new TreeParser(

0 commit comments

Comments
 (0)