Skip to content

HTML: end a comment at an abrupt close (<!--> and <!--->) - #134

Closed
yxshee wants to merge 1 commit into
tdewolff:masterfrom
yxshee:html-abrupt-comment-close
Closed

yxshee wants to merge 1 commit into
tdewolff:masterfrom
yxshee:html-abrupt-comment-close

Conversation

@yxshee

@yxshee yxshee commented Nov 9, 2026

Copy link
Copy Markdown

The HTML lexer does not end a comment at an abrupt close. Per the HTML standard, <!--> (comment start state, >: "abrupt-closing-of-empty-comment") and <!---> (comment start dash state, >) both close an empty comment, and whatever follows is text. The lexer only looks for --> and --!> after moving past <!--, so it keeps reading:

<!-->x-->      parse: Comment("<!-->x-->")           x/net/html: Comment("<!-->") Text("x-->")
<!--->x-->     parse: Comment("<!--->x-->")          x/net/html: Comment("<!--->") Text("x-->")

Through tdewolff/minify this drops content: <em><!-->x--></em> minifies to <em></em>, where browsers render x-->.

The fix checks for > or -> right after <!-- and emits an empty comment there. <!---->, <!-- -->, <!-- --!> and unterminated comments behave as before. Added five cases to TestTokens (<!-->, <!--->, <!---->, and the two followed by text); go test ./html passes, and the new cases fail on master (<!-->text--> is one token there).

Found by comparing the lexer with golang.org/x/net/html's tokenizer on generated documents while checking minify's HTML output.

The HTML standard closes an empty comment at <!--> (comment start state) and
at <!---> (comment start dash state), and everything after it is text. The
lexer only looked for --> and --!> after moving past <!--, so <!-->x--> and
<!--->x--> were lexed as a single comment and the text in between was lost,
which through minify drops content that browsers render.
@yxshee yxshee closed this Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant