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/html-comment-in-open-tag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"htmljs-parser": minor
---

An `<!--` inside an open tag now reports a targeted `INVALID_HTML_COMMENT` error pointing at the comment, instead of being read as tag type arguments, as attribute type parameters, or (after a whitespace-terminated attribute value) consumed as a less-than operator. Like `</`, a `<!--` no longer continues an unenclosed attribute value, so `<div class="a" <!-- note --> id="b">` reports the comment rather than silently parsing as `class=("a" < !--note)` plus two junk attributes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
1╭─ <div hidden=/<!--/.test(s) title="<!-- x -->"/>
│ ││ │ ││ │ ││ ╰─ openTagEnd:selfClosed "/>"
│ ││ │ ││ │ │╰─ attrValue.value "\"<!-- x -->\""
│ ││ │ ││ │ ╰─ attrValue "=\"<!-- x -->\""
│ ││ │ ││ ╰─ attrName "title"
│ ││ │ │╰─ attrValue.value "/<!--/.test(s)"
│ ││ │ ╰─ attrValue "=/<!--/.test(s)"
│ ││ ╰─ attrName "hidden"
│ │╰─ tagName "div"
╰─ ╰─ openTagStart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div hidden=/<!--/.test(s) title="<!-- x -->"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1╭─ <div hidden <!-- the label --> id="b">hi</div>
│ ││ │ ╰─ error(INVALID_HTML_COMMENT:An html comment cannot be used within an open tag. Use a JavaScript comment (// or /* */) instead.)
│ ││ ╰─ attrName "hidden"
│ │╰─ tagName "div"
╰─ ╰─ openTagStart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div hidden <!-- the label --> id="b">hi</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
1╭─ <div class="foo" <!-- the label --> id="b">hi</div>
│ ││ │ ││ ╰─ error(INVALID_HTML_COMMENT:An html comment cannot be used within an open tag. Use a JavaScript comment (// or /* */) instead.)
│ ││ │ │╰─ attrValue.value "\"foo\""
│ ││ │ ╰─ attrValue "=\"foo\""
│ ││ ╰─ attrName "class"
│ │╰─ tagName "div"
╰─ ╰─ openTagStart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="foo" <!-- the label --> id="b">hi</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1╭─ div class="foo" <!-- the label --> id="b"
│ │ │ ││ ╰─ error(INVALID_HTML_COMMENT:An html comment cannot be used within an open tag. Use a JavaScript comment (// or /* */) instead.)
│ │ │ │╰─ attrValue.value "\"foo\""
│ │ │ ╰─ attrValue "=\"foo\""
│ │ ╰─ attrName "class"
╰─ ╰─ tagName "div"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
div class="foo" <!-- the label --> id="b"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
1╭─ <div
│ │╰─ tagName "div"
╰─ ╰─ openTagStart
2╭─ <!-- the label -->
╰─ ╰─ error(INVALID_HTML_COMMENT:An html comment cannot be used within an open tag. Use a JavaScript comment (// or /* */) instead.)
3├─ class="foo"
4╰─ >hi</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div
<!-- the label -->
class="foo"
>hi</div>
5 changes: 5 additions & 0 deletions src/states/ATTRIBUTE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ export const ATTRIBUTE: StateDefinition<AttrMeta> = {
continue;
}

if (code === CODE.OPEN_ANGLE_BRACKET && this.lookAheadFor("!--")) {
this.exitState();
return; // the open tag reports the html comment
}

if (
code === CODE.EQUAL ||
(code === CODE.COLON && data.charCodeAt(this.pos + 1) === CODE.EQUAL) ||
Expand Down
22 changes: 14 additions & 8 deletions src/states/EXPRESSION.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,14 +420,20 @@ function checkForOperators(
pos + 1,
);

// In HTML mode a "</" is a close tag rather than a less-than operator
// (which lookAheadForOperator would otherwise continue across).
if (
!parser.isConcise &&
data.charCodeAt(nextNonSpace) === CODE.OPEN_ANGLE_BRACKET &&
data.charCodeAt(nextNonSpace + 1) === CODE.FORWARD_SLASH
) {
return false;
// A "</" close tag (html mode) or a "<!--" html comment is markup rather
// than a less-than operator, which lookAheadForOperator would otherwise
// continue across.
if (data.charCodeAt(nextNonSpace) === CODE.OPEN_ANGLE_BRACKET) {
if (
!parser.isConcise &&
data.charCodeAt(nextNonSpace + 1) === CODE.FORWARD_SLASH
) {
return false;
}

if (parser.lookAheadFor("!--", nextNonSpace + 1)) {
return false;
}
}

if (
Expand Down
6 changes: 6 additions & 0 deletions src/states/OPEN_TAG.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,12 @@ export const OPEN_TAG: StateDefinition<OpenTagMeta> = {
this.pos += 2; // skip /*
return;
}
} else if (code === CODE.OPEN_ANGLE_BRACKET && this.lookAheadFor("!--")) {
return this.emitError(
this.pos,
ErrorCode.INVALID_HTML_COMMENT,
"An html comment cannot be used within an open tag. Use a JavaScript comment (// or /* */) instead.",
);
}

if (isWhitespaceCode(code)) {
Expand Down
1 change: 1 addition & 0 deletions src/util/error-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ export const INVALID_TAG_PARAMS = 26;
export const INVALID_TAG_TYPES = 27;
export const INVALID_ATTR_TYPE_PARAMS = 28;
export const AMBIGUOUS_ATTRIBUTE_VALUE = 29;
export const INVALID_HTML_COMMENT = 30;