Skip to content

Commit 28fde07

Browse files
authored
feat: improve js style comment support (#119)
1 parent 112cf54 commit 28fde07

File tree

8 files changed

+115
-11
lines changed

8 files changed

+115
-11
lines changed

.changeset/empty-trains-grin.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+
Support JS line comments inside the open tag (previously just block comments could be used).

.changeset/gold-radios-eat.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+
Support JS style comments in HTML bodies (previously allowed in parsed text and concise mode).

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,19 @@
1010
│ ││ │ ╰─ attrValue "=\"bar\""
1111
│ ││ ╰─ attrName "foo"
1212
│ │╰─ tagName "div"
13-
╰─ ╰─ openTagStart
13+
╰─ ╰─ openTagStart
14+
2╭─ <div foo="bar" // this is a test
15+
│ ││ │ │╰─ attrValue.value "\"bar\""
16+
│ ││ │ ╰─ attrValue "=\"bar\""
17+
│ ││ ╰─ attrName "foo"
18+
│ │╰─ tagName "div"
19+
╰─ ╰─ openTagStart
20+
3╭─ hello="world" // this is another test
21+
│ │ │╰─ attrValue.value "\"world\""
22+
│ │ ╰─ attrValue "=\"world\""
23+
╰─ ╰─ attrName "hello"
24+
4╭─ ></div>
25+
│ ││ │ ╰─ closeTagEnd(div)
26+
│ ││ ╰─ closeTagName "div"
27+
│ │╰─ closeTagStart "</"
28+
╰─ ╰─ openTagEnd
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
<div foo="bar" /*this is a comment*/ hello="world" /* this is another comment */ ></div>
1+
<div foo="bar" /*this is a comment*/ hello="world" /* this is another comment */ ></div>
2+
<div foo="bar" // this is a test
3+
hello="world" // this is another test
4+
></div>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
1╭─ <div>
2+
│ ││ ╰─ openTagEnd
3+
│ │╰─ tagName "div"
4+
╰─ ╰─ openTagStart
5+
2╭─ // This is a comment
6+
│ │ │ ╰─ comment.value " This is a comment"
7+
│ │ ╰─ comment "// This is a comment"
8+
╰─ ╰─ text "\n "
9+
3╭─ This is some real text
10+
╰─ ╰─ text "\n This is some real text\n "
11+
4╭─ /* But this is a comment */
12+
│ │ ╰─ comment.value " But this is a comment "
13+
╰─ ╰─ comment "/* But this is a comment */"
14+
5╭─ And this is more text
15+
╰─ ╰─ text "\n And this is more text\n "
16+
6╭─ <!-- and this is a comment -->
17+
│ │ ╰─ comment.value " and this is a comment "
18+
╰─ ╰─ comment "<!-- and this is a comment -->"
19+
7╭─ </div>
20+
│ │ │ ╰─ closeTagEnd(div)
21+
│ │ ╰─ closeTagName "div"
22+
│ ├─ text "\n"
23+
╰─ ╰─ closeTagStart "</"
24+
8╰─
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div>
2+
// This is a comment
3+
This is some real text
4+
/* But this is a comment */
5+
And this is more text
6+
<!-- and this is a comment -->
7+
</div>

src/states/HTML_CONTENT.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,23 @@ 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) {
98+
// Check next character to see if we are in a comment
99+
switch (this.lookAtCharCodeAhead(1)) {
100+
case CODE.FORWARD_SLASH:
101+
this.endText();
102+
this.enterState(STATE.JS_COMMENT_LINE);
103+
this.pos++; // skip /
104+
break;
105+
case CODE.ASTERISK:
106+
this.endText();
107+
this.enterState(STATE.JS_COMMENT_BLOCK);
108+
this.pos++; // skip *
109+
break;
110+
default:
111+
this.startText();
112+
break;
113+
}
97114
} else if (!STATE.checkForPlaceholder(this, code)) {
98115
this.startText();
99116
}
@@ -115,7 +132,31 @@ export const HTML_CONTENT: StateDefinition<HTMLContentMeta> = {
115132

116133
eof: htmlEOF,
117134

118-
return() {},
135+
return(child) {
136+
switch (child.state) {
137+
case STATE.JS_COMMENT_LINE:
138+
this.options.onComment?.({
139+
start: child.start,
140+
end: child.end,
141+
value: {
142+
start: child.start + 2, // strip //
143+
end: child.end,
144+
},
145+
});
146+
break;
147+
case STATE.JS_COMMENT_BLOCK: {
148+
this.options.onComment?.({
149+
start: child.start,
150+
end: child.end,
151+
value: {
152+
start: child.start + 2, // strip /*
153+
end: child.end - 2, // strip */,
154+
},
155+
});
156+
break;
157+
}
158+
}
159+
},
119160
};
120161

121162
function isBeginningOfLine(parser: Parser) {

src/states/OPEN_TAG.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -273,14 +273,18 @@ export const OPEN_TAG: StateDefinition<OpenTagMeta> = {
273273
);
274274
}
275275

276-
if (
277-
code === CODE.FORWARD_SLASH &&
278-
this.lookAtCharCodeAhead(1) === CODE.ASTERISK
279-
) {
280-
// Skip over code inside a JavaScript block comment
281-
this.enterState(STATE.JS_COMMENT_BLOCK);
282-
this.pos++; // skip *
283-
return;
276+
if (code === CODE.FORWARD_SLASH) {
277+
// Check next character to see if we are in a comment
278+
switch (this.lookAtCharCodeAhead(1)) {
279+
case CODE.FORWARD_SLASH:
280+
this.enterState(STATE.JS_COMMENT_LINE);
281+
this.pos++; // skip /
282+
return;
283+
case CODE.ASTERISK:
284+
this.enterState(STATE.JS_COMMENT_BLOCK);
285+
this.pos++; // skip *
286+
return;
287+
}
284288
}
285289

286290
if (isWhitespaceCode(code)) {

0 commit comments

Comments
 (0)