From 385c9dc0acba5383de356d7e42f2d4906f3fd883 Mon Sep 17 00:00:00 2001 From: Jack Smith Date: Wed, 8 Nov 2023 10:11:20 -0500 Subject: [PATCH] Handle trailing note without end --- .../hubspot/jinjava/tree/parse/NoteToken.java | 4 +- .../hubspot/jinjava/tree/TreeParserTest.java | 66 +++++++++++++++++++ 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/hubspot/jinjava/tree/parse/NoteToken.java b/src/main/java/com/hubspot/jinjava/tree/parse/NoteToken.java index c57f8e124..1bbc9bca5 100644 --- a/src/main/java/com/hubspot/jinjava/tree/parse/NoteToken.java +++ b/src/main/java/com/hubspot/jinjava/tree/parse/NoteToken.java @@ -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; @@ -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 = ""; diff --git a/src/test/java/com/hubspot/jinjava/tree/TreeParserTest.java b/src/test/java/com/hubspot/jinjava/tree/TreeParserTest.java index 6c15f396e..105fc6d9a 100644 --- a/src/test/java/com/hubspot/jinjava/tree/TreeParserTest.java +++ b/src/test/java/com/hubspot/jinjava/tree/TreeParserTest.java @@ -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' %}";