Skip to content

Commit d9a9d0b

Browse files
committed
Apply whitespace rules for LStrip and Trim to comment blocks
1 parent c392aac commit d9a9d0b

File tree

5 files changed

+52
-14
lines changed

5 files changed

+52
-14
lines changed

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,17 @@ private Node getLastSibling() {
170170

171171
private Node text(TextToken textToken) {
172172
if (interpreter.getConfig().isLstripBlocks()) {
173-
if (scanner.hasNext() && scanner.peek().getType() == symbols.getTag()) {
174-
textToken =
175-
new TextToken(
176-
StringUtils.stripEnd(textToken.getImage(), "\t "),
177-
textToken.getLineNumber(),
178-
textToken.getStartPosition(),
179-
symbols
180-
);
173+
if (scanner.hasNext()) {
174+
final int nextTokenType = scanner.peek().getType();
175+
if (nextTokenType == symbols.getTag() || nextTokenType == symbols.getNote()) {
176+
textToken =
177+
new TextToken(
178+
StringUtils.stripEnd(textToken.getImage(), "\t "),
179+
textToken.getLineNumber(),
180+
textToken.getStartPosition(),
181+
symbols
182+
);
183+
}
181184
}
182185
}
183186

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,13 +253,18 @@ private Token newToken(int kind) {
253253
lastStart - lastNewlinePos + 1
254254
);
255255

256-
if (t instanceof TagToken) {
257-
if (config.isTrimBlocks() && currPost < length && is[currPost] == '\n') {
258-
lastNewlinePos = currPost;
259-
++currPost;
260-
++tokenStart;
261-
}
256+
if (
257+
(t instanceof TagToken || t instanceof NoteToken) &&
258+
config.isTrimBlocks() &&
259+
currPost < length &&
260+
is[currPost] == '\n'
261+
) {
262+
lastNewlinePos = currPost;
263+
++currPost;
264+
++tokenStart;
265+
}
262266

267+
if (t instanceof TagToken) {
263268
TagToken tt = (TagToken) t;
264269
if ("raw".equals(tt.getTagName())) {
265270
inRaw = 1;

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,18 @@ public void trimAndLstripBlocks() {
147147
.isEqualTo("<div>\n" + " yay\n" + "</div>\n");
148148
}
149149

150+
@Test
151+
public void trimAndLstripCommentBlocks() {
152+
interpreter =
153+
new Jinjava(
154+
JinjavaConfig.newBuilder().withLstripBlocks(true).withTrimBlocks(true).build()
155+
)
156+
.newInterpreter();
157+
158+
assertThat(interpreter.render(parse("parse/tokenizer/whitespace-comment-tags.jinja")))
159+
.isEqualTo("<div>\n" + " yay\n" + " whoop\n" + "</div>\n");
160+
}
161+
150162
@Test
151163
public void itWarnsAgainstMissingStartTags() {
152164
String expression = "{% if true %} foo {% endif %} {% endif %}";

src/test/java/com/hubspot/jinjava/tree/parse/TokenWhitespaceTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ public void trimBlocksTrimsAfterTag() {
2121
assertThat(tokens.get(2).getImage()).isEqualTo(" yay\n ");
2222
}
2323

24+
@Test
25+
public void trimBlocksTrimsAfterCommentTag() {
26+
List<Token> tokens = scanTokens(
27+
"parse/tokenizer/whitespace-comment-tags.jinja",
28+
trimBlocksConfig()
29+
);
30+
assertThat(tokens.get(2).getImage()).isEqualTo(" yay\n ");
31+
assertThat(tokens.get(4).getImage()).isEqualTo(" whoop\n</div>\n");
32+
}
33+
2434
private List<Token> scanTokens(String srcPath, JinjavaConfig config) {
2535
try {
2636
return Lists.newArrayList(
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<div>
2+
{# a comment #}
3+
yay
4+
{# another comment
5+
This time, over multiple lines
6+
#}
7+
whoop
8+
</div>

0 commit comments

Comments
 (0)