W3C home > Mailing lists > Public > public-webapps@w3.org > October to December 2011

Re: [Selectors API 2] Is matchesSelector stable enough to unprefix in implementations?

From: Sean Hogan <shogun70@westnet.com.au>
Date: Fri, 25 Nov 2011 11:07:30 +1100
Message-ID: <4ECEDC42.8090903@westnet.com.au>
To: Lachlan Hunt <lachlan.hunt@lachy.id.au>
CC: Boris Zbarsky <bzbarsky@MIT.EDU>, public-webapps@w3.org
On 24/11/11 7:46 PM, Lachlan Hunt wrote:
> On 2011-11-23 23:38, Sean Hogan wrote:
>> Are there any issues with:
>>
>> - If you want to use selectors with explicit :scope then you use
>> querySelector / querySelectorAll / matchesSelector.
>>
>> - If you want to use selectors with :scope implied at the start of each
>> selector in the selector list (as most js libs currently do) then you
>> use find / findAll / matches.
>
> The matches method will not change behaviour depending on whether or 
> not there is an explicit :scope because it is always evaluated in the 
> context of the entire tree.  There is never an implied :scope inserted 
> into the selector, so there will not be two alternative matches methods.
>

A matching method that doesn't imply :scope should be called 
matchesSelector().

If and when there is a need for a matching method that does imply :scope 
(which I provided a use-case for in 
http://lists.w3.org/Archives/Public/public-webapps/2011OctDec/0342.html) 
then it could be called matches().

I should be able to define querySelectorAll() in terms of 
matchesSelector(),
and findAll() in terms of matches().

Thus:

function querySelectorAll(selector) {
     var refNode = this;
     return [].filter.call(refNode.getElementsByTagName("*"),
         function(elt) { return elt.matchesSelector(selector, refNode); });
}

function findAll(selector) {
     var refNode = this, list = [];
     for (var node=refNode; node; node=node.nextSibling) {
         if (node.nodeType != 1) continue;
         if (node != refNode && node.matches(selector, refNode)) 
list.push(node);
         [].push.apply(list, [].filter.call(node.getElementsByTagName("*"),
             function(elt) { return elt.matches(selector, refNode); });
     }
     return list;
}
Received on Friday, 25 November 2011 00:08:02 GMT

This archive was generated by hypermail 2.3.1 : Tuesday, 26 March 2013 18:49:49 GMT