admin管理员组

文章数量:1403107

I need to process the DOM children of a container element (to achieve some fancy layout magic) and leave non-rendered elements alone.

I was surprised to find that I can't use an instanceof HTMLNoScriptElement check for <noscript> elements; which was my go-to solution for <style>, <script>, etc.

const shouldIgnoreElement = (element: Element) =>
  element instanceof HTMLStyleElement ||
  element instanceof HTMLScriptElement ||
  element instanceof HTMLDialogElement ||
  element instanceof HTMLTemplateElement ||
  element.tagName === 'NOSCRIPT'

const shouldProcessElement = (element: Element) => !shouldIgnoreElement(element)
[...root.children].filter(shouldProcessElement).forEach(...)

As you can see I ended up using element.tagName === 'NOSCRIPT' for the check. But I still want to get a deeper understanding of this curious situation. And wonder whether I did something wrong or harbor a misunderstanding.

My research on mdn turned up:

  • the technical summary for <script> elements states their DOM interface is HTMLScriptElement
  • the technical summary for <noscript> elements states their DOM interface is HTMLElement
  • the Web API overview does not list any HTMLNoScriptElement

So it seems there simply is no class to represent <noscript> elements.

Why is there no corresponding class?

Is noscript the only HTML element type with this curious gap in the Web API?

本文标签: