W3C home > Mailing lists > Public > www-style@w3.org > February 2006

Re: property for selector text specificity [CSSStyleRule ]

From: Boris Zbarsky <bzbarsky@MIT.EDU>
Date: Thu, 16 Feb 2006 12:03:39 -0600
Message-ID: <43F4BE7B.2050701@mit.edu>
To: Garrett Smith <dhtmlkitchen@gmail.com>, W3C CSS <www-style@w3.org>

Garrett Smith wrote:
> This is about reading a cssRule's selector text specificity.
> 
> http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleRule
> 
> var rule = document.styleSheets[0].cssRules[0];
> 
> // we get this one for free.
> alert(rule.selectorText); // "p.aqua.marine"
> 
> // we can't get this one for free, but we should.
> alert(rule.specificity); // "33" (from 0x21).

OK.  What if rule.selectorText is "p.aqua.marine, div".  What should 
rule.specificity be?  "33"?  Or "1"?  Or "33, 1"?  Or an array?

> Dean Edwards' CssQuery parses out selector text using RegExp's. This
> hard work is already done by the browser in applying rules. It could
> easily be exposed so we would have it for free.

I agree that this information should be exposed, yeah.  I'm just not 
sure what a decent way to do it is.

> The reason this is necessary is for stylesheet editors. A stylesheet
> editor should sort the CSS Rules by their specificit so that the end
> user can edit the most specific rule to the selected element.

That approach really does not work for rules that have multiple 
selectors and a single declaration, unless your editor splits them up 
into multiple rules.

-Boris
Received on Thursday, 16 February 2006 18:55:38 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Monday, 27 April 2009 13:54:43 GMT