Skip to content

Commit

Permalink
Keep selectors unsorted
Browse files Browse the repository at this point in the history
  • Loading branch information
schlessera committed Jun 2, 2020
1 parent 584b96b commit 17ed19b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
24 changes: 21 additions & 3 deletions lib/optimizer/src/CssRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,28 @@ final class CssRule
*/
public function __construct($selectors, $properties)
{
$this->selectors = array_unique(array_filter(array_map([$this, 'normalizeSelector'], $this->separateSelectors($selectors))));
$this->properties = array_unique(array_filter(array_map([$this, 'normalizeProperty'], $this->separateProperties($properties))));
$this->selectors = array_values(
array_unique(
array_filter(
array_map(
[$this, 'normalizeSelector'],
$this->separateSelectors($selectors)
)
)
)
);

$this->properties = array_values(
array_unique(
array_filter(
array_map(
[$this, 'normalizeProperty'],
$this->separateProperties($properties)
)
)
)
);

sort($this->selectors);
sort($this->properties);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/optimizer/src/CssRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public function add(CssRule $cssRule)
}

foreach ($clone->cssRules as $index => $existingCssRule) {
if ($cssRule->canBeMerged($existingCssRule)) {
$clone->cssRules[$index] = $cssRule->mergeWith($existingCssRule);
if ($existingCssRule->canBeMerged($cssRule)) {
$clone->cssRules[$index] = $existingCssRule->mergeWith($cssRule);
// Rendered CSS and byte count need to be rebuilt, as some previously rendered CSS rule has changed.
$clone->renderedCss = null;
$clone->byteCount = null;
Expand Down
3 changes: 1 addition & 2 deletions lib/optimizer/tests/CssRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function dataCssRuleOutput()
'normalizes selectors and properties' => [
" \t\t , #some-id \n \n\n > \t .some-class \t \n .another-class \t \n + element ~ another-element \n \t , ,, #another-id \n .with-class \n \t , ",
" \t\t ; color \n \t : \n \t\n red ;;; ; \n ; \t \n background-color:white;; \t \n ; ",
'#another-id .with-class,#some-id>.some-class .another-class+element~another-element{background-color:white;color:red}'
'#some-id>.some-class .another-class+element~another-element,#another-id .with-class{background-color:white;color:red}'
],
];
}
Expand Down Expand Up @@ -165,7 +165,6 @@ public function testCssRulesCanBeMerged($ruleAValues, $ruleBValues, $expectedCan
$this->assertEquals($expectedMergedRuleValues[0], $mergedRuleFromB->getMediaQuery());

$this->assertEquals($expectedMergedRuleValues[1], $mergedRuleFromA->getSelectors());
$this->assertEquals($expectedMergedRuleValues[1], $mergedRuleFromB->getSelectors());

$this->assertEquals($expectedMergedRuleValues[2], $mergedRuleFromA->getProperties());
$this->assertEquals($expectedMergedRuleValues[2], $mergedRuleFromB->getProperties());
Expand Down

0 comments on commit 17ed19b

Please # to comment.