Skip to content

Commit

Permalink
#359 fix jte comments between HTML attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
casid committed May 16, 2024
1 parent de38a7c commit 3ad76aa
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 21 deletions.
32 changes: 11 additions & 21 deletions jte/src/main/java/gg/jte/compiler/TemplateParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,16 @@ private void doParse(int startingDepth) {

visitor.onRawEnd(depth);
}
} else if (isCommentAllowed() && regionMatches("<%--")) {
extractComment(Mode.Comment, i - 3);
} else if (isCommentAllowed() && regionMatches("<!--") && isHtmlCommentAllowed()) {
extractComment(Mode.HtmlComment, i - 3);
} else if (isCommentAllowed() && regionMatches("/*") && isCssCommentAllowed()) {
extractComment(Mode.CssComment, i - 1);
} else if (isCommentAllowed() && regionMatches("//") && isJsCommentAllowed()) {
extractComment(Mode.JsComment, i - 1);
} else if (isCommentAllowed() && regionMatches("/*") && isJsCommentAllowed()) {
extractComment(Mode.JsBlockComment, i - 1);
} else if (isCommentAllowed() && lookAheadRegionMatches("<%--")) {
extractComment(Mode.Comment, i);
} else if (isCommentAllowed() && lookAheadRegionMatches("<!--") && isHtmlCommentAllowed()) {
extractComment(Mode.HtmlComment, i);
} else if (isCommentAllowed() && lookAheadRegionMatches("/*") && isCssCommentAllowed()) {
extractComment(Mode.CssComment, i);
} else if (isCommentAllowed() && lookAheadRegionMatches("//") && isJsCommentAllowed()) {
extractComment(Mode.JsComment, i);
} else if (isCommentAllowed() && lookAheadRegionMatches("/*") && isJsCommentAllowed()) {
extractComment(Mode.JsBlockComment, i);
} else if (currentMode == Mode.Comment) {
if (regionMatches("--%>")) {
pop();
Expand Down Expand Up @@ -706,7 +706,7 @@ private void interceptHtmlTags() {
currentAttribute.value = templateCode.substring(currentAttribute.valueStartIndex, i);

if (currentAttribute.isSmartAttribute()) {
extractTextPart(getLastWhitespaceIndex(currentAttribute.startIndex - 1), Mode.Code);
extractTextPart(currentAttribute.startIndex - 1, Mode.Text);
lastIndex = i + 1;

visitor.onHtmlAttributeOutput(depth, currentHtmlTag, currentAttribute);
Expand Down Expand Up @@ -917,16 +917,6 @@ private boolean isHtmlAttributeSingleOutput(int nameEndIndex, char quotes) {
return false;
}

private int getLastWhitespaceIndex(int index) {
for (; index >= 0; --index) {
if (!Character.isWhitespace(templateCode.charAt(index))) {
++index;
break;
}
}
return index;
}

private boolean isHtmlTagIntercepted(String name) {
if (htmlTags != null) {
for (String htmlTag : htmlTags) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,39 @@ void doctype() {
assertThat(output.toString()).isEqualTo("<!DOCTYPE html>");
}

@Test
void commentBetweenAttributes() {
templateEngine.setTrimControlStructures(true);
codeResolver.givenCode("template.jte", """
@param String url
<form hx-post="${url}" <%-- (1) --%>
hx-target="${url}" <%-- (2) --%>
hx-swap="outerHTML"> <%-- (2) --%>
<select>
@for(var i = 0; i < 3; i++) <%-- (3) --%>
<option value="${i}">${i}</option>
@endfor
</select>
<button type="submit">Add User to group</button>
</form>
""");

templateEngine.render("template.jte", "https://jte.gg", output);

assertThat(output.toString()).isEqualTo("""
<form hx-post="https://jte.gg"\s
hx-target="https://jte.gg"\s
hx-swap="outerHTML">\s
<select>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<button type="submit">Add User to group</button>
</form>
""");
}

@Test
void htmlComment() {
codeResolver.givenCode("template.jte", "@param String url\n<!-- html comment --><a href=\"${url}\">Click me!</a>");
Expand Down

0 comments on commit 3ad76aa

Please sign in to comment.