Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply whitespace rules for LStrip and Trim to comment blocks, resolves #1218 #1217

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
Loading