Marina Marina - 8 months ago 52
Javascript Question

Function mapping JavaScript builtin class to html tag name

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

function classToTagName(clazz) {
// implementation

classToTagName(HTMLInputElement) // ->
classToTagName(HTMLAnchorElement) // ->

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

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


There is no such mapping, as an interface can belong to multiple tag names. Here is a (possibly not complete) list of ambiguous interfaces (based on the element list on mdn):

HTMLElement: address,article,aside,footer,header,hgroup,nav,section,dd,dt,figcaption,figure,main,abbr,b,bdi,bdo,cite,code,dfn,em,i,kbd,mark,rp,ruby,rt,rtc,rb,s,samp,small,strong,sub,sup,u,var,wbr,noscript,summary,acronym,basefont,big,center,command,noembed,plaintext,strike,tt
HTMLHeadingElement: h1,h6,h2,h3,h4,h5
HTMLPreElement: pre,listing,xmp
HTMLUnknownElement: data,time,menuitem,element,applet,blink,isindex,multicol,nextid,spacer
HTMLQuoteElement: q,blockquote
HTMLModElement: del,ins
HTMLTableColElement: col,colgroup
HTMLTableSectionElement: tbody,thead,tfoot
HTMLTableCellElement: td,th