An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:recycle: Consolidate escaping into single-pass character classes
Jeffrey Chen3b534ed2357f
diff --git a/app/src/util/escape.test.ts b/app/src/util/escape.test.tsindex 34bfff67c5..1b3408c497 100644--- a/app/src/util/escape.test.ts+++ b/app/src/util/escape.test.ts@@ -1,6 +1,9 @@import {describe, it} from "node:test";import * as assert from "node:assert/strict";-import {escapeHtmlTextAndAttr, escapeSearchHighlight, stripSearchMark} from "./escape";+import {+ escapeAriaLabel, escapeAttr, escapeHtml, escapeHtmlTextAndAttr, escapeMarkdownPlainText,+ escapeSearchHighlight, stripSearchMark,+} from "./escape";describe("escape HTML strings", () => {it("matches the bundled Lute engine for text and double-quoted attributes", () => {@@ -27,6 +30,31 @@ describe("escape HTML strings", () => {});});+describe("escape HTML fragments and attributes", () => {+ it("escapes only ampersands and less-than signs for HTML fragments", () => {+ assert.equal(escapeHtml(""), "");+ assert.equal(escapeHtml("a&b<c>d\"e'f"), "a&b<c>d\"e'f");+ });++ it("escapes both quote kinds for attributes", () => {+ assert.equal(escapeAttr(""), "");+ assert.equal(escapeAttr("&<>\"'"), "&<>"'");+ });++ it("escapes aria-label text with the inner attribute level", () => {+ assert.equal(escapeAriaLabel(""), "");+ assert.equal(escapeAriaLabel("<a<b>'c\"d"), "&lt;a&lt;b>'c"d");+ });+});++describe("escape Markdown plain text", () => {+ it("prefixes every escapable marker with a backslash", () => {+ assert.equal(escapeMarkdownPlainText("plain text"), "plain text");+ assert.equal(escapeMarkdownPlainText("\\*_[]!`<>&~{}()=#$^|."),+ "\\\\\\*\\_\\[\\]\\!\\`\\<\\>\\&\\~\\{\\}\\(\\)\\=\\#\\$\\^\\|\\.");+ });+});+describe("strip search mark", () => {it("removes search highlights without decoding escaped block names", () => {assert.equal(stripSearchMark("<mark>xw</mark>"), "xw");diff --git a/app/src/util/escape.ts b/app/src/util/escape.tsindex 447298b524..f2a6e68445 100644--- a/app/src/util/escape.ts+++ b/app/src/util/escape.ts@@ -1,6 +1,17 @@// 转义 HTML 文本和双引号属性值,保持 Lute.EscapeHTMLStr 的字符替换规则。export const escapeHtmlTextAndAttr = (html: unknown) => {- return String(html).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);+ return String(html).replace(/[&<>"]/g, (char) => {+ switch (char) {+ case "&":+ return "&";+ case "<":+ return "<";+ case ">":+ return ">";+ default:+ return """;+ }+ });};// 纯文本写入 HTML 模板中的提示属性,需分别保护属性解析及提示框的 innerHTML 解析。@@ -8,6 +19,8 @@ export const escapeHtmlTextAndAttr = (html: unknown) => {// setAttribute 接受的提示 HTML 不再转义属性层。export const escapeTooltipTextAttr = (text: string) => escapeHtmlTextAndAttr(escapeHtmlTextAndAttr(text));+// 两个字符的转义保持逐字面量替换:V8 对单字面量正则有专用扫描路径,+// 字符类的逐字符成本约为其四倍,长正文上单次字符类扫描反而更慢。export const escapeHtml = (html: string) => {if (!html) {return html;@@ -17,27 +30,7 @@ export const escapeHtml = (html: string) => {// 将纯文本中的 Markdown 标记转义,保留编辑器重新解析时的字面内容。export const escapeMarkdownPlainText = (text: string) => {- return text.replace(/\\/g, "\\\\")- .replace(/\*/g, "\\*")- .replace(/_/g, "\\_")- .replace(/\[/g, "\\[")- .replace(/]/g, "\\]")- .replace(/!/g, "\\!")- .replace(/`/g, "\\`")- .replace(/</g, "\\<")- .replace(/>/g, "\\>")- .replace(/&/g, "\\&")- .replace(/~/g, "\\~")- .replace(/\{/g, "\\{")- .replace(/}/g, "\\}")- .replace(/\(/g, "\\(")- .replace(/\)/g, "\\)")- .replace(/=/g, "\\=")- .replace(/#/g, "\\#")- .replace(/\$/g, "\\$")- .replace(/\^/g, "\\^")- .replace(/\|/g, "\\|")- .replace(/\./g, "\\.");+ return text.replace(/[\\*_[\]!`<>&~{}()=#$^|.]/g, "\\$&");};export const stripSearchMark = (html: string) => {@@ -53,6 +46,7 @@ export const escapeLessThans = (html: string) => {return html.replace(/</g, "<");};+// 理由同 escapeHtml:两个字符的转义保持逐字面量替换。export const escapeAttr = (html: string) => {if (!html) {return html;@@ -64,8 +58,16 @@ export const escapeAriaLabel = (html: string) => {if (!html) {return html;}- return html.replace(/"/g, """).replace(/'/g, "'")- .replace(/</g, "&lt;").replace(/</g, "&lt;");+ return html.replace(/<|["'<]/g, (match) => {+ switch (match) {+ case '"':+ return """;+ case "'":+ return "'";+ default:+ return "&lt;";+ }+ });};export const decodeHTML = (html: string) => {