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 * as assert from "node:assert/strict";
import {describe, it} from "node:test";
import {cloneAVCellValueSnapshot} from "./cellValue";
import {getInlineFontFamilyStyle} from "../../toolbar/fontFamilyCore";
import {
AV_RICH_TEXT_EDITOR_ALLOWED_ATTRIBUTES,
AV_RICH_TEXT_EDITOR_ALLOWED_TAGS,
AV_RICH_TEXT_PREVIEW_ALLOWED_ATTRIBUTES,
AV_RICH_TEXT_PREVIEW_ALLOWED_TAGS,
AV_RICH_TEXT_PREVIEW_TEXT_ONLY_TAGS,
configureAVRichTextLute,
createAVPlainTextEditValue,
createAVPlainTextValue,
createAVRichTextStyleBackslashEncoding,
createAVRichTextValue,
getAVTextCopyContent,
getAVTextPlainContent,
getAVTextSource,
getAVRichTextSafeURL,
isAVRichTextEditorAllowedTag,
isAVRichTextExecutableCodeLanguage,
projectAVRichTextPlainBlocks,
protectAVRichTextKramdownStyleEntities,
restoreAVRichTextStyleEntities,
sanitizeAVRichTextInlineMemoContent,
sanitizeAVRichTextInlineStyle,
} from "./richTextValue";
const hasDOM = typeof globalThis.document?.createElement === "function";
describe("attribute view text source compatibility", () => {
it("preserves escaped inline text through bundled Lute storage and preview conversions", () => {
if (typeof Lute === "undefined") {
require("../../../../stage/protyle/js/lute/lute.min.js");
}
const lute = configureAVRichTextLute(Lute.New());
lute.SetTextMark(true);
lute.SetHTMLTag2TextMark(true);
lute.SetKramdownIAL(true);
lute.SetSpin(true);
lute.SetProtyleWYSIWYG(true);
for (const mark of ["em", "strong", "s", "mark", "sup", "sub", "u", "kbd", "code", "text", "a em"]) {
const text = '<vitae> & < "quote"';
let dom = '<div data-node-id="20260918120000-abcdefg" data-type="NodeParagraph">' +
`<div contenteditable="true"><span data-type="${mark}" data-href="https://example.com">` +
'<vitae> & &lt; "quote"</span></div></div>';
for (let round = 0; round < 3; round++) {
const markdown = lute.BlockDOM2Md(dom);
dom = lute.Md2BlockDOM(markdown);
assert.equal(dom.includes("<vitae>"), false, `${mark}: raw HTML after round ${round}`);
assert.equal(lute.BlockDOM2Content(dom).replaceAll("\u200b", ""), text, mark);
dom = lute.SpinBlockDOM(dom);
assert.equal(lute.BlockDOM2Content(dom).replaceAll("\u200b", ""), text, mark);
}
}
});
it("keeps legacy Markdown-looking content literal", () => {
const literal = "**literal** ((20240101000000-abcdefg \"reference\")) $x$";
const value: IAVCellValue = {type: "text", text: {content: literal}};
assert.deepEqual(getAVTextSource(value), {kind: "plain", content: literal});
assert.equal(getAVTextPlainContent(value), literal);
});
it("uses only a valid rich envelope as the rich source", () => {
const value: IAVCellValue = {
type: "text",
text: {
content: "Projected text",
rich: {spec: 1, format: "kramdown", content: "**Rich** source"},
},
};
assert.deepEqual(getAVTextSource(value), {kind: "rich", content: "**Rich** source"});
assert.equal(getAVTextPlainContent(value), "Projected text");
assert.deepEqual(getAVTextSource({
type: "text",
text: {content: "Explicitly plain", rich: null},
}), {kind: "plain", content: "Explicitly plain"});
assert.deepEqual(getAVTextSource({
type: "text",
text: {
content: "Unsupported envelope",
rich: {spec: 2, format: "kramdown", content: "**not active**"},
},
} as unknown as IAVCellValue), {kind: "plain", content: "Unsupported envelope"});
});
it("keeps the plain projection authoritative for copy and export consumers", () => {
const value: IAVCellValue = {
type: "text",
text: {
content: "first\nsecond",
rich: {spec: 1, format: "kramdown", content: "first\n\nsecond"},
},
};
assert.equal(getAVTextPlainContent(value), "first\nsecond");
value.text.content = "\n first\nsecond \n";
assert.equal(getAVTextCopyContent(value), "first\nsecond");
});
it("identifies only executable code fence languages", () => {
for (const language of [
"abc", "echarts", "flowchart", "graphviz", "infographic", "mermaid", "plantuml",
]) {
assert.equal(isAVRichTextExecutableCodeLanguage(language), true);
}
assert.equal(isAVRichTextExecutableCodeLanguage("GraphViz options"), true);
assert.equal(isAVRichTextExecutableCodeLanguage("javascript"), false);
assert.equal(isAVRichTextExecutableCodeLanguage("mindmap"), false);
assert.equal(isAVRichTextExecutableCodeLanguage("go options"), false);
});
it("encodes only staged text style delimiters for Kramdown", () => {
const code = String.raw`<span data-type="text" style="font-family: 'Code\Font';">literal</span>`;
const style = String.raw`font-family: var(--b3-font-family-emoji-reset), 'Semi>` + "`" + String.raw`; \'Quoted\' \\ 字体', ` +
"var(--b3-font-family-editor), var(--b3-font-family);";
const firstSentinel = "\uE000av-rich-text-style-0\uF8FF";
const encoding = createAVRichTextStyleBackslashEncoding(code + firstSentinel);
const markdown = encoding.encodeMarkdown(`${encoding.protectStyle(style)}\n${code}`);
assert.notEqual(encoding.sentinel, firstSentinel);
assert.equal((code + firstSentinel).includes(encoding.sentinel), false);
assert.equal(markdown, `${style.replaceAll("\\", "\").replaceAll("`", "`")}\n${code}`);
assert.equal(markdown.includes(encoding.sentinel), false);
});
it("protects text span IAL style entities without treating arbitrary dollars as math", () => {
const styled = '<span title="a>b" data-type="strong text">styled</span>' +
'{: custom="a}>b" style="font-family: \'Semi>; \\'Quoted\\' & 字体\';" ' +
'other="\"}';
const nested = '<span data-type="strong"><span data-type="text">nested</span>' +
'{: style="font-family: \'A\B\';"}</span>';
const plain = '<span data-type="strong">plain</span>{: style="font-family: \'A\B\';"}';
const fake = '<span data-type="text">fake</span>{: style="font-family: \'A\B\';"}';
const source = [
styled,
nested,
plain,
"```html",
fake,
"```",
"~~~~",
fake,
"~~~~",
`\`${fake}\``,
`\`\`${fake}\`\``,
`$${fake}$`,
`$$${fake}$$`,
"$$",
fake,
"$$",
`\`cross\r\n${fake}\r\nline\``,
"prose \",
`\`unclosed ${styled}`,
].join("\r\n");
const protectedStyle = protectAVRichTextKramdownStyleEntities(source);
const restoredSource = protectedStyle.protections.reduce((content, protection) =>
content.replaceAll(protection.token, protection.encoded), protectedStyle.content);
const decoded = restoreAVRichTextStyleEntities(protectedStyle.content, protectedStyle.protections);
assert.equal(restoredSource, source);
assert.deepEqual(protectedStyle.protections.map((item) => item.encoded), [
">", "\", "\", "&", "\", "\", "\", ">", "\", "\", "&",
]);
assert.match(decoded, /font-family: 'Semi>; \\'Quoted\\' & 字体';/);
assert.equal(protectedStyle.content.includes(plain), true);
assert.equal(protectedStyle.content.includes("```html\r\n" + fake + "\r\n```"), true);
assert.equal(protectedStyle.content.includes("~~~~\r\n" + fake + "\r\n~~~~"), true);
assert.equal(protectedStyle.content.includes(`\`${fake}\``), true);
assert.equal(protectedStyle.content.includes(`\`\`${fake}\`\``), true);
assert.equal(protectedStyle.content.includes(`$${fake}$`), false);
assert.equal(protectedStyle.content.includes(`$$${fake}$$`), false);
assert.equal(protectedStyle.content.includes("$$\r\n" + fake + "\r\n$$"), true);
assert.equal(protectedStyle.content.includes(`\`cross\r\n${fake}\r\nline\``), true);
assert.equal(protectedStyle.content.includes("prose \"), true);
});
it("handles long malformed delimiter input in a single scan", () => {
const unmatchedRuns = Array.from({length: 512}, (_, index) => "`".repeat(index + 1) + "x").join("");
const occupiedSentinels = Array.from({length: 4096}, (_, index) =>
`\uE000av-rich-text-style-${index.toString(36)}\uF8FF`).join("");
const styled = '<span data-type="text">styled</span>' +
'{: style="font-family: \'A`B` \ 字体\';"}';
const source = "\\".repeat(65536) + unmatchedRuns + occupiedSentinels + styled;
const protectedStyle = protectAVRichTextKramdownStyleEntities(source);
const encoding = createAVRichTextStyleBackslashEncoding(occupiedSentinels);
assert.equal(protectedStyle.protections.length, 3);
assert.deepEqual(protectedStyle.protections.map((item) => item.encoded), ["`", "`", "\"]);
assert.equal(protectedStyle.content.includes(protectedStyle.protections[0].token), true);
assert.equal(encoding.sentinel, "\uE000av-rich-text-style-35s\uF8FF");
});
it("round-trips complex styles through the bundled Lute parser", () => {
if (typeof Lute === "undefined") {
require("../../../../stage/protyle/js/lute/lute.min.js");
}
const lute = Lute.New();
lute.SetTextMark(true);
lute.SetHTMLTag2TextMark(true);
lute.SetKramdownIAL(true);
lute.SetSpin(true);
lute.SetProtyleWYSIWYG(true);
lute.SetSanitize(true);
const fontFamily = getInlineFontFamilyStyle("A`B` Semi; 'Quoted' > \\ & {} 字体");
const style = `font-family: ${fontFamily};`;
const blockDOM = '<div data-type="NodeParagraph"><div contenteditable="true">' +
`<span data-type="text" style="${style}">font</span>` +
"</div></div>";
const writeEncoding = createAVRichTextStyleBackslashEncoding(blockDOM);
const protectedBlockDOM = blockDOM.replace(style, writeEncoding.protectStyle(style));
const persisted = writeEncoding.encodeMarkdown(lute.BlockDOM2Md(protectedBlockDOM).trim());
const source = `$5 ${persisted} $10`;
const protectedStyle = protectAVRichTextKramdownStyleEntities(source);
const parsed = lute.Md2BlockDOM(protectedStyle.content);
const restored = restoreAVRichTextStyleEntities(parsed, protectedStyle.protections);
assert.match(persisted, /\/);
assert.match(persisted, /`/);
assert.match(persisted, />/);
assert.equal(protectedStyle.protections.length > 0, true);
assert.match(restored, /\$5 /);
assert.match(restored, / \$10/);
assert.equal(restored.includes(`data-type="text" style="${style}"`), true);
assert.doesNotMatch(restored, /\{:\s*style=/);
});
it("keeps canonical multiline code spans literal through the bundled Lute parser", () => {
if (typeof Lute === "undefined") {
require("../../../../stage/protyle/js/lute/lute.min.js");
}
const lute = Lute.New();
lute.SetTextMark(true);
lute.SetHTMLTag2TextMark(true);
lute.SetKramdownIAL(true);
lute.SetSpin(true);
lute.SetProtyleWYSIWYG(true);
lute.SetSanitize(true);
const literal = "<span data-type="text">x</span>" +
"{: style="font-family: A&#92;B;"}";
const blockDOM = '<div data-type="NodeParagraph"><div contenteditable="true">' +
`<span data-type="code">start\n${literal}\nend</span>` +
"</div></div>";
const markdown = lute.BlockDOM2Md(blockDOM).trim();
const protectedStyle = protectAVRichTextKramdownStyleEntities(markdown);
const parsed = lute.Md2BlockDOM(protectedStyle.content);
assert.match(markdown, /start\n/);
assert.deepEqual(protectedStyle.protections, []);
assert.match(parsed, /data-type="code"/);
assert.match(parsed, /&#92;/);
});
it("projects block content with the same newline rules as the kernel", () => {
if (typeof Lute === "undefined") {
require("../../../../stage/protyle/js/lute/lute.min.js");
}
const lute = Lute.New();
lute.SetTextMark(true);
lute.SetHTMLTag2TextMark(true);
lute.SetKramdownIAL(true);
lute.SetSpin(true);
lute.SetProtyleWYSIWYG(true);
lute.SetSanitize(true);
const paragraph = lute.BlockDOM2Content(lute.Md2BlockDOM("first"));
const code = lute.BlockDOM2Content(lute.Md2BlockDOM("```txt\ncode\n\n```"));
const next = lute.BlockDOM2Content(lute.Md2BlockDOM("next"));
assert.equal(code, "code\n\n");
assert.equal(projectAVRichTextPlainBlocks([paragraph, code, next]), "first\ncode\nnext");
assert.equal(projectAVRichTextPlainBlocks([paragraph, ""]), "first");
assert.equal(projectAVRichTextPlainBlocks([], "fallback\n"), "fallback\n");
});
it("keeps image emoji aliases literal while preserving Unicode emoji", () => {
if (typeof Lute === "undefined") {
require("../../../../stage/protyle/js/lute/lute.min.js");
}
const lute = Lute.New();
lute.SetTextMark(true);
lute.SetHTMLTag2TextMark(true);
lute.SetKramdownIAL(true);
lute.SetSpin(true);
lute.SetProtyleWYSIWYG(true);
lute.SetSanitize(true);
configureAVRichTextLute(lute);
const markdown = ":siyuan: 😀";
const blockDOM = lute.Md2BlockDOM(markdown);
assert.equal(blockDOM.includes("<img"), false);
assert.match(blockDOM, /:siyuan: 😀/);
assert.equal(lute.BlockDOM2Content(blockDOM), markdown);
});
it("restores every fixed inline syntax switch on the isolated Lute", () => {
const values = new Map<PropertyKey, boolean>();
const lute = new Proxy({}, {
get: (_target, property) => (enabled: boolean) => values.set(property, enabled),
}) as Lute;
assert.equal(configureAVRichTextLute(lute), lute);
for (const property of [
"SetCustomBlock", "SetGitConflict", "SetInlineAsterisk", "SetInlineUnderscore",
"SetGFMStrikethrough", "SetSup", "SetSub", "SetTag", "SetInlineMath", "SetMark",
"SetFullWidthStrikethrough",
]) {
assert.equal(values.get(property), true, property);
}
for (const property of ["SetEmoji", "SetGFMStrikethrough1", "SetExportNormalizeTaskListMarker"]) {
assert.equal(values.get(property), false, property);
}
});
it("uses a strict active-content and interaction policy for previews", () => {
for (const tag of ["svg", "SVG", "use", "USE"]) {
assert.equal(isAVRichTextEditorAllowedTag(tag), true);
}
assert.equal(isAVRichTextEditorAllowedTag("script"), false);
for (const tag of ["h1", "h2", "h3", "h4", "h5", "h6"]) {
assert.equal(AV_RICH_TEXT_PREVIEW_ALLOWED_TAGS.includes(tag), true);
}
for (const tag of ["audio", "embed", "iframe", "img", "object", "script", "style", "video"]) {
assert.equal(AV_RICH_TEXT_PREVIEW_ALLOWED_TAGS.includes(tag), false);
assert.equal(AV_RICH_TEXT_EDITOR_ALLOWED_TAGS.includes(tag), false);
}
for (const attribute of ["onerror", "onclick", "src", "srcdoc", "xlink:href"]) {
assert.equal(AV_RICH_TEXT_PREVIEW_ALLOWED_ATTRIBUTES.includes(attribute), false);
if (attribute !== "xlink:href") {
assert.equal(AV_RICH_TEXT_EDITOR_ALLOWED_ATTRIBUTES.includes(attribute), false);
}
}
assert.deepEqual(AV_RICH_TEXT_PREVIEW_TEXT_ONLY_TAGS, ["sup"]);
const escapedMemo = "&lt;img src=x onerror=alert(1)&gt;memo</img>";
assert.equal(sanitizeAVRichTextInlineMemoContent(escapedMemo), escapedMemo);
assert.equal(sanitizeAVRichTextInlineMemoContent("2 < 3\tline\nnext\r\u200D\u200F"),
"2 < 3\tline\nnext\r\u200D\u200F");
assert.equal(sanitizeAVRichTextInlineMemoContent("heart <3"), "heart <3");
assert.equal(sanitizeAVRichTextInlineMemoContent("2 < 3"), "2 < 3");
assert.equal(sanitizeAVRichTextInlineMemoContent('<span title=">">memo</span>'), "");
assert.equal(sanitizeAVRichTextInlineMemoContent("<script"), "");
assert.equal(sanitizeAVRichTextInlineMemoContent("memo </span"), "");
assert.equal(sanitizeAVRichTextInlineMemoContent("memo <!--"), "");
assert.equal(sanitizeAVRichTextInlineMemoContent("memo <?xml"), "");
assert.equal(sanitizeAVRichTextInlineMemoContent("<script>"), "<script>");
assert.equal(sanitizeAVRichTextInlineMemoContent("memo\u0000content"), "");
assert.equal(sanitizeAVRichTextInlineMemoContent("memo\u007Fcontent"), "");
let deeplyEncodedMemo = "&";
for (let depth = 0; depth < 8; depth++) {
deeplyEncodedMemo = deeplyEncodedMemo.replace(/&/g, "&");
}
assert.equal(sanitizeAVRichTextInlineMemoContent(deeplyEncodedMemo), "");
for (const unsafe of [
"javascript:alert(1)",
"java\nscript:alert(1)",
"java\u200Bscript:alert(1)",
"javascript:alert(1)",
"javascript&colon;alert(1)",
"java%73cript:alert(1)",
"vbscript:msgbox(1)",
"data:text/html,<script>alert(1)</script>",
"file:///tmp/payload.html",
"blob:https://example.com/id",
"ftp://example.com/file",
"https:example.com",
"http:///path",
"siyuan:/blocks/20240101000000-abcdefg",
"mailto://example.com",
"mailto:/[email protected]",
"tel://12025550123",
"//",
"///path",
"/javascript:alert(1)",
"folder:name",
"assets/../payload.html",
"%zz",
"\\\\server\\payload.html",
" https://example.com",
]) {
assert.equal(getAVRichTextSafeURL(unsafe), "");
}
for (const safe of [
"https://example.com/a?q=v#f", "mailto:[email protected]", "tel:+12025550123",
"siyuan://blocks/20240101000000-abcdefg", "web+siyuan://blocks/20240101000000-abcdefg",
"../relative/note.md", "/relative/note.md", "#section", "?q=v", "//example.com/path",
"assets/file with space.pdf", "assets/percent%25.txt",
]) {
assert.equal(getAVRichTextSafeURL(safe), safe);
}
});
it("allows local link targets only with the table cell policy", () => {
for (const target of [
"D:\\基线测试\\测试文档1.docx", "D:/基线测试/测试文档1.docx", "D:\\目录\\",
"file:///D:\\基线测试\\测试文档1.docx", "file:///D:/基线测试/测试文档1.docx",
"file:///tmp/test%20document.txt", "file://server/share/document.txt",
"\\\\server\\share\\document.txt",
]) {
assert.equal(getAVRichTextSafeURL(target, true), target);
assert.equal(getAVRichTextSafeURL(target), "");
}
for (const target of [
"javascript:alert(1)", "javascript:alert(1)", "java%73cript:alert(1)",
"data:text/html,payload", "vbscript:payload", "file:relative.txt", "file://",
"file://user@server/share/document.txt", "file://server:123/share/document.txt",
"D:relative.txt", "D:\\file%0a.txt", "file:///tmp/file%00.txt",
"D:\\file\u200B.txt", "https:\\\\example.com\\document.txt", "\\\\server",
]) {
assert.equal(getAVRichTextSafeURL(target, true), "", target);
}
});
it("allows custom protocols only for explicit links and keeps dangerous schemes blocked", () => {
for (const target of [
"hook://file/EqhAm6xHS?p=YXJjaGl2ZXMvcHJvdmVuYW5jZQ==&n=2026%2D10%2D05",
"obsidian://open?vault=notes&file=book", "custom+app://action/path", "CUSTOM://action",
"ftp://example.com/file",
]) {
assert.equal(getAVRichTextSafeURL(target, false, true), target);
assert.equal(getAVRichTextSafeURL(target), "", "automatic resource policy must remain restricted");
}
for (const target of [
"javascript://host/payload", "JaVaScRiPt://host/payload", "vbscript://host/payload",
"data://host/payload", "blob://host/payload", "file://host/payload", "filesystem://host/payload",
"about://host/payload", "chrome://settings", "chrome-extension://id/page", "devtools://host/page",
"view-source://host/page", "resource://host/page", "moz-extension://id/page",
"java%73cript://host/payload", "java\u200Bscript://host/payload", "custom:payload",
"hook:/file/path", "hook://", "hook:payload", "hook://host/path%0a", "folder:name",
]) {
assert.equal(getAVRichTextSafeURL(target, false, true), "", target);
}
});
it("keeps only supported inline color declarations", () => {
for (const id of ["error", "warning", "info", "success"]) {
const style = `color: var(--b3-card-${id}-color); background-color: var(--b3-card-${id}-background);`;
assert.equal(sanitizeAVRichTextInlineStyle(style), style);
assert.equal(sanitizeAVRichTextInlineStyle(`color: var(--b3-card-${id}-background);`), "");
assert.equal(sanitizeAVRichTextInlineStyle(`background-color: var(--b3-card-${id}-color);`), "");
}
for (const value of ["var(--b3-card-danger-color)", "var(--b3-card-info-color, red)",
"var(--b3-inline-builtin-info-color, var(--b3-card-error-color))"]) {
assert.equal(sanitizeAVRichTextInlineStyle(`color: ${value};`), "");
}
assert.equal(sanitizeAVRichTextInlineStyle(
"background-color: var(--b3-font-background8); color:var(--b3-font-color2);"
), "color: var(--b3-font-color2); background-color: var(--b3-font-background8);");
assert.equal(sanitizeAVRichTextInlineStyle(
"color: var(--b3-inline-builtin-info-color, var(--b3-card-info-color)); " +
"background-color: var(--b3-inline-builtin-info-background-color, " +
"var(--b3-card-info-background));"
), "color: var(--b3-inline-builtin-info-color, var(--b3-card-info-color)); " +
"background-color: var(--b3-inline-builtin-info-background-color, " +
"var(--b3-card-info-background));");
assert.equal(sanitizeAVRichTextInlineStyle(
"color: var(--b3-inline-style-20260821120000-abcdefg-color, #AABBCC); " +
"background-color: var(--b3-inline-style-20260821120000-abcdefg-background-color, #112233);"
), "color: var(--b3-inline-style-20260821120000-abcdefg-color, #aabbcc); " +
"background-color: var(--b3-inline-style-20260821120000-abcdefg-background-color, #112233);");
assert.equal(sanitizeAVRichTextInlineStyle(
"color: red; background-image: url(https://example.com/pixel); " +
"background-color: var(--b3-font-background13);"
), "background-color: var(--b3-font-background13);");
assert.equal(sanitizeAVRichTextInlineStyle(
"color: var(--b3-font-color14); background-color: var(--b3-font-color1);"
), "");
assert.equal(sanitizeAVRichTextInlineStyle(
"color: var(--b3-font-color8, url(https://example.com/pixel));"
), "");
});
it("normalizes the complete inline font style set", () => {
const fontFamily = getInlineFontFamilyStyle("Semi; 'Quoted' > \\ 字体");
const style = [
"unicode-bidi: isolate",
`font-family: ${fontFamily}`,
"direction: rtl",
"font-size: .56em",
"-webkit-text-fill-color: transparent",
"-webkit-text-stroke: 0.2px var(--b3-theme-on-background)",
"text-shadow: 1px 1px var(--b3-theme-surface-lighter), " +
"2px 2px var(--b3-theme-surface-lighter), 3px 3px var(--b3-theme-surface-lighter), " +
"4px 4px var(--b3-theme-surface-lighter)",
"--b3-parent-background: var(--b3-font-background2)",
].join("; ") + ";";
assert.equal(sanitizeAVRichTextInlineStyle(style), [
"font-size: 0.56em;",
`font-family: ${fontFamily};`,
"-webkit-text-stroke: 0.2px var(--b3-theme-on-background);",
"-webkit-text-fill-color: transparent;",
"text-shadow: 1px 1px var(--b3-theme-surface-lighter), " +
"2px 2px var(--b3-theme-surface-lighter), 3px 3px var(--b3-theme-surface-lighter), " +
"4px 4px var(--b3-theme-surface-lighter);",
"direction: rtl;",
"unicode-bidi: isolate;",
].join(" "));
});
it("drops incomplete or non-canonical inline style declarations", () => {
const maximumFontFamily = getInlineFontFamilyStyle("字".repeat(256));
assert.equal(sanitizeAVRichTextInlineStyle(
`font-size: 72.00px; font-family: ${maximumFontFamily};`
), `font-size: 72px; font-family: ${maximumFontFamily};`);
assert.equal(sanitizeAVRichTextInlineStyle(
"color: var(--b3-font-color2); -webkit-text-stroke: 0.2px var(--b3-theme-on-background); " +
"direction: ltr; font-size: 8px; font-family: url(https://example.com/font);"
), "color: var(--b3-font-color2);");
assert.equal(sanitizeAVRichTextInlineStyle(
"-webkit-text-fill-color: transparent; unicode-bidi: isolate; font-size: 4.51em; " +
"text-shadow: 1px 1px red;"
), "");
assert.equal(sanitizeAVRichTextInlineStyle(
"font-family: var(--b3-font-family-emoji-reset), 'unterminated; color: var(--b3-font-color2);"
), "");
assert.equal(sanitizeAVRichTextInlineStyle(
`font-family: ${getInlineFontFamilyStyle("字".repeat(257))};`
), "");
});
});
describe("attribute view text value creation", () => {
it("creates a rich envelope without mutating the source snapshot", () => {
const source: IAVCellValue = {
id: "cell-id",
type: "text",
isDetached: true,
text: {content: "old", rich: null},
};
const created = createAVRichTextValue("**new**", "new", source);
assert.deepEqual(created, {
id: "cell-id",
type: "text",
isDetached: true,
text: {
content: "new",
rich: {spec: 1, format: "kramdown", content: "**new**"},
},
});
assert.deepEqual(source.text, {content: "old", rich: null});
});
it("supports omitted and explicit-null rich state for plain writes", () => {
assert.deepEqual(createAVPlainTextValue("plain"), {
type: "text",
text: {content: "plain"},
});
assert.deepEqual(createAVPlainTextValue("plain", undefined, true), {
type: "text",
text: {content: "plain", rich: null},
});
});
it("preserves template rich text only while its plain projection is unchanged", () => {
const source: IAVCellValue = {
type: "text",
text: {
content: "projected",
rich: {spec: 1, format: "kramdown", content: "**projected**"},
},
};
assert.deepEqual(createAVPlainTextEditValue("projected", source).text, source.text);
assert.deepEqual(createAVPlainTextEditValue("changed", source).text,
{content: "changed", rich: null});
assert.deepEqual(createAVPlainTextEditValue("legacy", {type: "text", text: {content: "old"}}).text,
{content: "legacy"});
assert.notEqual(createAVPlainTextEditValue("projected", source).text.rich, source.text.rich);
});
it("preserves rich data in independent undo snapshots and removes rendered HTML", () => {
const value: IAVCellValue = {
type: "text",
renderedContent: "<strong>preview</strong>",
text: {
content: "preview",
rich: {spec: 1, format: "kramdown", content: "**preview**"},
},
};
const snapshot = cloneAVCellValueSnapshot(value);
assert.equal(snapshot.renderedContent, undefined);
assert.deepEqual(snapshot.text, value.text);
snapshot.text!.rich!.content = "changed";
assert.equal(value.text!.rich!.content, "**preview**");
});
});
describe("attribute view rich text DOM policy", () => {
it("canonicalizes a single empty paragraph and preserves structural empty paragraphs", {
skip: hasDOM && typeof Lute !== "undefined" ? false :
"The Node test environment does not provide DOM and Lute globals",
}, async () => {
Object.assign(globalThis, {NODE_ENV: "test", SIYUAN_VERSION: "test"});
const richText = await import("./richText");
const empty = richText.serializeAVRichTextBlockDOM(
'<div data-type="NodeParagraph"><div contenteditable="true"></div></div>'
);
assert.deepEqual(empty, {blockDOM: "", markdown: "", plainText: ""});
for (const paragraphs of [["", ""], ["", "first", "", "", "last", ""]]) {
let dom = paragraphs.map(content => '<div data-type="NodeParagraph">' +
`<div contenteditable="true">${content}</div></div>`).join("");
let previous: string;
for (let round = 0; round < 3; round++) {
const serialized = richText.serializeAVRichTextBlockDOM(dom);
if (round > 0) {
assert.equal(serialized.markdown, previous);
}
previous = serialized.markdown;
dom = richText.getAVRichTextBlockDOM(serialized.markdown);
const template = document.createElement("template");
template.innerHTML = dom;
assert.deepEqual(Array.from(template.content.querySelectorAll(
'[data-type="NodeParagraph"] > [contenteditable="true"]'
)).map(element => element.textContent), paragraphs);
template.innerHTML = richText.getAVRichTextPreviewHTML(serialized.markdown);
assert.deepEqual(Array.from(template.content.querySelectorAll("p"))
.map(element => element.textContent), paragraphs);
assert.equal(template.content.querySelector("[id], [data-node-id]"), null);
}
}
});
it("preserves code content and prepares block and inline math for preview rendering", {
skip: hasDOM && typeof Lute !== "undefined" ? false :
"The Node test environment does not provide DOM and Lute globals",
}, async () => {
Object.assign(globalThis, {NODE_ENV: "test", SIYUAN_VERSION: "test"});
const richText = await import("./richText");
const {genRenderFrame} = await import("../util");
const template = document.createElement("template");
template.innerHTML = richText.getAVRichTextPreviewHTML([
"```go", "package main", "```", "", "$$", "x^2", "$$", "", "$a^2 + b^2$",
].join("\n"));
const code = template.content.querySelector("pre.code-block > code");
assert.equal(code?.textContent, "package main\n");
assert.equal(code.parentElement.dataset.language, "go");
assert.equal(template.content.querySelector(".protyle-action"), null);
const blockMath = template.content.querySelector<HTMLElement>('div[data-subtype="math"]');
assert.equal(blockMath?.dataset.content, "x^2");
assert.doesNotThrow(() => genRenderFrame(blockMath));
assert.ok(blockMath.firstElementChild.firstElementChild);
assert.equal(template.content.querySelector<HTMLElement>('span[data-subtype="math"]')?.dataset.content,
"a^2 + b^2");
});
it("preserves supported headings and inline marks while dropping unsupported blocks", {
skip: hasDOM ? false : "The Node test environment does not provide a DOM implementation",
}, async () => {
Object.assign(globalThis, {NODE_ENV: "test", SIYUAN_VERSION: "test"});
const richText = await import("./richText");
const blockDOM = [
'<div data-type="NodeHeading" data-subtype="h2"><div contenteditable="true">heading</div></div>',
'<div data-type="NodeHeading" data-subtype="h7"><div contenteditable="true">invalid</div></div>',
'<div data-type="NodeParagraph"><div contenteditable="true">',
'<span data-type="tag">tag</span>',
'<span data-type="file-annotation-ref">annotation</span>',
'<span data-type="inline-memo" data-inline-memo-content="memo">memo</span>',
'<span data-type="strong" style="color: var(--b3-font-color2);">unstyled strong</span>',
'<span data-type="strong text" style="color: var(--b3-font-color2);">styled strong</span>',
"</div></div>",
'<div data-type="NodeImage"><span class="img">image</span></div>',
'<div data-type="NodeTable">table</div>',
'<div data-type="NodeHTMLBlock">html</div>',
'<div data-type="NodeBlockQueryEmbed">embed</div>',
'<div data-type="NodeCodeBlock" data-subtype="mermaid" class="render-node">diagram</div>',
'<div data-type="NodeCodeBlock" data-subtype="GraphViz options" class="code-block">graph</div>',
].join("");
const sanitized = richText.sanitizeAVRichTextBlockDOM(blockDOM);
assert.match(sanitized, /data-type="NodeHeading"[^>]*data-subtype="h2"[^>]*class="h2"/);
assert.doesNotMatch(sanitized, /data-subtype="h7"/);
assert.match(sanitized, /data-type="tag"/);
assert.match(sanitized, /data-type="file-annotation-ref"/);
assert.match(sanitized, /data-type="inline-memo"/);
assert.match(sanitized, /data-type="strong">unstyled strong/);
assert.match(sanitized, /data-type="strong text" style="color: var\(--b3-font-color2\);">styled strong/);
for (const type of ["NodeImage", "NodeTable", "NodeHTMLBlock", "NodeBlockQueryEmbed"]) {
assert.doesNotMatch(sanitized, new RegExp(`data-type="${type}"`));
}
assert.doesNotMatch(sanitized, /data-subtype="mermaid"/);
assert.doesNotMatch(sanitized, /data-subtype="GraphViz options"/);
});
it("does not expose generated structure IDs and keeps rendered links identifiable", {
skip: hasDOM && typeof Lute !== "undefined" ? false :
"The Node test environment does not provide DOM and Lute globals",
}, async () => {
Object.assign(globalThis, {NODE_ENV: "test", SIYUAN_VERSION: "test"});
const richText = await import("./richText");
const html = richText.getAVRichTextPreviewHTML("[SiYuan](https://b3log.org/siyuan)");
assert.doesNotMatch(html, /\sid="[^"]+"/);
assert.match(html, /data-type="[^"]*a[^"]*"/);
assert.match(html, /data-href="https:\/\/b3log\.org\/siyuan"/);
});
it("preserves disabled task checkboxes and nested mixed lists in previews", {
skip: hasDOM && typeof Lute !== "undefined" ? false :
"The Node test environment does not provide DOM and Lute globals",
}, async () => {
Object.assign(globalThis, {NODE_ENV: "test", SIYUAN_VERSION: "test"});
const richText = await import("./richText");
const template = document.createElement("template");
template.innerHTML = richText.getAVRichTextPreviewHTML([
"- [ ] pending",
"- [x] completed",
" - [ ] nested pending",
" - ordinary bullet",
"",
"1. numbered item",
].join("\n"));
const checkboxes = Array.from(template.content.querySelectorAll<HTMLInputElement>("input"));
assert.equal(checkboxes.length, 3);
assert.deepEqual(checkboxes.map((element) => element.checked), [false, true, false]);
assert.ok(checkboxes.every((element) => element.type === "checkbox" && element.disabled));
assert.ok(template.content.querySelector("li li input"));
assert.ok(Array.from(template.content.querySelectorAll("li")).some((element) =>
element.textContent.trim() === "ordinary bullet" && !element.querySelector("input")));
assert.equal(template.content.querySelector("ol > li")?.textContent.trim(), "numbered item");
assert.equal(template.content.querySelector(".protyle-action, svg, use"), null);
});
it("flattens inline memo HTML and removes dangerous preview links", {
skip: hasDOM && typeof Lute !== "undefined" ? false :
"The Node test environment does not provide DOM and Lute globals",
}, async () => {
Object.assign(globalThis, {NODE_ENV: "test", SIYUAN_VERSION: "test"});
const richText = await import("./richText");
const markdown = '<span data-type="inline-memo" data-inline-memo-content="' +
"<img src=x onerror=alert(1)><span data-type=file-annotation-ref " +
'data-id=javascript:alert(1)>spoof</span>">memo</span>\n\n' +
"[unsafe](javascript:alert(1))";
const html = richText.getAVRichTextPreviewHTML(markdown);
assert.doesNotMatch(html, /<(?:img|script|iframe)\b/i);
assert.doesNotMatch(html, /\son\w+=/i);
assert.doesNotMatch(html, /javascript:/i);
assert.doesNotMatch(html, /data-type="file-annotation-ref"/i);
assert.match(html, /<sup>\([^<]*spoof[^<]*\)<\/sup>/i);
});
it("round-trips escaped font family names through Kramdown style IAL", {
skip: hasDOM && typeof Lute !== "undefined" ? false :
"The Node test environment does not provide DOM and Lute globals",
}, async () => {
Object.assign(globalThis, {NODE_ENV: "test", SIYUAN_VERSION: "test"});
const richText = await import("./richText");
const fontFamily = getInlineFontFamilyStyle("Semi; 'Quoted' 字体");
const blockDOM = '<div data-type="NodeParagraph"><div contenteditable="true">' +
`<span data-type="text" style="font-family: ${fontFamily};">font</span>` +
"</div></div>";
const serialized = richText.serializeAVRichTextBlockDOM(blockDOM);
assert.doesNotMatch(serialized.markdown, /\\'/);
assert.match(serialized.markdown, /\'Quoted\'/);
assert.equal(richText.serializeAVRichTextBlockDOM(serialized.blockDOM).markdown, serialized.markdown);
});
});