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.