Skip to content

Commit

Permalink
fix: Protect against matches() being undefined (#154)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
billyvg authored Jan 10, 2024
1 parent 2edc45b commit 70b74a7
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions packages/rrweb-snapshot/src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
}

Expand Down

0 comments on commit 70b74a7

Please sign in to comment.