Skip to content

Commit

Permalink
Merge pull request #623 from HubSpot/trim-quote-check
Browse files Browse the repository at this point in the history
Trim before checking if expression is quoted
  • Loading branch information
jasmith-hs authored Mar 16, 2021
2 parents 76efb34 + 2d835e1 commit 6b61dbf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/main/java/com/hubspot/jinjava/util/WhitespaceUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ public static boolean isExpressionQuoted(String s) {
if (Strings.isNullOrEmpty(s)) {
return false;
}
char[] charArray = s.toCharArray();
char[] charArray = s.trim().toCharArray();
if (charArray.length == 1) {
return false;
}
char quoteChar = 0;
for (char c : QUOTE_CHARS) {
if (charArray[0] == c) {
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/com/hubspot/jinjava/util/WhitespaceUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,17 @@ public void itKnowsWhenAnExpressionIsQuoted() {
assertThat(isExpressionQuoted("\"foo 'and' bar\"")).isTrue();
assertThat(isExpressionQuoted("\"foo 'and' bar'")).isFalse();
}

@Test
public void itKnowsUntrimmedExpressionIsQuoted() {
assertThat(isExpressionQuoted(" 'foo'")).isTrue();
assertThat(isExpressionQuoted("'foo' ")).isTrue();
assertThat(isExpressionQuoted(" 'foo' ")).isTrue();
}

@Test
public void itDoesntCountSingleQuoteChar() {
assertThat(isExpressionQuoted("'")).isFalse();
assertThat(isExpressionQuoted("\" ")).isFalse();
}
}

0 comments on commit 6b61dbf

Please sign in to comment.