Skip to content

Commit 9dc2b2b

Browse files
committed
fix(cdk/platform): account for composedPath error during event replay (#33409)
Calling `event.composedPath` during event replay throws an error. These changes add a `try/catch` around it so it doesn't show up for users. Fixes #33386. (cherry picked from commit 10db3e3)
1 parent 766b7ac commit 9dc2b2b

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

src/cdk/platform/features/shadow-dom.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,14 @@ export function _getFocusedElementPierceShadowDom(): HTMLElement | null {
5757

5858
/** Gets the target of an event while accounting for Shadow DOM. */
5959
export function _getEventTarget<T extends EventTarget>(event: Event): T | null {
60-
// If an event is bound outside the Shadow DOM, the `event.target` will
61-
// point to the shadow root so we have to use `composedPath` instead.
62-
return (event.composedPath ? event.composedPath()[0] : event.target) as T | null;
60+
// If an event is bound outside the Shadow DOM, the `event.target` will point to the shadow root
61+
// so we have to use `composedPath` instead. Note that `composedPath` can throw if it's called
62+
// during event replay (see #33386).
63+
// TODO(crisbeto): it seems like `preventDefault` throws during replay as well.
64+
if (event.composedPath) {
65+
try {
66+
return event.composedPath()[0] as T | null;
67+
} catch {}
68+
}
69+
return event.target as T | null;
6370
}

0 commit comments

Comments
 (0)