Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/quick-comments-attrs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"htmljs-parser": minor
---

Support comments between concise mode line attributes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
1╭─ div
╰─ ╰─ tagName "div"
2╭─ ,foo=bar
│ │ │╰─ attrValue.value "bar"
│ │ ╰─ attrValue "=bar"
╰─ ╰─ attrName "foo"
3╭─ // trailing comment
│ │ │ ╰─ comment.value " trailing comment"
│ │ ╰─ comment "// trailing comment"
╰─ ╰─ openTagEnd
4╭─
╰─ ╰─ closeTagEnd(div)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
div
,foo=bar
// trailing comment
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
1╭─ div
╰─ ╰─ tagName "div"
2╭─ ,foo=bar
│ │ │╰─ attrValue.value "bar"
│ │ ╰─ attrValue "=bar"
╰─ ╰─ attrName "foo"
3├─ // comment
4╭─ ,bar=baz
│ │ │╰─ attrValue.value "baz"
│ │ ╰─ attrValue "=baz"
╰─ ╰─ attrName "bar"
5├─ /* block */
6╭─ ,qux=qux
│ │ │╰─ attrValue.value "qux"
│ │ ╰─ attrValue "=qux"
╰─ ╰─ attrName "qux"
7╭─
│ ├─ openTagEnd
╰─ ╰─ closeTagEnd(div)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
div
,foo=bar
// comment
,bar=baz
/* block */
,qux=qux
34 changes: 31 additions & 3 deletions src/states/OPEN_TAG.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
isIndentCode,
isLineCode,
isWhitespaceCode,
matchesCloseAngleBracket,
matchesCloseParen,
Expand Down Expand Up @@ -105,13 +106,40 @@ export const OPEN_TAG: StateDefinition<OpenTagMeta> = {
: 1;

if (this.isConcise && tag.stage !== TAG_STAGE.ATTR_GROUP) {
if (this.consumeWhitespaceIfBefore(",")) {
this.pos++; // skip ,
let cur = this.pos;
while (cur < maxPos) {
const peek = data.charCodeAt(cur);
if (isWhitespaceCode(peek)) {
cur++;
} else if (
peek === CODE.FORWARD_SLASH &&
data.charCodeAt(cur + 1) === CODE.FORWARD_SLASH
) {
// line comment
cur += 2;
while (cur < maxPos && !isLineCode(data.charCodeAt(cur))) cur++;
} else if (
peek === CODE.FORWARD_SLASH &&
data.charCodeAt(cur + 1) === CODE.ASTERISK
) {
// block comment
const end = data.indexOf("*/", cur + 2);
if (end === -1) break;
cur = end + 2;
} else {
break;
}
}

// comma continues the open tag with another line attribute
if (data.charCodeAt(cur) === CODE.COMMA) {
this.pos = cur + 1;
this.consumeWhitespace();
continue;
}

this.exitState();
return; // parent handles newline
return;
}

this.pos += len;
Expand Down
Loading