Dom: Consider adding more utils to Elements

Created on 7 Jan 2016  路  6Comments  路  Source: whatwg/dom

One thing people seem to especially dislike about "vanilla dom" is that they are forced to loop over collection of elements to perform simple operations; for instance, adding a simple class="" to all elements selected.

Sure having Elements extending Array simplifies this now, as you can use forEach, map, reduce, etc., but I believe for some cases the code to use would be too verbose to write by hand every time (especially if compared to the equivalent in jQuery).

I'm very aware that the risk here is introducing new functionality that is redundant to something that is already possible using existing DOM or ES, but at the same time I think that a very limited set of functionalities/shortcuts would be very handy to have.

Though, I don't have anything particular in mind yet, apart classList and dataList. Do you guys have any idea?

{
    boolean allContain(token);
    boolean someContain(token);
    void add(...tokens);
    void remove(...tokens);
}
needs implementer interest

Most helpful comment

I'd appreciate to have .map, .filter available for HTMLCollections, NodeLists...

and be able to do things more simply: [].concat(...document.all.map(x=>x.childNodes.filter(n=>n.nodeType==3))) // get all text nodes in page

All 6 comments

I seem to recall some NodeList extension ideas for this sort of thing awhile back from jresig: https://github.com/jeresig/nodelist. Might be worth taking a second look at.

Yes! Also EventTarget interface would be useful.

I'd appreciate to have .map, .filter available for HTMLCollections, NodeLists...

and be able to do things more simply: [].concat(...document.all.map(x=>x.childNodes.filter(n=>n.nodeType==3))) // get all text nodes in page

I'm closing this since we removed Elements. @caub I think that might be worth opening a new issue for. I think the main question with those kind of array utility functions is what they end up returning and how exactly they are supposed to be implemented. Another concern is that we might clash with existing prototype extensions of these objects.

@annevk Thanks, well nowadays using React or such libs, I don't find myself needing that much

What may be interesting is adding more methods to NodeList (possibly on HTMLCollection too), other than forEach, like those, but again I don't really need this at all, I'll open an issue if I find some better usecases

I guess the question is if React et al would benefit from native support, but I somewhat doubt that.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jakearchibald picture jakearchibald  路  11Comments

a-tarasyuk picture a-tarasyuk  路  3Comments

saschanaz picture saschanaz  路  5Comments

zandaqo picture zandaqo  路  11Comments

domenic picture domenic  路  8Comments