From 624d090c8a93027ee9631fefdd2f665d84a719c7 Mon Sep 17 00:00:00 2001 From: Yun Feng Date: Wed, 12 Oct 2022 13:45:19 +1100 Subject: [PATCH] fix: recording bug in youtube and bitbucket when getting shadow host elements, the anchor element also has a host property as a string. This unexpected value can crash recorder --- packages/rrweb/src/record/mutation.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/rrweb/src/record/mutation.ts b/packages/rrweb/src/record/mutation.ts index d7724a16ba..5f54842b95 100644 --- a/packages/rrweb/src/record/mutation.ts +++ b/packages/rrweb/src/record/mutation.ts @@ -266,15 +266,20 @@ export default class MutationBuffer { return nextId; }; const pushAdd = (n: Node) => { - const shadowHost: Element | null = n.getRootNode - ? (n.getRootNode() as ShadowRoot)?.host - : null; + let shadowHost: Element | null = null; + if ( + n.getRootNode?.()?.nodeType === Node.DOCUMENT_FRAGMENT_NODE && + (n.getRootNode() as ShadowRoot).host + ) + shadowHost = (n.getRootNode() as ShadowRoot).host; // If n is in a nested shadow dom. let rootShadowHost = shadowHost; - while ((rootShadowHost?.getRootNode?.() as ShadowRoot | undefined)?.host) - rootShadowHost = - (rootShadowHost?.getRootNode?.() as ShadowRoot | undefined)?.host || - null; + while ( + rootShadowHost?.getRootNode?.()?.nodeType === + Node.DOCUMENT_FRAGMENT_NODE && + (rootShadowHost.getRootNode() as ShadowRoot).host + ) + rootShadowHost = (rootShadowHost.getRootNode() as ShadowRoot).host; // ensure contains is passed a Node, or it will throw an error const notInDoc = !this.doc.contains(n) &&