utils/dom/isshadowroot
Functions
isNativeShadowRoot( obj ) → obj is ShadowRootinternalmodule:utils/dom/isshadowroot~isNativeShadowRootChecks 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 globalShadowRootwith its own constructor and patchesSymbol.hasInstanceon it. It emulates the tree faithfully enough for traversal (host,parentNode,contains,activeElementall behave), which is whyisShadowRootstays truthful about it, but being no platform object it cannot be:- passed to a DOM API expecting a real root –
Selection#getComposedRanges()andDocument#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
isShadowRooteverywhere else. Answeringfalsehere 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: unknownThe object to check.
Returns
obj is ShadowRoot
- passed to a DOM API expecting a real root –
isShadowRoot( obj ) → obj is ShadowRootmodule:utils/dom/isshadowroot~isShadowRootChecks 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: unknownThe object to check.
Returns
obj is ShadowRoot