W3C home > Mailing lists > Public > public-webapps@w3.org > April to June 2011

Re: More questions about contextual reference nodes

From: Boris Zbarsky <bzbarsky@MIT.EDU>
Date: Sat, 09 Apr 2011 10:14:41 -0700
Message-ID: <4DA09401.7070305@mit.edu>
To: Lachlan Hunt <lachlan.hunt@lachy.id.au>
CC: public-webapps <public-webapps@w3.org>, Cameron McCormack <cam@mcc.id.au>
On 4/9/11 6:27 AM, Lachlan Hunt wrote:
> There were cases in JQuery where the script wanted to iteratively run a
> selector on all nodes in a collection, and return elements that are
> descendants of those elements. This allows :scope to be used in those
> cases by passing the collection as the refNodes. There was previous
> discussion of this somewhere in the public-webapps archive.

Hmm... ok.

But is jquery's collection a JS Array?

> I also have to include one for HTMLCollection, which doesn't inherit
> from NodeList.

Yeah, that's broken...  I wonder whether we can just fix that in Web DOM 
Core or something....

> WebIDL says sequences are passed by value and arrays are passed by
> reference. So I suspect using an array is better here so that it doesn't
> have to create a copy.

The by reference thing only happens for array host objects, which a JS 
Array is not.  So the copy would effectively happen in either case, I 
believe, for JS Array.

> Yes, I would like feedback about the best way to define this,
> particularly for Arrays. In particular, how to address the following cases.
>
> var ref = [];
> ref[0] = el1;
> ref[1000000] = el2;
>
> That will contain almost a million empty cells. Will iterating through
> that cause any unwanted performance issues?

I believe my current Gecko implementation (using our existing variant 
facilities, since that's all we have to deal with overloads for the 
moment) will allocate a C array with 1000001 entries, then ignore it 
because not everything in the array is an Element.

A DOM binding implementation using WebIDL arrays or sequences would 
allocate a C++ array with 1000001 entries, all of which except for the 
first and last are null.  Then the actual implementation of the 
selectors API will walk this array and add only the first and last entry 
to the set of scope elements.  I think.  I'm pretty sure this is true 
for Array; the behavior for sequence is less clear to me.  And maybe 
this can all be optimized in implementations, somehow...  Ccing Cameron 
for his input.

 > Should we require arrays to
> contain contiguous Elements and only iterate up to the first empty or
> non-Element cell, ignoring anything beyond that?

If using webidl array/sequence types, that would help with the 
iteration, but not the large allocation.

> ref[1] = "string";
>
> Should passing this now throw an exception, or should it just accept
> that ref[0] was an element and use that?

Webidl array/sequence would throw in this case, I think.

> For NodeLists, should it accept any NodeList and only use the Element
> nodes? e.g. element.childNodes may contain text or comment nodes.

I think that's the right thing to do, yes.

-Boris
Received on Saturday, 9 April 2011 17:15:13 GMT

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