Skip to content

[BUGFIX] Include comments for all rules in declaration block #1169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 16, 2025
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions src/Rule/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,16 @@ public function __construct($rule, int $lineNumber = 0, $columnNumber = 0)
}

/**
* @param list<Comment> $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(),
Expand All @@ -98,8 +100,6 @@ public static function parse(ParserState $parserState): Rule
$parserState->consume(';');
}

$parserState->consumeWhiteSpace();

return $rule;
}

Expand Down
10 changes: 7 additions & 3 deletions src/RuleSet/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
40 changes: 38 additions & 2 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1091,26 +1091,62 @@ 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());
}

/**
* @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());
Expand Down