web
web copied to clipboard
[feat] Add helper to convert HtmlCollection to a dart list
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,
- Add a similar extension on
HtmlCollection
likeTouchListConvert
that would return aHtmlCollectionWrapper
that would be a dart list. - Create a generic wrapper like
TouchListConvert
which could work for bothTouchListConvert
andHtmlCollection
, and also other web facing apis likeNodeList
that represent a list of items. This one I'm not so sure abt how one would approach for this scenario.