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

On 1/21/10 1:01 PM, Bert Bos wrote:
>>>       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?

I didn't say that the selector is not applied to the whole tree.  I said 
that only nodes that are descendants of "e" and match the selector can 
be returned by the API.

> So you're saying that
>
>      e.querySelector(":first-child")
>
> gives the first child of e (if any)

Yes.

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

Does the same thing as e.querySelector(":first-child").

> 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.)

Uh... no.  It does in fact return the root.

Again, given a DOM node N the call N.querySelector(someSelector) returns 
all elements that:

   1)  Are descendants of N
   2)  Match the selector someSelector

Both conditions must be satisfied.  If N is a Document, then the root 
element of course satisfies condition 1.

> 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.

As it happens, no.  It's not.  Unless I'm completely misunderstanding 
what you mean here, of course.

> 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).

That's correct.

> 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) {...}

You've suddenly switched from calling querySelector on a document to 
calling it on an element....  So not sure how this is an "e.g.".

But in any case, the above code will always test false.  On the other 
hand there is a proposal for a matchesSelector() function on elements 
which will return a boolean which does exactly what you want here. 
Gecko and Webkit have experimental implementations.

> And the one-liner to get all grandchildren of e would be this:
>
>      e.querySelectorAll(":root>  *>  *")

Again, this requires a change in the CSS Selectors definition of :root.

-Boris

Received on Thursday, 21 January 2010 18:14:47 UTC