Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ public Object resolveObject(String variable, int lineNumber, int startPosition)
return "";
}
if (WhitespaceUtils.isQuoted(variable)) {
return WhitespaceUtils.unquote(variable);
return WhitespaceUtils.unquoteAndUnescape(variable);
} else {
Object val = retraceVariable(variable, lineNumber, startPosition);
if (val == null) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/hubspot/jinjava/lib/tag/BlockTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public OutputNode interpretOutput(TagNode tagNode, JinjavaInterpreter interprete
);
}

String blockName = WhitespaceUtils.unquote(tagData.next());
String blockName = WhitespaceUtils.unquoteAndUnescape(tagData.next());

interpreter.addBlock(
blockName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class HelperStringTokenizer extends AbstractIterator<String> {
private boolean useComma = false;
private char quoteChar = 0;
private boolean inQuote = false;
private boolean isEscaped = false;

public HelperStringTokenizer(String s) {
value = s.toCharArray();
Expand Down Expand Up @@ -71,7 +72,8 @@ protected String computeNext() {

private String makeToken() {
char c = value[currPost++];
if (c == '"' || c == '\'') {

if ((c == '"' || c == '\'') && !isEscaped) {
if (inQuote) {
if (quoteChar == c) {
inQuote = false;
Expand All @@ -81,6 +83,9 @@ private String makeToken() {
quoteChar = c;
}
}

isEscaped = (c == '\\' && !isEscaped);

if ((Character.isWhitespace(c) || (useComma && c == ',')) && !inQuote) {
return newToken();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ public static String unquote(String s) {
return s.trim();
}

// TODO see if all usages of unquote can use this method instead
public static String unquoteAndUnescape(String s) {
if (Strings.isNullOrEmpty(s)) {
return "";
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/com/hubspot/jinjava/lib/tag/CycleTagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,11 @@ public void itDefaultsMultipleNullToImageUsingAs() {
"{% for item in [0,1] %}{% cycle {{foo}},{{bar}} as var %}{% cycle var %}{% endfor %}";
assertThat(interpreter.render(template)).isEqualTo("{{foo}}{{bar}}");
}

@Test
public void itHandlesEscapedQuotes() {
String template =
"{% for item in [0,1] %}{% cycle 'a','class=\\'foo bar\\'' %}.{% endfor %}";
assertThat(interpreter.render(template)).isEqualTo("a.class='foo bar'.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.hubspot.jinjava.lib.tag.Tag;
import com.hubspot.jinjava.mode.EagerExecutionMode;
import com.hubspot.jinjava.tree.parse.TagToken;
import java.util.List;
import java.util.Optional;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -65,9 +66,22 @@ public void itAddCycleTagAsADeferredToken() {

@Test
public void itHandlesDeferredCycle() {
interpreter.getContext().put("deferred", DeferredValue.instance());
String template =
"{% set l = [] %}{% for item in deferred %}{% cycle l.append(deferred),5 %}{% endfor %}{{ l }}";
assertThat(interpreter.render(template)).isEqualTo(template);
}

@Test
public void iitHandlesEscapedQuotesInVariable() {
String template =
"{% set class = \"class='foo bar'\" %}{% for item in deferred %}{% cycle 'item-1',class %}.{% endfor %}";
String firstPass = interpreter.render(template);
assertThat(firstPass)
.isEqualTo(
"{% for item in deferred %}{% cycle 'item-1','class=\\'foo bar\\'' %}.{% endfor %}"
);
interpreter.getContext().put("deferred", List.of(0, 1));
String secondPass = interpreter.render(firstPass);
assertThat(secondPass).isEqualTo("item-1.class='foo bar'.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,30 @@ public void itDoesntReturnTrailingNull() {
.containsExactly("product", "in", "collections.frontpage.products")
.doesNotContainNull();
}

@Test
public void itHandlesEscapedQuotesWithinQuotedStrings() {
assertThat(
new HelperStringTokenizer("'hi','y\\'all don\\'t'").splitComma(true).allTokens()
)
.containsExactly("'hi'", "'y\\'all don\\'t'");
}

@Test
public void itHandlesEscapedDoubleQuotesWithinQuotedStrings() {
assertThat(
new HelperStringTokenizer("\"hi\",\"say \\\"hello\\\"\"")
.splitComma(true)
.allTokens()
)
.containsExactly("\"hi\"", "\"say \\\"hello\\\"\"");
}

@Test
public void itHandlesEscapedBackslashes() {
assertThat(
new HelperStringTokenizer("'path\\\\to\\file'").splitComma(true).allTokens()
)
.containsExactly("'path\\\\to\\file'");
}
}