From 40aabeb8f9fbcf00cd30bf14261945d9163d8866 Mon Sep 17 00:00:00 2001 From: Jack Smith Date: Thu, 19 Aug 2021 10:17:56 -0400 Subject: [PATCH] Refactor logic for doing nested interpretation and make it part of the ExpressionStrategy interface --- .../expression/DefaultExpressionStrategy.java | 19 ++---- .../expression/EagerExpressionStrategy.java | 58 +++++++++++-------- .../lib/expression/ExpressionStrategy.java | 16 +++++ 3 files changed, 54 insertions(+), 39 deletions(-) diff --git a/src/main/java/com/hubspot/jinjava/lib/expression/DefaultExpressionStrategy.java b/src/main/java/com/hubspot/jinjava/lib/expression/DefaultExpressionStrategy.java index cc1c2b589..cd2e5eac1 100644 --- a/src/main/java/com/hubspot/jinjava/lib/expression/DefaultExpressionStrategy.java +++ b/src/main/java/com/hubspot/jinjava/lib/expression/DefaultExpressionStrategy.java @@ -6,7 +6,6 @@ import com.hubspot.jinjava.tree.output.RenderedOutputNode; import com.hubspot.jinjava.tree.parse.ExpressionToken; import com.hubspot.jinjava.util.Logging; -import org.apache.commons.lang3.StringUtils; public class DefaultExpressionStrategy implements ExpressionStrategy { private static final long serialVersionUID = 436239440273704843L; @@ -21,19 +20,11 @@ public RenderedOutputNode interpretOutput( ); String result = interpreter.getAsString(var); - if (interpreter.getConfig().isNestedInterpretationEnabled()) { - if ( - !StringUtils.equals(result, master.getImage()) && - ( - StringUtils.contains(result, master.getSymbols().getExpressionStart()) || - StringUtils.contains(result, master.getSymbols().getExpressionStartWithTag()) - ) - ) { - try { - result = interpreter.renderFlat(result); - } catch (Exception e) { - Logging.ENGINE_LOG.warn("Error rendering variable node result", e); - } + if (shouldDoNestedInterpretation(result, master, interpreter)) { + try { + result = interpreter.renderFlat(result); + } catch (Exception e) { + Logging.ENGINE_LOG.warn("Error rendering variable node result", e); } } diff --git a/src/main/java/com/hubspot/jinjava/lib/expression/EagerExpressionStrategy.java b/src/main/java/com/hubspot/jinjava/lib/expression/EagerExpressionStrategy.java index fb23d1924..5238f5213 100644 --- a/src/main/java/com/hubspot/jinjava/lib/expression/EagerExpressionStrategy.java +++ b/src/main/java/com/hubspot/jinjava/lib/expression/EagerExpressionStrategy.java @@ -48,27 +48,17 @@ private String eagerResolveExpression( } if (eagerExecutionResult.getResult().isFullyResolved()) { String result = eagerExecutionResult.getResult().toString(true); - if ( - !StringUtils.equals(result, master.getImage()) && - ( - StringUtils.contains(result, master.getSymbols().getExpressionStart()) || - StringUtils.contains(result, master.getSymbols().getExpressionStartWithTag()) - ) - ) { - if (interpreter.getConfig().isNestedInterpretationEnabled()) { - long errorSizeStart = getUnclosedCommentErrorsCount(interpreter); - interpreter.parse(result); - if (getUnclosedCommentErrorsCount(interpreter) == errorSizeStart) { - try { - result = interpreter.renderFlat(result); - } catch (Exception e) { - Logging.ENGINE_LOG.warn("Error rendering variable node result", e); - } - } - } else { - // Possible macro/set tag in front of this one. Includes result - result = wrapInRawOrExpressionIfNeeded(result, interpreter); + long errorSizeStart = getUnclosedCommentErrorsCount(interpreter); + + if (shouldDoNestedInterpretation(result, master, interpreter)) { + try { + result = interpreter.renderFlat(result); + } catch (Exception e) { + Logging.ENGINE_LOG.warn("Error rendering variable node result", e); } + } else if (getUnclosedCommentErrorsCount(interpreter) == errorSizeStart) { + // Possible macro/set tag in front of this one. Includes result + result = wrapInRawOrExpressionIfNeeded(result, master, interpreter); } if (interpreter.getContext().isAutoEscape()) { @@ -113,6 +103,22 @@ private String eagerResolveExpression( ); } + @Override + public boolean shouldDoNestedInterpretation( + String result, + ExpressionToken master, + JinjavaInterpreter interpreter + ) { + if ( + ExpressionStrategy.super.shouldDoNestedInterpretation(result, master, interpreter) + ) { + long errorSizeStart = getUnclosedCommentErrorsCount(interpreter); + interpreter.parse(result); + return getUnclosedCommentErrorsCount(interpreter) == errorSizeStart; + } + return false; + } + private long getUnclosedCommentErrorsCount(JinjavaInterpreter interpreter) { return interpreter .getErrors() @@ -122,21 +128,23 @@ private long getUnclosedCommentErrorsCount(JinjavaInterpreter interpreter) { } private static String wrapInRawOrExpressionIfNeeded( - String output, + String result, + ExpressionToken master, JinjavaInterpreter interpreter ) { JinjavaConfig config = interpreter.getConfig(); if ( config.getExecutionMode().isPreserveRawTags() && !interpreter.getContext().isUnwrapRawOverride() && + !StringUtils.equals(result, master.getImage()) && ( - output.contains(config.getTokenScannerSymbols().getExpressionStart()) || - output.contains(config.getTokenScannerSymbols().getExpressionStartWithTag()) + result.contains(master.getSymbols().getExpressionStart()) || + result.contains(master.getSymbols().getExpressionStartWithTag()) ) ) { - return EagerTagDecorator.wrapInTag(output, RawTag.TAG_NAME, interpreter); + return EagerTagDecorator.wrapInTag(result, RawTag.TAG_NAME, interpreter); } - return output; + return result; } private static String wrapInExpression(String output, JinjavaInterpreter interpreter) { diff --git a/src/main/java/com/hubspot/jinjava/lib/expression/ExpressionStrategy.java b/src/main/java/com/hubspot/jinjava/lib/expression/ExpressionStrategy.java index bcb5edc9a..80946902d 100644 --- a/src/main/java/com/hubspot/jinjava/lib/expression/ExpressionStrategy.java +++ b/src/main/java/com/hubspot/jinjava/lib/expression/ExpressionStrategy.java @@ -4,10 +4,26 @@ import com.hubspot.jinjava.tree.output.RenderedOutputNode; import com.hubspot.jinjava.tree.parse.ExpressionToken; import java.io.Serializable; +import org.apache.commons.lang3.StringUtils; public interface ExpressionStrategy extends Serializable { RenderedOutputNode interpretOutput( ExpressionToken master, JinjavaInterpreter interpreter ); + + default boolean shouldDoNestedInterpretation( + String result, + ExpressionToken master, + JinjavaInterpreter interpreter + ) { + return ( + interpreter.getConfig().isNestedInterpretationEnabled() && + !StringUtils.equals(result, master.getImage()) && + ( + StringUtils.contains(result, master.getSymbols().getExpressionStart()) || + StringUtils.contains(result, master.getSymbols().getExpressionStartWithTag()) + ) + ); + } }