Skip to content
Open
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
8 changes: 5 additions & 3 deletions src/Rules/HtmlCommentRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ public function shouldParse(Parser $parser): bool

public function parse(Parser $parser): Token
{
$buffer = $parser->consumeWhile('<!--');
$buffer .= $parser->consumeUntil('-->');
$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);
}
Expand Down
30 changes: 30 additions & 0 deletions tests/Rules/HtmlCommentRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'<!--start-show-more-->',
(string) $parser->parse('<!--start-show-more-->'),
);
$this->assertSame(
'<!-- a - b -->',
(string) $parser->parse('<!-- a - b -->'),
);
$this->assertSame(
'<!-- a > b < c -->',
(string) $parser->parse('<!-- a > b < c -->'),
);
}

#[Test]
public function lex_an_unterminated_comment(): void
{
$html =
(string) new Parser(highlighter: null, rules: [new HtmlCommentRule()])->parse(
'<!-- never closed',
);

$this->assertSame('<!-- never closed', $html);
}

#[Test]
public function test_lex_with_surrounding_content(): void
{
Expand Down
Loading