Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix-escaping-double-quotes #330

Merged
merged 2 commits into from
Feb 10, 2024
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
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
Loading