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( + '