Skip to content

Build your personal knowledge base with Trilium Notes

fix(text): unwrap divs in the editor instead of modelling them

Elian Dorand1dda881ed86
diff --git a/apps/client/src/widgets/type_widgets/text/config.spec.ts b/apps/client/src/widgets/type_widgets/text/config.spec.ts
index 4e294921a5..18e3998758 100644
--- a/apps/client/src/widgets/type_widgets/text/config.spec.ts
+++ b/apps/client/src/widgets/type_widgets/text/config.spec.ts
@@ -1,4 +1,4 @@
-import { DISPLAYABLE_LOCALE_IDS, IMAGE_MIMES, LOCALES } from "@triliumnext/commons";
+import { DISPLAYABLE_LOCALE_IDS, IMAGE_MIMES, LOCALES, SANITIZER_DEFAULT_ALLOWED_TAGS } from "@triliumnext/commons";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import imageService from "../../../services/image.js";
@@ -242,6 +242,34 @@ describe("CK config - HTML support", () => {
expect(config.htmlSupport?.allow).toEqual([]);
});
+
+ it("withholds div from the editor even when the option allows it", async () => {
+ // GHS gives div a dual content model — a paragraph impostor around inline content, an
+ // affordance-less container around a block — which is what strands the caret in a wrapped
+ // code block. Unwrapping it is the point; the sanitizer reads the same option and is
+ // deliberately left accepting div on import.
+ optionsState.map.allowedHtmlTags = JSON.stringify(["div", "p", "section"]);
+
+ const config = await buildConfig(baseOpts());
+
+ expect(config.htmlSupport?.allow).toEqual([
+ { name: "p", attributes: true, classes: true, styles: true },
+ { name: "section", attributes: true, classes: true, styles: true }
+ ]);
+ });
+
+ it("withholds div from the shipped default list too", async () => {
+ // The default is what nearly every install runs with, so the filter has to bite there
+ // rather than only on a hand-edited list.
+ optionsState.map.allowedHtmlTags = JSON.stringify(SANITIZER_DEFAULT_ALLOWED_TAGS);
+
+ const config = await buildConfig(baseOpts());
+
+ const names = (config.htmlSupport?.allow ?? []).map((pattern) => (pattern as { name: string }).name);
+ expect(names).not.toContain("div");
+ // Everything else the default list names still comes through.
+ expect(names).toEqual(SANITIZER_DEFAULT_ALLOWED_TAGS.filter((tag) => tag !== "div"));
+ });
});
describe("CK config - licensing", () => {
diff --git a/apps/client/src/widgets/type_widgets/text/config.ts b/apps/client/src/widgets/type_widgets/text/config.ts
index efc73b3aed..0477690902 100644
--- a/apps/client/src/widgets/type_widgets/text/config.ts
+++ b/apps/client/src/widgets/type_widgets/text/config.ts
@@ -400,10 +400,24 @@ function buildHtmlSupportConfig(): EditorConfig["htmlSupport"] {
const allowedTags: string[] = JSON.parse(options.get("allowedHtmlTags"));
return {
- allow: allowedTags.map((name) => ({ name, attributes: true, classes: true, styles: true }))
+ allow: allowedTags
+ .filter((name) => !EDITOR_ONLY_DISALLOWED_TAGS.has(name))
+ .map((name) => ({ name, attributes: true, classes: true, styles: true }))
};
}
+/**
+ * Withheld from the editor even when the option lists them, and only from the editor — the
+ * sanitizer reads the same option and still accepts them on import.
+ *
+ * `div` earns its place through GHS's dual content model: with inline content it becomes an
+ * `htmlDivParagraph` that every paragraph-keyed feature mistreats, and around a block it becomes a
+ * bare `htmlDiv` with no type-around, so a wrapped code block has nothing after it to escape into.
+ * Unwrapping loses nothing — no in-tree feature needs GHS to handle a div, footnotes claim their own
+ * by attribute — and it flattens the nested-div cruft that pasting from a web page drags in.
+ */
+const EDITOR_ONLY_DISALLOWED_TAGS = new Set(["div"]);
+
function getDisabledPlugins() {
const disabledPlugins: string[] = [];