An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
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", () => {
require("../../stage/protyle/js/lute/lute.min.js");
for (const value of ["", "plain text", "&<>\"'", "& ' "", "</textarea><img src=\"x\">",
"中文、日本語、😀\n\t'quoted'"]) {
assert.equal(escapeHtmlTextAndAttr(value), Lute.EscapeHTMLStr(value));
}
assert.equal(escapeHtmlTextAndAttr("&<>\"'"), "&<>"'");
});
it("preserves Lute string conversion for non-string values", () => {
require("../../stage/protyle/js/lute/lute.min.js");
for (const value of [undefined, null, 0, 123, NaN, false, true, ["<&", 1],
{toString: () => '<"&>'}]) {
assert.equal(escapeHtmlTextAndAttr(value), Reflect.apply(Lute.EscapeHTMLStr, Lute, [value]));
}
});
it("preserves lone surrogates while escaping HTML characters", () => {
assert.equal(escapeHtmlTextAndAttr("\ud800&<\udc00>\"'"), "\ud800&<\udc00>"'");
assert.equal(escapeHtmlTextAndAttr("\ud800"), "\ud800");
assert.equal(escapeHtmlTextAndAttr("\udc00"), "\udc00");
});
it("escapes text around the single-pass length boundary and long text", () => {
const text = "<<tag>\"'\\\ud800";
for (const length of [511, 512, 513, 8192]) {
const prefix = "x".repeat(length - text.length);
assert.equal(escapeHtmlTextAndAttr(prefix + text), prefix + "&lt;<tag>"'\\\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) + "<&>"");
assert.equal(conversions, 1);
});
});
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 short and long aria-label text with the inner attribute level", () => {
assert.equal(escapeAriaLabel(""), "");
assert.equal(escapeAriaLabel("<a<b>'c\"d"), "&lt;a&lt;b>'c"d");
const text = "<<tag>\"'\\\ud800";
for (const length of [511, 512, 513, 8192]) {
const prefix = "x".repeat(length - text.length);
assert.equal(escapeAriaLabel(prefix + text), prefix + "&lt;&lt;tag>"'\\\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", () => {
it("removes search highlights without decoding escaped block names", () => {
assert.equal(stripSearchMark("<mark>xw</mark>"), "xw");
assert.equal(stripSearchMark("<mark><mark>xw</mark></mark>"), "<mark>xw</mark>");
});
});
describe("escape search highlight", () => {
it("preserves search highlight tags", () => {
assert.equal(escapeSearchHighlight("<mark><mark>xw</mark></mark>"), "<mark><mark>xw</mark></mark>");
});
it("escapes raw HTML tags except mark", () => {
assert.equal(escapeSearchHighlight('<img src="x" onerror="alert(1)">'), '<img src="x" onerror="alert(1)">');
assert.equal(escapeSearchHighlight("<script>alert(1)</script>"), "<script>alert(1)</script>");
assert.equal(escapeSearchHighlight('<mark onclick="alert(1)">x</mark>'), '<mark onclick="alert(1)">x</mark>');
});
});