Can custom element classes extends subclasses of HTMLElement? |
|||
Issue descriptionA question arose from the review comment https://codereview.chromium.org/2054433002#msg6 class C extends HTMLImageElement { constructor() { super(); } } customElements.define('a-a', C); Is this allowed? A few question points: * Current our impl throws "illegal constructor" * Sync create element may work, but when async or upgrade, we create HTMLElement first, and can't upgrade it to HTMLImageElement.
,
Jun 11 2016
Thank you for the comment. Filed this bug because I originally thought this should work spec-wise and this is impl issue, but sounds like this is still a spec issue? If there's no consensus to allow this yet, I personally vote not to allow in v1. Not only spec/js part but impl has an issue that once it creates a native HTMLElement instance, replacing it with a native HTMLImageElement during upgrade isn't easy. rniwa seems to have some wish to change upgrade logic in future, so we could revisit that at that point. I'll check with dominicc@ on Monday.
,
Jun 21 2016
I have posted a spec fix to prevent this at https://github.com/whatwg/html/pull/1451.
,
Sep 2 2016
,
Sep 28 2016
The call to customElements.define() in the OP's code should, I think, contain a third parameter which also specifies which element to extend:
class C extends HTMLImageElement {
constructor() { super(); }
}
customElements.define('a-a', C, {extends: 'img');
Doing this, in Chrome 55.0.2873.4, throws "TypeError: Illegal constructor".
FWIW - I came across this problem when reading these documents about CE v1:
https://html.spec.whatwg.org/multipage/scripting.html#custom-elements-customized-builtin-example
https://developers.google.com/web/fundamentals/primers/customelements/
,
Sep 28 2016
Actually I see that this is already being addressed: https://bugs.chromium.org/p/chromium/issues/detail?id=618606
,
Oct 27 2016
I'm going to mark this obsolete. The short answer is: Yes, you can write customized built-in elements with "is" and "extends". Yes, you could write extends HTMLImageElement and call "super". You won't be able to construct these, but the failure is well specified: The super call will check whether the tag name you're creating (eg "a-a") is a legal one for the active function object (HTMLImageElement) and throw a TypeError. Those checks are not implemented yet, but will be real soon now. In the meantime, per Comment 5, we throw a TypeError. So the behavior is almost identical. We'll just change the wording of the error message to make it more detailed. |
|||
►
Sign in to add a comment |
|||
Comment 1 by domenic@chromium.org
, Jun 10 2016