Skip to content

Commit

Permalink
#308 fix for backtick in comment within content block
Browse files Browse the repository at this point in the history
  • Loading branch information
casid committed Dec 29, 2023
1 parent 9915fd7 commit 0c83398
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
5 changes: 5 additions & 0 deletions jte/src/main/java/gg/jte/compiler/ContentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public void process() {
push(Mode.Raw);
} else if (regionMatches("@endraw") && currentMode == Mode.Raw) {
pop();
} else if (regionMatches("<%--") && currentMode == Mode.Content) {
push(Mode.Comment);
} else if (regionMatches("--%>") && currentMode == Mode.Comment) {
pop();
}
}

Expand Down Expand Up @@ -79,6 +83,7 @@ private enum Mode {
Code,
Content,
Raw,
Comment,
;

boolean isContentBlockAllowed() {
Expand Down
10 changes: 7 additions & 3 deletions jte/src/main/java/gg/jte/compiler/TemplateParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ private void doParse(int startingDepth) {
} else if (currentMode == Mode.Comment) {
if (regionMatches("--%>")) {
pop();
lastIndex = i + 1;
if (currentMode == Mode.Text) {
lastIndex = i + 1;
}
}
} else if (currentMode == Mode.HtmlComment) {
if (regionMatches("-->")) {
Expand Down Expand Up @@ -403,7 +405,7 @@ private int getCurrentTemplateLine() {
}

private boolean isCommentAllowed() {
return currentMode == Mode.Text;
return currentMode == Mode.Text || currentMode == Mode.Content;
}

private boolean isParamOrImportAllowed() {
Expand Down Expand Up @@ -621,7 +623,9 @@ private void extractHtmlCodePart() {

private void extractComment(Mode mode, int startIndex) {
if (paramsComplete || areParamsComplete(startIndex)) {
extractTextPart(startIndex, mode);
if (currentMode == Mode.Text) {
extractTextPart(startIndex, mode);
}
}
push(mode);
}
Expand Down
12 changes: 12 additions & 0 deletions jte/src/test/java/gg/jte/TemplateEngineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,18 @@ void htmlCommentInPlainTemplate() {
thenOutputIs("Hello<!--This is an HTML comment--> World");
}

@Test
void commentWithBacktickInContentBlock() {
givenTemplate("""
!{var content = @`<%--`--%>Hello there!`;}
${content}
""");
thenOutputIs("""
Hello there!
""");
}

@Test
void importInCss() {
givenTemplate("<style type=\"text/css\" rel=\"stylesheet\" media=\"all\">\n" +
Expand Down

0 comments on commit 0c83398

Please sign in to comment.