diff --git a/CHANGELOG.md b/CHANGELOG.md index ea3474c8e..e90ac7c89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ This project adheres to [Semantic Versioning](https://semver.org/). ### Fixed +- Include comments for all rules in declaration block (#1169) - Render rules in line and column number order (#1059) - Create `Size` with correct types in `expandBackgroundShorthand` (#814) - Parse `@font-face` `src` property as comma-delimited list (#794) diff --git a/src/Rule/Rule.php b/src/Rule/Rule.php index 984071c0f..037132e33 100644 --- a/src/Rule/Rule.php +++ b/src/Rule/Rule.php @@ -75,6 +75,8 @@ public function __construct($sRule, $iLineNo = 0, $iColNo = 0) } /** + * @param array $commentsBeforeRule + * * @return Rule * * @throws UnexpectedEOFException @@ -82,9 +84,9 @@ public function __construct($sRule, $iLineNo = 0, $iColNo = 0) * * @internal since V8.8.0 */ - public static function parse(ParserState $oParserState) + public static function parse(ParserState $oParserState, $commentsBeforeRule = []) { - $aComments = $oParserState->consumeWhiteSpace(); + $aComments = \array_merge($commentsBeforeRule, $oParserState->consumeWhiteSpace()); $oRule = new Rule( $oParserState->parseIdentifier(!$oParserState->comes("--")), $oParserState->currentLine(), @@ -114,8 +116,6 @@ public static function parse(ParserState $oParserState) $oParserState->consume(';'); } - $oParserState->consumeWhiteSpace(); - return $oRule; } diff --git a/src/RuleSet/RuleSet.php b/src/RuleSet/RuleSet.php index 258fefcf2..c08433898 100644 --- a/src/RuleSet/RuleSet.php +++ b/src/RuleSet/RuleSet.php @@ -67,11 +67,15 @@ public static function parseRuleSet(ParserState $oParserState, RuleSet $oRuleSet while ($oParserState->comes(';')) { $oParserState->consume(';'); } - while (!$oParserState->comes('}')) { + while (true) { + $commentsBeforeRule = $oParserState->consumeWhiteSpace(); + if ($oParserState->comes('}')) { + break; + } $oRule = null; if ($oParserState->getSettings()->bLenientParsing) { try { - $oRule = Rule::parse($oParserState); + $oRule = Rule::parse($oParserState, $commentsBeforeRule); } catch (UnexpectedTokenException $e) { try { $sConsume = $oParserState->consumeUntil(["\n", ";", '}'], true); @@ -89,7 +93,7 @@ public static function parseRuleSet(ParserState $oParserState, RuleSet $oRuleSet } } } else { - $oRule = Rule::parse($oParserState); + $oRule = Rule::parse($oParserState, $commentsBeforeRule); } if ($oRule) { $oRuleSet->addRule($oRule); diff --git a/tests/ParserTest.php b/tests/ParserTest.php index 59f1bd643..7c9778aba 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -1167,9 +1167,11 @@ public function flatCommentExtractingOneComment() { $parser = new Parser('div {/*Find Me!*/left:10px; text-align:left;}'); $doc = $parser->parse(); + $contents = $doc->getContents(); $divRules = $contents[0]->getRules(); $comments = $divRules[0]->getComments(); + self::assertCount(1, $comments); self::assertSame("Find Me!", $comments[0]->getComment()); } @@ -1177,16 +1179,50 @@ public function flatCommentExtractingOneComment() /** * @test */ - public function flatCommentExtractingTwoComments() + public function flatCommentExtractingTwoConjoinedCommentsForOneRule() { - 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() + { + $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() + { $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());