Skip to content

Commit 77ba2a2

Browse files
authored
Merge pull request #1319 from HubSpot/rw-correctly-parse-empty-string
fix: Handle empty strings in StrictWhitespaceControlParser
2 parents ffa6ca1 + 2a2eed8 commit 77ba2a2

3 files changed

Lines changed: 61 additions & 4 deletions

File tree

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@ public class StrictWhitespaceControlParser implements WhitespaceControlParser {
44

55
@Override
66
public boolean hasLeftTrim(String unwrapped) {
7-
return unwrapped.charAt(0) == '-';
7+
return !unwrapped.isEmpty() && unwrapped.charAt(0) == '-';
88
}
99

1010
@Override
1111
public String stripLeft(String unwrapped) {
12-
return unwrapped.substring(1);
12+
return unwrapped.isEmpty() ? unwrapped : unwrapped.substring(1);
1313
}
1414

1515
@Override
1616
public boolean hasRightTrim(String unwrapped) {
17-
return unwrapped.charAt(unwrapped.length() - 1) == '-';
17+
return !unwrapped.isEmpty() && unwrapped.charAt(unwrapped.length() - 1) == '-';
1818
}
1919

2020
@Override
2121
public String stripRight(String unwrapped) {
22-
return unwrapped.substring(0, unwrapped.length() - 1);
22+
return unwrapped.isEmpty()
23+
? unwrapped
24+
: unwrapped.substring(0, unwrapped.length() - 1);
2325
}
2426
}

src/test/java/com/hubspot/jinjava/interpret/LegacyWhitespaceControlParsingTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,11 @@ public void itErrorsOnSpacedDashesOnBothSides() {
8282
.isThrownBy(() -> modern.render(template, new HashMap<>()))
8383
.withMessageContaining("syntax error at position 9");
8484
}
85+
86+
@Test
87+
public void itHandlesEmptyExpressionToken() {
88+
String template = "{{}}";
89+
90+
assertThat(modern.render(template, new HashMap<>())).isEqualTo("");
91+
}
8592
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.hubspot.jinjava.tree.parse;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
8+
public class StrictWhitespaceControlParserTest {
9+
10+
StrictWhitespaceControlParser parser;
11+
12+
@Before
13+
public void setUp() {
14+
parser = new StrictWhitespaceControlParser();
15+
}
16+
17+
@Test
18+
public void itDoesNotTrimEmptyTokenOnLeft() {
19+
assertThat(parser.hasLeftTrim("")).isFalse();
20+
}
21+
22+
@Test
23+
public void itDoesNotTrimEmptyTokenOnRight() {
24+
assertThat(parser.hasRightTrim("")).isFalse();
25+
}
26+
27+
@Test
28+
public void itStripsLeftOfEmptyTokenWithoutThrowing() {
29+
assertThat(parser.stripLeft("")).isEqualTo("");
30+
}
31+
32+
@Test
33+
public void itStripsRightOfEmptyTokenWithoutThrowing() {
34+
assertThat(parser.stripRight("")).isEqualTo("");
35+
}
36+
37+
@Test
38+
public void itDetectsLeftTrim() {
39+
assertThat(parser.hasLeftTrim("-foo")).isTrue();
40+
assertThat(parser.stripLeft("-foo")).isEqualTo("foo");
41+
}
42+
43+
@Test
44+
public void itDetectsRightTrim() {
45+
assertThat(parser.hasRightTrim("foo-")).isTrue();
46+
assertThat(parser.stripRight("foo-")).isEqualTo("foo");
47+
}
48+
}

0 commit comments

Comments
 (0)