From a9080ce8b3ea14bce89d978c707ca906fc6f331d Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Fri, 18 Jul 2014 15:52:31 +0200 Subject: [PATCH] Alternative change for specificity This is an alternative to #59 The patch in #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.. --- CssToInlineStyles.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/CssToInlineStyles.php b/CssToInlineStyles.php index 95282bb..cd5e64f 100644 --- a/CssToInlineStyles.php +++ b/CssToInlineStyles.php @@ -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; } @@ -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; } }