Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor: fix some issues

Adorian Doran4cfa7113855d
diff --git a/packages/ckeditor5/src/plugins.ts b/packages/ckeditor5/src/plugins.ts
index df1a4e52cd..367a239464 100644
--- a/packages/ckeditor5/src/plugins.ts
+++ b/packages/ckeditor5/src/plugins.ts
@@ -4,6 +4,7 @@ import { Autoformat, AutoLink, BlockQuote, BlockToolbar, Bold, CKFinderUploadAda
// `FormatPainter` -> `TriliumFormatPainter`, `Template` -> `TriliumSnippets`. See each plugin's
// doc comment for the details.
import type { Plugin } from "ckeditor5";
+import AdaptiveColors from "./plugins/adaptive_colors.js";
import CutToNotePlugin from "./plugins/cuttonote.js";
import UploadimagePlugin from "./plugins/uploadimage.js";
import ItalicAsEmPlugin from "./plugins/italic_as_em.js";
@@ -37,7 +38,6 @@ import CopyLinkUrlButton from "./plugins/copy_link_url.js";
import ImageActions from "./plugins/image_actions.js";
import ClipboardBareImage from "./plugins/clipboard_bare_image.js";
import ClipboardImageEmbed from "./plugins/clipboard_image_embed.js";
-import AdaptiveColors from "./plugins/adaptive_colors.js";
import TriliumSnippets from "./plugins/snippets/snippets.js";
import TriliumAiAssistant from "./plugins/ai_assistant/ai_assistant.js";
diff --git a/packages/ckeditor5/src/plugins/adaptive_colors.spec.ts b/packages/ckeditor5/src/plugins/adaptive_colors.spec.ts
index 4cdb4bc46d..bbe1d532d9 100644
--- a/packages/ckeditor5/src/plugins/adaptive_colors.spec.ts
+++ b/packages/ckeditor5/src/plugins/adaptive_colors.spec.ts
@@ -58,6 +58,23 @@ describe("AdaptiveColors", () => {
editor.execute("fontColor");
expect(getModelData(editor.model, { withoutSelection: true }))
.toBe("<paragraph>text</paragraph>");
+
+ editor.execute("fontBackgroundColor", { value: "#e64d4d" });
+ editor.execute("fontBackgroundColor", { value: "" });
+ expect(getModelData(editor.model, { withoutSelection: true }))
+ .toBe("<paragraph>text</paragraph>");
+ });
+
+ it("converts text colors in an editor without the table properties plugins", async () => {
+ const textOnly = await createTestEditor([
+ Essentials, Paragraph, FontColor, FontBackgroundColor, AdaptiveColors
+ ]);
+ setModelData(textOnly.model, "<paragraph>[text]</paragraph>");
+
+ textOnly.execute("fontColor", { value: "#e64d4d" });
+ const pair = adaptColor("#e64d4d", "text");
+ expect(getModelData(textOnly.model, { withoutSelection: true }))
+ .toBe(`<paragraph><$text fontColor="${pair}">text</$text></paragraph>`);
});
it("stores table and cell colors as pairs, through saving and loading", () => {
diff --git a/packages/ckeditor5/src/plugins/adaptive_colors.ts b/packages/ckeditor5/src/plugins/adaptive_colors.ts
index 45f58fae76..7f5697c5ff 100644
--- a/packages/ckeditor5/src/plugins/adaptive_colors.ts
+++ b/packages/ckeditor5/src/plugins/adaptive_colors.ts
@@ -26,7 +26,7 @@ export default class AdaptiveColors extends Plugin {
this.listenTo<CommandExecuteEvent>(command, "execute", (_eventInfo, args) => {
const options = args[0] as { value?: unknown } | undefined;
- if (typeof options?.value === "string" && options.value) {
+ if (typeof options?.value === "string") {
args[0] = { ...options, value: adaptColor(options.value, role) };
}
}, { priority: "high" });
diff --git a/packages/trilium-core/src/services/sanitizer.spec.ts b/packages/trilium-core/src/services/sanitizer.spec.ts
index 749970bcc4..2aefeee0ad 100644
--- a/packages/trilium-core/src/services/sanitizer.spec.ts
+++ b/packages/trilium-core/src/services/sanitizer.spec.ts
@@ -98,6 +98,18 @@ describe("sanitize", () => {
.not.toContain("light-dark");
});
+ it("keeps the border styles CKEditor writes for tables and cells", () => {
+ // A border with a custom width is written as the shorthand, one with the default width as
+ // separate properties.
+ const dirty = `<figure class="table">`
+ + `<table style="border:2px solid light-dark(#4795e1,#4d99e6)"><tbody><tr>`
+ + `<th style="border:1.5px double #000000">H</th>`
+ + `<td style="border-color:light-dark(#000000,#6f6f6f);border-style:dashed">C</td>`
+ + `<td style="border-style:dotted;border-width:3px">D</td>`
+ + "</tr></tbody></table></figure>";
+ expect(sanitizeHtml(dirty)).toBe(dirty);
+ });
+
it("keeps a fractional image aspect-ratio (OneNote reports fractional pixel dimensions)", () => {
// CKEditor's usual integer ratio still passes...
expect(sanitizeHtml(`<img style="aspect-ratio:991/403" src="x.png" />`)).toContain("aspect-ratio:991/403");
diff --git a/packages/trilium-core/src/services/sanitizer.ts b/packages/trilium-core/src/services/sanitizer.ts
index d6a432678b..198229585b 100644
--- a/packages/trilium-core/src/services/sanitizer.ts
+++ b/packages/trilium-core/src/services/sanitizer.ts
@@ -39,6 +39,18 @@ export function sanitizeHtml(dirtyHtml: string) {
// The light and dark theme pair the text editor writes (see `adaptColor`).
const adaptiveColorRegex = /^light-dark\(\s*#[0-9a-f]{6}\s*,\s*#[0-9a-f]{6}\s*\)$/i;
const sizeRegex = [/^\d+\.?\d*(?:px|em|%)$/];
+ // The border styles CKEditor writes for tables and cells: the shorthand for a border with a
+ // custom width, separate properties otherwise.
+ const tableBorderStyles = {
+ "border": [
+ /^\s*\d+\.?\d*(?:px|em|%)\s*(none|hidden|dotted|dashed|solid|double|groove|ridge|inset|outset)\s*(#(0x)?[0-9a-fA-F]+|rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)|hsl\(\s*(\d{1,3})\s*,\s*(\d{1,3})%\s*,\s*(\d{1,3})%\)|light-dark\(\s*#[0-9a-fA-F]{6}\s*,\s*#[0-9a-fA-F]{6}\s*\))\s*$/
+ ],
+ "border-color": [ ...colorRegex, adaptiveColorRegex, /^\s*transparent\s*$/ ],
+ "border-style": [
+ /^\s*(none|hidden|dotted|dashed|solid|double|groove|ridge|inset|outset)\s*$/
+ ],
+ "border-width": sizeRegex
+ };
// to minimize document changes, compress H
return sanitizeHtmlCustom(dirtyHtml, {
@@ -77,19 +89,9 @@ export function sanitizeHtml(dirtyHtml: string) {
width: sizeRegex,
height: sizeRegex
},
- table: {
- "border-color": [...colorRegex, adaptiveColorRegex, /^\s*transparent\s*$/],
- "border-style": [/^\s*(none|hidden|dotted|dashed|solid|double|groove|ridge|inset|outset)\s*$/]
- },
- td: {
- "border-color": [...colorRegex, adaptiveColorRegex, /^\s*transparent\s*$/],
- border: [
- /^\s*\d+(?:px|em|%)\s*(none|hidden|dotted|dashed|solid|double|groove|ridge|inset|outset)\s*(#(0x)?[0-9a-fA-F]+|rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)|hsl\(\s*(\d{1,3})\s*,\s*(\d{1,3})%\s*,\s*(\d{1,3})%\)|light-dark\(\s*#[0-9a-fA-F]{6}\s*,\s*#[0-9a-fA-F]{6}\s*\))\s*$/
- ]
- },
- th: {
- "border-color": [...colorRegex, adaptiveColorRegex, /^\s*transparent\s*$/]
- },
+ table: tableBorderStyles,
+ td: tableBorderStyles,
+ th: tableBorderStyles,
col: {
width: sizeRegex
}