Skip to content

Commit 6543eb2

Browse files
authored
[BUGFIX] Render rules in line and column number order (#1062)
This is the V8.x backport of #1059.
1 parent 3cd1d06 commit 6543eb2

File tree

3 files changed

+55
-15
lines changed

3 files changed

+55
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
2828

2929
### Fixed
3030

31+
- Render rules in line and column number order (#1059)
3132
- Create `Size` with correct types in `expandBackgroundShorthand` (#814)
3233
- Parse `@font-face` `src` property as comma-delimited list (#794)
3334

src/RuleSet/RuleSet.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -287,22 +287,20 @@ protected function renderRules(OutputFormat $oOutputFormat)
287287
$sResult = '';
288288
$bIsFirst = true;
289289
$oNextLevel = $oOutputFormat->nextLevel();
290-
foreach ($this->aRules as $aRules) {
291-
foreach ($aRules as $oRule) {
292-
$sRendered = $oNextLevel->safely(function () use ($oRule, $oNextLevel) {
293-
return $oRule->render($oNextLevel);
294-
});
295-
if ($sRendered === null) {
296-
continue;
297-
}
298-
if ($bIsFirst) {
299-
$bIsFirst = false;
300-
$sResult .= $oNextLevel->spaceBeforeRules();
301-
} else {
302-
$sResult .= $oNextLevel->spaceBetweenRules();
303-
}
304-
$sResult .= $sRendered;
290+
foreach ($this->getRules() as $oRule) {
291+
$sRendered = $oNextLevel->safely(function () use ($oRule, $oNextLevel) {
292+
return $oRule->render($oNextLevel);
293+
});
294+
if ($sRendered === null) {
295+
continue;
296+
}
297+
if ($bIsFirst) {
298+
$bIsFirst = false;
299+
$sResult .= $oNextLevel->spaceBeforeRules();
300+
} else {
301+
$sResult .= $oNextLevel->spaceBetweenRules();
305302
}
303+
$sResult .= $sRendered;
306304
}
307305

308306
if (!$bIsFirst) {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sabberworm\CSS\Tests\Functional\RuleSet;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Sabberworm\CSS\OutputFormat;
9+
use Sabberworm\CSS\Property\Selector;
10+
use Sabberworm\CSS\Rule\Rule;
11+
use Sabberworm\CSS\RuleSet\DeclarationBlock;
12+
13+
/**
14+
* @covers \Sabberworm\CSS\RuleSet\DeclarationBlock
15+
*/
16+
final class DeclarationBlockTest extends TestCase
17+
{
18+
/**
19+
* @test
20+
*/
21+
public function rendersRulesInOrderProvided()
22+
{
23+
$declarationBlock = new DeclarationBlock();
24+
$declarationBlock->setSelectors([new Selector('.test')]);
25+
26+
$rule1 = new Rule('background-color');
27+
$rule1->setValue('transparent');
28+
$declarationBlock->addRule($rule1);
29+
30+
$rule2 = new Rule('background');
31+
$rule2->setValue('#222');
32+
$declarationBlock->addRule($rule2);
33+
34+
$rule3 = new Rule('background-color');
35+
$rule3->setValue('#fff');
36+
$declarationBlock->addRule($rule3);
37+
38+
$expectedRendering = 'background-color: transparent;background: #222;background-color: #fff';
39+
self::assertContains($expectedRendering, $declarationBlock->render(new OutputFormat()));
40+
}
41+
}

0 commit comments

Comments
 (0)