Skip to content

Commit

Permalink
fix-escaping-double-quotes (#330)
Browse files Browse the repository at this point in the history
* Fix escaping double quotes

* Return backslashInString test
  • Loading branch information
mhdeeb authored Feb 10, 2024
1 parent ceaa1d2 commit 1daa14b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
14 changes: 13 additions & 1 deletion jte/src/main/java/gg/jte/compiler/TemplateParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private void doParse(int startingDepth) {
}
} else if (currentChar == '"' && currentMode.isTrackStrings()) {
push(Mode.JavaCodeString);
} else if (currentChar == '"' && currentMode == Mode.JavaCodeString && (previousChar != '\\' || previousChar(2) == '\\')) {
} else if (currentChar == '"' && currentMode == Mode.JavaCodeString && (countBefore('\\') % 2 == 0)) {
pop();
} else if (currentChar == '}' && currentMode == Mode.Code) {
pop();
Expand Down Expand Up @@ -364,6 +364,18 @@ private void doParse(int startingDepth) {
}
}

private int countBefore(char c) {
int count = 0;
for (int j = i - 1; j >= 0; --j) {
if (templateCode.charAt(j) == c) {
++count;
} else {
break;
}
}
return count;
}

@SuppressWarnings("SameParameterValue")
private char previousChar(int offset) {
if (i - offset < 0) {
Expand Down
6 changes: 6 additions & 0 deletions jte/src/test/java/gg/jte/TemplateEngineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,12 @@ void backslashInString() {
thenOutputIs("\\");
}

@Test
void backslashAndDoubleQuoteInString() {
givenTemplate("${\"\\\\\\\"\\\\\"}");
thenOutputIs("\\\"\\");
}

@Test
void npe_internal() {
givenTemplate("This is ${model.getThatThrows()} world");
Expand Down

0 comments on commit 1daa14b

Please sign in to comment.