web icon indicating copy to clipboard operation
web copied to clipboard

Make `NodeList` `Iterable<Node>`

Open ditman opened this issue 1 year ago • 1 comments

Any chance of making NodeList mix-in Iterable<Node>?

Something like:

@JS()
@staticInterop
class IterableNodeList extends html.NodeList with Iterable<html.Node> {
  @override
  Iterator<html.Node> get iterator => _NodeListIterator(this);
}

class _NodeListIterator implements Iterator<html.Node> {
  _NodeListIterator(html.NodeList data) : _data = data;

  final html.NodeList _data;
  int _currentIndex = 0;

  @override
  html.Node get current => _data.item(_currentIndex)!;

  @override
  bool moveNext() {
    _currentIndex++;
    return _currentIndex < _data.length;
  }
}

(Or similar!)

ditman avatar Nov 21 '23 10:11 ditman

Maybe it should also mixin List, similar to dart:html https://api.dart.dev/stable/2.19.5/dart-html/NodeList-class.html

ahmednfwela avatar Jun 10 '24 10:06 ahmednfwela

FYI - @rutvik110 added a nifty wrapper to handle such immutable lists easier with JSImmutableListWrapper here: https://github.com/dart-lang/web/issues/281.

That being said, it's a wrapper and not like a dart:html list type where it's both the interop type and a List. Doing both is not really feasible since it'll require modifying the type system quite heavily in dart2wasm (and we're trying to get away from this in the JS compilers). If this is insufficient, feel free to reopen, but for now marking as completed.

srujzs avatar Aug 24 '24 00:08 srujzs