whatwg / dom

DOM Standard
https://dom.spec.whatwg.org/
Other
1.58k stars 294 forks source link

Delegate to Iterator helper methods on NodeList #1211

Open keithamus opened 1 year ago

keithamus commented 1 year ago

Now that the “iterator helpers” proposal is Stage 3 and browsers are intending to ship it, perhaps it’s a good time to investigate how we can make these ergonomic for DOM collection APIs like NodeList.

Currently NodeList can be coerced to an iterator, which means it will be possible to do document.querySelectorAll('.foo').entries().take(5) however this isn’t exactly in the top 10 list of elegant APIs.

I propose we expend some energy to see if it’s web compat to add iterator helper method aliases to NodeList proper, so that developers can use the much more pleasant document.querySelectorAll('.foo').take(5).

The following method names are part of iterator helpers:

domenic commented 1 year ago

I think we've found this is not web-compatible when it's been tried in the past. Too much code does things like conditional polyfilling, or assuming that if map does not exist then it must be a NodeList that needs to be wrapped in an Array or similar.

I doubt implementations will be excited to try this, to save people from typing .entries().

annevk commented 1 year ago

There's probably not enough benefit in typing less and fixing this to add parentNode.query[All](), but it does seem nice.