From 3b1a8598704766bd73a48408f78574703be3e2d4 Mon Sep 17 00:00:00 2001 From: Michael Dellanoce Date: Thu, 6 Apr 2023 13:25:37 -0400 Subject: [PATCH] optimization for when maskAllText is false #1096 --- packages/rrweb-snapshot/src/snapshot.ts | 45 ++++++++++++++++++------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/packages/rrweb-snapshot/src/snapshot.ts b/packages/rrweb-snapshot/src/snapshot.ts index c06806aad4..f2053d2245 100644 --- a/packages/rrweb-snapshot/src/snapshot.ts +++ b/packages/rrweb-snapshot/src/snapshot.ts @@ -363,21 +363,40 @@ export function needMaskingText( : node.parentElement; if (el === null) return false; - const unmaskDistance = distanceToMatch( - el, - createMatchPredicate(unmaskTextClass, unmaskTextSelector), - ); - let maskDistance = -1; - if (maskAllText && unmaskDistance < 0) { - return true; - } + let unmaskDistance = -1; - maskDistance = distanceToMatch( - el, - createMatchPredicate(maskTextClass, maskTextSelector), - unmaskDistance >= 0 ? unmaskDistance : Infinity, - ); + if (maskAllText) { + unmaskDistance = distanceToMatch( + el, + createMatchPredicate(unmaskTextClass, unmaskTextSelector), + ); + + if (unmaskDistance < 0) { + return true; + } + + maskDistance = distanceToMatch( + el, + createMatchPredicate(maskTextClass, maskTextSelector), + unmaskDistance >= 0 ? unmaskDistance : Infinity, + ); + } else { + maskDistance = distanceToMatch( + el, + createMatchPredicate(maskTextClass, maskTextSelector), + ); + + if (maskDistance < 0) { + return false; + } + + unmaskDistance = distanceToMatch( + el, + createMatchPredicate(unmaskTextClass, unmaskTextSelector), + maskDistance >= 0 ? maskDistance : Infinity, + ); + } return maskDistance >= 0 ? unmaskDistance >= 0