Skip to content

Allow rules to be in any order in shorthand tests #1065

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 2 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Sabberworm\CSS\RuleSet\DeclarationBlock;
use Sabberworm\CSS\RuleSet\RuleSet;
use Sabberworm\CSS\Settings;
use Sabberworm\CSS\Tests\RuleSet\DeclarationBlockTest;
use Sabberworm\CSS\Value\Color;
use Sabberworm\CSS\Value\Size;
use Sabberworm\CSS\Value\URL;
Expand Down Expand Up @@ -511,7 +512,7 @@ public function expandShorthands()
. 'font-family: "Trebuchet MS",Georgia,serif;background-color: #ccc;'
. 'background-image: url("/images/foo.png");background-repeat: no-repeat;background-attachment: scroll;'
. 'background-position: left top;}';
self::assertSame($sExpected, $oDoc->render());
DeclarationBlockTest::assertDeclarationBlockEquals($sExpected, $oDoc->render());
}

/**
Expand All @@ -528,7 +529,7 @@ public function createShorthands()
$oDoc->createShorthands();
$sExpected = 'body {background: #fff url("foobar.png") repeat-y;margin: 2px 5px 4px 3px;'
. 'border: 2px dotted #999;font: bold 2em Helvetica,Arial,sans-serif;}';
self::assertSame($sExpected, $oDoc->render());
DeclarationBlockTest::assertDeclarationBlockEquals($sExpected, $oDoc->render());
}

/**
Expand Down
54 changes: 50 additions & 4 deletions tests/RuleSet/DeclarationBlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function expandBorderShorthand($sCss, $sExpected)
foreach ($oDoc->getAllDeclarationBlocks() as $oDeclaration) {
$oDeclaration->expandBorderShorthand();
}
self::assertSame(trim((string)$oDoc), $sExpected);
self::assertDeclarationBlockEquals(trim((string)$oDoc), $sExpected);
}

/**
Expand Down Expand Up @@ -62,7 +62,7 @@ public function expandFontShorthand($sCss, $sExpected)
foreach ($oDoc->getAllDeclarationBlocks() as $oDeclaration) {
$oDeclaration->expandFontShorthand();
}
self::assertSame(trim((string)$oDoc), $sExpected);
self::assertDeclarationBlockEquals(trim((string)$oDoc), $sExpected);
}

/**
Expand Down Expand Up @@ -118,7 +118,7 @@ public function expandBackgroundShorthand($sCss, $sExpected)
foreach ($oDoc->getAllDeclarationBlocks() as $oDeclaration) {
$oDeclaration->expandBackgroundShorthand();
}
self::assertSame(trim((string)$oDoc), $sExpected);
self::assertDeclarationBlockEquals(trim((string)$oDoc), $sExpected);
}

/**
Expand Down Expand Up @@ -171,7 +171,7 @@ public function expandDimensionsShorthand($sCss, $sExpected)
foreach ($oDoc->getAllDeclarationBlocks() as $oDeclaration) {
$oDeclaration->expandDimensionsShorthand();
}
self::assertSame(trim((string)$oDoc), $sExpected);
self::assertDeclarationBlockEquals(trim((string)$oDoc), $sExpected);
}

/**
Expand Down Expand Up @@ -505,4 +505,50 @@ public function canRemoveCommentsFromRulesUsingStrictParsing(

self::assertSame($cssWithoutComments, $renderedDocument);
}

/**
* Asserts two declaration blocks are equivalent, allowing the rules to be in any order.
*
* @param string $expected
* @param string $actual
*/
public static function assertDeclarationBlockEquals($expected, $actual)
{
$normalizedExpected = self::sortRulesInDeclarationBlock($expected);
$normalizedActual = self::sortRulesInDeclarationBlock($actual);

self::assertSame($normalizedExpected, $normalizedActual);
}

/**
* Sorts the rules within a declaration block by property name.
*
* @param string $declarationBlock
*
* @return string
*/
private static function sortRulesInDeclarationBlock($declarationBlock)
{
// Match everything between `{` and `}`.
return \preg_replace_callback(
'/(?<=\{)[^\}]*+(?=\})/',
[self::class, 'sortDeclarationBlockRules'],
$declarationBlock
);
}

/**
* Sorts rules from within a declaration block by property name.
*
* @param array{0: string} $rulesMatches
* This method is intended as a callback for `preg_replace_callback`.
*
* @return string
*/
private static function sortDeclarationBlockRules($rulesMatches)
{
$rules = \explode(';', $rulesMatches[0]);
\sort($rules);
return \implode(';', $rules);
}
}