From 9dd0642df0834b17eda5be55a1ae48a8de4e0586 Mon Sep 17 00:00:00 2001 From: RobinDev Date: Wed, 16 Sep 2026 12:51:53 +0200 Subject: [PATCH] fix: end an HTML comment on the closing string --- src/Rules/HtmlCommentRule.php | 8 +++++--- tests/Rules/HtmlCommentRuleTest.php | 30 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/Rules/HtmlCommentRule.php b/src/Rules/HtmlCommentRule.php index 3513afc..b1aeac4 100644 --- a/src/Rules/HtmlCommentRule.php +++ b/src/Rules/HtmlCommentRule.php @@ -19,9 +19,11 @@ public function shouldParse(Parser $parser): bool public function parse(Parser $parser): Token { - $buffer = $parser->consumeWhile(''); - $buffer .= $parser->consumeWhile('-->'); + // consumeUntil() takes a set of characters, so it stops on the first + // `-` in the body; a comment ends at the `-->` string. + $buffer = $parser->consume(4); + $buffer .= $parser->consumeUntilString('-->'); + $buffer .= $parser->consume(3); return new HtmlCommentToken($buffer); } diff --git a/tests/Rules/HtmlCommentRuleTest.php b/tests/Rules/HtmlCommentRuleTest.php index 4ee0bd8..4da6117 100644 --- a/tests/Rules/HtmlCommentRuleTest.php +++ b/tests/Rules/HtmlCommentRuleTest.php @@ -35,6 +35,36 @@ public function test_lex_multiline(): void $this->assertSame($comment, $html); } + #[Test] + public function lex_a_body_holding_a_dash(): void + { + $parser = new Parser(highlighter: null, rules: [new HtmlCommentRule()]); + + $this->assertSame( + '', + (string) $parser->parse(''), + ); + $this->assertSame( + '', + (string) $parser->parse(''), + ); + $this->assertSame( + '', + (string) $parser->parse(''), + ); + } + + #[Test] + public function lex_an_unterminated_comment(): void + { + $html = + (string) new Parser(highlighter: null, rules: [new HtmlCommentRule()])->parse( + '