Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/tab bar: use custom tooltip for tabs
Adorian Doran6d4a2deaf926
diff --git a/apps/client/src/services/tab_title.spec.ts b/apps/client/src/services/tab_title.spec.tsindex 143607998f..2d4a9fd097 100644--- a/apps/client/src/services/tab_title.spec.ts+++ b/apps/client/src/services/tab_title.spec.ts@@ -42,9 +42,46 @@ describe("buildTabTitle", () => {});it("handles an empty split list", () => {- const { segments, tooltip } = buildTabTitle([], "New tab");+ const { segments, tooltip, tooltipHtml } = buildTabTitle([], "New tab");expect(segments).toEqual([]);expect(tooltip).toBe("");+ expect(tooltipHtml).toBe("");+ });+});++describe("buildTabTitle tooltipHtml", () => {+ it("wraps the active segment in <strong> and joins with the separator", () => {+ const { tooltipHtml } = buildTabTitle(+ [+ { title: "Inbox", active: false },+ { title: "Tasks", active: true }+ ],+ "New tab"+ );++ expect(tooltipHtml).toBe(`Inbox${TAB_TITLE_SEPARATOR}<strong>Tasks</strong>`);+ });++ it("escapes HTML in note titles to prevent injection (active and inactive)", () => {+ const { tooltipHtml } = buildTabTitle(+ [+ { title: `<img src=x onerror=alert(1)>`, active: false },+ { title: `<script>alert("xss")</script>`, active: true }+ ],+ "New tab"+ );++ expect(tooltipHtml).toBe(+ `<img src=x onerror=alert(1)>${TAB_TITLE_SEPARATOR}<strong><script>alert("xss")</script></strong>`+ );+ expect(tooltipHtml).not.toContain("<img");+ expect(tooltipHtml).not.toContain("<script>");+ });++ it("escapes ampersands before other entities", () => {+ const { tooltipHtml } = buildTabTitle([{ title: "A & B <c>", active: false }], "New tab");++ expect(tooltipHtml).toBe("A & B <c>");});});diff --git a/apps/client/src/services/tab_title.ts b/apps/client/src/services/tab_title.tsindex 149bc9c0c2..e61ff88139 100644--- a/apps/client/src/services/tab_title.ts+++ b/apps/client/src/services/tab_title.ts@@ -10,14 +10,27 @@ export interface TabTitleSegment {active: boolean;}+/** Escapes HTML special characters so note titles can be safely embedded in the tooltip markup. */+function escapeHtml(text: string): string {+ return text+ .replace(/&/g, "&")+ .replace(/</g, "<")+ .replace(/>/g, ">")+ .replace(/"/g, """)+ .replace(/'/g, "'");+}+/**- * Builds the per-split segments and the plain tooltip string for a tab title. Each split contributes- * one segment; an empty/untitled split falls back to `emptyLabel` so the title stays 1:1 with the panes.+ * Builds the per-split segments and the tooltip strings for a tab title. Each split contributes one+ * segment; an empty/untitled split falls back to `emptyLabel` so the title stays 1:1 with the panes.+ *+ * `tooltip` is plain text; `tooltipHtml` escapes every title (untrusted) and wraps the active split in+ * trusted `<strong>` so it stands out — only the surrounding markup is trusted, never the titles.*/export function buildTabTitle(splits: { title: string | null | undefined; active: boolean }[],emptyLabel: string-): { segments: TabTitleSegment[]; tooltip: string } {+): { segments: TabTitleSegment[]; tooltip: string; tooltipHtml: string } {const segments = splits.map((split) => ({text: split.title || emptyLabel,active: split.active@@ -25,6 +38,12 @@ export function buildTabTitle(return {segments,- tooltip: segments.map((segment) => segment.text).join(TAB_TITLE_SEPARATOR)+ tooltip: segments.map((segment) => segment.text).join(TAB_TITLE_SEPARATOR),+ tooltipHtml: segments+ .map((segment) => {+ const escaped = escapeHtml(segment.text);+ return segment.active ? `<strong>${escaped}</strong>` : escaped;+ })+ .join(TAB_TITLE_SEPARATOR)};}diff --git a/apps/client/src/widgets/tab_row.ts b/apps/client/src/widgets/tab_row.tsindex 943742566d..7685d4d19d 100644--- a/apps/client/src/widgets/tab_row.ts+++ b/apps/client/src/widgets/tab_row.ts@@ -1,3 +1,4 @@+import { Tooltip } from "bootstrap";import Draggabilly, { type MoveVector } from "draggabilly";import appContext, { type CommandListenerData, type CommandNames, type EventData } from "../components/app_context.js";@@ -33,8 +34,8 @@ const TAB_TPL = `<div class="note-tab-drag-handle"></div><div class="note-tab-icon"></div><div class="note-tab-title"></div>- <div class="note-tab-pin-indicator bx bx-pin" title="${t("tab_row.unpin_tab")}"></div>- <div class="note-tab-close bx bx-x" title="${t("tab_row.close_tab")}"></div>+ <div class="note-tab-pin-indicator bx bx-pin" aria-label="${t("tab_row.unpin_tab")}"></div>+ <div class="note-tab-close bx bx-x" aria-label="${t("tab_row.close_tab")}"></div></div></div>`;@@ -588,6 +589,21 @@ export default class TabRowWidget extends BasicWidget {this.setVisibility();this.setTabCloseEvent($tab);this.updateTitle($tab, t("tab_row.new_tab"));++ // Trilium-styled tooltip instead of the native one; reads the live composite title each time.+ // Attach to the wrapper, not .note-tab — the latter has `pointer-events: none`, which makes+ // Bootstrap's hover tracking misfire (tooltip shows then instantly hides).+ new Tooltip($tab.find(".note-tab-wrapper")[0], {+ // titles are pre-escaped in buildTabTitle (only the active <strong> wrapper is trusted);+ // Bootstrap's own sanitizer stays on as a second layer+ html: true,+ title: () => $tab.attr("data-tab-title") || "",+ trigger: "hover",+ placement: "bottom",+ container: "body",+ delay: { show: 500, hide: 0 }+ });+this.cleanUpPreviouslyDraggedTabs();this.layoutTabs();this.setupDraggabilly();@@ -656,6 +672,10 @@ export default class TabRowWidget extends BasicWidget {const tabEl = this.getTabById(ntxId)[0];if (tabEl) {+ const wrapperEl = tabEl.querySelector(".note-tab-wrapper");+ if (wrapperEl) {+ Tooltip.getInstance(wrapperEl)?.dispose();+ }tabEl.parentNode?.removeChild(tabEl);this.cleanUpPreviouslyDraggedTabs();this.layoutTabs();@@ -669,7 +689,7 @@ export default class TabRowWidget extends BasicWidget {}updateTitle($tab: JQuery<HTMLElement>, title: string) {- $tab.attr("title", title);+ $tab.attr("data-tab-title", title);$tab.find(".note-tab-title").text(title);}@@ -966,9 +986,9 @@ export default class TabRowWidget extends BasicWidget {})));- const { segments, tooltip } = buildTabTitle(splits, t("tab_row.new_tab"));+ const { segments, tooltipHtml } = buildTabTitle(splits, t("tab_row.new_tab"));- $tab.attr("title", tooltip);+ $tab.attr("data-tab-title", tooltipHtml);const $title = $tab.find(".note-tab-title").empty();segments.forEach((segment, idx) => {