web icon indicating copy to clipboard operation
web copied to clipboard

[feat] Add helper to convert HtmlCollection to a dart list

Open rutvik110 opened this issue 6 months ago • 6 comments

Issue:

When working with html element childrens which are represented through the HtmlCollection, if someone wants to iterate through these childrens in dart, only approach is to use a for loop like follow to either create a dart list which would then allow running operations like forEach, map, etc on the list or simply iterate through them to execute any functions.

      final childrens = <Element>[];

      for (var i = 0; i < element.children.length; i++) {
        final child = element.children.item(i);

        childrens.add(child);
      }

This works but it becomes tedious when I've to do the same thing in multiple places. Off course, I could add an extension for this on HtmlCollection but it would be nice if such helper was present in web package itself.

TouchListConvert extension on TouchList is an example of such helper. And the approach seems better instead of running a for loop which results in extra computations. Adding a similar helper for converting HtmlCollection to a dart list would be nice addition I believe.

Proposal:

I can see this addition going two ways,

  1. Add a similar extension on HtmlCollection like TouchListConvert that would return a HtmlCollectionWrapper that would be a dart list.
  2. Create a generic wrapper like TouchListConvert which could work for both TouchListConvert and HtmlCollection, and also other web facing apis like NodeList that represent a list of items. This one I'm not so sure abt how one would approach for this scenario.

rutvik110 avatar Aug 15 '24 10:08 rutvik110