Re: [SelectorsAPI] Thoughts on querySelectorAll

Jonas Sicking wrote:
> John Resig wrote:
>
>>>  There are two contrary statements in your proposal.
>>>
>>>  ":root" matches the root element (i.e. the "test" element in your  
>>> example)
>>>
>>>  All simple selectors has to match a descendant of the element on  
>>> which
>>>
>>>  .querySelectorAll was called.
>>>
>>>  Clearly the "test" node isn't a descendant of itself, so the  
>>> ":root"  part couldn't match anything.
>>
>> That is correct - :root would be overloaded in this case (or simply  
>> called something else - like :scope). Isn't this how the combinator- 
>> leading selectors were proposed to work?
>>
>>   // Finding all child, div, elements
>>   .querySelectorAll(":scope > div")
>>
>> Looking through the archives it appears as if that's what Maciej  
>> proposed. If that's not the case - and the :root/:scope points back  
>> to the document root (?) then please disregard all of this, as it's  
>> no longer useful.
>
> The issue isn't what we define ":scope" to match in general. But  
> rather that you are saying that only descendants of the "context  
> node" are allowed to match the individual parts of the selector.
>
> You are saying that for the selector .querySelectorAll("div span"),  
> the "div" part and the "span" part both need to match a descendant  
> of the context node. So while matching the implementation should  
> only test descendants of the context node while matching the "div"  
> part.
>
> But you are also saying that for the  
> selector.querySelectorAll(":scope span") the "span" part needs to  
> match a descendant of the context node, but the ":scope" part can  
> match the context node itself. So here the implementation would have  
> to test not just descendants of the context node, but also the  
> context node itself.
>
> What would a selector like ":link span" match? Can the ":link" part  
> match the context node? I.e. will anything be returned for the  
> following DOM
>
> <a href="..." id="context"><b><span>hello</span></b></a>
>
> What about "[foo] span", can the [foo] part match the context node?
>
> <a href="..." foo="hi mom" id="context"><b><span>hello</span></b></a>
>
> What about if the selector was ":scope[foo] span"? Would that  
> selector return anything for the above DOM?
>
> / Jonas

I think that there is a simple solution. Instead of starting from the  
descendants of the current element unless the first clause is :scope  
or $self (or what ever it would be), make the first clause refer to  
the current element.

document.getElementById("example").querySelectorAll(".foo span") // 
selects spans when the current element has a class of foo
document.getElementById("example").querySelectorAll("div span") // 
selects spans when the current element is a div element

But what if you don't care what the current element is?

document.getElementById("example").querySelectorAll("* span") // 
selects spans
document.getElementById("example").querySelectorAll("> span") // 
Implies  *>span ???

I think that this, while giving a different meaning when used from the  
document element provides the least confusion. It also imposes a very  
minor usability impact (one character, although on a large number of  
uses). Also, providing an explicit descendant combinator in addition  
to space might simplify the usability.

This may also have advantages as a filter for the current element,  
particularly when you're creating unobtrusive javascript.

document.getElementById("example").querySelectorAll(".foo")

Obviously that's only useful if you've selected the element first,  
done stuff then apply the querySelector.

This would also be much more valuable if it could be applied to a  
nodelist (which would be similar to what jQuery does today).

Adam van den Hoven
Web Developer
MemberDirect® Product Development
phone: 604 730 6380
email: avandenhoven@memberdirect.ca
website: http://memberdirect.ca

Received on Friday, 2 May 2008 19:56:29 UTC