Skip to content

Build your personal knowledge base with Trilium Notes

fix(client): don't open a target=_blank link on right-click (regression of #3971)

Elian Doran1ff82b4195ed
diff --git a/apps/client/src/services/link.spec.ts b/apps/client/src/services/link.spec.ts
index dec7012c8e..74451f8254 100644
--- a/apps/client/src/services/link.spec.ts
+++ b/apps/client/src/services/link.spec.ts
@@ -777,6 +777,22 @@ describe("goToLinkExt", () => {
expect(triggerCommand).not.toHaveBeenCalled();
});
+ it("opens nothing on a right click on a target=_blank link, leaving it to the context menu", () => {
+ const openSpy = vi.spyOn(window, "open").mockImplementation(() => null);
+ // Browsers fire auxclick for the right button as well as the middle one.
+ const rightClick = () => ({ type: "auxclick", which: 3, preventDefault: vi.fn(), stopPropagation: vi.fn() }) as any;
+
+ goToLinkExt(rightClick(), "https://example.com", $("<a>").attr({ href: "https://example.com", target: "_blank" }));
+ goToLinkExt(rightClick(), "#root/aaaaaaaaaaaa", $("<a>").attr({ href: "#root/aaaaaaaaaaaa", target: "_blank" }));
+ expect(openSpy).not.toHaveBeenCalled();
+ expect(openTabWithNoteWithHoisting).not.toHaveBeenCalled();
+
+ // A left click on the same link still opens it in a new tab.
+ goToLinkExt(leftClick(), "#root/aaaaaaaaaaaa", $("<a>").attr({ href: "#root/aaaaaaaaaaaa", target: "_blank" }));
+ expect(openTabWithNoteWithHoisting).toHaveBeenCalledWith("root/aaaaaaaaaaaa", expect.objectContaining({ activate: true }));
+ openSpy.mockRestore();
+ });
+
it("does not handle a non-root hash anchor that does not resolve to an element", () => {
const $link = $("<a>").attr("href", "#missing-anchor");
// no .ck-content ancestor containing #missing-anchor => handleAnchor returns false
diff --git a/apps/client/src/services/link.ts b/apps/client/src/services/link.ts
index cf11b6816b..62815ad3b1 100644
--- a/apps/client/src/services/link.ts
+++ b/apps/client/src/services/link.ts
@@ -552,7 +552,7 @@ export function goToLinkExt(evt: MouseEvent | JQuery.ClickEvent | JQuery.MouseDo
const isMiddleClick = evt && "which" in evt && evt.which === 2;
const targetIsBlank = ($link?.attr("target") === "_blank");
const isDoubleClick = isLeftClick && evt?.type === "dblclick";
- const openInNewTab = (isLeftClick && ctrlKey) || isDoubleClick || isMiddleClick || targetIsBlank;
+ const openInNewTab = (isLeftClick && (ctrlKey || targetIsBlank)) || isDoubleClick || isMiddleClick;
const activate = (isLeftClick && ctrlKey && shiftKey) || (isMiddleClick && shiftKey);
const openInNewWindow = isLeftClick && evt?.shiftKey && !ctrlKey;