Skip to content

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

:zap: Preserve long-text escaping performance https://github.com/siyuan-note/siyuan/pull/21060

Daniel61e57ed10517
diff --git a/app/src/util/escape.test.ts b/app/src/util/escape.test.ts
index 1b3408c497..90e43abed1 100644
--- a/app/src/util/escape.test.ts
+++ b/app/src/util/escape.test.ts
@@ -28,6 +28,26 @@ describe("escape HTML strings", () => {
assert.equal(escapeHtmlTextAndAttr("\ud800"), "\ud800");
assert.equal(escapeHtmlTextAndAttr("\udc00"), "\udc00");
});
+
+ it("escapes text around the single-pass length boundary and long text", () => {
+ const text = "&lt;<tag>\"'\\\ud800";
+ for (const length of [511, 512, 513, 8192]) {
+ const prefix = "x".repeat(length - text.length);
+ assert.equal(escapeHtmlTextAndAttr(prefix + text), prefix + "&amp;lt;&lt;tag&gt;&quot;'\\\ud800");
+ }
+ });
+
+ it("converts long non-string input only once", () => {
+ let conversions = 0;
+ const value = {
+ toString: () => {
+ conversions++;
+ return "x".repeat(1024) + "<&>\"";
+ },
+ };
+ assert.equal(escapeHtmlTextAndAttr(value), "x".repeat(1024) + "&lt;&amp;&gt;&quot;");
+ assert.equal(conversions, 1);
+ });
});
describe("escape HTML fragments and attributes", () => {
@@ -41,18 +61,34 @@ describe("escape HTML fragments and attributes", () => {
assert.equal(escapeAttr("&<>\"'"), "&<>&quot;&apos;");
});
- it("escapes aria-label text with the inner attribute level", () => {
+ it("escapes short and long aria-label text with the inner attribute level", () => {
assert.equal(escapeAriaLabel(""), "");
assert.equal(escapeAriaLabel("&lt;a<b>'c\"d"), "&amp;lt;a&amp;lt;b>&apos;c&quot;d");
+ const text = "&lt;<tag>\"'\\\ud800";
+ for (const length of [511, 512, 513, 8192]) {
+ const prefix = "x".repeat(length - text.length);
+ assert.equal(escapeAriaLabel(prefix + text), prefix + "&amp;lt;&amp;lt;tag>&quot;&apos;\\\ud800");
+ }
});
});
describe("escape Markdown plain text", () => {
it("prefixes every escapable marker with a backslash", () => {
+ assert.equal(escapeMarkdownPlainText(""), "");
assert.equal(escapeMarkdownPlainText("plain text"), "plain text");
assert.equal(escapeMarkdownPlainText("\\*_[]!`<>&~{}()=#$^|."),
"\\\\\\*\\_\\[\\]\\!\\`\\<\\>\\&\\~\\{\\}\\(\\)\\=\\#\\$\\^\\|\\.");
});
+
+ it("escapes text around the single-pass length boundary and dense long text", () => {
+ const text = "\\*_[]!`<>&~{}()=#$^|.";
+ const escaped = "\\\\\\*\\_\\[\\]\\!\\`\\<\\>\\&\\~\\{\\}\\(\\)\\=\\#\\$\\^\\|\\.";
+ for (const length of [511, 512, 513, 8192]) {
+ const prefix = "x".repeat(length - text.length);
+ assert.equal(escapeMarkdownPlainText(prefix + text), prefix + escaped);
+ }
+ assert.equal(escapeMarkdownPlainText(text.repeat(400)), escaped.repeat(400));
+ });
});
describe("strip search mark", () => {
diff --git a/app/src/util/escape.ts b/app/src/util/escape.ts
index f2a6e68445..20adb0ad39 100644
--- a/app/src/util/escape.ts
+++ b/app/src/util/escape.ts
@@ -1,6 +1,13 @@
+// 字符类扫描适合短文本,长文本使用 V8 更高效的字面量扫描路径。
+const SINGLE_PASS_ESCAPE_MAX_LENGTH = 512;
+
// 转义 HTML 文本和双引号属性值,保持 Lute.EscapeHTMLStr 的字符替换规则。
export const escapeHtmlTextAndAttr = (html: unknown) => {
- return String(html).replace(/[&<>"]/g, (char) => {
+ const text = String(html);
+ if (text.length > SINGLE_PASS_ESCAPE_MAX_LENGTH) {
+ return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
+ }
+ return text.replace(/[&<>"]/g, (char) => {
switch (char) {
case "&":
return "&amp;";
@@ -19,8 +26,7 @@ export const escapeHtmlTextAndAttr = (html: unknown) => {
// setAttribute 接受的提示 HTML 不再转义属性层。
export const escapeTooltipTextAttr = (text: string) => escapeHtmlTextAndAttr(escapeHtmlTextAndAttr(text));
-// 两个字符的转义保持逐字面量替换:V8 对单字面量正则有专用扫描路径,
-// 字符类的逐字符成本约为其四倍,长正文上单次字符类扫描反而更慢。
+// V8 的字面量正则扫描更适合长正文,两个字符的转义分别处理。
export const escapeHtml = (html: string) => {
if (!html) {
return html;
@@ -30,6 +36,29 @@ export const escapeHtml = (html: string) => {
// 将纯文本中的 Markdown 标记转义,保留编辑器重新解析时的字面内容。
export const escapeMarkdownPlainText = (text: string) => {
+ if (text.length > SINGLE_PASS_ESCAPE_MAX_LENGTH) {
+ 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, "\\$&");
};
@@ -58,6 +87,10 @@ export const escapeAriaLabel = (html: string) => {
if (!html) {
return html;
}
+ if (html.length > SINGLE_PASS_ESCAPE_MAX_LENGTH) {
+ return html.replace(/"/g, "&quot;").replace(/'/g, "&apos;")
+ .replace(/</g, "&amp;lt;").replace(/&lt;/g, "&amp;lt;");
+ }
return html.replace(/&lt;|["'<]/g, (match) => {
switch (match) {
case '"':