Anne van Kesteren

DOM: element constructors

Elements are rather curious web platform objects in that they are instantiated based on their name (and namespace). <a> creates an HTMLAnchorElement object. So does document.createElement("a"). Elements are also identified by their name, more so than by their class. Well written code branches on localName (not instanceof HTMLAnchorElement, which has cross-Realm issues to boot). Selectors match based on element names. Ergo, there is a lot of code assuming that an element whose name is input and namespace is, is also an instance of an HTMLInputElement class.

That is further compounded by only an instance of an HTMLInputElement class having the correct internal slots (as explained in Web platform and JavaScript). An element named input with namespace would cause havoc in browsers (and specifications) if its object was not an instance of an HTMLInputElement class.

These invariants start becoming problematic when introducing constructors and allowing for subclassing. Domenic started a GitHub repository element-constructors to tease them out. If you are interested in how we bring the DOM and JavaScript together I recommend participating.