From f632489af89ca0d166edb2c85b657cfb15dcedba Mon Sep 17 00:00:00 2001 From: Jack Smith Date: Fri, 9 Oct 2020 17:24:56 -0400 Subject: [PATCH 01/12] Add preserving raw tags for double-renderings --- .../com/hubspot/jinjava/JinjavaConfig.java | 20 +++++++++++++++++-- .../hubspot/jinjava/interpret/Context.java | 16 +++++++++++++++ .../jinjava/interpret/JinjavaInterpreter.java | 3 +++ .../interpret/PreservedRawTagException.java | 12 +++++++++++ .../com/hubspot/jinjava/lib/tag/RawTag.java | 7 +++++++ 5 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/hubspot/jinjava/interpret/PreservedRawTagException.java diff --git a/src/main/java/com/hubspot/jinjava/JinjavaConfig.java b/src/main/java/com/hubspot/jinjava/JinjavaConfig.java index d6932405c..857b52411 100644 --- a/src/main/java/com/hubspot/jinjava/JinjavaConfig.java +++ b/src/main/java/com/hubspot/jinjava/JinjavaConfig.java @@ -56,6 +56,7 @@ public class JinjavaConfig { private TokenScannerSymbols tokenScannerSymbols; private ELResolver elResolver; private final boolean iterateOverMapKeys; + private final boolean preserveRawTags; public static Builder newBuilder() { return new Builder(); @@ -85,6 +86,7 @@ public JinjavaConfig(InterpreterFactory interpreterFactory) { interpreterFactory, new DefaultTokenScannerSymbols(), JinjavaInterpreterResolver.DEFAULT_RESOLVER_READ_ONLY, + false, false ); } @@ -114,6 +116,7 @@ public JinjavaConfig( new JinjavaInterpreterFactory(), new DefaultTokenScannerSymbols(), JinjavaInterpreterResolver.DEFAULT_RESOLVER_READ_ONLY, + false, false ); } @@ -137,7 +140,8 @@ private JinjavaConfig( InterpreterFactory interpreterFactory, TokenScannerSymbols tokenScannerSymbols, ELResolver elResolver, - boolean iterateOverMapKeys + boolean iterateOverMapKeys, + boolean preserveRawTags ) { this.charset = charset; this.locale = locale; @@ -158,6 +162,7 @@ private JinjavaConfig( this.tokenScannerSymbols = tokenScannerSymbols; this.elResolver = elResolver; this.iterateOverMapKeys = iterateOverMapKeys; + this.preserveRawTags = preserveRawTags; } public Charset getCharset() { @@ -240,6 +245,10 @@ public boolean isIterateOverMapKeys() { return iterateOverMapKeys; } + public boolean isPreserveRawTags() { + return preserveRawTags; + } + public static class Builder { private Charset charset = StandardCharsets.UTF_8; private Locale locale = Locale.ENGLISH; @@ -263,6 +272,7 @@ public static class Builder { private TokenScannerSymbols tokenScannerSymbols = new DefaultTokenScannerSymbols(); private ELResolver elResolver = JinjavaInterpreterResolver.DEFAULT_RESOLVER_READ_ONLY; private boolean iterateOverMapKeys; + private boolean preserveRawTags; private Builder() {} @@ -371,6 +381,11 @@ public Builder withIterateOverMapKeys(boolean iterateOverMapKeys) { return this; } + public Builder withPreserveRawTags(boolean preserveRawTags) { + this.preserveRawTags = preserveRawTags; + return this; + } + public JinjavaConfig build() { return new JinjavaConfig( charset, @@ -391,7 +406,8 @@ public JinjavaConfig build() { interpreterFactory, tokenScannerSymbols, elResolver, - iterateOverMapKeys + iterateOverMapKeys, + preserveRawTags ); } } diff --git a/src/main/java/com/hubspot/jinjava/interpret/Context.java b/src/main/java/com/hubspot/jinjava/interpret/Context.java index 0afa5d4fb..c57c535a4 100644 --- a/src/main/java/com/hubspot/jinjava/interpret/Context.java +++ b/src/main/java/com/hubspot/jinjava/interpret/Context.java @@ -78,6 +78,7 @@ public enum Library { private final Set resolvedFunctions = new HashSet<>(); private Set deferredNodes = new HashSet<>(); + private boolean hasPreservedRawTags = false; private final ExpTestLibrary expTestLibrary; private final FilterLibrary filterLibrary; @@ -290,6 +291,21 @@ public Set getDeferredNodes() { return ImmutableSet.copyOf(deferredNodes); } + public void handlePreservedRawTag() { + hasPreservedRawTags = true; + if (getParent() != null) { + Context parent = getParent(); + //Ignore global context + if (parent.getParent() != null) { + getParent().handlePreservedRawTag(); + } + } + } + + public boolean getHasPreservedRawTags() { + return hasPreservedRawTags; + } + public List getSuperBlock() { if (superBlock != null) { return superBlock; diff --git a/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java b/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java index 1ee0b5363..dcba8b2bb 100644 --- a/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java +++ b/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java @@ -269,6 +269,9 @@ public String render(Node root, boolean processExtendRoots) { } catch (DeferredValueException e) { context.handleDeferredNode(node); out = new RenderedOutputNode(node.getMaster().getImage()); + } catch (PreservedRawTagException e) { + context.handlePreservedRawTag(); + out = new RenderedOutputNode(node.getMaster().getImage()); } context.popRenderStack(); try { diff --git a/src/main/java/com/hubspot/jinjava/interpret/PreservedRawTagException.java b/src/main/java/com/hubspot/jinjava/interpret/PreservedRawTagException.java new file mode 100644 index 000000000..ddfa2292b --- /dev/null +++ b/src/main/java/com/hubspot/jinjava/interpret/PreservedRawTagException.java @@ -0,0 +1,12 @@ +package com.hubspot.jinjava.interpret; + +public class PreservedRawTagException extends InterpretException { + + public PreservedRawTagException() { + super("Encountered a preserved raw tag"); + } + + public PreservedRawTagException(int lineNumber, int startPosition) { + super("Encountered a preserved raw tag", lineNumber, startPosition); + } +} diff --git a/src/main/java/com/hubspot/jinjava/lib/tag/RawTag.java b/src/main/java/com/hubspot/jinjava/lib/tag/RawTag.java index 67fc13be5..cc2c21bbc 100644 --- a/src/main/java/com/hubspot/jinjava/lib/tag/RawTag.java +++ b/src/main/java/com/hubspot/jinjava/lib/tag/RawTag.java @@ -3,6 +3,7 @@ import com.hubspot.jinjava.doc.annotations.JinjavaDoc; import com.hubspot.jinjava.doc.annotations.JinjavaSnippet; import com.hubspot.jinjava.interpret.JinjavaInterpreter; +import com.hubspot.jinjava.interpret.PreservedRawTagException; import com.hubspot.jinjava.tree.Node; import com.hubspot.jinjava.tree.TagNode; import com.hubspot.jinjava.util.LengthLimitingStringBuilder; @@ -30,6 +31,12 @@ public String getName() { @Override public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) { + if (interpreter.getConfig().isPreserveRawTags()) { + throw new PreservedRawTagException( + tagNode.getLineNumber(), + tagNode.getStartPosition() + ); + } LengthLimitingStringBuilder result = new LengthLimitingStringBuilder( interpreter.getConfig().getMaxOutputSize() ); From 4f120e5afd4da6255c4a0bb55b83198bf4732019 Mon Sep 17 00:00:00 2001 From: Jack Smith Date: Mon, 12 Oct 2020 10:12:53 -0400 Subject: [PATCH 02/12] Include image in exception, allow length limiting --- .../jinjava/interpret/JinjavaInterpreter.java | 2 +- .../jinjava/interpret/PreservedRawTagException.java | 12 +++++++++++- .../java/com/hubspot/jinjava/lib/tag/RawTag.java | 8 +++++--- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java b/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java index dcba8b2bb..ad16cdbf5 100644 --- a/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java +++ b/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java @@ -271,7 +271,7 @@ public String render(Node root, boolean processExtendRoots) { out = new RenderedOutputNode(node.getMaster().getImage()); } catch (PreservedRawTagException e) { context.handlePreservedRawTag(); - out = new RenderedOutputNode(node.getMaster().getImage()); + out = new RenderedOutputNode(e.getPreservedImage()); } context.popRenderStack(); try { diff --git a/src/main/java/com/hubspot/jinjava/interpret/PreservedRawTagException.java b/src/main/java/com/hubspot/jinjava/interpret/PreservedRawTagException.java index ddfa2292b..151d9f4f1 100644 --- a/src/main/java/com/hubspot/jinjava/interpret/PreservedRawTagException.java +++ b/src/main/java/com/hubspot/jinjava/interpret/PreservedRawTagException.java @@ -1,12 +1,22 @@ package com.hubspot.jinjava.interpret; public class PreservedRawTagException extends InterpretException { + private String preservedImage; public PreservedRawTagException() { super("Encountered a preserved raw tag"); } - public PreservedRawTagException(int lineNumber, int startPosition) { + public PreservedRawTagException( + String preservedImage, + int lineNumber, + int startPosition + ) { super("Encountered a preserved raw tag", lineNumber, startPosition); + this.preservedImage = preservedImage; + } + + public String getPreservedImage() { + return preservedImage; } } diff --git a/src/main/java/com/hubspot/jinjava/lib/tag/RawTag.java b/src/main/java/com/hubspot/jinjava/lib/tag/RawTag.java index cc2c21bbc..43c38994b 100644 --- a/src/main/java/com/hubspot/jinjava/lib/tag/RawTag.java +++ b/src/main/java/com/hubspot/jinjava/lib/tag/RawTag.java @@ -31,15 +31,17 @@ public String getName() { @Override public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) { + LengthLimitingStringBuilder result = new LengthLimitingStringBuilder( + interpreter.getConfig().getMaxOutputSize() + ); if (interpreter.getConfig().isPreserveRawTags()) { + result.append(renderNodeRaw(tagNode)); throw new PreservedRawTagException( + result.toString(), tagNode.getLineNumber(), tagNode.getStartPosition() ); } - LengthLimitingStringBuilder result = new LengthLimitingStringBuilder( - interpreter.getConfig().getMaxOutputSize() - ); for (Node n : tagNode.getChildren()) { result.append(renderNodeRaw(n)); From 279806671bd15351aa3314803cbdc0c4c799d814 Mon Sep 17 00:00:00 2001 From: Jack Smith Date: Mon, 12 Oct 2020 11:35:48 -0400 Subject: [PATCH 03/12] Handle preserved raw tag exception in TagNode --- src/main/java/com/hubspot/jinjava/tree/TagNode.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/com/hubspot/jinjava/tree/TagNode.java b/src/main/java/com/hubspot/jinjava/tree/TagNode.java index f98486347..6a613ca26 100644 --- a/src/main/java/com/hubspot/jinjava/tree/TagNode.java +++ b/src/main/java/com/hubspot/jinjava/tree/TagNode.java @@ -20,6 +20,7 @@ import com.hubspot.jinjava.interpret.InvalidArgumentException; import com.hubspot.jinjava.interpret.InvalidInputException; import com.hubspot.jinjava.interpret.JinjavaInterpreter; +import com.hubspot.jinjava.interpret.PreservedRawTagException; import com.hubspot.jinjava.lib.tag.Tag; import com.hubspot.jinjava.tree.output.OutputNode; import com.hubspot.jinjava.tree.output.RenderedOutputNode; @@ -53,6 +54,9 @@ public OutputNode render(JinjavaInterpreter interpreter) { } catch (DeferredValueException e) { interpreter.getContext().handleDeferredNode(this); return new RenderedOutputNode(reconstructImage()); + } catch (PreservedRawTagException e) { + interpreter.getContext().handlePreservedRawTag(); + return new RenderedOutputNode(e.getPreservedImage()); } catch (InterpretException | InvalidInputException | InvalidArgumentException e) { throw e; } catch (Exception e) { From 09f47a2e2bbb8f1b30f609c0b7e1f66a13104d27 Mon Sep 17 00:00:00 2001 From: Jack Smith Date: Mon, 12 Oct 2020 11:38:10 -0400 Subject: [PATCH 04/12] Test preserving raw tags --- .../interpret/JinjavaInterpreterTest.java | 20 ++++++++++++ .../hubspot/jinjava/lib/tag/RawTagTest.java | 32 ++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/hubspot/jinjava/interpret/JinjavaInterpreterTest.java b/src/test/java/com/hubspot/jinjava/interpret/JinjavaInterpreterTest.java index 7bf3a48ea..086fbd68f 100644 --- a/src/test/java/com/hubspot/jinjava/interpret/JinjavaInterpreterTest.java +++ b/src/test/java/com/hubspot/jinjava/interpret/JinjavaInterpreterTest.java @@ -220,4 +220,24 @@ public void itLimitsOutputSizeWhenSumOfNodeSizesExceedsMax() { assertThat(renderResult.getErrors().get(0).getMessage()) .contains("OutputTooBigException"); } + + @Test + public void itCanPreserveRawTags() { + JinjavaConfig preserveRawTagsConfig = JinjavaConfig + .newBuilder() + .withPreserveRawTags(true) + .build(); + String input = "1{% raw %}2{% endraw %}3"; + String normalOutput = "123"; + String preservedOutput = "1{% raw %}2{% endraw %}3"; + + RenderResult renderResult = new Jinjava().renderForResult(input, new HashMap<>()); + assertThat(renderResult.getOutput()).isEqualTo(normalOutput); + assertThat(renderResult.hasErrors()).isFalse(); + + renderResult = + new Jinjava(preserveRawTagsConfig).renderForResult(input, new HashMap<>()); + assertThat(renderResult.getOutput()).isEqualTo(preservedOutput); + assertThat(renderResult.hasErrors()).isFalse(); + } } diff --git a/src/test/java/com/hubspot/jinjava/lib/tag/RawTagTest.java b/src/test/java/com/hubspot/jinjava/lib/tag/RawTagTest.java index 244ea4013..a26367a78 100644 --- a/src/test/java/com/hubspot/jinjava/lib/tag/RawTagTest.java +++ b/src/test/java/com/hubspot/jinjava/lib/tag/RawTagTest.java @@ -1,10 +1,13 @@ package com.hubspot.jinjava.lib.tag; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.catchThrowable; import com.google.common.io.Resources; import com.hubspot.jinjava.Jinjava; +import com.hubspot.jinjava.JinjavaConfig; import com.hubspot.jinjava.interpret.JinjavaInterpreter; +import com.hubspot.jinjava.interpret.PreservedRawTagException; import com.hubspot.jinjava.tree.Node; import com.hubspot.jinjava.tree.TagNode; import com.hubspot.jinjava.tree.TreeParser; @@ -17,12 +20,14 @@ import org.junit.Test; public class RawTagTest { + Jinjava jinjava; JinjavaInterpreter interpreter; RawTag tag; @Before public void setup() { - interpreter = new Jinjava().newInterpreter(); + jinjava = new Jinjava(); + interpreter = jinjava.newInterpreter(); tag = new RawTag(); } @@ -91,6 +96,31 @@ public void itDoesntProcessJinjaCommentsWithinARawBlock() { .contains("{{#each people}}"); } + @Test + public void itPreservesRawTags() { + TagNode tagNode = fixture("hubl"); + JinjavaInterpreter preserveInterpreter = new JinjavaInterpreter( + jinjava, + jinjava.getGlobalContextCopy(), + JinjavaConfig.newBuilder().withPreserveRawTags(true).build() + ); + Throwable throwable = catchThrowable( + () -> tag.interpret(tagNode, preserveInterpreter) + ); + assertThat(throwable instanceof PreservedRawTagException); + try { + assertThat(((PreservedRawTagException) throwable).getPreservedImage()) + .isEqualTo( + Resources.toString( + Resources.getResource("tags/rawtag/hubl.jinja"), + StandardCharsets.UTF_8 + ) + ); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + private TagNode fixture(String name) { return (TagNode) fixtures(name).getFirst(); } From dd642d0dba03031d85fd9b17b2dce2d5c8776aeb Mon Sep 17 00:00:00 2001 From: Jack Smith Date: Mon, 12 Oct 2020 11:47:07 -0400 Subject: [PATCH 05/12] Exception isn't thrown here --- .../java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java b/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java index ad16cdbf5..1ee0b5363 100644 --- a/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java +++ b/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java @@ -269,9 +269,6 @@ public String render(Node root, boolean processExtendRoots) { } catch (DeferredValueException e) { context.handleDeferredNode(node); out = new RenderedOutputNode(node.getMaster().getImage()); - } catch (PreservedRawTagException e) { - context.handlePreservedRawTag(); - out = new RenderedOutputNode(e.getPreservedImage()); } context.popRenderStack(); try { From cc431d4f523d3f34014086e72af61ba77c62173a Mon Sep 17 00:00:00 2001 From: Jack Smith Date: Mon, 12 Oct 2020 12:36:01 -0400 Subject: [PATCH 06/12] Add license --- .../interpret/PreservedRawTagException.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/java/com/hubspot/jinjava/interpret/PreservedRawTagException.java b/src/main/java/com/hubspot/jinjava/interpret/PreservedRawTagException.java index 151d9f4f1..9083f625f 100644 --- a/src/main/java/com/hubspot/jinjava/interpret/PreservedRawTagException.java +++ b/src/main/java/com/hubspot/jinjava/interpret/PreservedRawTagException.java @@ -1,3 +1,18 @@ +/********************************************************************** + * Copyright (c) 2020 HubSpot Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + **********************************************************************/ package com.hubspot.jinjava.interpret; public class PreservedRawTagException extends InterpretException { From 229b277c0df8b9099f1885cba9bd555e5977cf3f Mon Sep 17 00:00:00 2001 From: Jack Smith Date: Wed, 21 Oct 2020 09:20:25 -0400 Subject: [PATCH 07/12] Refactor to remove PreservedRawTagException --- .../interpret/PreservedRawTagException.java | 37 ------------------- .../com/hubspot/jinjava/lib/tag/RawTag.java | 14 +++---- .../com/hubspot/jinjava/tree/TagNode.java | 4 -- .../hubspot/jinjava/lib/tag/RawTagTest.java | 9 +---- 4 files changed, 7 insertions(+), 57 deletions(-) delete mode 100644 src/main/java/com/hubspot/jinjava/interpret/PreservedRawTagException.java diff --git a/src/main/java/com/hubspot/jinjava/interpret/PreservedRawTagException.java b/src/main/java/com/hubspot/jinjava/interpret/PreservedRawTagException.java deleted file mode 100644 index 9083f625f..000000000 --- a/src/main/java/com/hubspot/jinjava/interpret/PreservedRawTagException.java +++ /dev/null @@ -1,37 +0,0 @@ -/********************************************************************** - * Copyright (c) 2020 HubSpot Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - **********************************************************************/ -package com.hubspot.jinjava.interpret; - -public class PreservedRawTagException extends InterpretException { - private String preservedImage; - - public PreservedRawTagException() { - super("Encountered a preserved raw tag"); - } - - public PreservedRawTagException( - String preservedImage, - int lineNumber, - int startPosition - ) { - super("Encountered a preserved raw tag", lineNumber, startPosition); - this.preservedImage = preservedImage; - } - - public String getPreservedImage() { - return preservedImage; - } -} diff --git a/src/main/java/com/hubspot/jinjava/lib/tag/RawTag.java b/src/main/java/com/hubspot/jinjava/lib/tag/RawTag.java index 43c38994b..9000e8eff 100644 --- a/src/main/java/com/hubspot/jinjava/lib/tag/RawTag.java +++ b/src/main/java/com/hubspot/jinjava/lib/tag/RawTag.java @@ -3,7 +3,6 @@ import com.hubspot.jinjava.doc.annotations.JinjavaDoc; import com.hubspot.jinjava.doc.annotations.JinjavaSnippet; import com.hubspot.jinjava.interpret.JinjavaInterpreter; -import com.hubspot.jinjava.interpret.PreservedRawTagException; import com.hubspot.jinjava.tree.Node; import com.hubspot.jinjava.tree.TagNode; import com.hubspot.jinjava.util.LengthLimitingStringBuilder; @@ -31,17 +30,14 @@ public String getName() { @Override public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) { + if (interpreter.getConfig().isPreserveRawTags()) { + interpreter.getContext().handlePreservedRawTag(); + return renderNodeRaw(tagNode); + } + LengthLimitingStringBuilder result = new LengthLimitingStringBuilder( interpreter.getConfig().getMaxOutputSize() ); - if (interpreter.getConfig().isPreserveRawTags()) { - result.append(renderNodeRaw(tagNode)); - throw new PreservedRawTagException( - result.toString(), - tagNode.getLineNumber(), - tagNode.getStartPosition() - ); - } for (Node n : tagNode.getChildren()) { result.append(renderNodeRaw(n)); diff --git a/src/main/java/com/hubspot/jinjava/tree/TagNode.java b/src/main/java/com/hubspot/jinjava/tree/TagNode.java index 6a613ca26..f98486347 100644 --- a/src/main/java/com/hubspot/jinjava/tree/TagNode.java +++ b/src/main/java/com/hubspot/jinjava/tree/TagNode.java @@ -20,7 +20,6 @@ import com.hubspot.jinjava.interpret.InvalidArgumentException; import com.hubspot.jinjava.interpret.InvalidInputException; import com.hubspot.jinjava.interpret.JinjavaInterpreter; -import com.hubspot.jinjava.interpret.PreservedRawTagException; import com.hubspot.jinjava.lib.tag.Tag; import com.hubspot.jinjava.tree.output.OutputNode; import com.hubspot.jinjava.tree.output.RenderedOutputNode; @@ -54,9 +53,6 @@ public OutputNode render(JinjavaInterpreter interpreter) { } catch (DeferredValueException e) { interpreter.getContext().handleDeferredNode(this); return new RenderedOutputNode(reconstructImage()); - } catch (PreservedRawTagException e) { - interpreter.getContext().handlePreservedRawTag(); - return new RenderedOutputNode(e.getPreservedImage()); } catch (InterpretException | InvalidInputException | InvalidArgumentException e) { throw e; } catch (Exception e) { diff --git a/src/test/java/com/hubspot/jinjava/lib/tag/RawTagTest.java b/src/test/java/com/hubspot/jinjava/lib/tag/RawTagTest.java index a26367a78..2ce0d1883 100644 --- a/src/test/java/com/hubspot/jinjava/lib/tag/RawTagTest.java +++ b/src/test/java/com/hubspot/jinjava/lib/tag/RawTagTest.java @@ -1,13 +1,11 @@ package com.hubspot.jinjava.lib.tag; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.catchThrowable; import com.google.common.io.Resources; import com.hubspot.jinjava.Jinjava; import com.hubspot.jinjava.JinjavaConfig; import com.hubspot.jinjava.interpret.JinjavaInterpreter; -import com.hubspot.jinjava.interpret.PreservedRawTagException; import com.hubspot.jinjava.tree.Node; import com.hubspot.jinjava.tree.TagNode; import com.hubspot.jinjava.tree.TreeParser; @@ -104,12 +102,9 @@ public void itPreservesRawTags() { jinjava.getGlobalContextCopy(), JinjavaConfig.newBuilder().withPreserveRawTags(true).build() ); - Throwable throwable = catchThrowable( - () -> tag.interpret(tagNode, preserveInterpreter) - ); - assertThat(throwable instanceof PreservedRawTagException); + String result = tag.interpret(tagNode, preserveInterpreter); try { - assertThat(((PreservedRawTagException) throwable).getPreservedImage()) + assertThat(result) .isEqualTo( Resources.toString( Resources.getResource("tags/rawtag/hubl.jinja"), From 17c5a1cf0ebea4a1142dde68dcf74fa806016212 Mon Sep 17 00:00:00 2001 From: Jack Smith Date: Wed, 21 Oct 2020 09:30:04 -0400 Subject: [PATCH 08/12] Add deferred preserving test --- .../hubspot/jinjava/lib/tag/RawTagTest.java | 50 +++++++++++++++++++ src/test/resources/tags/rawtag/deferred.jinja | 1 + 2 files changed, 51 insertions(+) create mode 100644 src/test/resources/tags/rawtag/deferred.jinja diff --git a/src/test/java/com/hubspot/jinjava/lib/tag/RawTagTest.java b/src/test/java/com/hubspot/jinjava/lib/tag/RawTagTest.java index 2ce0d1883..4bc9c3c2b 100644 --- a/src/test/java/com/hubspot/jinjava/lib/tag/RawTagTest.java +++ b/src/test/java/com/hubspot/jinjava/lib/tag/RawTagTest.java @@ -5,6 +5,7 @@ import com.google.common.io.Resources; import com.hubspot.jinjava.Jinjava; import com.hubspot.jinjava.JinjavaConfig; +import com.hubspot.jinjava.interpret.DeferredValue; import com.hubspot.jinjava.interpret.JinjavaInterpreter; import com.hubspot.jinjava.tree.Node; import com.hubspot.jinjava.tree.TagNode; @@ -116,6 +117,55 @@ public void itPreservesRawTags() { } } + @Test + public void itPreservesDeferredWhilePreservingRawTags() { + TagNode tagNode = fixture("deferred"); + JinjavaInterpreter preserveInterpreter = new JinjavaInterpreter( + jinjava, + jinjava.getGlobalContextCopy(), + JinjavaConfig.newBuilder().withPreserveRawTags(true).build() + ); + preserveInterpreter.getContext().put("deferred", DeferredValue.instance()); + interpreter.getContext().put("deferred", DeferredValue.instance()); + + String preservedResult = tag.interpret(tagNode, preserveInterpreter); + String nonPreservedResult = tag.interpret(tagNode, interpreter); + try { + assertThat(preservedResult) + .isEqualTo( + Resources.toString( + Resources.getResource("tags/rawtag/deferred.jinja"), + StandardCharsets.UTF_8 + ) + ); + } catch (IOException e) { + throw new RuntimeException(e); + } + assertThat(nonPreservedResult).isEqualTo("{{ deferred }}"); + + // Should not get evaluated because it's wrapped in a raw tag. + String deferredRealValue = "Resolved value."; + preserveInterpreter.getContext().put("deferred", deferredRealValue); + interpreter.getContext().put("deferred", deferredRealValue); + String preservedIdempotent = tag.interpret( + (TagNode) new TreeParser(preserveInterpreter, preservedResult) + .buildTree() + .getChildren() + .getFirst(), + preserveInterpreter + ); + String secondPass = tag.interpret( + (TagNode) new TreeParser(interpreter, preservedResult) + .buildTree() + .getChildren() + .getFirst(), + interpreter + ); + + assertThat(preservedIdempotent).isEqualTo(preservedResult); + assertThat(secondPass).isEqualTo("{{ deferred }}"); + } + private TagNode fixture(String name) { return (TagNode) fixtures(name).getFirst(); } diff --git a/src/test/resources/tags/rawtag/deferred.jinja b/src/test/resources/tags/rawtag/deferred.jinja new file mode 100644 index 000000000..6bff37ec6 --- /dev/null +++ b/src/test/resources/tags/rawtag/deferred.jinja @@ -0,0 +1 @@ +{% raw %}{{ deferred }}{% endraw %} \ No newline at end of file From bccbc982f797494ef5e9a9d80c4f5bc544bc8091 Mon Sep 17 00:00:00 2001 From: Jack Smith Date: Fri, 23 Oct 2020 11:21:49 -0400 Subject: [PATCH 09/12] Removed hasPreservedRawTags --- .../com/hubspot/jinjava/interpret/Context.java | 16 ---------------- .../java/com/hubspot/jinjava/lib/tag/RawTag.java | 1 - 2 files changed, 17 deletions(-) diff --git a/src/main/java/com/hubspot/jinjava/interpret/Context.java b/src/main/java/com/hubspot/jinjava/interpret/Context.java index c57c535a4..0afa5d4fb 100644 --- a/src/main/java/com/hubspot/jinjava/interpret/Context.java +++ b/src/main/java/com/hubspot/jinjava/interpret/Context.java @@ -78,7 +78,6 @@ public enum Library { private final Set resolvedFunctions = new HashSet<>(); private Set deferredNodes = new HashSet<>(); - private boolean hasPreservedRawTags = false; private final ExpTestLibrary expTestLibrary; private final FilterLibrary filterLibrary; @@ -291,21 +290,6 @@ public Set getDeferredNodes() { return ImmutableSet.copyOf(deferredNodes); } - public void handlePreservedRawTag() { - hasPreservedRawTags = true; - if (getParent() != null) { - Context parent = getParent(); - //Ignore global context - if (parent.getParent() != null) { - getParent().handlePreservedRawTag(); - } - } - } - - public boolean getHasPreservedRawTags() { - return hasPreservedRawTags; - } - public List getSuperBlock() { if (superBlock != null) { return superBlock; diff --git a/src/main/java/com/hubspot/jinjava/lib/tag/RawTag.java b/src/main/java/com/hubspot/jinjava/lib/tag/RawTag.java index 9000e8eff..ce973544b 100644 --- a/src/main/java/com/hubspot/jinjava/lib/tag/RawTag.java +++ b/src/main/java/com/hubspot/jinjava/lib/tag/RawTag.java @@ -31,7 +31,6 @@ public String getName() { @Override public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) { if (interpreter.getConfig().isPreserveRawTags()) { - interpreter.getContext().handlePreservedRawTag(); return renderNodeRaw(tagNode); } From 277cc64356f344e112974e441366101474d11cc5 Mon Sep 17 00:00:00 2001 From: Jack Smith Date: Fri, 23 Oct 2020 11:24:01 -0400 Subject: [PATCH 10/12] Use more generic config flag name: "preserveForSecondPass" --- .../com/hubspot/jinjava/JinjavaConfig.java | 18 +++++++++--------- .../com/hubspot/jinjava/lib/tag/RawTag.java | 2 +- .../interpret/JinjavaInterpreterTest.java | 7 +++---- .../hubspot/jinjava/lib/tag/RawTagTest.java | 4 ++-- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/hubspot/jinjava/JinjavaConfig.java b/src/main/java/com/hubspot/jinjava/JinjavaConfig.java index 857b52411..e11169e12 100644 --- a/src/main/java/com/hubspot/jinjava/JinjavaConfig.java +++ b/src/main/java/com/hubspot/jinjava/JinjavaConfig.java @@ -56,7 +56,7 @@ public class JinjavaConfig { private TokenScannerSymbols tokenScannerSymbols; private ELResolver elResolver; private final boolean iterateOverMapKeys; - private final boolean preserveRawTags; + private final boolean preserveForSecondPass; public static Builder newBuilder() { return new Builder(); @@ -141,7 +141,7 @@ private JinjavaConfig( TokenScannerSymbols tokenScannerSymbols, ELResolver elResolver, boolean iterateOverMapKeys, - boolean preserveRawTags + boolean preserveForSecondPass ) { this.charset = charset; this.locale = locale; @@ -162,7 +162,7 @@ private JinjavaConfig( this.tokenScannerSymbols = tokenScannerSymbols; this.elResolver = elResolver; this.iterateOverMapKeys = iterateOverMapKeys; - this.preserveRawTags = preserveRawTags; + this.preserveForSecondPass = preserveForSecondPass; } public Charset getCharset() { @@ -245,8 +245,8 @@ public boolean isIterateOverMapKeys() { return iterateOverMapKeys; } - public boolean isPreserveRawTags() { - return preserveRawTags; + public boolean isPreserveForSecondPass() { + return preserveForSecondPass; } public static class Builder { @@ -272,7 +272,7 @@ public static class Builder { private TokenScannerSymbols tokenScannerSymbols = new DefaultTokenScannerSymbols(); private ELResolver elResolver = JinjavaInterpreterResolver.DEFAULT_RESOLVER_READ_ONLY; private boolean iterateOverMapKeys; - private boolean preserveRawTags; + private boolean preserveForSecondPass; private Builder() {} @@ -381,8 +381,8 @@ public Builder withIterateOverMapKeys(boolean iterateOverMapKeys) { return this; } - public Builder withPreserveRawTags(boolean preserveRawTags) { - this.preserveRawTags = preserveRawTags; + public Builder withPreserveForSecondPass(boolean preserveForSecondPass) { + this.preserveForSecondPass = preserveForSecondPass; return this; } @@ -407,7 +407,7 @@ public JinjavaConfig build() { tokenScannerSymbols, elResolver, iterateOverMapKeys, - preserveRawTags + preserveForSecondPass ); } } diff --git a/src/main/java/com/hubspot/jinjava/lib/tag/RawTag.java b/src/main/java/com/hubspot/jinjava/lib/tag/RawTag.java index ce973544b..1f5afbccd 100644 --- a/src/main/java/com/hubspot/jinjava/lib/tag/RawTag.java +++ b/src/main/java/com/hubspot/jinjava/lib/tag/RawTag.java @@ -30,7 +30,7 @@ public String getName() { @Override public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) { - if (interpreter.getConfig().isPreserveRawTags()) { + if (interpreter.getConfig().isPreserveForSecondPass()) { return renderNodeRaw(tagNode); } diff --git a/src/test/java/com/hubspot/jinjava/interpret/JinjavaInterpreterTest.java b/src/test/java/com/hubspot/jinjava/interpret/JinjavaInterpreterTest.java index 086fbd68f..a0483f0bf 100644 --- a/src/test/java/com/hubspot/jinjava/interpret/JinjavaInterpreterTest.java +++ b/src/test/java/com/hubspot/jinjava/interpret/JinjavaInterpreterTest.java @@ -223,9 +223,9 @@ public void itLimitsOutputSizeWhenSumOfNodeSizesExceedsMax() { @Test public void itCanPreserveRawTags() { - JinjavaConfig preserveRawTagsConfig = JinjavaConfig + JinjavaConfig preserveConfig = JinjavaConfig .newBuilder() - .withPreserveRawTags(true) + .withPreserveForSecondPass(true) .build(); String input = "1{% raw %}2{% endraw %}3"; String normalOutput = "123"; @@ -235,8 +235,7 @@ public void itCanPreserveRawTags() { assertThat(renderResult.getOutput()).isEqualTo(normalOutput); assertThat(renderResult.hasErrors()).isFalse(); - renderResult = - new Jinjava(preserveRawTagsConfig).renderForResult(input, new HashMap<>()); + renderResult = new Jinjava(preserveConfig).renderForResult(input, new HashMap<>()); assertThat(renderResult.getOutput()).isEqualTo(preservedOutput); assertThat(renderResult.hasErrors()).isFalse(); } diff --git a/src/test/java/com/hubspot/jinjava/lib/tag/RawTagTest.java b/src/test/java/com/hubspot/jinjava/lib/tag/RawTagTest.java index 4bc9c3c2b..1ac2d2790 100644 --- a/src/test/java/com/hubspot/jinjava/lib/tag/RawTagTest.java +++ b/src/test/java/com/hubspot/jinjava/lib/tag/RawTagTest.java @@ -101,7 +101,7 @@ public void itPreservesRawTags() { JinjavaInterpreter preserveInterpreter = new JinjavaInterpreter( jinjava, jinjava.getGlobalContextCopy(), - JinjavaConfig.newBuilder().withPreserveRawTags(true).build() + JinjavaConfig.newBuilder().withPreserveForSecondPass(true).build() ); String result = tag.interpret(tagNode, preserveInterpreter); try { @@ -123,7 +123,7 @@ public void itPreservesDeferredWhilePreservingRawTags() { JinjavaInterpreter preserveInterpreter = new JinjavaInterpreter( jinjava, jinjava.getGlobalContextCopy(), - JinjavaConfig.newBuilder().withPreserveRawTags(true).build() + JinjavaConfig.newBuilder().withPreserveForSecondPass(true).build() ); preserveInterpreter.getContext().put("deferred", DeferredValue.instance()); interpreter.getContext().put("deferred", DeferredValue.instance()); From 1e041b1871fa8f24b675e9aa7d7c991f607ece91 Mon Sep 17 00:00:00 2001 From: Jack Smith Date: Fri, 23 Oct 2020 11:27:23 -0400 Subject: [PATCH 11/12] Add trailing newline to test jinja resource --- src/test/resources/tags/rawtag/deferred.jinja | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/resources/tags/rawtag/deferred.jinja b/src/test/resources/tags/rawtag/deferred.jinja index 6bff37ec6..8af7865b1 100644 --- a/src/test/resources/tags/rawtag/deferred.jinja +++ b/src/test/resources/tags/rawtag/deferred.jinja @@ -1 +1 @@ -{% raw %}{{ deferred }}{% endraw %} \ No newline at end of file +{% raw %}{{ deferred }}{% endraw %} From 05b5b9ca5941c722e85148c8d3675581941a483b Mon Sep 17 00:00:00 2001 From: Jack Smith Date: Fri, 23 Oct 2020 15:08:01 -0400 Subject: [PATCH 12/12] Trim before comparing in raw test --- .../java/com/hubspot/jinjava/lib/tag/RawTagTest.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/hubspot/jinjava/lib/tag/RawTagTest.java b/src/test/java/com/hubspot/jinjava/lib/tag/RawTagTest.java index 1ac2d2790..a5fb94466 100644 --- a/src/test/java/com/hubspot/jinjava/lib/tag/RawTagTest.java +++ b/src/test/java/com/hubspot/jinjava/lib/tag/RawTagTest.java @@ -133,10 +133,12 @@ public void itPreservesDeferredWhilePreservingRawTags() { try { assertThat(preservedResult) .isEqualTo( - Resources.toString( - Resources.getResource("tags/rawtag/deferred.jinja"), - StandardCharsets.UTF_8 - ) + Resources + .toString( + Resources.getResource("tags/rawtag/deferred.jinja"), + StandardCharsets.UTF_8 + ) + .trim() ); } catch (IOException e) { throw new RuntimeException(e);