Skip to content

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

:bug: Preserve plain search scope titles in tooltips https://github.com/siyuan-note/siyuan/issues/20844

Danielfbc7db0ef188
diff --git a/app/src/dialog/tooltipTitles.test.ts b/app/src/dialog/tooltipTitles.test.ts
index 462e5a97e7..8ff467cc6d 100644
--- a/app/src/dialog/tooltipTitles.test.ts
+++ b/app/src/dialog/tooltipTitles.test.ts
@@ -51,13 +51,13 @@ const browserCases = async (sources: Record<string, string>) => {
const escape = modules.escape as typeof import("../util/escape");
for (const title of ["A%2FB", "A%3Ctag%3E.sy", "100%25/Incomplete%2", "A&copy;B", "A&amp;B",
"A&lt;tag&gt;", 'A<tag> & "quotes"', "A&#47;B", "A&#x2f;B"]) {
- // hPath is derived from escaped IAL attributes; outline content is BlockDOM.
+ // Search result/tree paths are rendered HTML; search scopes from getFullHPathByID are plain text.
hPath = escape.escapeHtmlTextAndAttr(title);
const fragments = [
tree.genHTML([{id: "doc", type: "backlink", depth: 0, name: title, hPath}]),
tree.genHTML([{id: "doc", type: "outline", depth: 0, name: hPath}]),
tree.genBlockHTML([{id: "doc", depth: 0, content: hPath}], true, "outline"),
- search.render({hPath}),
+ search.render({hPath: title}),
...search.results(hPath),
];
carrier.innerHTML = '<div id="searchUnRefResult"></div><div id="searchUnRefList"></div>' +
@@ -75,7 +75,7 @@ const browserCases = async (sources: Record<string, string>) => {
check.equal(tip.querySelector("tag"), null);
if (target.id === "searchPathInput") {
await searchPath.refreshCurrentSearchPath({element: carrier, config: {idPath: ["doc"]} as Config.IUILayoutTabSearchConfig,
- resolveHPath: async () => hPath});
+ resolveHPath: async () => title});
target.dispatchEvent(new MouseEvent("mouseover", {bubbles: true}));
check.equal(tip.textContent, title, "refreshed search path matches the initial tooltip");
}
@@ -109,7 +109,7 @@ test("document and heading tooltips preserve percent escapes and entities across
/<span class="b3-list-item__(?:text|meta)[^\n]*aria-label="\$\{escapeHtmlTextAndAttr\(title\)}"[^\n]*<\/span>/g,
)];
assert.equal(resultTemplates.length, 2);
- sources.search = compile('const {escapeHtml, escapeHtmlTextAndAttr} = require("escape"); exports.render = config => `' +
+ sources.search = compile('const {escapeHtml, escapeHtmlTextAndAttr, escapeTooltipTextAttr} = require("escape"); exports.render = config => `' +
searchSource.slice(start, end) + "`; exports.results = title => [" +
resultTemplates.map(match => "`" + match[0] + "`").join(",") + "];");
const temporary = mkdtempSync(path.join(tmpdir(), "siyuan-title-tooltip-test-"));
diff --git a/app/src/search/path.ts b/app/src/search/path.ts
index 2fd5e09de1..2e9d798709 100644
--- a/app/src/search/path.ts
+++ b/app/src/search/path.ts
@@ -1,5 +1,5 @@
import {fetchSyncPost} from "../util/fetch";
-import {escapeHtml} from "../util/escape";
+import {escapeHtml, escapeHtmlTextAndAttr} from "../util/escape";
import {
getSearchPathID,
isCurrentSearchPath,
@@ -52,8 +52,8 @@ const renderSearchPath = (element: Element, hPath: string) => {
const pathElement = element.querySelector("#searchPathInput");
if (pathElement) {
pathElement.innerHTML = `${escapeHtml(hPath)}<svg class="search__rmpath"><use xlink:href="#iconCloseRound"></use></svg>`;
- // hPath 已包含标题属性的实体转义,setAttribute 不再经过 HTML 属性解析。
- pathElement.setAttribute("aria-label", hPath);
+ // 搜索范围接口返回纯文本,动态赋值仅需保护提示框的 HTML 解析。
+ pathElement.setAttribute("aria-label", escapeHtmlTextAndAttr(hPath));
}
};
diff --git a/app/src/search/util.ts b/app/src/search/util.ts
index 4202d8ae7f..7295b73c9d 100644
--- a/app/src/search/util.ts
+++ b/app/src/search/util.ts
@@ -9,7 +9,7 @@ import {getAllModels} from "../layout/getAll";
import * as path from "path";
/// #endif
import {Constants} from "../constants";
-import {escapeHtml, escapeHtmlTextAndAttr} from "../util/escape";
+import {escapeHtml, escapeHtmlTextAndAttr, escapeTooltipTextAttr} from "../util/escape";
import {fetchPost, fetchSyncPost} from "../util/fetch";
import {openFile, openFileById} from "../editor/util";
import {showMessage} from "../dialog/message";
@@ -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" data-tooltip-encoding="none" class="search__path ft__on-surface fn__flex-center ft__smaller fn__ellipsis ariaLabel" aria-label="${escapeHtmlTextAndAttr(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="${escapeTooltipTextAttr(config.hPath)}">
${escapeHtml(config.hPath)}
<svg class="search__rmpath${config.hPath ? "" : " fn__none"}"><use xlink:href="#iconCloseRound"></use></svg>
</span>
@@ -549,7 +549,7 @@ export const genSearch = (app: App, config: Config.IUILayoutTabSearchConfig, ele
config.hPath = hPathList.join(" ");
config.page = 1;
searchPathInputElement.innerHTML = `${escapeHtml(config.hPath)}<svg class="search__rmpath"><use xlink:href="#iconCloseRound"></use></svg>`;
- searchPathInputElement.setAttribute("aria-label", config.hPath);
+ searchPathInputElement.setAttribute("aria-label", escapeHtmlTextAndAttr(config.hPath));
const includeElement = element.querySelector("#searchInclude");
includeElement.firstElementChild.classList.add("ft__primary");
if (enableIncludeChild) {
@@ -1174,7 +1174,7 @@ export const updateConfig = (element: Element, item: Config.IUILayoutTabSearchCo
const searchPathInputElement = element.querySelector("#searchPathInput");
if (runtimeConfig.hPath) {
searchPathInputElement.innerHTML = `${escapeHtml(runtimeConfig.hPath)}<svg class="search__rmpath"><use xlink:href="#iconCloseRound"></use></svg>`;
- searchPathInputElement.setAttribute("aria-label", runtimeConfig.hPath);
+ searchPathInputElement.setAttribute("aria-label", escapeHtmlTextAndAttr(runtimeConfig.hPath));
} else {
searchPathInputElement.innerHTML = "";
searchPathInputElement.setAttribute("aria-label", "");
diff --git a/app/src/util/escape.ts b/app/src/util/escape.ts
index 3a74fd104e..447298b524 100644
--- a/app/src/util/escape.ts
+++ b/app/src/util/escape.ts
@@ -4,7 +4,8 @@ export const escapeHtmlTextAndAttr = (html: unknown) => {
};
// 纯文本写入 HTML 模板中的提示属性,需分别保护属性解析及提示框的 innerHTML 解析。
-// 已转义的 hPath 只需 escapeHtmlTextAndAttr;setAttribute 接受的提示 HTML 不再转义属性层。
+// 已渲染为 HTML 的路径只需 escapeHtmlTextAndAttr;纯文本搜索范围仍需两层。
+// setAttribute 接受的提示 HTML 不再转义属性层。
export const escapeTooltipTextAttr = (text: string) => escapeHtmlTextAndAttr(escapeHtmlTextAndAttr(text));
export const escapeHtml = (html: string) => {
diff --git a/kernel/model/structured_titles_test.go b/kernel/model/structured_titles_test.go
index 21ab572848..d606798ea0 100644
--- a/kernel/model/structured_titles_test.go
+++ b/kernel/model/structured_titles_test.go
@@ -16,6 +16,7 @@ import (
"github.com/siyuan-note/siyuan/kernel/conf"
"github.com/siyuan-note/siyuan/kernel/documentaddressing"
"github.com/siyuan-note/siyuan/kernel/filesys"
+ "github.com/siyuan-note/siyuan/kernel/sql"
"github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util"
)
@@ -54,6 +55,18 @@ func TestStructuredTitlesCreationAndIdentity(t *testing.T) {
return ret
}
literal := create(nil, "A/B")
+ t.Run("plain scope paths and rendered result paths", func(t *testing.T) {
+ const title = "A&amp;B<tag>"
+ doc := create(nil, title)
+ hPath, err := GetFullHPathByID(doc.ID)
+ if err != nil || hPath != boxConf.Name+"/"+title {
+ t.Fatalf("search scope path is not literal text: %q, %v", hPath, err)
+ }
+ result := fromSQLBlock(&sql.Block{ID: doc.ID, RootID: doc.ID, Box: box.ID, Type: "d", HPath: "/" + title}, "", 0)
+ if result.HPath != util.EscapeHTML("/"+title) {
+ t.Fatalf("search result path is not escaped HTML: %q", result.HPath)
+ }
+ })
parent := create(nil, "A")
child := create(&parent.ID, "B")
if literal.DisplayPath != child.DisplayPath || literal.Depth != 1 || child.Depth != 2 {