Sign up (with export icon)

utils/dom/isshadowroot

Api-module iconmodule

Functions

  • Chevron-right icon

    isNativeShadowRoot( obj ) → obj is ShadowRoot
    internal

    Checks whether the given object is a shadow root the platform itself accepts.

    A page may run a shadow DOM polyfill that emulates the boundary in the light DOM instead of asking the engine for one – a synthetic shadow root. Such a root passes isShadowRoot, because the polyfill replaces the global ShadowRoot with its own constructor and patches Symbol.hasInstance on it. It emulates the tree faithfully enough for traversal (host, parentNode, contains, activeElement all behave), which is why isShadowRoot stays truthful about it, but being no platform object it cannot be:

    • passed to a DOM API expecting a real root – Selection#getComposedRanges() and Document#caretPositionFromPoint() reject it while converting their arguments;
    • asked for its selection or stylesheets – those are the members a polyfill cannot emulate, so it usually makes them throw;
    • used as a mount target – its appendChild() delegates to the host element, so the node ends up in the surrounding tree rather than at the top of the one it was mounted in.

    Use it wherever a root crosses into one of those, and isShadowRoot everywhere else. Answering false here falls back to the light-DOM path, which is the correct answer for a synthetic root: the boundary it emulates is not one the engine actually has, so the document-level APIs already see the real nodes behind it.

    Parameters

    obj: unknown

    The object to check.

    Returns

    obj is ShadowRoot
  • Chevron-right icon

    isShadowRoot( obj ) → obj is ShadowRoot

    Checks whether the given object is a native DOM ShadowRoot.

    A type guard, safe to call on any value, and correct for a root belonging to another document or iframe.

    Parameters

    obj: unknown

    The object to check.

    Returns

    obj is ShadowRoot