diff --git a/jte/src/main/java/gg/jte/compiler/ContentProcessor.java b/jte/src/main/java/gg/jte/compiler/ContentProcessor.java index 96bcb7b1..12e8caaa 100644 --- a/jte/src/main/java/gg/jte/compiler/ContentProcessor.java +++ b/jte/src/main/java/gg/jte/compiler/ContentProcessor.java @@ -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(); } } @@ -79,6 +83,7 @@ private enum Mode { Code, Content, Raw, + Comment, ; boolean isContentBlockAllowed() { diff --git a/jte/src/main/java/gg/jte/compiler/TemplateParser.java b/jte/src/main/java/gg/jte/compiler/TemplateParser.java index ce9ec162..136a5065 100644 --- a/jte/src/main/java/gg/jte/compiler/TemplateParser.java +++ b/jte/src/main/java/gg/jte/compiler/TemplateParser.java @@ -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("-->")) { @@ -403,7 +405,7 @@ private int getCurrentTemplateLine() { } private boolean isCommentAllowed() { - return currentMode == Mode.Text; + return currentMode == Mode.Text || currentMode == Mode.Content; } private boolean isParamOrImportAllowed() { @@ -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); } diff --git a/jte/src/test/java/gg/jte/TemplateEngineTest.java b/jte/src/test/java/gg/jte/TemplateEngineTest.java index 18d69e5c..f55406bc 100644 --- a/jte/src/test/java/gg/jte/TemplateEngineTest.java +++ b/jte/src/test/java/gg/jte/TemplateEngineTest.java @@ -791,6 +791,18 @@ void htmlCommentInPlainTemplate() { thenOutputIs("Hello World"); } + @Test + void commentWithBacktickInContentBlock() { + givenTemplate(""" + !{var content = @`<%--`--%>Hello there!`;} + ${content} + """); + thenOutputIs(""" + + Hello there! + """); + } + @Test void importInCss() { givenTemplate("