Re: querySelectorAll() -- selecting _immediate_ children of element

09.01.2012, 21:07, "Tab Atkins Jr." <jackalmage@gmail.com>:
> On Mon, Jan 9, 2012 at 6:17 AM, Marat Tanalin | tanalin.com
> <mtanalin@yandex.ru> wrote:
>
>> šquerySelector() and querySelectorAll() methods are exciting features, but they do not allow select _immediate_ children of reference element.
>>
>> šTo address this lack, we could use following syntax:
>>
>> šš švar divs = refElement.querySelectorAll('> DIV');
>>
>> šHere 'divs' variable contains list of DIV elements that are immediate children (not just descendant elements) of reference element (refElement).
>
> [snip]
>
> This is already supported in the Selectors API 2 draft:
> <http://dev.w3.org/2006/webapi/selectors-api2/>
>
> Using the qS and qSA functions, you can pass a reference node as the
> second argument, and select it with the :scope pseudoclass, like so:
>
> document.querySelectorAll(':scope > div', refElement)
>
> Using the find and findAll functions, you can just start a selector
> with a combinator, like in your example:
>
> refElement.findAll('> div')
>
> ~TJ

Thanks, Tab, it's somewhat good news.

However there is no need for new findAll() method. We can do the same with existing qS/qSA.

Nor we need second parameter in querySelectorAll. This addition looks like something purely theoretical (by the way, I've not found it in the draft you've linked to). We need just subject element (which's qS/qSA method is called) to be expressable inside selector.

Even if second parameter for qS/qSA will be in the spec, it should be equal to subject element by default so that we couldn't be forced to explicitly pass subject element every time:

    // Absurd non-DRY way: refElement is passed as argument to method of itself.
    refElement.querySelectorAll(':scope > div', refElement);

    // Correct way: ':scope' is refElement by default.
    refElement.querySelectorAll(':scope > div');

Furthermore, ':scope' looks like inoptimal name for pseudoclass since it may lead to confusion/conflicts between HTML5 scopes defined with 'scope' attribute and scopes inside qS/qSA.

Received on Monday, 9 January 2012 17:25:54 UTC