W3C home > Mailing lists > Public > www-style@w3.org > September 2009

Re: Supporting Scoped Selectors in Selectors API 2

From: Lachlan Hunt <lachlan.hunt@lachy.id.au>
Date: Wed, 30 Sep 2009 10:24:47 +0200
Message-ID: <4AC315CF.6040509@lachy.id.au>
To: Daniel Glazman <daniel.glazman@disruptive-innovations.com>
Cc: www-style <www-style@w3.org>
Daniel Glazman wrote:
> FWIW I have always thought that we are underusing an existing selector
> that could be very useful for scoped stylesheets: the :root pseudo.
> It currently matches the root of the document but I don't think it
> makes a lot of sense in the case of a scoped stylesheet anyway.
> I mentioned it multiple times in the past.
> Furthermore, it's currently hard to refer to the root of a subtree
> a scoped stylesheet applies to if that root element is an arbitrary
> element with no specific or known class, id or type the author can
> refer to. Hence :reference.
>  From my own perspective - my co-chairman hat off - I'd love to this
> the definition of :root changed to match the root of the tree or
> subtree the stylesheet applies to.

:root would not be appropriate, since the reference elements are not 
always going to be the root of any particular sub tree.  For example, 
when selecting elements in relation to a collection of reference 
elements, or when filtering a collection.


var list = [...]; // Some collection of previously selected elements
document.querySelectorAll(":reference+p", list)

This selects the next sibling P elements of all elements in the 
collection.  Here, the reference elements are not the root of anything 
in particular.

(This provides equivalent functionality to $(list).find("+p") in JQuery.)

document.querySelectorAll("p>:reference", list);

This selects all the reference elements from the list who's parent is a 
p element.  Again, not the root of anything in particular.

(This is equivalent to $(list).filter("p>*"); in JQuery)

> That would solve Lachlan's problem and clarify a few other isssues
> with scoped stylesheets. It would also make :root more visible because
> it's pretty unused for the time being.

I don't think it's a good idea to redefine an existing pseudo-class, 
which is inappropriately named for theses purposes, based solely on the 
lack of compelling use cases of its own.  Also, even in scoped 
stylesheets, selectors need to be evaluated in the context of the whole 
tree, not just the sub tree.

Consider this:

<body onload="document.body.className='javascript-on';">
     <style scoped>
       .javascript-on .foo { display: none; }
     <p class="foo">...</p>

That's a fairly well known technique for being able to adjust styles 
based on whether or not JavaScript support is enabled, and it makes 
sense to allow it to work within scoped stylesheets.  So it really 
wouldn't make much sense if :root were redefined to match the root of 
the subtree - the div in this case - since then you'd have weird 
constructs like this:

   .something :root>div {}

It makes much more sense for it to be :reference, or possibly :scope 
which many people have asked me to change its name back to.  Though, I'm 
not convinced that makes the most sense either given the use cases, but 
I'm thinking about it.

Lachlan Hunt - Opera Software
Received on Wednesday, 30 September 2009 08:25:23 UTC

This archive was generated by hypermail 2.4.0 : Friday, 25 March 2022 10:07:39 UTC