Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor: do not rewrite "lang" and "dir" attributes each time the editor is focused

Adorian Doranf168c45bb7ba
diff --git a/packages/ckeditor5/src/plugins.ts b/packages/ckeditor5/src/plugins.ts
index 6547b6451c..29f88ca1e5 100644
--- a/packages/ckeditor5/src/plugins.ts
+++ b/packages/ckeditor5/src/plugins.ts
@@ -86,6 +86,7 @@ import ContextToolbar from "./plugins/context_toolbar.js";
import ImagePosition from "./plugins/image_position.js";
import ColorMenu from "./plugins/color_menu.js";
import BlockDragHandle from "./plugins/block_drag_handle.js";
+import SkipUnchangedAttributes from "./plugins/skip_unchanged_attributes.js";
/**
* Plugins that are specific to Trilium and not part of the CKEditor 5 core, included in both text editors but not in the attribute editor.
@@ -154,6 +155,7 @@ const TRILIUM_PLUGINS: typeof Plugin[] = [
TriliumSnippets,
TriliumAiAssistant,
FindInLinkWidgets,
+ SkipUnchangedAttributes,
];
/**
diff --git a/packages/ckeditor5/src/plugins/skip_unchanged_attributes.spec.ts b/packages/ckeditor5/src/plugins/skip_unchanged_attributes.spec.ts
new file mode 100644
index 0000000000..1fdb7022db
--- /dev/null
+++ b/packages/ckeditor5/src/plugins/skip_unchanged_attributes.spec.ts
@@ -0,0 +1,76 @@
+import { ClassicEditor, Essentials, Paragraph } from "ckeditor5";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+import { createTestEditor } from "../../test/editor-kit.js";
+import { registerTestCleanup } from "../../test/globals-test-kit.js";
+import SkipUnchangedAttributes from "./skip_unchanged_attributes.js";
+
+describe("SkipUnchangedAttributes", () => {
+ let editor: ClassicEditor;
+ let domRoot: HTMLElement;
+
+ beforeEach(async () => {
+ editor = await createTestEditor([Essentials, Paragraph, SkipUnchangedAttributes]);
+ domRoot = getDomRoot(editor);
+ });
+
+ it("writes only the class of the root when the focus changes", () => {
+ expect(domRoot.getAttribute("lang")).toBe("en");
+ expect(domRoot.getAttribute("dir")).toBe("ltr");
+
+ const written = recordAttributeWrites(domRoot, () => {
+ editor.ui.focusTracker.isFocused = true;
+ });
+
+ expect(domRoot.classList).toContain("ck-focused");
+ expect(written).toEqual(["class"]);
+ });
+
+ it("writes attributes that are new or change value", () => {
+ const written = recordAttributeWrites(domRoot, () => {
+ editor.editing.view.change((writer) => {
+ writer.setAttribute("lang", "ro", getViewRoot(editor));
+ writer.setAttribute("data-trilium-test", "1", getViewRoot(editor));
+ });
+ });
+
+ expect(written.sort()).toEqual(["data-trilium-test", "lang"]);
+ expect(domRoot.getAttribute("lang")).toBe("ro");
+ expect(domRoot.getAttribute("data-trilium-test")).toBe("1");
+ });
+
+ it("renames an unsafe attribute that the DOM already has", () => {
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
+ registerTestCleanup(() => warn.mockRestore());
+ domRoot.setAttribute("onclick", "void 0");
+
+ editor.editing.view.change((writer) => {
+ writer.setAttribute("onclick", "void 0", getViewRoot(editor));
+ });
+
+ expect(domRoot.hasAttribute("onclick")).toBe(false);
+ expect(domRoot.getAttribute("data-ck-unsafe-attribute-onclick")).toBe("void 0");
+ });
+});
+
+/** Runs `action` and returns the names of the attributes written on `element`, in order. */
+function recordAttributeWrites(element: HTMLElement, action: () => void) {
+ const observer = new MutationObserver(() => {});
+ observer.observe(element, { attributes: true });
+ action();
+ const names = observer.takeRecords().map((record) => record.attributeName);
+ observer.disconnect();
+ return names;
+}
+
+function getViewRoot(editor: ClassicEditor) {
+ const root = editor.editing.view.document.getRoot();
+ if (!root) throw new Error("The editing view has no root.");
+ return root;
+}
+
+function getDomRoot(editor: ClassicEditor) {
+ const root = editor.editing.view.getDomRoot();
+ if (!root) throw new Error("The editing view has no DOM root.");
+ return root;
+}
diff --git a/packages/ckeditor5/src/plugins/skip_unchanged_attributes.ts b/packages/ckeditor5/src/plugins/skip_unchanged_attributes.ts
new file mode 100644
index 0000000000..4f50f6e06a
--- /dev/null
+++ b/packages/ckeditor5/src/plugins/skip_unchanged_attributes.ts
@@ -0,0 +1,31 @@
+import { Plugin } from "ckeditor5";
+
+/**
+ * Skips the DOM writes of the editing view that set an attribute to the value it already has.
+ *
+ * `Renderer` writes every attribute of an element again when one of them changes, such as the
+ * `ck-focused` class of the root on each focus change. Chromium restyles the whole subtree on a
+ * write to `lang` or `dir`, even when the value is the same.
+ */
+export default class SkipUnchangedAttributes extends Plugin {
+
+ static get pluginName() {
+ return "SkipUnchangedAttributes" as const;
+ }
+
+ init() {
+ const domConverter = this.editor.editing.view.domConverter;
+ const setDomElementAttribute = domConverter.setDomElementAttribute.bind(domConverter);
+
+ domConverter.setDomElementAttribute = (domElement, key, value, relatedViewElement) => {
+ // `setDomElementAttribute()` renames an unsafe attribute, so that write still happens.
+ const tagName = domElement.tagName.toLowerCase();
+ if (domElement.getAttribute(key) === value
+ && domConverter.shouldRenderAttribute(key, value, tagName)) {
+ return;
+ }
+ setDomElementAttribute(domElement, key, value, relatedViewElement);
+ };
+ }
+
+}