diff --git a/CHANGELOG.md b/CHANGELOG.md index f6bcb805..73fed056 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -66,6 +66,7 @@ Please also have a look at our ### Fixed +- Include comments for all rules in declaration block (#1169) - Render rules in line and column number order (#1059) - Don't render `rgb` colors with percentage values using hex notation (#803) - Parse `@font-face` `src` property as comma-delimited list (#790) diff --git a/src/Rule/Rule.php b/src/Rule/Rule.php index 42fef09d..dac05cd1 100644 --- a/src/Rule/Rule.php +++ b/src/Rule/Rule.php @@ -68,14 +68,16 @@ public function __construct($rule, int $lineNumber = 0, $columnNumber = 0) } /** + * @param list $commentsBeforeRule + * * @throws UnexpectedEOFException * @throws UnexpectedTokenException * * @internal since V8.8.0 */ - public static function parse(ParserState $parserState): Rule + public static function parse(ParserState $parserState, array $commentsBeforeRule = []): Rule { - $comments = $parserState->consumeWhiteSpace(); + $comments = \array_merge($commentsBeforeRule, $parserState->consumeWhiteSpace()); $rule = new Rule( $parserState->parseIdentifier(!$parserState->comes('--')), $parserState->currentLine(), @@ -98,8 +100,6 @@ public static function parse(ParserState $parserState): Rule $parserState->consume(';'); } - $parserState->consumeWhiteSpace(); - return $rule; } diff --git a/src/RuleSet/RuleSet.php b/src/RuleSet/RuleSet.php index 311f06be..9d5ab411 100644 --- a/src/RuleSet/RuleSet.php +++ b/src/RuleSet/RuleSet.php @@ -65,11 +65,15 @@ public static function parseRuleSet(ParserState $parserState, RuleSet $ruleSet): while ($parserState->comes(';')) { $parserState->consume(';'); } - while (!$parserState->comes('}')) { + while (true) { + $commentsBeforeRule = $parserState->consumeWhiteSpace(); + if ($parserState->comes('}')) { + break; + } $rule = null; if ($parserState->getSettings()->usesLenientParsing()) { try { - $rule = Rule::parse($parserState); + $rule = Rule::parse($parserState, $commentsBeforeRule); } catch (UnexpectedTokenException $e) { try { $consumedText = $parserState->consumeUntil(["\n", ';', '}'], true); @@ -87,7 +91,7 @@ public static function parseRuleSet(ParserState $parserState, RuleSet $ruleSet): } } } else { - $rule = Rule::parse($parserState); + $rule = Rule::parse($parserState, $commentsBeforeRule); } if ($rule instanceof Rule) { $ruleSet->addRule($rule); diff --git a/tests/ParserTest.php b/tests/ParserTest.php index 47ec1ffa..930f12f7 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -1091,9 +1091,11 @@ public function flatCommentExtractingOneComment(): void { $parser = new Parser('div {/*Find Me!*/left:10px; text-align:left;}'); $document = $parser->parse(); + $contents = $document->getContents(); $divRules = $contents[0]->getRules(); $comments = $divRules[0]->getComments(); + self::assertCount(1, $comments); self::assertSame('Find Me!', $comments[0]->getComment()); } @@ -1101,16 +1103,50 @@ public function flatCommentExtractingOneComment(): void /** * @test */ - public function flatCommentExtractingTwoComments(): void + public function flatCommentExtractingTwoConjoinedCommentsForOneRule(): void { - self::markTestSkipped('This is currently broken.'); + $parser = new Parser('div {/*Find Me!*//*Find Me Too!*/left:10px; text-align:left;}'); + $document = $parser->parse(); + + $contents = $document->getContents(); + $divRules = $contents[0]->getRules(); + $comments = $divRules[0]->getComments(); + self::assertCount(2, $comments); + self::assertSame('Find Me!', $comments[0]->getComment()); + self::assertSame('Find Me Too!', $comments[1]->getComment()); + } + + /** + * @test + */ + public function flatCommentExtractingTwoSpaceSeparatedCommentsForOneRule(): void + { + $parser = new Parser('div { /*Find Me!*/ /*Find Me Too!*/ left:10px; text-align:left;}'); + $document = $parser->parse(); + + $contents = $document->getContents(); + $divRules = $contents[0]->getRules(); + $comments = $divRules[0]->getComments(); + + self::assertCount(2, $comments); + self::assertSame('Find Me!', $comments[0]->getComment()); + self::assertSame('Find Me Too!', $comments[1]->getComment()); + } + + /** + * @test + */ + public function flatCommentExtractingCommentsForTwoRules(): void + { $parser = new Parser('div {/*Find Me!*/left:10px; /*Find Me Too!*/text-align:left;}'); $document = $parser->parse(); + $contents = $document->getContents(); $divRules = $contents[0]->getRules(); $rule1Comments = $divRules[0]->getComments(); $rule2Comments = $divRules[1]->getComments(); + self::assertCount(1, $rule1Comments); self::assertCount(1, $rule2Comments); self::assertSame('Find Me!', $rule1Comments[0]->getComment());