Skip to content

Commit 222b145

Browse files
authored
fix: require a whitespace before js style comments inside html content (#127)
1 parent 89fe2c7 commit 222b145

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

.changeset/wicked-mangos-camp.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"htmljs-parser": patch
3+
---
4+
5+
Fix regression around JS style comments in the body by requiring that they are preceded by a whitespace.

src/__tests__/fixtures/comments-within-tag-body/__snapshots__/comments-within-tag-body.expected.txt

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,19 @@
1616
6╭─ <!-- and this is a comment -->
1717
│ │ ╰─ comment.value " and this is a comment "
1818
╰─ ╰─ comment "<!-- and this is a comment -->"
19-
7╭─ </div>
20-
│ │ │ ╰─ closeTagEnd(div)
21-
│ │ ╰─ closeTagName "div"
22-
│ ├─ text "\n"
23-
╰─ ╰─ closeTagStart "</"
24-
8╰─
19+
7╭─ But this, this is not a http://comment.com
20+
╰─ ╰─ text "\n But this, this is not a http://comment.com\n And yet this is a "
21+
8╭─ And yet this is a //comment
22+
│ │ ╰─ comment.value "comment"
23+
╰─ ╰─ comment "//comment"
24+
9╭─ And also /* this is a comment */
25+
│ │ │ ╰─ comment.value " this is a comment "
26+
│ │ ╰─ comment "/* this is a comment */"
27+
╰─ ╰─ text "\n And also "
28+
10╭─ Because a//comment must be preceded by whitespace.
29+
╰─ ╰─ text "\n Because a//comment must be preceded by whitespace.\n"
30+
11╭─ </div>
31+
│ │ │ ╰─ closeTagEnd(div)
32+
│ │ ╰─ closeTagName "div"
33+
╰─ ╰─ closeTagStart "</"
34+
12╰─

src/__tests__/fixtures/comments-within-tag-body/input.marko

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@
44
/* But this is a comment */
55
And this is more text
66
<!-- and this is a comment -->
7+
But this, this is not a http://comment.com
8+
And yet this is a //comment
9+
And also /* this is a comment */
10+
Because a//comment must be preceded by whitespace.
711
</div>

src/states/HTML_CONTENT.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ export const HTML_CONTENT: StateDefinition<HTMLContentMeta> = {
9494
this.endText();
9595
this.enterState(STATE.INLINE_SCRIPT);
9696
this.pos++; // skip space
97-
} else if (code === CODE.FORWARD_SLASH) {
97+
} else if (
98+
code === CODE.FORWARD_SLASH &&
99+
isWhitespaceCode(this.lookAtCharCodeAhead(-1))
100+
) {
98101
// Check next character to see if we are in a comment
99102
switch (this.lookAtCharCodeAhead(1)) {
100103
case CODE.FORWARD_SLASH:

0 commit comments

Comments
 (0)