Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(task states): stop rejecting valid CSS colors in the generated stylesheet
Claude99de67ecdcba
diff --git a/packages/trilium-core/src/services/task_states.spec.ts b/packages/trilium-core/src/services/task_states.spec.tsindex e280ea877f..a64580cec5 100644--- a/packages/trilium-core/src/services/task_states.spec.ts+++ b/packages/trilium-core/src/services/task_states.spec.ts@@ -235,6 +235,29 @@ describe("task_states (real DB)", () => {expect(css).toContain("--task-state-color: inherit");});+ it("passes through the CSS color syntaxes a picker or a hand-written label can produce", () => {+ const colors = [+ "#ff0000",+ "rgb(255 0 0 / 50%)",+ "oklch(0.7 0.1 200)",+ "color-mix(in srgb, red 40%, blue)",+ "var(--my-color, #fff)",+ "hsl(calc(120 + 30) 50% 50%)",+ "hsl(from var(--c) h s calc(l * 0.8))"+ ];++ for (const [ index, color ] of colors.entries()) {+ const name = `okcolor${index}`;+ getContext().init(() => createTaskStateNote(+ customState({ name, markdownSymbol: String.fromCharCode(65 + index), color })+ ));++ const css = generateTaskStateCss();+ const parts = css.split(`[data-trilium-task-state="${name}"]`);+ expect(parts[parts.length - 1]).toContain(`--task-state-color: ${color}`);+ }+ });+it("skips states whose icon cannot be resolved to a glyph", () => {const note = getContext().init(() =>createTaskStateNote(diff --git a/packages/trilium-core/src/services/task_states.ts b/packages/trilium-core/src/services/task_states.tsindex 0fac282771..f5a4d781c6 100644--- a/packages/trilium-core/src/services/task_states.ts+++ b/packages/trilium-core/src/services/task_states.ts@@ -116,11 +116,13 @@ export function seedDefaultTaskStates() {}/**- * Charset a task state's `color` must match to be emitted as a raw CSS value. Keeps- * `var()`, `rgb()` and `color-mix()` usable while excluding the characters that end a- * declaration, a rule, or the inline `<style>` element the stylesheet is served in.+ * Characters that stop a task state's `color` from being emitted as a raw CSS value. These+ * are the only ones that can end the declaration, the rule, or the inline `<style>` element+ * the stylesheet is served in — an escape resolves within a token rather than terminating+ * anything, and an unbalanced paren cannot reach past the element either. Everything else+ * is inert inside a value, so `color-mix()`, `var()` and `calc()` arithmetic pass through.*/-const SAFE_CSS_VALUE_PATTERN = /^[a-zA-Z0-9\s,.()%#/_-]+$/;+const UNSAFE_CSS_VALUE_PATTERN = /["'<>;{}\\]|[\u0000-\u001F\u007F]/;/*** Resolves an icon class (e.g. `bx bx-cancel`) to its font glyph and family@@ -180,7 +182,7 @@ export function generateTaskStateCss(): string {continue;}const name = escapeCssString(state.name);- const color = (state.color && SAFE_CSS_VALUE_PATTERN.test(state.color)) ? state.color : "";+ const color = (state.color && !UNSAFE_CSS_VALUE_PATTERN.test(state.color)) ? state.color : "";const hue = color ? computeHue(color) : undefined;rules.push(`[data-trilium-task-state="${name}"], .tn-task-checkbox[data-trilium-task-state="${name}"] {