Skip to content

Commit

Permalink
Merge pull request #1217 from hrdixon/master
Browse files Browse the repository at this point in the history
Apply whitespace rules for LStrip and Trim to comment blocks, resolves #1218
  • Loading branch information
jasmith-hs authored Dec 3, 2024
2 parents c392aac + d9a9d0b commit 703b11c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 14 deletions.
19 changes: 11 additions & 8 deletions src/main/java/com/hubspot/jinjava/tree/TreeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,17 @@ private Node getLastSibling() {

private Node text(TextToken textToken) {
if (interpreter.getConfig().isLstripBlocks()) {
if (scanner.hasNext() && scanner.peek().getType() == symbols.getTag()) {
textToken =
new TextToken(
StringUtils.stripEnd(textToken.getImage(), "\t "),
textToken.getLineNumber(),
textToken.getStartPosition(),
symbols
);
if (scanner.hasNext()) {
final int nextTokenType = scanner.peek().getType();
if (nextTokenType == symbols.getTag() || nextTokenType == symbols.getNote()) {
textToken =
new TextToken(
StringUtils.stripEnd(textToken.getImage(), "\t "),
textToken.getLineNumber(),
textToken.getStartPosition(),
symbols
);
}
}
}

Expand Down
17 changes: 11 additions & 6 deletions src/main/java/com/hubspot/jinjava/tree/parse/TokenScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,18 @@ private Token newToken(int kind) {
lastStart - lastNewlinePos + 1
);

if (t instanceof TagToken) {
if (config.isTrimBlocks() && currPost < length && is[currPost] == '\n') {
lastNewlinePos = currPost;
++currPost;
++tokenStart;
}
if (
(t instanceof TagToken || t instanceof NoteToken) &&
config.isTrimBlocks() &&
currPost < length &&
is[currPost] == '\n'
) {
lastNewlinePos = currPost;
++currPost;
++tokenStart;
}

if (t instanceof TagToken) {
TagToken tt = (TagToken) t;
if ("raw".equals(tt.getTagName())) {
inRaw = 1;
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/com/hubspot/jinjava/tree/TreeParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,18 @@ public void trimAndLstripBlocks() {
.isEqualTo("<div>\n" + " yay\n" + "</div>\n");
}

@Test
public void trimAndLstripCommentBlocks() {
interpreter =
new Jinjava(
JinjavaConfig.newBuilder().withLstripBlocks(true).withTrimBlocks(true).build()
)
.newInterpreter();

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

@Test
public void itWarnsAgainstMissingStartTags() {
String expression = "{% if true %} foo {% endif %} {% endif %}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ public void trimBlocksTrimsAfterTag() {
assertThat(tokens.get(2).getImage()).isEqualTo(" yay\n ");
}

@Test
public void trimBlocksTrimsAfterCommentTag() {
List<Token> tokens = scanTokens(
"parse/tokenizer/whitespace-comment-tags.jinja",
trimBlocksConfig()
);
assertThat(tokens.get(2).getImage()).isEqualTo(" yay\n ");
assertThat(tokens.get(4).getImage()).isEqualTo(" whoop\n</div>\n");
}

private List<Token> scanTokens(String srcPath, JinjavaConfig config) {
try {
return Lists.newArrayList(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div>
{# a comment #}
yay
{# another comment
This time, over multiple lines
#}
whoop
</div>

0 comments on commit 703b11c

Please sign in to comment.