Reputation: 471
I want to get all of the elements unknown to the browser (and JSDOM) via document.querySelectorAll
.
I've tried using a .toString
then checking if the element registers as HTMLUnknownElement
like so:
if (node.toString() === "[object HTMLUnknownElement]") ...
But this doesn't work if the element in question has a -
in its name such as <test-element>
. I assume for compatibility with HTML Custom Elements. Right now, I'm simply listing all of the elements with a load of :not
's for every tag I know about, but this feels like a hack:
document.querySelectorAll("*:not(html):not(head):not(link):not(meta):not(base):not(script):not(style):not(title):not(body):not(address):not(article):not(aside):not(footer):not(h1):not(h2):not(h3):not(h4):not(h5):not(h6):not(header):not(hgroup):not(nav):not(section):not(blockquote):not(cite):not(dd):not(dl):not(div):not(dt):not(figcaption):not(figure):not(hr):not(li):not(ol):not(ul):not(menu):not(main):not(p):not(pre):not(a):not(abbr):not(b):not(bdi):not(bdo):not(br):not(code):not(data):not(time):not(dfn):not(em):not(i):not(kbd):not(mark):not(q):not(rp):not(ruby):not(rt):not(rtc):not(rb):not(s):not(del):not(ins):not(samp):not(small):not(span):not(strong):not(sub):not(sup):not(u):not(var):not(wbr):not(area):not(map):not(audio):not(source):not(img):not(track):not(video):not(embed):not(object):not(param):not(picture):not(canvas):not(noscript):not(caption):not(table):not(col):not(colgroup):not(tbody):not(tr):not(td):not(tfoot):not(th):not(thead):not(button):not(datalist):not(option):not(fieldset):not(label):not(form):not(input):not(legend):not(meter):not(optgroup):not(select):not(output):not(progress):not(textarea):not(details):not(dialog):not(menuitem):not(summary):not(content):not(slot):not(element):not(shadow):not(template):not(acronym):not(applet):not(basefont):not(font):not(big):not(blink):not(center):not(command):not(dir):not(frame):not(frameset):not(image):not(isindex):not(keygen):not(listing):not(marquee):not(multicol):not(nextid):not(noembed):not(plaintext):not(spacer):not(strike):not(tt):not(xmp)")
Does anybody know of a better way to do this?
Upvotes: 3
Views: 1112
Reputation: 169401
The other answer did not find custom elements correctly for my application.
I changed the snippet and the following works for me
function isUnknownElement(element) {
const tagName = element.tagName
if (tagName.includes('-')) {
return element.constructor.name === 'HTMLUnknownElement' ||
element.constructor.name === 'HTMLElement'
}
return element.constructor.name === 'HTMLUnknownElement';
}
function identifyUnknownElements(selector) {
const elements = document.querySelectorAll(selector);
const unknownElements = [];
for (let i = 0, length = elements.length; i < length; i++) {
if (isUnknownElement(elements[i])) {
elements[i].classList.add('unknown-element');
unknownElements.push(elements[i]);
}
}
return unknownElements;
}
console.log(identifyUnknownElements('*'))
Upvotes: 1
Reputation: 3842
Hope this will help,
function isUnknownElement(element) {
return element.constructor.name === 'HTMLUnknownElement';
}
function identifyUnknownElements(selector) {
const elements = document.querySelectorAll(selector);
const unknownElements = [];
for (let i = 0, length = elements.length; i < length; i++) {
if (/-/g.test(elements[i].nodeName)) {
const temp = document.createElement(elements[i].nodeName.replace(/-+/, ""));
if (isUnknownElement(temp)) {
elements[i].classList.add('unknown-element');
unknownElements.push(elements[i]);
}
continue;
}
if (isUnknownElement(elements[i])) {
elements[i].classList.add('unknown-element');
unknownElements.push(elements[i]);
}
}
return unknownElements;
}
console.log(identifyUnknownElements('*'))
<temp></temp>
<test-element></test-element>
Upvotes: 0