[dom] Consider support for ES2015 iterator protocol for NodeIterator (#147)

ES2015 introduces support for [iteration protocols](https://developer.mozilla.org/pl/docs/Web/JavaScript/Reference/Iteration_protocols). Iterators are objects with `.next` method which return object `{value: value, done: true/false}`.
Iterables are objects with `Symbol.iterator` method that returns iterator.

Making NodeIterator an iterator would be non-breaking change that will allow simple iteration of object using `for-of` loop or converting results to static array by spread operator.

Simplest implementation would be:
```
NodeIterator.prototype.next = function() {
     const value = this.getNextNode();
     return {
          value,
          done: !!val
     };
};
NodeIterator.prototype[Symbol.iterator] = function() {
   return this;
}
```

I think real specification should include cloning "initial state" of NodeIterator to allow multiple iterations over the same NodeIterator.

Similar logic can be applied to `XPathResult` objects that expose `iterateNext` method.

Related [Firefox bug](https://bugzilla.mozilla.org/show_bug.cgi?id=1238662).

---
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/147

Received on Tuesday, 12 January 2016 10:13:03 UTC