From 60ccfe79408f7b37be054c0b130a77fb71428151 Mon Sep 17 00:00:00 2001 From: Hakan Shehu Date: Mon, 2 Feb 2026 10:01:43 +0100 Subject: [PATCH] Fix internal link handling in editor (#315) --- packages/ui/src/editor/extensions/link.tsx | 23 +++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/packages/ui/src/editor/extensions/link.tsx b/packages/ui/src/editor/extensions/link.tsx index cdd1200a..7fb097e8 100644 --- a/packages/ui/src/editor/extensions/link.tsx +++ b/packages/ui/src/editor/extensions/link.tsx @@ -23,22 +23,27 @@ export const LinkMark = Link.extend({ props: { handleClick: (_, __, event) => { // Don't handle clicks on links that are created and handled by Tanstack Router + // Find the link element that is closest to the target - based on the original Tiptap link implementation let link: HTMLAnchorElement | null = null; if (event.target instanceof HTMLAnchorElement) { link = event.target; } else { - let a = event.target as HTMLElement; - const els = []; - - while (a.nodeName !== 'DIV') { - els.push(a); - a = a.parentNode as HTMLElement; + const target = event.target as HTMLElement | null; + if (!target) { + return false; + } + + const root = this.editor.view.dom; + + // Tntentionally limit the lookup to the editor root. + // Using tag names like DIV as boundaries breaks with custom NodeViews, + link = target.closest('a'); + + if (link && !root.contains(link)) { + link = null; } - link = els.find( - (value) => value.nodeName === 'A' - ) as HTMLAnchorElement; } if (!link) {