Skip to content

Commit

Permalink
Fix issue #1230 with lstrip blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
jnothman committed Jan 26, 2025
1 parent 0dc554b commit 441c3d6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/main/java/com/hubspot/jinjava/tree/TreeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,18 @@ private Node text(TextToken textToken) {
if (scanner.hasNext()) {
final int nextTokenType = scanner.peek().getType();
if (nextTokenType == symbols.getTag() || nextTokenType == symbols.getNote()) {
String content = textToken.getImage();
int lastNewline = content.lastIndexOf('\n');
String afterNewline = lastNewline == -1
? content
: content.substring(lastNewline + 1);

if (afterNewline.matches("^[ \t]*$")) {
content = lastNewline == -1 ? "" : content.substring(0, lastNewline + 1);
}
textToken =
new TextToken(
StringUtils.stripEnd(textToken.getImage(), "\t "),
content,
textToken.getLineNumber(),
textToken.getStartPosition(),
symbols
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/hubspot/jinjava/tree/TreeParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void trimAndLstripBlocks() {
.newInterpreter();

assertThat(interpreter.render(parse("parse/tokenizer/whitespace-tags.jinja")))
.isEqualTo("<div>\n" + " yay\n" + "</div>\n");
.isEqualTo("<div>\n" + " yay whoop\n" + "</div>\n");
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/parse/tokenizer/whitespace-tags.jinja
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div>
{% if true %}
yay
yay {% if true %}whoop{% endif %}
{% endif %}
</div>

0 comments on commit 441c3d6

Please sign in to comment.