From ddd9ea7e307fd79dd5296c517f3466cad645c6da 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 (#671) (#671) 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 | 2 ++ src/Rule/Rule.php | 7 ++++++- tests/ParserTest.php | 18 +++++++++++++++++- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d1837d0b..cc91e719 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ This project adheres to [Semantic Versioning](https://semver.org/). ### Fixed +- Fix comment parsing to support multiple comments (#671) + ## 8.6.0 ### Added diff --git a/src/Rule/Rule.php b/src/Rule/Rule.php index fc00c880..9b693ece 100644 --- a/src/Rule/Rule.php +++ b/src/Rule/Rule.php @@ -107,7 +107,12 @@ public static function parse(ParserState $oParserState) while ($oParserState->comes(';')) { $oParserState->consume(';'); } - $oParserState->consumeWhiteSpace(); + + // NOTE: This is a backport to fix comment parsing to support multiple + // comments. This will be rectified in version 9.0.0. + while (preg_match('/\\s/isSu', $oParserState->peek()) === 1) { + $oParserState->consume(1); + } return $oRule; } diff --git a/tests/ParserTest.php b/tests/ParserTest.php index a48ac0e7..036fd033 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -1162,7 +1162,7 @@ public function commentExtracting() /** * @test */ - public function flatCommentExtracting() + public function flatCommentExtractingOneComment() { $parser = new Parser('div {/*Find Me!*/left:10px; text-align:left;}'); $doc = $parser->parse(); @@ -1172,6 +1172,22 @@ public function flatCommentExtracting() self::assertCount(1, $comments); self::assertSame("Find Me!", $comments[0]->getComment()); } + /** + * @test + */ + public function flatCommentExtractingTwoComments() + { + $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