From 24f36dae7e048d496b2cf7687bbd21fbb3905fc9 Mon Sep 17 00:00:00 2001 From: Donghoon Yoo Date: Fri, 4 Sep 2026 20:55:04 +0900 Subject: [PATCH] fix: Prevent overlapping nested list markers (#13584) --- shared/editor/lib/listInputRule.test.ts | 44 ++++++++++++++++++++++++- shared/editor/lib/listInputRule.ts | 29 ++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/shared/editor/lib/listInputRule.test.ts b/shared/editor/lib/listInputRule.test.ts index f64d5e2056..774e641cf2 100644 --- a/shared/editor/lib/listInputRule.test.ts +++ b/shared/editor/lib/listInputRule.test.ts @@ -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([ diff --git a/shared/editor/lib/listInputRule.ts b/shared/editor/lib/listInputRule.ts index 065ace0a6f..b5731ed3d4 100644 --- a/shared/editor/lib/listInputRule.ts +++ b/shared/editor/lib/listInputRule.ts @@ -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);