Re: More questions about contextual reference nodes

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 UTC