From 70b74a72915466c3808486003e1fd06aa660a661 Mon Sep 17 00:00:00 2001 From: Billy Vong Date: Wed, 10 Jan 2024 13:16:10 -0330 Subject: [PATCH] fix: Protect against `matches()` being undefined (#154) try/catch to protect against cases where `el.matches` is not a function (not sure exact cause, but possibly `el` is undefined, or not an Element type) --- packages/rrweb-snapshot/src/snapshot.ts | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/packages/rrweb-snapshot/src/snapshot.ts b/packages/rrweb-snapshot/src/snapshot.ts index 7477f18a6d..67583f8555 100644 --- a/packages/rrweb-snapshot/src/snapshot.ts +++ b/packages/rrweb-snapshot/src/snapshot.ts @@ -350,17 +350,21 @@ export function createMatchPredicate( const el = node as HTMLElement; if (el === null) return false; - if (className) { - if (typeof className === 'string') { - if (el.matches(`.${className}`)) return true; - } else if (elementClassMatchesRegex(el, className)) { - return true; + try { + if (className) { + if (typeof className === 'string') { + if (el.matches(`.${className}`)) return true; + } else if (elementClassMatchesRegex(el, className)) { + return true; + } } - } - if (selector && el.matches(selector)) return true; + if (selector && el.matches(selector)) return true; - return false; + return false; + } catch { + return false; + } }; }