UNPKG

1.05 kBPlain TextView Raw
1export const getOwnerDocument = (el: Element | null | undefined): Document => {
2 return el?.ownerDocument ?? document;
3};
4
5export const getOwnerWindow = (
6 el: (Window & typeof global) | Element | null | undefined
7): Window & typeof global => {
8 if (el && 'window' in el && el.window === el) {
9 return el;
10 }
11
12 const doc = getOwnerDocument(el as Element | null | undefined);
13 return doc.defaultView || window;
14};
15
16/**
17 * Type guard that checks if a value is a Node. Verifies the presence and type of the nodeType property.
18 */
19function isNode(value: unknown): value is Node {
20 return value !== null &&
21 typeof value === 'object' &&
22 'nodeType' in value &&
23 typeof (value as Node).nodeType === 'number';
24}
25/**
26 * Type guard that checks if a node is a ShadowRoot. Uses nodeType and host property checks to
27 * distinguish ShadowRoot from other DocumentFragments.
28 */
29export function isShadowRoot(node: Node | null): node is ShadowRoot {
30 return isNode(node) &&
31 node.nodeType === Node.DOCUMENT_FRAGMENT_NODE &&
32 'host' in node;
33}