Re: Supporting Scoped Selectors in Selectors API 2

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.

e.g.

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';">
   <div>
     <style scoped>
       .javascript-on .foo { display: none; }
     </style>
     <p class="foo">...</p>
   </div>
</body>

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
http://lachy.id.au/
http://www.opera.com/

Received on Wednesday, 30 September 2009 08:25:23 UTC