My main goal is to try to reorder a CSS style block based on specificity. I previously had helped from SO and I managed to produce this function. See gist.
Here is an example:
function specificity($selector){
// https://gist.github.com/2774085
}
$compare = function($a, $b) use ($specificity) {
return $specificity($a) - $specificity($b)
};
$array = css_array();
uksort($array, $compare);
The above has been working great until I came across this CSS:
html, body, body div{
background: transparent;
}
body {
background-color: #D96F02;
}
This gets reordered into this:
body { // 1 point
background-color: #D96F02;
}
html, body, body div{ // 4 points
background: transparent;
}
However, this isn't how the browser applies the CSS.
I think my specificity function may be missing the importance of order of CSS rather than ordering based on selector specificity? Is this true?
Update
I think what I should do is in my compare function, I should always add an extra points of say 10
because specificity isn't just based on selectors its based on the order of the selector too. So something like this:
$compare = function($a, $b) use ($specificity) {
return ($specificity($a) + 10) - $specificity($b)
};
How does this look and how do I determine number of points to give in this case!?
+10
to specificity?!), it won't be robust. The spec: w3.org/TR/CSS2/cascade.html. – Adal4 points
should actually be calculating three separate specificity ratings of 1, 1, and 2. The comma's make it three different css selectors in one line, not a combining selector likehtml > body > div
or some such. – Aretina