Re: [whatwg/dom] Add NodeList.setAttributes (#895)

> Thanks for the clarification!
> I see three variations of bulk setting attributes API.

I would prefer polymorphism over a different function for each use case with slightly different signatures that need to be learned separately. But I'm aware that often the preference has been towards multiple functions for performance reasons, so that's not a strong objection. Having a new `ElementSetter` interface just for this, when these methods would make perfect sense as static methods of existing classes seems like overfragmentation TBH.

> 3. Method for setting one attribute for a list of elements where the values are unique to each element in the list.
> 
> ```js
> interface ElementSetter {
>   undefined setAttributes(sequence<Element>, DOMString qualifiedName,  sequence<DOMString> values);
> }
> ```
> 
> These APIs should simplify the implementations of jQuery ([link](https://www.w3schools.com/jquery/html_attr.asp)) and D3.js ([link](https://github.com/d3/d3-selection)) and improve performance by eliminating the needs to access DOM inside a loop.

If I correctly understand what you're proposing, the ergonomics of this seem suboptimal. In general try to avoid disconnected sequences that the developer needs to ensure match up. It creates unnecessary error conditions (what happens when the sequences don't have the same length?), and is awkward to specify literally. Also, there are several cases that are not covered:
- It doesn't cover the case of multiple attributes, some with dynamic values, which would need multiple function calls. Why not accept a map as well, which matches the common library practice of accepting object literals?
- For dynamic values, precomputing a sequence of values is not the end of the world, but it is a little clumsy, as it would require two statements every time it's used, one for mapping and one for the actual setting, as you rarely have the values in a sequence like that ready to go. Why not follow the jQuery example and accept a mapping function as the attribute value? Then static and dynamic values can be combined in the same map.
- Not sure if `DOMStringMap` is the right type for this, which is currently [defined in a very dataset-centric way](https://html.spec.whatwg.org/multipage/dom.html#domstringmap) and is unclear whether it's even constructible.

I wonder if it would be feasible to study usage of jQuery's `attr()` function in the wild and see how developers actually use it. It might be feasible with a custom HTTPArchive metric, though it would take a month to get results. I could look into it.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/895#issuecomment-739920074

Received on Monday, 7 December 2020 13:32:17 UTC