From 9d2fd20134935df6ea4bff2a0a4ba28fa483a6c4 Mon Sep 17 00:00:00 2001 From: Stephen Date: Fri, 27 Sep 2024 17:59:21 +0200 Subject: [PATCH] Removes need for list of constructors by finding constructors on the fly --- modules/element.js | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/modules/element.js b/modules/element.js index e816355..ef85434 100644 --- a/modules/element.js +++ b/modules/element.js @@ -100,6 +100,7 @@ import { createInternals, getInternals } from './element/internals.js'; const define = Object.defineProperties; const nothing = {}; +/* const constructors = { // List only those elements whose constructor names do not match their tag 'a': HTMLAnchorElement, @@ -127,6 +128,7 @@ const constructors = { 'tfoot': HTMLTableSectionElement, 'ul': HTMLUListElement }; +*/ const formProperties = { // These properties echo those provided by native form controls. @@ -152,19 +154,14 @@ const shadowParameterIndex = 0; let supportsCustomisedBuiltIn = false; function getElementConstructor(tag) { - // Return a constructor from the known list of tag names – not all tags - // have constructor names that match their tags - return constructors[tag] - // Or assemble the tag name in the form "HTMLTagElement" and return - // that property of the window object - || window['HTML' + tag[0].toUpperCase() + tag.slice(1) + 'Element'] - // Or use HTMLElement, which is the constructor for
and various - // other elements - || HTMLELement ; - - (() => { - throw new Error('Constructor not found for tag "' + tag + '"'); - })(); + if (constructors[tag]) return constructors[tag]; + + const constructor = document.createElement(tag).constructor; + if (constructor === HTMLUnknownElement) { + throw new Error('Cannot define customised built-in - constructor for <' + tag + '> is HTMLUnknownElement'); + } + + return constructors[tag] = constructor; } // Capture name and tag from or , syntax