Skip to content
This repository has been archived by the owner on Sep 15, 2018. It is now read-only.

Commit

Permalink
store autoEscape flag in context outside of user-editable properties
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredstehler committed Dec 4, 2015
1 parent 22f028d commit ff2bea9
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Version 2.1.2 ([Maven Central](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.hubspot.jinjava%22%20AND%20v%3A%222.1.2%22)) ###

* Use resolved path value in include tag cycle detection
* Store autoEscape flag in context outside of user-scoped properties

### Version 2.1.1 ([Maven Central](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.hubspot.jinjava%22%20AND%20v%3A%222.1.1%22)) ###

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/hubspot/jinjava/interpret/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public class Context extends ScopeMap<String, Object> {

private final Context parent;

private Boolean autoEscape;

public Context() {
this(null);
}
Expand Down Expand Up @@ -111,6 +113,22 @@ public boolean isGlobalMacro(String identifier) {
return getGlobalMacro(identifier) != null;
}

public boolean isAutoEscape() {
if (autoEscape != null) {
return autoEscape;
}

if (parent != null) {
return parent.isAutoEscape();
}

return false;
}

public void setAutoEscape(Boolean autoEscape) {
this.autoEscape = autoEscape;
}

@SafeVarargs
@SuppressWarnings("unchecked")
public final void registerClasses(Class<? extends Importable>... classes) {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/hubspot/jinjava/lib/tag/AutoEscapeTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"{% endautoescape %}")
})
public class AutoEscapeTag implements Tag {
public static final String AUTOESCAPE_CONTEXT_VAR = "__auto3sc@pe__";
private static final long serialVersionUID = 786006577642541285L;

@Override
Expand All @@ -38,7 +37,7 @@ public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
try (InterpreterScopeClosable c = interpreter.enterScope()) {
String boolFlagStr = StringUtils.trim(tagNode.getHelpers());
boolean escapeFlag = BooleanUtils.toBoolean(StringUtils.isNotBlank(boolFlagStr) ? boolFlagStr : "true");
interpreter.getContext().put(AUTOESCAPE_CONTEXT_VAR, escapeFlag);
interpreter.getContext().setAutoEscape(escapeFlag);

StringBuilder result = new StringBuilder();

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/hubspot/jinjava/tree/ExpressionNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.lib.filter.EscapeFilter;
import com.hubspot.jinjava.lib.tag.AutoEscapeTag;
import com.hubspot.jinjava.tree.output.OutputNode;
import com.hubspot.jinjava.tree.output.RenderedOutputNode;
import com.hubspot.jinjava.tree.parse.ExpressionToken;
Expand Down Expand Up @@ -51,7 +50,7 @@ public OutputNode render(JinjavaInterpreter interpreter) {
}
}

if (interpreter.getContext().get(AutoEscapeTag.AUTOESCAPE_CONTEXT_VAR, Boolean.FALSE).equals(Boolean.TRUE)) {
if (interpreter.getContext().isAutoEscape()) {
result = EscapeFilter.escapeHtmlEntities(result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.hubspot.jinjava.Jinjava;
import com.hubspot.jinjava.interpret.Context;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.lib.tag.AutoEscapeTag;

public class ExpressionNodeTest {

Expand Down Expand Up @@ -60,7 +59,7 @@ public void itEscapesValueWhenContextSet() throws Exception {
context.put("a", "foo < bar");
assertThat(val("{{ a }}")).isEqualTo("foo < bar");

context.put(AutoEscapeTag.AUTOESCAPE_CONTEXT_VAR, Boolean.TRUE);
context.setAutoEscape(true);
assertThat(val("{{ a }}")).isEqualTo("foo &lt; bar");
}

Expand Down

0 comments on commit ff2bea9

Please sign in to comment.