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

Handle trailing note without end #1129

Merged
merged 1 commit into from
Nov 8, 2023
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
4 changes: 1 addition & 3 deletions src/main/java/com/hubspot/jinjava/tree/parse/NoteToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
**********************************************************************/
package com.hubspot.jinjava.tree.parse;

import org.apache.commons.lang3.StringUtils;

public class NoteToken extends Token {
private static final long serialVersionUID = -3859011447900311329L;

Expand All @@ -39,7 +37,7 @@ public int getType() {
*/
@Override
protected void parse() {
if (StringUtils.isNotEmpty(image)) {
if (image.length() > 4) { // {# #}
handleTrim(image.substring(2, image.length() - 2));
}
content = "";
Expand Down
66 changes: 66 additions & 0 deletions src/test/java/com/hubspot/jinjava/tree/TreeParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,72 @@ public void itTrimsNotes() {
assertThat(interpreter.render(newTree)).isEqualTo("AB");
}

@Test
public void itAllowsTrailingNote() {
String expression = "A\n{# ";
final Node tree = new TreeParser(interpreter, expression).buildTree();
assertThat(interpreter.render(tree)).isEqualTo("A\n");
interpreter =
new Jinjava(
JinjavaConfig
.newBuilder()
.withLegacyOverrides(
LegacyOverrides
.newBuilder()
.withUseTrimmingForNotesAndExpressions(true)
.build()
)
.build()
)
.newInterpreter();
final Node newTree = new TreeParser(interpreter, expression).buildTree();
assertThat(interpreter.render(newTree)).isEqualTo("A\n");
}

@Test
public void itAllowsTrailingExpression() {
String expression = "A\n{{ ";
final Node tree = new TreeParser(interpreter, expression).buildTree();
assertThat(interpreter.render(tree)).isEqualTo("A\n{{ ");
interpreter =
new Jinjava(
JinjavaConfig
.newBuilder()
.withLegacyOverrides(
LegacyOverrides
.newBuilder()
.withUseTrimmingForNotesAndExpressions(true)
.build()
)
.build()
)
.newInterpreter();
final Node newTree = new TreeParser(interpreter, expression).buildTree();
assertThat(interpreter.render(newTree)).isEqualTo("A\n{{ ");
}

@Test
public void itAllowsTrailingTag() {
String expression = "A\n{% ";
final Node tree = new TreeParser(interpreter, expression).buildTree();
assertThat(interpreter.render(tree)).isEqualTo("A\n{% ");
interpreter =
new Jinjava(
JinjavaConfig
.newBuilder()
.withLegacyOverrides(
LegacyOverrides
.newBuilder()
.withUseTrimmingForNotesAndExpressions(true)
.build()
)
.build()
)
.newInterpreter();
final Node newTree = new TreeParser(interpreter, expression).buildTree();
assertThat(interpreter.render(newTree)).isEqualTo("A\n{% ");
}

@Test
public void itMergesTextNodesWhileRespectingTrim() {
String expression = "{% print 'A' -%}\n{#- note -#}\nB\n{%- print 'C' %}";
Expand Down