Open tabatkins opened 5 years ago
Or wait, I misunderstood the nature of the holes, and thus their APIs. A "whole attribute" hole should just have setAttribute()
, indirecting to the element's own attributes, and a node
getter for the element itself (as all the other holes have a reference to the object). You can set whatever attributes you want that way, boolean or not.
(Maybe getAttribute()
/etc as well for convenience, so you can do all the attribute manipulation without having to jump thru fooHole.node.getAttribute(...)
.)
In my own experience writing "JS, but in a template-looking way" with a simple
mk(tagname, attrs, ...children)
function, I've found that I commonly need to substitute arbitrary whole attributes into a chunk of HTML. In particular, boolean attributes can't be done via value-substitution, since their mere presence with any value activates them.This needs to accept multiple attributes at once, too, since you often can't foresee precisely what attributes you'll need to sub in. Suggested API is to have the substitution method take a
record<DOMString, (bool or DOMString)>
and sub in the values appropriately. Afalse
results in the attribute not being put into the element, atrue
adds the attribute with an empty string as value, and a string adds the attribute with the given string as value.(This false/string dichotomy is very useful in practice to let you deal with booleans that also have significant values, without having to contort your code to output different object shapes. It's often much easier to have stable object shapes and just fill in distinct values for the different cases.)