Skip to content

Commit

Permalink
Alternative change for specificity
Browse files Browse the repository at this point in the history
This is an alternative to tijsverkoyen#59

The patch in tijsverkoyen#59 is better, but this illustrates the problem better. The order shouldn't have any effect on the specificity, it should only count when the specificity is the same..
  • Loading branch information
barryvdh committed Jul 18, 2014
1 parent c462ea1 commit a9080ce
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions CssToInlineStyles.php
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,11 @@ private function processCSS()
// calculate specifity
$ruleSet['specifity'] = $this->calculateCSSSpecifity(
$selector
) + $i;
);

// remember the order of appearance
$ruleSet['order'] = $i;

// add into global rules
$this->cssRules[] = $ruleSet;
}
Expand Down Expand Up @@ -638,9 +641,12 @@ private function stripOriginalStyleTags($html)
*/
private static function sortOnSpecifity($e1, $e2)
{
if ($e1['specifity'] == $e2['specifity']) {
return 0;
if ($e1['specifity'] !== $e2['specifity']) {
return ($e1['specifity'] < $e2['specifity']) ? -1 : 1;
} elseif ($e1['order'] !== $e2['order']) {
return ($e1['order'] < $e2['order']) ? -1 : 1;
}
return ($e1['specifity'] < $e2['specifity']) ? -1 : 1;

return 0;
}
}

0 comments on commit a9080ce

Please sign in to comment.