-
Notifications
You must be signed in to change notification settings - Fork 0
Description
creation
If attributes are scoped to classes, then presumably document.createAttribute would not be able to create a custom attribute. While this might be totally fine, it's a difference from custom elements, which can be created with document.createElement. Defined custom elements can also be directly constructed with new MyCustomElement().
Should custom attributes be able to be constructed in this way, with new MyCustomAttr()?
If custom attributes are like custom elements, the answer would presumably be yes. It's worth noting, however, that his might be the only case in which they could not rely on ownerElement being set in the constructor.
ownerElement attachment
Attribute nodes can be manipulated directly with setAttributeNode and removeAttributeNode. This means that a custom attribute's ownerElement can change. Given this, having lifecycle hooks indicating this change seems desirable. And if these hooks exist, its value in the constructor does not seem required. This brings up a related point:
What happens if a custom attribute is added to an element on which is isn't defined?
For example:
const inputAttr = input.getAttributeNode('input-behavior');
div.setAttributeNode(inputAttr);If this is ok, then authors will sometimes need to guard against the ownerElement being an unexpected type. Otherwise something like this.ownerElement.checkValidity() would throw. If this is considered too onerous, then attaching a custom attribute to a element class on which it's not defined could just always throw.
possible simplification
Another option would be to disallow ownerElement from changing completely and make setAttributeNode(customAttr) on any element always throw. This might be a reasonable simplification but depends on evaluating the motivating use cases.
If, in addition, direct construction is disallowed, then perhaps a custom element could rely on ownerElement in its constructor and not need to be concerned with it ever changing, other than being removed.