Skip to content

Commit 44f1b25

Browse files
oliverkleeJakeQZ
andauthored
[BUGFIX] Render rules in line and column number order (#1059)
Fixes #1052 Closes #1058 Co-authored-by: Jake Hotson <jake.github@qzdesign.co.uk>
1 parent bd4e549 commit 44f1b25

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
@@ -54,6 +54,7 @@ Please also have a look at our
5454

5555
### Fixed
5656

57+
- Render rules in line and column number order (#1059)
5758
- Don't render `rgb` colors with percentage values using hex notation (#803)
5859
- Parse `@font-face` `src` property as comma-delimited list (#790)
5960

src/RuleSet/RuleSet.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -277,22 +277,20 @@ protected function renderRules(OutputFormat $outputFormat)
277277
$result = '';
278278
$isFirst = true;
279279
$nextLevelFormat = $outputFormat->nextLevel();
280-
foreach ($this->rules as $rules) {
281-
foreach ($rules as $rule) {
282-
$renderedRule = $nextLevelFormat->safely(static function () use ($rule, $nextLevelFormat): string {
283-
return $rule->render($nextLevelFormat);
284-
});
285-
if ($renderedRule === null) {
286-
continue;
287-
}
288-
if ($isFirst) {
289-
$isFirst = false;
290-
$result .= $nextLevelFormat->spaceBeforeRules();
291-
} else {
292-
$result .= $nextLevelFormat->spaceBetweenRules();
293-
}
294-
$result .= $renderedRule;
280+
foreach ($this->getRules() as $rule) {
281+
$renderedRule = $nextLevelFormat->safely(static function () use ($rule, $nextLevelFormat): string {
282+
return $rule->render($nextLevelFormat);
283+
});
284+
if ($renderedRule === null) {
285+
continue;
286+
}
287+
if ($isFirst) {
288+
$isFirst = false;
289+
$result .= $nextLevelFormat->spaceBeforeRules();
290+
} else {
291+
$result .= $nextLevelFormat->spaceBetweenRules();
295292
}
293+
$result .= $renderedRule;
296294
}
297295

298296
if (!$isFirst) {
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(): void
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::assertStringContainsString($expectedRendering, $declarationBlock->render(new OutputFormat()));
40+
}
41+
}

0 commit comments

Comments
 (0)