From e92d92b0b6019ca6ddc044fcc80f424ce91f3ba4 Mon Sep 17 00:00:00 2001 From: Yanzhen Yu Date: Wed, 1 Apr 2026 12:00:00 +0800 Subject: [PATCH] add isShadow flag if a node is under shadow root --- src/index.ts | 1 + src/snapshot.ts | 6 +++++- src/utils.ts | 5 +++++ typings/index.d.ts | 1 + typings/utils.d.ts | 1 + 5 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 20d5b02d..9c1fd389 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,6 +7,7 @@ import snapshot, { } from './snapshot'; import rebuild, { buildNodeWithSN, addHoverClass } from './rebuild'; export * from './types'; +export * from './utils'; export { snapshot, diff --git a/src/snapshot.ts b/src/snapshot.ts index 62832cc0..210386aa 100644 --- a/src/snapshot.ts +++ b/src/snapshot.ts @@ -8,7 +8,7 @@ import { MaskInputOptions, SlimDOMOptions, } from './types'; -import { isElement } from './utils'; +import { isElement, isShadowRoot } from './utils'; let _id = 1; const tagNameRegex = RegExp('[^a-z0-9-_]'); @@ -657,6 +657,10 @@ export function serializeNodeWithId( } } + if (n.parentNode && isShadowRoot(n.parentNode)) { + serializedNode.isShadow = true; + } + if ( serializedNode.type === NodeType.Element && serializedNode.tagName === 'iframe' diff --git a/src/utils.ts b/src/utils.ts index de0e1e8c..df6cf20b 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -3,3 +3,8 @@ import { INode } from './types'; export function isElement(n: Node | INode): n is Element { return n.nodeType === n.ELEMENT_NODE; } + +export function isShadowRoot(n: Node): n is ShadowRoot { + const host: Element | null = (n as ShadowRoot)?.host; + return Boolean(host && host.shadowRoot && host.shadowRoot === n); +} diff --git a/typings/index.d.ts b/typings/index.d.ts index b60dbf59..c9f12359 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1,4 +1,5 @@ import snapshot, { serializeNodeWithId, transformAttribute, visitSnapshot, cleanupSnapshot, IGNORED_NODE } from './snapshot'; import rebuild, { buildNodeWithSN, addHoverClass } from './rebuild'; export * from './types'; +export * from './utils'; export { snapshot, serializeNodeWithId, rebuild, buildNodeWithSN, addHoverClass, transformAttribute, visitSnapshot, cleanupSnapshot, IGNORED_NODE, }; diff --git a/typings/utils.d.ts b/typings/utils.d.ts index e1571f38..a896748e 100644 --- a/typings/utils.d.ts +++ b/typings/utils.d.ts @@ -1,2 +1,3 @@ import { INode } from './types'; export declare function isElement(n: Node | INode): n is Element; +export declare function isShadowRoot(n: Node): n is ShadowRoot;