Re: An iterable DOM

On Wed, Jun 20, 2012 at 1:59 AM, Cameron McCormack <cam@mcc.id.au> wrote:
>> Arrays are iterable, because there's a new built-in
>> Array.prototype.iterator method. Many DOM objects should be iterable
>> too. Certainly those with indexed properties and .length:
>>
>>   for (var elt of document.querySelectorAll(".post:not(.grouchy)"))
>>       addUnicorns(elt);
>>
>> Possibly also dictionaries and DOM iterators.
>
> Sounds good to me, in general.  Since platform array objects and instances
> of interfaces annotated with [ArrayClass] have Array.prototype as their
> [[Prototype]], the intentionally generic Array.prototype.iterator should
> just work on them.

Yes.

I could imagine NodeList.prototype.iterator returning an actual DOM
NodeIterator, rather than using the builtin Array.prototype.iterator.
That would be robust against changes to the front of the array while
it's being iterated. But an Array.prototype.iterator might run faster.

> The interface prototype object for interfaces that support indexed
> properties should automatically get an iterator method.
>
> Should objects with named properties automatically get an iterator method?
>  I'm not sure.

Probably best to make the DOM and HTML specs ask for it in specific
cases where it's useful. Iterating over all the stuff that gets named
properties in Document seems pretty meaningless. Window has both
indexed and named properties. Iterating over a .dataset, I don't know.

>    DOMString iterator;

Works for me.

> By "DOM iterators" above did you mean NodeIterators and TreeWalkers from DOM
> Traversal:
> http://www.w3.org/TR/DOM-Level-2-Traversal-Range/traversal.html#Traversal-NodeIterator

I meant NodeIterators. Maybe we don't care. I don't mean to take off
into the weeds here.

WebIDL could provide an empty Iterator interface, with understanding
that each language has to specify what that means. So we could have
'interface NodeIterator : Iterator', and in ECMAScript, that means
that NodeIterator.prototype inherits from the builtin
Iterator.prototype, and there's a NodeIterator.prototype.next method
that throws StopIteration when it hits the end.

-j

Received on Wednesday, 20 June 2012 17:27:35 UTC