Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function mapping JavaScript builtin class to html tag name

Tags:

javascript

I need a function that maps JavaScript builtin class to corresponding html tag name, e.g.:

function classToTagName(clazz) {
  // implementation
}

classToTagName(HTMLInputElement) // ->
"input" 
classToTagName(HTMLAnchorElement) // ->
"a"

Is it possible to implement it without using explicit mapping or regex?

I could use DOM tagName property if I had object instance, but browsers seem to prohibit invoking constructors of builtin tag classes with TypeError: Illegal constructor.

like image 656
Marina Avatar asked Mar 12 '23 09:03

Marina


1 Answers

According to Custom Elements,

When defining our custom element, we have to also specify the extends option:

customElements.define("plastic-button", PlasticButton, { extends: "button" });

The spec editors already know that may seem redundant information, but they explain:

In general, the name of the element being extended cannot be determined simply by looking at what element interface it extends, as many elements share the same interface (such as q and blockquote both sharing HTMLQuoteElement).

So it's not possible. Your mapping is not well-defined.

like image 152
Oriol Avatar answered Mar 23 '23 04:03

Oriol