fix: Prevent overlapping nested list markers (#13584)

This commit is contained in:
Donghoon Yoo authored and GitHub committed 2026-09-04 07:55:04 -04:00
1 parent 1a83b9b1da
commit 24f36dae7e
2 files changed
+72 -1

No files matched your search

+43 -1
View File
@@ -6,7 +6,7 @@ import {
p,
schema,
} from "@shared/test/editor";
import { checkboxListInputRule } from "./listInputRule";
import { checkboxListInputRule, listWrappingInputRule } from "./listInputRule";
const { bullet_list, ordered_list, list_item, checkbox_list, checkbox_item } =
schema.nodes;
@@ -81,6 +81,48 @@ const rule = checkboxListInputRule(
checkbox_item
);
const bulletRule = listWrappingInputRule(/^\s*([-+*])\s$/, bullet_list);
const orderedRule = listWrappingInputRule(
/^(\d+)\.\s$/,
ordered_list,
(match) => ({ order: Number(match[1]), listStyle: "number" })
);
describe("listWrappingInputRule", () => {
it("exits a list when the same marker is typed into an empty item", () => {
const testDoc = doc([bullet_list.create(null, [li([p("*")])])]);
const result = typeTrigger(bulletRule, testDoc, "*", " ");
expect(result.doc.childCount).toBe(1);
expect(result.doc.firstChild?.type.name).toBe("paragraph");
expect(result.doc.textContent).toBe("");
});
it("converts a list when a different marker is typed into an empty item", () => {
const testDoc = doc([bullet_list.create(null, [li([p("1.")])])]);
const result = typeTrigger(orderedRule, testDoc, "1.", " ");
expect(result.doc.childCount).toBe(1);
expect(result.doc.firstChild?.type.name).toBe("ordered_list");
expect(result.doc.firstChild?.firstChild?.type.name).toBe("list_item");
expect(result.doc.textContent).toBe("");
});
it("allows a nested list after content in the parent item", () => {
const testDoc = doc([
bullet_list.create(null, [li([p("parent"), p("*")])]),
]);
const result = typeTrigger(bulletRule, testDoc, "*", " ");
const item = result.doc.firstChild?.firstChild;
expect(item?.child(0).textContent).toBe("parent");
expect(item?.child(1).type.name).toBe("bullet_list");
});
});
describe("checkboxListInputRule", () => {
it("converts a plain bullet list to a checklist", () => {
const testDoc = doc([
+29
View File
@@ -30,6 +30,35 @@ export function listWrappingInputRule(
return null;
}
const { $from } = state.selection;
const itemType = state.schema.nodes.list_item;
const parent = $from.node(-1);
if (itemType && parent.type === itemType && $from.index(-1) === 0) {
// Wrapping a list item's first block creates an empty parent item whose
// marker overlaps the nested list marker. Convert or exit the current
// list instead.
const tr = state.tr.delete(start, end);
const after = state.apply(tr);
let handled = false;
let selection = after.selection;
toggleList(nodeType, itemType)(after, (toggleTr) => {
toggleTr.steps.forEach((step) => tr.step(step));
selection = toggleTr.selection;
handled = true;
});
if (!handled) {
return null;
}
tr.setSelection(
TextSelection.near(
tr.doc.resolve(Math.min(selection.from, tr.doc.content.size))
)
);
return tr;
}
// Otherwise, execute the original wrappingInputRule handler
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (rule as any).handler(state, match, start, end);