Bug: List: delete the leading ListItemNode will also delete the previous…
- User
Bug: List: delete the leading ListItemNode will also delete the previous DecoratorBlockNode Lexical version: 0.12.2
Steps To Reproduce
- Open the playground at https://playground.lexical.dev/
- Append a
DecoratorBlockNodeto the document, for example, aTweetNode - Insert a list after the
DecoratorBlockNode - Delete the first list item, and the previous
DecoratorBlockNodewill also be removed
https://github.com/facebook/lexical/assets/6022672/2ccbc2a0-af27-4d3f-a244-804f43249fec
The current behavior
Both the
ListNodeand the previousDecoratorBlockNodeare deleted.The expected behavior
Only
ListNodewould be deleted. (I will be more than happy to see theListNodebe replaced by aParagraphNodeif possible! That's the default behavior in other editors.)Interface
No new interfaces are introduced.
- Assistant
I'll investigate how list deletion interacts with
DecoratorBlockNodeby searching the list and deletion handling code. - Assistant
I'll write a test to reproduce the bug, then trace the deletion logic.
- Assistant
Implementing the fix: backspace at a list item start was removing the previous decorator before
collapseAtStartcould run. - Assistant
Tests confirm the bug. Running broader tests to check for regressions.
- Assistant
Summary
Backspacing at the start of the first list item was deleting the previous
DecoratorBlockNode(e.g. a Tweet) along with the list.Root cause
In
deleteCharacter, when there’s no text to delete backward, the selection logic walks up to the previous sibling. If that sibling is a block decorator, it was removed immediately:// When the anchor is not an empty element then the // adjacent decorator is removed caret.origin.remove();ListItemNode.collapseAtStart()— which correctly turns the list into a paragraph or removes the first item — only ran later in the flow, so it never got a chance.Fix
Before removing an adjacent decorator on backspace, try
$collapseAtStartfirst. That lets list items (and other blocks withcollapseAtStart) handle the deletion themselves.Expected behavior (now working)
- Single-item list after a decorator → decorator preserved, list replaced with a paragraph containing the item text.
- Multi-item list → decorator preserved, first item becomes a paragraph, remaining list items stay.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.