fast icon indicating copy to clipboard operation
fast copied to clipboard

`DefaultLinkHandler.findClosestAnchor` falls back to `event.target`, hijacking non-anchor clicks (e.g. SVG `<use>`) while ignoring SVG `<a>` links

Open codeCraft-Ritik opened this issue 1 month ago • 0 comments


🐛 Bug Report

DefaultLinkHandler.prototype.findClosestAnchor falls back to returning event.target when no HTMLAnchorElement exists in event.composedPath(). Consequently, any click on non-anchor elements possessing a relative href attribute (such as SVG <use href="icons.svg#icon"> or custom elements) is mistakenly hijacked as router navigation, while clicks on SVG <a href="..."> links are ignored due to case-sensitive tag name checking.

💻 Repro or Code Sample

import { customElement, FASTElement, html } from "@microsoft/fast-element";
import { DefaultLinkHandler, Route } from "@microsoft/fast-router";

// Connect the default link handler
const linkHandler = new DefaultLinkHandler();
linkHandler.connect();

// Sample Component using standard SVG symbol icon references
@customElement({
    name: "icon-button",
    template: html`
        <button type="button" @click="${() => console.log('Button clicked')}">
            <svg width="24" height="24">
                <use href="assets/icons.svg#chevron-right"></use>
            </svg>
            Click Me
        </button>
    `
})
export class IconButton extends FASTElement {}

document.body.appendChild(document.createElement("icon-button"));

// Listen for Route path updates
console.log("Current path before click:", Route.path.current);

// User clicks on the SVG icon inside the button:
const useElement = document.querySelector("use")!;
useElement.dispatchEvent(new MouseEvent("click", {
    bubbles: true,
    composed: true,
    cancelable: true,
    which: 1,
}));

console.log("Current path after icon click:", Route.path.current);

🤔 Expected Behavior

  1. Clicking an SVG icon (<use href="...">) or button inside a custom element should NOT trigger single-page routing or alter the browser URL.
  2. Clicking a valid SVG link (<svg><a href="/dashboard"><circle .../></a></svg>) should be intercepted and routed.

😯 Current Behavior

  1. Clicking <use href="assets/icons.svg#chevron-right"> causes DefaultLinkHandler to call event.preventDefault(), push "assets/icons.svg#chevron-right" into browser history via Route.path.push(), and break the button's native behavior.
  2. Clicking an SVG <a> tag fails to route because current.tagName is "a" (lowercase in SVG), which fails the strict current.tagName === "A" equality check.

Root Cause Analysis

In packages/fast-router/src/links.ts, lines 75–87 define findClosestAnchor:

private findClosestAnchor(event: MouseEvent): Element {
    const path: EventTarget[] = event.composedPath();

    for (let i = 0, ii = path.length; i < ii; ++i) {
        const current = path[i] as any;

        if (current.tagName === "A") {
            return current;
        }
    }

    return event.target as any; // <--- Line 86: BUG
}

And lines 45–73:

const target = this.findClosestAnchor(event);

if (!target || !this.targetIsThisWindow(target)) {
    return info;
}
...
const href = target.getAttribute("href");
info.anchor = target;
info.href = href;

const leftButtonClicked = event.which === 1;
const isRelative = href && !(href.charAt(0) === "#" || /^[a-z]+:/i.test(href));

info.shouldHandleEvent = leftButtonClicked && !!isRelative;

When no <a> tag is present:

  1. findClosestAnchor returns event.target instead of null.
  2. The null-check if (!target) never triggers.
  3. If event.target is an SVG <use href="...">, <image href="...">, or custom component with an href attribute, target.getAttribute("href") returns a non-null relative path.
  4. info.shouldHandleEvent evaluates to true, and line 25 cancels the event and triggers route navigation to the asset file.
  5. In SVG documents, SVGElement.tagName returns lowercase "a", so current.tagName === "A" is false for genuine SVG links.

💁 Possible Solution

  1. Return null when no anchor is found in event.composedPath().
  2. Check for both HTML anchor elements and SVG anchor elements (current.tagName.toUpperCase() === "A" or current instanceof HTMLAnchorElement || current instanceof SVGAElement).
private findClosestAnchor(event: MouseEvent): Element | null {
    const path: EventTarget[] = event.composedPath ? event.composedPath() : [];

    for (let i = 0, ii = path.length; i < ii; ++i) {
        const current = path[i] as Element;

        if (
            current &&
            current.tagName &&
            current.tagName.toUpperCase() === "A" &&
            current.hasAttribute("href")
        ) {
            return current;
        }
    }

    return null;
}

And in getEventInfo:

const target = this.findClosestAnchor(event);

if (!target || !this.targetIsThisWindow(target)) {
    return info;
}

🔦 Context

Modern web applications make extensive use of SVG sprite systems (<svg><use href="...#icon"></use></svg>), SVG graphics, and custom component design systems. Having the router global click handler hijack SVG icon clicks and navigate the browser to sprite asset paths disrupts entire web applications and causes spurious 404 navigation errors.

🌍 Your Environment

  • OS & Device: Windows 11 / macOS / Linux on PC
  • Browser: Microsoft Edge, Google Chrome, Mozilla Firefox, Apple Safari
  • Version: @microsoft/fast-router 1.8.0 / @microsoft/fast 1.8.0

codeCraft-Ritik avatar Oct 12 '26 04:10 codeCraft-Ritik