Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Preserve literal title escapes in panel tooltips https://github.com/siyuan-note/siyuan/issues/20842

Daniel67d133483357
diff --git a/app/src/dialog/tooltipTitles.test.ts b/app/src/dialog/tooltipTitles.test.ts
new file mode 100644
index 0000000000..5ad3131e11
--- /dev/null
+++ b/app/src/dialog/tooltipTitles.test.ts
@@ -0,0 +1,116 @@
+import * as assert from "node:assert/strict";
+import {execFile} from "node:child_process";
+import {mkdtempSync, readFileSync, rmSync, writeFileSync} from "node:fs";
+import {tmpdir} from "node:os";
+import * as path from "node:path";
+import {test} from "node:test";
+import {promisify} from "node:util";
+import * as ts from "typescript";
+
+const browserCases = (sources: Record<string, string>) => {
+ const check: typeof assert = require("node:assert/strict");
+ document.body.innerHTML = "<style>#carrier{width:200px}span{display:inline-block;min-height:20px}</style>" +
+ '<div id="carrier"></div><div id="tooltip" class="fn__none"></div>';
+ window.siyuan = {languages: {findInDoc: "${x}/${y}", _kernel: {16: "Untitled"}}, notebooks: []} as unknown as typeof window.siyuan;
+ window.DOMPurify = {sanitize: (value: string) => value} as typeof window.DOMPurify;
+ let mobile = false;
+ let hPath = "";
+ const modules: Record<string, any> = {};
+ const load = (name: string) => {
+ const exports: Record<string, unknown> = {};
+ new Function("exports", "require", "Lute", sources[name])(exports, (id: string) => {
+ const key = id.split("/").pop();
+ if (modules[key]) return modules[key];
+ return {isMobile: () => mobile, isInIOS: () => false, emitToPlugins() {}, forEachPluginSubscriber() {},
+ getIconByType: () => "iconFile", getTreeItemTailHTML: () => "", getAttr: () => "",
+ unicode2Emoji: () => "", getNotebookName: () => "", getDisplayName: (value: string) => value,
+ getArticle() {}, fetchPost: (_url: string, _data: unknown, callback: (value: unknown) => void) => callback({
+ data: {pageCount: 1, blocks: [{id: "doc", rootID: "doc", type: "d", hPath, content: "", ial: {}}]},
+ })};
+ }, {BlockDOM2Content: (value: string) => value});
+ modules[name] = exports;
+ return exports;
+ };
+ load("escape");
+ load("hoverInput");
+ const tooltip = load("tooltip") as typeof import("./tooltip");
+ const treeModule = load("Tree") as unknown as {Tree: {prototype: object}};
+ const tree = Object.create(treeModule.Tree.prototype);
+ tree.titleTooltipPosition = "parentE";
+ const search = load("search") as {render: (config: {hPath: string}) => string};
+ const unRef = load("unRef") as typeof import("../search/unRef");
+ const carrier = document.getElementById("carrier");
+ const tip = document.getElementById("tooltip");
+ tooltip.initTooltips();
+ for (const title of ["A%2FB", "A%3Ctag%3E.sy", "100%25/Incomplete%2"]) {
+ hPath = title;
+ const fragments = [
+ tree.genHTML([{id: "doc", type: "backlink", depth: 0, name: title, hPath}]),
+ tree.genHTML([{id: "doc", type: "outline", depth: 0, name: title}]),
+ tree.genBlockHTML([{id: "doc", depth: 0, content: title}], true, "outline"),
+ search.render({hPath}),
+ ];
+ carrier.innerHTML = '<div id="searchUnRefResult"></div><div id="searchUnRefList"></div>' +
+ '<div data-type="unRefPrevious"></div><div data-type="unRefNext"></div><div class="fn__loading"></div>';
+ unRef.getUnRefList(carrier, {protyle: {element: document.createElement("div")}} as unknown as import("../protyle").Protyle);
+ fragments.push(carrier.querySelector("#searchUnRefList").innerHTML);
+ for (const fragment of fragments) {
+ carrier.innerHTML = fragment;
+ const target = carrier.querySelector(".ariaLabel");
+ check.equal(target.getAttribute("data-tooltip-encoding"), "none");
+ check.equal(target.getAttribute("aria-label"), title);
+ target.dispatchEvent(new PointerEvent("pointerover", {bubbles: true, pointerType: "mouse"}));
+ target.dispatchEvent(new MouseEvent("mouseover", {bubbles: true}));
+ check.equal(tip.textContent, title, fragment);
+ check.equal(tip.querySelector("tag"), null);
+ }
+ mobile = true;
+ carrier.innerHTML = tree.genHTML([{id: "doc", type: "backlink", depth: 0, name: title, hPath}]);
+ check.equal(carrier.querySelector(".ariaLabel").getAttribute("aria-label"), title);
+ mobile = false;
+ }
+ carrier.innerHTML = '<span class="ariaLabel" aria-label="A%2FB">Legacy encoded tooltip</span>';
+ carrier.firstElementChild.dispatchEvent(new MouseEvent("mouseover", {bubbles: true}));
+ check.equal(tip.textContent, "A/B");
+ return "Title tooltip cases passed";
+};
+
+test("document and heading tooltips preserve percent escapes across trees and search", {
+ skip: process.platform === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY,
+ timeout: 30000,
+}, async () => {
+ const sources: Record<string, string> = {};
+ const compile = (source: string) => ts.transpileModule(source, {
+ compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2021},
+ }).outputText;
+ for (const file of ["util/escape", "util/hoverInput", "dialog/tooltip", "util/Tree", "search/unRef"]) {
+ sources[file.split("/").pop()] = compile(readFileSync(`src/${file}.ts`, "utf8"));
+ }
+ const searchSource = readFileSync("src/search/util.ts", "utf8");
+ const start = searchSource.indexOf('<span id="searchPathInput"');
+ const end = searchSource.indexOf("</span>", start) + "</span>".length;
+ sources.search = compile('const {escapeAriaLabel, escapeHtml} = require("escape"); exports.render = config => `' +
+ searchSource.slice(start, end) + "`;");
+ const temporary = mkdtempSync(path.join(tmpdir(), "siyuan-title-tooltip-test-"));
+ const script = path.join(temporary, "run.cjs");
+ writeFileSync(script, `const {app,BrowserWindow}=require("electron");
+app.setPath("userData",${JSON.stringify(path.join(temporary, "profile"))});
+app.commandLine.appendSwitch("disable-gpu");app.whenReady().then(async()=>{
+const win=new BrowserWindow({show:false,width:800,height:600,webPreferences:{nodeIntegration:true,contextIsolation:false,offscreen:true}});
+try {await win.loadURL("data:text/html,<html><body></body></html>");
+console.log(await win.webContents.executeJavaScript(${JSON.stringify("const __name = value => value; (() => {try {return (" +
+ browserCases.toString() + ")(" + JSON.stringify(sources) + ");} catch (error) {return error.stack;}})()")}));win.destroy();app.exit(0);
+}catch(e){console.error(e);win.destroy();app.exit(1);}});`, "utf8");
+ const env = {...process.env};
+ delete env.ELECTRON_RUN_AS_NODE;
+ try {
+ const result = await promisify(execFile)(require("electron") as unknown as string, [script],
+ {env, windowsHide: true, timeout: 25000});
+ assert.match(result.stdout, /Title tooltip cases passed/);
+ } finally {
+ if (path.dirname(path.resolve(temporary)) === path.resolve(tmpdir()) &&
+ path.basename(temporary).startsWith("siyuan-title-tooltip-test-")) {
+ rmSync(temporary, {recursive: true, force: true});
+ }
+ }
+});
diff --git a/app/src/search/unRef.ts b/app/src/search/unRef.ts
index 14c534b79b..c5ac020584 100644
--- a/app/src/search/unRef.ts
+++ b/app/src/search/unRef.ts
@@ -114,7 +114,7 @@ export const getUnRefList = (element: Element, edit: Protyle, page = 1) => {
${unicode2Emoji(item.ial.icon, "b3-list-item__graphic", true)}
<span class="b3-list-item__text">${item.content}</span>
${getAttr(item)}
-<span class="b3-list-item__meta b3-list-item__meta--ellipsis ariaLabel" aria-label="${escapeAriaLabel(title)}">${escapeLessThans(title)}</span>
+<span class="b3-list-item__meta b3-list-item__meta--ellipsis ariaLabel" data-tooltip-encoding="none" aria-label="${escapeAriaLabel(title)}">${escapeLessThans(title)}</span>
</div>`;
});
if (response.data.blocks.length > 0) {
diff --git a/app/src/search/util.ts b/app/src/search/util.ts
index d0e03a8492..2c9bd833d8 100644
--- a/app/src/search/util.ts
+++ b/app/src/search/util.ts
@@ -157,7 +157,7 @@ export const genSearch = (app: App, config: Config.IUILayoutTabSearchConfig, ele
<span id="searchResult" class="fn__flex-shrink ft__selectnone"></span>
<span class="fn__space"></span>
<span class="fn__flex-1${closeCB ? " resize__move" : ""}" style="min-height: 100%"></span>
- <span id="searchPathInput" data-position="9south" class="search__path ft__on-surface fn__flex-center ft__smaller fn__ellipsis ariaLabel" aria-label="${escapeAriaLabel(escapeHtml(escapeHtml(config.hPath)))}">
+ <span id="searchPathInput" data-position="9south" data-tooltip-encoding="none" class="search__path ft__on-surface fn__flex-center ft__smaller fn__ellipsis ariaLabel" aria-label="${escapeAriaLabel(escapeHtml(escapeHtml(config.hPath)))}">
${escapeHtml(config.hPath)}
<svg class="search__rmpath${config.hPath ? "" : " fn__none"}"><use xlink:href="#iconCloseRound"></use></svg>
</span>
diff --git a/app/src/util/Tree.ts b/app/src/util/Tree.ts
index 50c29579d6..a84370f175 100644
--- a/app/src/util/Tree.ts
+++ b/app/src/util/Tree.ts
@@ -95,10 +95,10 @@ export class Tree {
} else if (item.type === "tag") {
iconHTML = '<svg class="b3-list-item__graphic"><use xlink:href="#iconTag"></use></svg>';
} else if (item.type === "backlink") {
- titleTip = ` aria-label="${escapeAriaLabel(item.hPath)}"`;
+ titleTip = ` data-tooltip-encoding="none" aria-label="${escapeAriaLabel(item.hPath)}"`;
iconHTML = `<svg class="b3-list-item__graphic popover__block" data-id="${item.id}"><use xlink:href="#${getIconByType(item.nodeType, item.subType)}"></use></svg>`;
} else if (item.type === "outline") {
- titleTip = ` aria-label="${escapeAriaLabel(Lute.BlockDOM2Content(item.name))}"`;
+ titleTip = ` data-tooltip-encoding="none" aria-label="${escapeAriaLabel(Lute.BlockDOM2Content(item.name))}"`;
iconHTML = `<svg class="b3-list-item__graphic popover__block" data-id="${item.id}" style="height: ${isM ? 16 : 22}px;width: 16px;"><use xlink:href="#${getIconByType(item.nodeType, item.subType)}"></use></svg>`;
}
let countHTML = "";
@@ -191,7 +191,7 @@ data-def-path="${item.defPath}">
</span>
${iconHTML}
${numberHTML}
- <span class="b3-list-item__text ariaLabel" data-position="${this.titleTooltipPosition}" ${type === "outline" ? ' aria-label="' + escapeAriaLabel(Lute.BlockDOM2Content(item.content)) + '"' : ""}>${item.content}</span>
+ <span class="b3-list-item__text ariaLabel" data-position="${this.titleTooltipPosition}" ${type === "outline" ? ' data-tooltip-encoding="none" aria-label="' + escapeAriaLabel(Lute.BlockDOM2Content(item.content)) + '"' : ""}>${item.content}</span>
${getTreeItemTailHTML(countHTML, this.blockExtHTML || "", isM)}
</li>`;
if (item.children && item.children.length > 0) {