From a85525099b2152c5814e50ac53095b0e12ec62c4 Mon Sep 17 00:00:00 2001 From: Daniel Ziegenberg Date: Sun, 25 Aug 2024 21:54:37 +0200 Subject: [PATCH] [BUGFIX] Fix comment parsing to support multiple comments Because of an eager consumption of whitespace, the rule parsing would swallow a trailing comment, meaning the comment for the next rule would be affected. This patch addresses this by only consuming real whitespace without comments after a rule. Fixes #173 Signed-off-by: Daniel Ziegenberg --- CHANGELOG.md | 1 + src/Rule/Rule.php | 5 ++++- tests/ParserTest.php | 18 +++++++++++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed390901..e4386c42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,7 @@ This project adheres to [Semantic Versioning](https://semver.org/). ### Fixed +- Fix comment parsing to support multiple comments (#672) - Fix undefined local variable in `CalcFunction::parse()` (#593) - Fix PHP notice caused by parsing invalid color values having less than 6 characters (#485) - Fix (regression) failure to parse at-rules with strict parsing (#456) diff --git a/src/Rule/Rule.php b/src/Rule/Rule.php index ca9f380c..9514be29 100644 --- a/src/Rule/Rule.php +++ b/src/Rule/Rule.php @@ -105,7 +105,10 @@ public static function parse(ParserState $oParserState): Rule while ($oParserState->comes(';')) { $oParserState->consume(';'); } - $oParserState->consumeWhiteSpace(); + + while (\preg_match('/\\s/isSu', $oParserState->peek()) === 1) { + $oParserState->consume(1); + } return $oRule; } diff --git a/tests/ParserTest.php b/tests/ParserTest.php index 2c1c7287..dde4aaca 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -1159,7 +1159,7 @@ public function commentExtracting(): void /** * @test */ - public function flatCommentExtracting(): void + public function flatCommentExtractingOneComment(): void { $parser = new Parser('div {/*Find Me!*/left:10px; text-align:left;}'); $doc = $parser->parse(); @@ -1169,6 +1169,22 @@ public function flatCommentExtracting(): void self::assertCount(1, $comments); self::assertSame('Find Me!', $comments[0]->getComment()); } + /** + * @test + */ + public function flatCommentExtractingTwoComments(): void + { + $parser = new Parser('div {/*Find Me!*/left:10px; /*Find Me Too!*/text-align:left;}'); + $doc = $parser->parse(); + $contents = $doc->getContents(); + $divRules = $contents[0]->getRules(); + $rule1Comments = $divRules[0]->getComments(); + $rule2Comments = $divRules[1]->getComments(); + self::assertCount(1, $rule1Comments); + self::assertCount(1, $rule2Comments); + self::assertEquals('Find Me!', $rule1Comments[0]->getComment()); + self::assertEquals('Find Me Too!', $rule2Comments[0]->getComment()); + } /** * @test