Skip to content

Commit 9d6696b

Browse files
committed
add isShadow flag if a node is under shadow root
1 parent 5f96f77 commit 9d6696b

File tree

5 files changed

+13
-1
lines changed

5 files changed

+13
-1
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import snapshot, {
77
} from './snapshot';
88
import rebuild, { buildNodeWithSN, addHoverClass } from './rebuild';
99
export * from './types';
10+
export * from './utils';
1011

1112
export {
1213
snapshot,

src/snapshot.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
MaskInputOptions,
99
SlimDOMOptions,
1010
} from './types';
11-
import { isElement } from './utils';
11+
import { isElement, isShadowRoot } from './utils';
1212

1313
let _id = 1;
1414
const tagNameRegex = RegExp('[^a-z0-9-_]');
@@ -657,6 +657,10 @@ export function serializeNodeWithId(
657657
}
658658
}
659659

660+
if (n.parentNode && isShadowRoot(n.parentNode)) {
661+
serializedNode.isShadow = true;
662+
}
663+
660664
if (
661665
serializedNode.type === NodeType.Element &&
662666
serializedNode.tagName === 'iframe'

src/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@ import { INode } from './types';
33
export function isElement(n: Node | INode): n is Element {
44
return n.nodeType === n.ELEMENT_NODE;
55
}
6+
7+
export function isShadowRoot(n: Node): n is ShadowRoot {
8+
const host: Element | null = (n as ShadowRoot)?.host;
9+
return Boolean(host && host.shadowRoot && host.shadowRoot === n);
10+
}

typings/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import snapshot, { serializeNodeWithId, transformAttribute, visitSnapshot, cleanupSnapshot, IGNORED_NODE } from './snapshot';
22
import rebuild, { buildNodeWithSN, addHoverClass } from './rebuild';
33
export * from './types';
4+
export * from './utils';
45
export { snapshot, serializeNodeWithId, rebuild, buildNodeWithSN, addHoverClass, transformAttribute, visitSnapshot, cleanupSnapshot, IGNORED_NODE, };

typings/utils.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
import { INode } from './types';
22
export declare function isElement(n: Node | INode): n is Element;
3+
export declare function isShadowRoot(n: Node): n is ShadowRoot;

0 commit comments

Comments
 (0)