Re: [selectors-api] comments on Selectors API Level 2

On Thursday 21 January 2010, Boris Zbarsky wrote:
> On 1/21/10 11:11 AM, Bert Bos wrote:
> > Here are some examples of relations that always hold. (Assume e is
> > an element != NULL.)
> >
> >      e.querySelector("*") == e.querySelector(":root")
>
> Not unless we've recently redefined :root.  Can you point me to the
> place where that happened?
>
> >      e.querySelector("*") == e
>
> Nope.  querySelector on an element can only return descendants of the
> element.  In fact, e.querySelector("*") will return the element's
> first element child, if any.

That's surprising... What is the reason to not apply the selector to the 
whole tree?

So you're saying that

    e.querySelector(":first-child")

gives the first child of e (if any), because in that case e counts as a 
parent; but

    e.querySelector("* > :first-child")

does not, because in this case e *doesn't* count as the parent? And yet 
in CSS these two selectors mean the same thing.

It seems rather confusing and unnecessary. It means, e.g., that 
D.querySelectorAll("*") doesn't actually return all elements of 
document D. (It omits the root.) And that the ':root' selector is 
useless in these functions, because any selector with ':root' in it 
always returns NULL.

And it is unnecessary, because if you want to exclude e itself from a 
query, it's as simple as adding "* " in front of the selector.

I assumed that, given a document D and a selector S, S in a CSS style 
sheet matches exactly the same elements as would be returned by 
D.querySelectorAll(S).

E.g., I would think that the following is a nice and short way to check 
if a given element e is of type "P" and has a class "abc":

    if (e.querySelector("P.abc") == e) {...}

or, equivalently:

    if (e.querySelector("P.abc:root") != NULL) {...}

And the one-liner to get all grandchildren of e would be this:

    e.querySelectorAll(":root > * > *")

>
> >      e.querySelector(":root + *") == NULL
> >      e.querySelector(":root:first-child") == NULL
>
> Agreed, because as currently defined :root will not match anything in
> the subtree rooted at |e|, ever.
>
> >      e.querySelector("* *") == e.querySelector(":root> 
> > :first-child")
> >      e.querySelector(":odd") == e.querySelector(":root>  
> > :first-child")
>
> Again, not as :root is currently defined.



Bert
-- 
  Bert Bos                                ( W 3 C ) http://www.w3.org/
  http://www.w3.org/people/bos                               W3C/ERCIM
  bert@w3.org                             2004 Rt des Lucioles / BP 93
  +33 (0)4 92 38 76 92            06902 Sophia Antipolis Cedex, France

Received on Thursday, 21 January 2010 18:02:01 UTC