Skip to content

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

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 1 commit into from
Mar 17, 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 @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions src/Rule/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,18 @@ public function __construct($sRule, $iLineNo = 0, $iColNo = 0)
}

/**
* @param array<int, Comment> $commentsBeforeRule
*
* @return Rule
*
* @throws UnexpectedEOFException
* @throws UnexpectedTokenException
*
* @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(),
Expand Down Expand Up @@ -114,8 +116,6 @@ public static function parse(ParserState $oParserState)
$oParserState->consume(';');
}

$oParserState->consumeWhiteSpace();

return $oRule;
}

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

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