From c16328bb6dc96b5c330015ac560cf95920b9c7b2 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Sun, 13 Mar 2022 02:30:00 +0330 Subject: [PATCH] fix(router): trigger-click error when tagName undefined --- package/router/src/trigger-click.ts | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/package/router/src/trigger-click.ts b/package/router/src/trigger-click.ts index afdf73494..57c79b915 100644 --- a/package/router/src/trigger-click.ts +++ b/package/router/src/trigger-click.ts @@ -31,23 +31,19 @@ export const clickTrigger = { } // find the element that the click is at (or within) - let anchor = event + const anchor = event .composedPath() - .filter( - (eventTarget) => (eventTarget as HTMLElement).tagName.toLowerCase() === 'a', - )[0] as HTMLAnchorElement | null; - - while (anchor != null && anchor.tagName.toLowerCase() !== 'a') { - anchor = anchor.parentNode as HTMLAnchorElement | null; - } + .find( + (target) => (target as HTMLElement)?.tagName?.toLowerCase() === 'a', + ) as HTMLAnchorElement | undefined; if ( // ignore if the anchor is not found. anchor == null || // ignore if the anchor is not an element. - anchor.tagName.toLowerCase() !== 'a' || + anchor.tagName?.toLowerCase() !== 'a' || // ignore if the element has a non-default target. - (anchor.target !== '' && anchor.target.toLowerCase() !== '_self' ) || + (anchor.target !== '' && anchor.target?.toLowerCase() !== '_self' ) || // ignore if the element has a download attribute. anchor.hasAttribute('download') || // ignore if the element has a rel attribute.