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
Investigating list deletion when a list follows a DecoratorBlockNode — searching the codebase for the relevant handlers.
- Assistant
Implementing the fix:
deleteCharacterremoves the previous decorator beforecollapseAtStartcan convert the list. RunningcollapseAtStartfirst when backspacing at offset 0. - Assistant
The multi-item case inserts a paragraph from the removed item's content — updating the test to match that behavior.
- Assistant
Root cause
When backspacing at the start of a list item with no text behind the caret,
RangeSelection.deleteCharacter()walked backward and hit the previousDecoratorBlockNodefirst. Core deletion logic removed that decorator and returned early, soListItemNode.collapseAtStart()never ran.Fix
In
LexicalSelection.ts, call$collapseAtStart()before the decorator-removal path when backspacing at offset 0 and there’s no text in the deletion direction. That lets list collapse behavior run (convert a single-item list to a paragraph, or remove the first item from a multi-item list) without touching the preceding block decorator.Tests
Added two tests in
formatList.test.ts:- Single-item list after a block decorator → decorator kept, list becomes a paragraph
- Multi-item list → decorator kept, first item becomes a paragraph, remaining list items stay
All new tests and existing
LexicalSelectiontests pass.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.