Skip to content

Commit 4d28e16

Browse files
committed
feat: support comments between concise mode line attributes
Allow `//` and `/*...*/` comment lines between comma-prefixed line attributes in concise mode. The comments are scanned over (not emitted) and no longer terminate the open tag. Fixes #203
1 parent aaeb3fd commit 4d28e16

6 files changed

Lines changed: 76 additions & 3 deletions

File tree

.changeset/quick-comments-attrs.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"htmljs-parser": minor
3+
---
4+
5+
Support comments between concise mode line attributes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
1╭─ div
2+
╰─ ╰─ tagName "div"
3+
2╭─ ,foo=bar
4+
│ │ │╰─ attrValue.value "bar"
5+
│ │ ╰─ attrValue "=bar"
6+
╰─ ╰─ attrName "foo"
7+
3╭─ // trailing comment
8+
│ │ │ ╰─ comment.value " trailing comment"
9+
│ │ ╰─ comment "// trailing comment"
10+
╰─ ╰─ openTagEnd
11+
4╭─
12+
╰─ ╰─ closeTagEnd(div)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
div
2+
,foo=bar
3+
// trailing comment
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
1╭─ div
2+
╰─ ╰─ tagName "div"
3+
2╭─ ,foo=bar
4+
│ │ │╰─ attrValue.value "bar"
5+
│ │ ╰─ attrValue "=bar"
6+
╰─ ╰─ attrName "foo"
7+
3├─ // comment
8+
4╭─ ,bar=baz
9+
│ │ │╰─ attrValue.value "baz"
10+
│ │ ╰─ attrValue "=baz"
11+
╰─ ╰─ attrName "bar"
12+
5├─ /* block */
13+
6╭─ ,qux=qux
14+
│ │ │╰─ attrValue.value "qux"
15+
│ │ ╰─ attrValue "=qux"
16+
╰─ ╰─ attrName "qux"
17+
7╭─
18+
│ ├─ openTagEnd
19+
╰─ ╰─ closeTagEnd(div)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
div
2+
,foo=bar
3+
// comment
4+
,bar=baz
5+
/* block */
6+
,qux=qux

src/states/OPEN_TAG.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
isIndentCode,
3+
isLineCode,
34
isWhitespaceCode,
45
matchesCloseAngleBracket,
56
matchesCloseParen,
@@ -105,13 +106,40 @@ export const OPEN_TAG: StateDefinition<OpenTagMeta> = {
105106
: 1;
106107

107108
if (this.isConcise && tag.stage !== TAG_STAGE.ATTR_GROUP) {
108-
if (this.consumeWhitespaceIfBefore(",")) {
109-
this.pos++; // skip ,
109+
let cur = this.pos;
110+
while (cur < maxPos) {
111+
const peek = data.charCodeAt(cur);
112+
if (isWhitespaceCode(peek)) {
113+
cur++;
114+
} else if (
115+
peek === CODE.FORWARD_SLASH &&
116+
data.charCodeAt(cur + 1) === CODE.FORWARD_SLASH
117+
) {
118+
// line comment
119+
cur += 2;
120+
while (cur < maxPos && !isLineCode(data.charCodeAt(cur))) cur++;
121+
} else if (
122+
peek === CODE.FORWARD_SLASH &&
123+
data.charCodeAt(cur + 1) === CODE.ASTERISK
124+
) {
125+
// block comment
126+
const end = data.indexOf("*/", cur + 2);
127+
if (end === -1) break;
128+
cur = end + 2;
129+
} else {
130+
break;
131+
}
132+
}
133+
134+
// comma continues the open tag with another line attribute
135+
if (data.charCodeAt(cur) === CODE.COMMA) {
136+
this.pos = cur + 1;
110137
this.consumeWhitespace();
111138
continue;
112139
}
140+
113141
this.exitState();
114-
return; // parent handles newline
142+
return;
115143
}
116144

117145
this.pos += len;

0 commit comments

Comments
 (0)