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
Let me look at how the caret iteration works and check existing tests around this behavior.
- Assistant
Let me write a quick reproduction script to confirm the current behavior.
- Assistant
Now let me create a scratch reproduction test:
- Assistant
Bug reproduced: the decorator is removed and the list stays intact. Now let me understand the traversal logic and
collapseAtStartimplementations: - Assistant
Let me understand the caret traversal order:
- Assistant
Let me map current behavior across several scenarios by extending the scratch test:
- Assistant
The fix works — the decorator is preserved and the list item collapses to a paragraph (the behavior the issue author hoped for). Now let me run the broader test suites for regressions:
- Assistant
All pass. Now let me replace the scratch file with a proper regression test. Let me check where similar deleteCharacter/decorator tests live:
- Assistant
Done. Summary of the fix:
Root cause
In
RangeSelection.deleteCharacter(packages/lexical/src/LexicalSelection.ts), when Backspace is pressed at the start of a block with no text in the deletion direction, a caret traversal walks backward to decide what to do. Starting from the firstListItemNode, the traversal leaves theListItemNode, then leaves theListNode, and then encounters the preceding block-levelDecoratorNode— at which point it unconditionally removed the decorator (), even though the caret's actual block (the ) is nested and not adjacent to the decorator. The handler (which converts the item into a paragraph) never got a chance to run.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.