From ea9033fc0872f52c9f76f7d157152205a767dd4e Mon Sep 17 00:00:00 2001 From: Yun Feng Date: Wed, 1 Apr 2026 12:00:00 +0800 Subject: [PATCH] improve: add try catch to snapshot.ts 's masking text function (#1148) * improve: add try catch to snapshot.ts to make it robust * add change log --- .changeset/pretty-schools-remember.md | 5 +++ packages/rrweb-snapshot/src/snapshot.ts | 58 ++++++++++++++----------- 2 files changed, 38 insertions(+), 25 deletions(-) create mode 100644 .changeset/pretty-schools-remember.md diff --git a/.changeset/pretty-schools-remember.md b/.changeset/pretty-schools-remember.md new file mode 100644 index 00000000..920125d1 --- /dev/null +++ b/.changeset/pretty-schools-remember.md @@ -0,0 +1,5 @@ +--- +'rrweb-snapshot': patch +--- + +Improve: Add try catch to snapshot.ts 's masking text function. Fixes [#1118](https://github.com/rrweb-io/rrweb/issues/1118). diff --git a/packages/rrweb-snapshot/src/snapshot.ts b/packages/rrweb-snapshot/src/snapshot.ts index f587bd83..8c3fda1a 100644 --- a/packages/rrweb-snapshot/src/snapshot.ts +++ b/packages/rrweb-snapshot/src/snapshot.ts @@ -268,20 +268,24 @@ export function _isBlockedElement( blockClass: string | RegExp, blockSelector: string | null, ): boolean { - if (typeof blockClass === 'string') { - if (element.classList.contains(blockClass)) { - return true; - } - } else { - for (let eIndex = element.classList.length; eIndex--; ) { - const className = element.classList[eIndex]; - if (blockClass.test(className)) { + try { + if (typeof blockClass === 'string') { + if (element.classList.contains(blockClass)) { return true; } + } else { + for (let eIndex = element.classList.length; eIndex--; ) { + const className = element.classList[eIndex]; + if (blockClass.test(className)) { + return true; + } + } } - } - if (blockSelector) { - return element.matches(blockSelector); + if (blockSelector) { + return element.matches(blockSelector); + } + } catch (e) { + // } return false; @@ -313,22 +317,26 @@ export function needMaskingText( maskTextClass: string | RegExp, maskTextSelector: string | null, ): boolean { - const el: HTMLElement | null = - node.nodeType === node.ELEMENT_NODE - ? (node as HTMLElement) - : node.parentElement; - if (el === null) return false; + try { + const el: HTMLElement | null = + node.nodeType === node.ELEMENT_NODE + ? (node as HTMLElement) + : node.parentElement; + if (el === null) return false; - if (typeof maskTextClass === 'string') { - if (el.classList.contains(maskTextClass)) return true; - if (el.closest(`.${maskTextClass}`)) return true; - } else { - if (classMatchesRegex(el, maskTextClass, true)) return true; - } + if (typeof maskTextClass === 'string') { + if (el.classList.contains(maskTextClass)) return true; + if (el.closest(`.${maskTextClass}`)) return true; + } else { + if (classMatchesRegex(el, maskTextClass, true)) return true; + } - if (maskTextSelector) { - if (el.matches(maskTextSelector)) return true; - if (el.closest(maskTextSelector)) return true; + if (maskTextSelector) { + if (el.matches(maskTextSelector)) return true; + if (el.closest(maskTextSelector)) return true; + } + } catch (e) { + // } return false; }