Skip to content

Build your personal knowledge base with Trilium Notes

refactor(ckeditor5): drop the unreachable collapsible gap post-fixer

Elian Doranb52722059dac
diff --git a/packages/ckeditor5/src/plugins/collapsible/collapsible_editing.ts b/packages/ckeditor5/src/plugins/collapsible/collapsible_editing.ts
index 75ad5313ad..77e04b8e84 100644
--- a/packages/ckeditor5/src/plugins/collapsible/collapsible_editing.ts
+++ b/packages/ckeditor5/src/plugins/collapsible/collapsible_editing.ts
@@ -1167,7 +1167,11 @@ export default class CollapsibleEditing extends Plugin {
document.registerPostFixer(writer => this.structuralPostFixer(writer));
document.registerPostFixer(writer => this.summaryInvariantPostFixer(writer));
document.registerPostFixer(writer => this.bodyExistsPostFixer(writer));
- document.registerPostFixer(writer => this.gapPostFixer(writer));
+ // No post-fixer for a caret parked directly inside a <details> (in the "gap" between
+ // its children): CKEditor's own selection post-fixer runs before every plugin
+ // post-fixer and already moves it to the nearest valid text position, and the
+ // post-fixer loop restarts from the top after any fix — so a gap position never
+ // survives to be observed here. See the note on hiddenBodyPostFixer.
document.registerPostFixer(writer => this.hiddenBodyPostFixer(writer));
}
@@ -1318,41 +1322,6 @@ export default class CollapsibleEditing extends Plugin {
return false;
}
- /**
- * Prevent the caret from sitting in the "gap" position directly inside a
- * <details> (between summary and a body block). Prefer the previous sibling
- * so the caret stays on the summary line when the block is collapsed.
- */
- private gapPostFixer(writer: any): boolean {
- const selection = this.editor.model.document.selection;
- // Only re-pin the caret — never collapse a user's multi-block selection.
- if (!selection.isCollapsed) return false;
- const position = selection.getFirstPosition();
- if (!position || !position.parent.is("element", "details")) return false;
- const details = position.parent;
- const before = position.offset > 0 ? details.getChild(position.offset - 1) : null;
- const after = details.getChild(position.offset);
-
- // If `before` is a nested <details>, landing at its "end" would just put us
- // in another gap (childCount). Dig one level deeper to its last child block.
- if (before?.is("element", "details") && before.childCount > 0) {
- const last = before.getChild(before.childCount - 1);
- if (last?.is("element")) {
- writer.setSelection(last, "end");
- return true;
- }
- }
- if (before?.is("element")) {
- writer.setSelection(before, "end");
- return true;
- }
- if (after?.is("element")) {
- writer.setSelection(after, 0);
- return true;
- }
- return false;
- }
-
/**
* Never let the caret rest inside a body whose enclosing <details> is
* collapsed (so it doesn't disappear into hidden content, and so widget
diff --git a/packages/ckeditor5/src/plugins/collapsible/post_fixer_gap.spec.ts b/packages/ckeditor5/src/plugins/collapsible/post_fixer_gap.spec.ts
index 2f51956400..0fb26c4281 100644
--- a/packages/ckeditor5/src/plugins/collapsible/post_fixer_gap.spec.ts
+++ b/packages/ckeditor5/src/plugins/collapsible/post_fixer_gap.spec.ts
@@ -2,7 +2,7 @@ import { ClassicEditor, Essentials, Paragraph, _setModelData as setModelData, _g
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import CollapsibleEditing from "./collapsible_editing.js";
-describe("gapPostFixer + onEnterInBody", () => {
+describe("caret pinning inside a collapsible + onEnterInBody", () => {
let domElement: HTMLDivElement;
let editor: ClassicEditor;
let model: ClassicEditor["model"];
@@ -22,8 +22,14 @@ describe("gapPostFixer + onEnterInBody", () => {
return editor.destroy();
});
- describe("gapPostFixer", () => {
- it("re-pins a caret that lands directly between <summary> and the body to the end of <summary>", () => {
+ /**
+ * These guard the outcome, not an implementation of ours: the plugin used to carry a
+ * `gapPostFixer` for this, but CKEditor's own selection post-fixer runs before every plugin
+ * post-fixer and already moves a caret out of a container position, so ours never ran. The
+ * assertions below are kept as regression cover for the behaviour users actually get.
+ */
+ describe("a caret never rests between a collapsible's children", () => {
+ it("moves a caret dropped between <summary> and the body to the end of <summary>", () => {
setModelData(model,
"<details><summary>Title</summary><paragraph>body</paragraph></details>"
);
@@ -39,9 +45,11 @@ describe("gapPostFixer + onEnterInBody", () => {
);
});
- it("dives into a nested <details>'s last block when the gap before the caret is a nested <details>", () => {
- // Both details are open so hiddenBodyPostFixer doesn't rescue the caret
- // away from the nested body — we're isolating gapPostFixer here.
+ it("descends into a nested collapsible's last block when that is the nearest text position", () => {
+ // Both details are open so hiddenBodyPostFixer doesn't rescue the caret away from the
+ // nested body. The nested collapsible is the outer one's last child, so the nearest
+ // valid position is backwards, inside it. (With a sibling after the gap the caret goes
+ // forward to that sibling instead.)
setModelData(model,
"<details open=\"true\">" +
"<summary>Outer</summary>" +