An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Preserve plugin styles during snippet rendering https://github.com/siyuan-note/siyuan/issues/20150
Danielc9f9c1e3323f
diff --git a/app/src/config/util/snippets.test.ts b/app/src/config/util/snippets.test.tsindex 88d9f34ef0..e4f03e6bd1 100644--- a/app/src/config/util/snippets.test.ts+++ b/app/src/config/util/snippets.test.ts@@ -68,6 +68,39 @@ test("shared snippet renderer preserves CSS, JS enable flags, nonce and unchangeassert.equal(f.elements.size, 0);});+test("snippet cleanup preserves plugin styles and scripts used by exports", async () => {+ const f = fixture();+ const pluginCSS = {id: "snippetCSSplugin-font", tagName: "STYLE", textContent: "body {font-family: plugin-font;}",+ remove() { f.elements.delete(this.id); }};+ const pluginJS = {id: "snippetJSplugin-emoji", tagName: "SCRIPT", textContent: "pluginEmoji();",+ remove() { f.elements.delete(this.id); }};+ f.elements.set(pluginCSS.id, pluginCSS);+ f.elements.set(pluginJS.id, pluginJS);+ await f.render([]);+ assert.equal(f.elements.get(pluginCSS.id), pluginCSS);+ assert.equal(f.elements.get(pluginJS.id), pluginJS);+ assert.equal(f.measurements(), 0);++ const css = {id: "css", type: "css", enabled: true, content: "body {color: red;}"};+ const js = {id: "js", type: "js", enabled: true, content: "run();"};+ await f.render([css, js]);+ await f.render([{...css, content: "body {color: blue;}"}, {...js, content: "updated();"}]);+ assert.equal(f.elements.get("snippetCSScss").textContent, "body {color: blue;}");+ assert.deepEqual(f.executed, [js.content, "updated();"]);+ await f.render([]);+ assert.equal(f.elements.size, 2);+ assert.equal(f.elements.get(pluginCSS.id), pluginCSS);+ assert.equal(f.elements.get(pluginJS.id), pluginJS);+ assert.equal(f.measurements(), 3);++ f.config.snippet.enabledCSS = false;+ f.config.snippet.enabledJS = false;+ await f.render([]);+ assert.equal(f.elements.get(pluginCSS.id), pluginCSS);+ assert.equal(f.elements.get(pluginJS.id), pluginJS);+ assert.equal(f.measurements(), 3);+});+test("snippet capability and active guards prevent fetches and late script/style execution", async () => {const f = fixture();f.setAllowed(false);diff --git a/app/src/config/util/snippets.ts b/app/src/config/util/snippets.tsindex c0052bf75a..e59933bf02 100644--- a/app/src/config/util/snippets.ts+++ b/app/src/config/util/snippets.ts@@ -6,6 +6,8 @@ import {Constants} from "../../constants";import {refreshHeadingNumberMeasurements} from "../../util/assets";import {getExtensionScriptNonce, getHostCapabilities} from "../../util/hostCapabilities";+const snippetElements = new WeakSet<HTMLElement>();+export const renderSnippet = (timeout = 0, isActive = () => true, beforeJS?: () => Promise<void>, includeJS = true) => {if (!isActive() || !getHostCapabilities().customAppearance) {return Promise.resolve();@@ -27,12 +29,13 @@ export const renderSnippet = (timeout = 0, isActive = () => true, beforeJS?: ()if (nonce) script.nonce = nonce;script.text = item.content;script.id = `snippetJS${item.id}`;+ snippetElements.add(script);document.head.appendChild(script);};const snippetIds = new Set(response.data.snippets.map((item: ISnippet) =>`snippet${item.type === "css" ? "CSS" : "JS"}${item.id}`));- document.querySelectorAll('style[id^="snippetCSS"], script[id^="snippetJS"]').forEach(element => {- if (!snippetIds.has(element.id)) {+ document.querySelectorAll<HTMLStyleElement | HTMLScriptElement>('style[id^="snippetCSS"], script[id^="snippetJS"]').forEach(element => {+ if (snippetElements.has(element) && !snippetIds.has(element.id)) {cssChanged ||= element.tagName === "STYLE";element.remove();}@@ -68,6 +71,7 @@ export const renderSnippet = (timeout = 0, isActive = () => true, beforeJS?: ()const styleEl = document.createElement("style");styleEl.id = id;styleEl.textContent = item.content;+ snippetElements.add(styleEl);document.head.appendChild(styleEl);cssChanged = true;} else if (item.type === "js") {