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
This commit is contained in:
Yun Feng
2026-04-01 12:00:00 +08:00
committed by GitHub
parent 3c61ec91b5
commit ea9033fc08
2 changed files with 38 additions and 25 deletions

View File

@@ -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).

View File

@@ -268,20 +268,24 @@ export function _isBlockedElement(
blockClass: string | RegExp, blockClass: string | RegExp,
blockSelector: string | null, blockSelector: string | null,
): boolean { ): boolean {
if (typeof blockClass === 'string') { try {
if (element.classList.contains(blockClass)) { if (typeof blockClass === 'string') {
return true; if (element.classList.contains(blockClass)) {
}
} else {
for (let eIndex = element.classList.length; eIndex--; ) {
const className = element.classList[eIndex];
if (blockClass.test(className)) {
return true; return true;
} }
} else {
for (let eIndex = element.classList.length; eIndex--; ) {
const className = element.classList[eIndex];
if (blockClass.test(className)) {
return true;
}
}
} }
} if (blockSelector) {
if (blockSelector) { return element.matches(blockSelector);
return element.matches(blockSelector); }
} catch (e) {
//
} }
return false; return false;
@@ -313,22 +317,26 @@ export function needMaskingText(
maskTextClass: string | RegExp, maskTextClass: string | RegExp,
maskTextSelector: string | null, maskTextSelector: string | null,
): boolean { ): boolean {
const el: HTMLElement | null = try {
node.nodeType === node.ELEMENT_NODE const el: HTMLElement | null =
? (node as HTMLElement) node.nodeType === node.ELEMENT_NODE
: node.parentElement; ? (node as HTMLElement)
if (el === null) return false; : node.parentElement;
if (el === null) return false;
if (typeof maskTextClass === 'string') { if (typeof maskTextClass === 'string') {
if (el.classList.contains(maskTextClass)) return true; if (el.classList.contains(maskTextClass)) return true;
if (el.closest(`.${maskTextClass}`)) return true; if (el.closest(`.${maskTextClass}`)) return true;
} else { } else {
if (classMatchesRegex(el, maskTextClass, true)) return true; if (classMatchesRegex(el, maskTextClass, true)) return true;
} }
if (maskTextSelector) { if (maskTextSelector) {
if (el.matches(maskTextSelector)) return true; if (el.matches(maskTextSelector)) return true;
if (el.closest(maskTextSelector)) return true; if (el.closest(maskTextSelector)) return true;
}
} catch (e) {
//
} }
return false; return false;
} }