W3C home > Mailing lists > Public > public-webapi@w3.org > May 2008

Re: [SelectorsAPI] Thoughts on querySelectorAll

From: John Resig <jresig@mozilla.com>
Date: Fri, 2 May 2008 15:08:27 -0700 (PDT)
To: Adam van den Hoven <avandenhoven@cucbc.com>
Cc: public-webapi@w3.org
Message-ID: <13401460.59311209766107878.JavaMail.root@cm-mail02.mozilla.org>

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

I disagree with this. These pieces of functionality should be broken up into two separate methods - a querySelectorAll that contextually finds elements (and uses :scope to find child elements and the like) and something like filterSelectorAll which filters an element - or set of elements - based upon a selector.

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

I absolutely agree - having DOMNodeList.querySelectorAll would be... amazing. With the addition of a filter method, as well, this API could be wonderfully useful.

However, that's a completely different debate that will need to be explored later.

Received on Friday, 2 May 2008 22:09:07 UTC

This archive was generated by hypermail 2.3.1 : Tuesday, 6 January 2015 21:16:26 UTC