Skip to content

Commit

Permalink
Handle trailing note without end
Browse files Browse the repository at this point in the history
  • Loading branch information
jasmith-hs committed Nov 8, 2023
1 parent eafc139 commit 385c9dc
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
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

0 comments on commit 385c9dc

Please sign in to comment.