ISSUE DESCRIPTION Bug: List: delete the leading ListItemNode will also delete the…
- User
ISSUE DESCRIPTION
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.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
Assistant
I'll start by exploring the repository structure to understand the codebase and find the relevant files for this issue.
Assistant
Let me explore the lexical package structure to understand the codebase, particularly the list-related functionality.
Assistant
Let me look at the core
removemethod in the LexicalNode:Assistant
Let me look at how the delete/backspace is handled in the core:
Assistant
This is a complex codebase. Let me look at how the deletion of a list item happens. The key path is
collapseAtStartin the ListItemNode. But the issue title says "delete the leading ListItemNode will also delete the previous DecoratorBlockNode".Let me look at the
$collapseAtStartfunction and the ListItemNode'scollapseAtStart.Assistant
Let me look at
forwardDeletionand the caret logic:Assistant
Let me look at existing tests for ListItemNode to understand the testing patterns and reproduce the issue:
Assistant
Let me find a DecoratorBlockNode test utility:
Assistant
Let me look at how deleteCharacter is tested in the lexical test suite:
Assistant
Let me look at the test utils to understand how to set up an editor and dispatch commands.
Assistant
Let me look at the caret utilities used in
deleteCharacter:Assistant
Let me write a reproduction test to see the actual behavior:
Assistant
Now let me create a reproduction test:
Assistant
Let me put the test in the correct location:
Assistant
The decorator was removed. Let me print the full structure to understand what happened:
Assistant
The decorator was deleted while the list remained - confirming the bug. Let me understand the caret iteration logic. Let me look at the caret implementation:
Assistant
Let me add logging to understand the caret iteration. Let me write an exploration:
Assistant
So the carets iterated backward from the start of "item1" text are:
- SIBLING caret, origin = listitem (inline=false, not shadowRoot)
- SIBLING caret, origin = list (inline=false)
- SIBLING caret, origin = test_decorator (block, inline=false)
Wait, these are SIBLING carets. The first caret is "origin=listitem" - that's the sibling caret before the text node? Hmm, but the text node's previous sibling is null. Let me think. Actually
iterNodeCaretsusesgetSiblingCaretfor text point caret. Let me trace.Actually, the initialRange.anchor is the text point caret at the start of "item1". The
iterNodeCaretsfirst does:const anchor = $isTextPointCaret(this.anchor) ? this.anchor.getSiblingCaret() : ...So anchor becomes the sibling caret of the text node in the 'previous' direction. For a text node at the start, its previous sibling is null. So the sibling caret of the text node in 'previous' direction... Let me look at for a node with no previous sibling.
Assistant
Now let me understand the iteration. The
iterNodeCaretsstep function:const step = (state) => state.isSameNodeCaret(focus) ? null : $getAdjacentChildCaret(state) || state.getParentCaret(rootMode);So starting from
anchorwhich is$getSiblingCaret(textNode, 'previous')(a sibling caret whose origin is the text node, and getNodeAtCaret returns previous sibling = null). But the iterator's initial isanchor.isSameNodeCaret(focus) ? null : step(anchor).So
step(anchor)=$getAdjacentChildCaret(anchor) || anchor.getParentCaret('shadowRoot').The anchor is a SiblingCaretPrevious with origin=textNode.
$getAdjacentChildCaretof this... Let me look at what getAdjacentChildCaret does. Actually, let me just look at the output again.The output shows the carets yielded are:
- SIBLING origin=listitem (inline=false)
- SIBLING origin=list (inline=false)
- SIBLING origin=test_decorator (block)
So
step(anchor)produces a SIBLING caret with origin=listitem. How? The textNode's sibling caret in 'previous' direction. getAdjacentChildCaret would try to go to the child of the node at the caret (which is null - the previous sibling of textNode is null). So getAdjacentChildCaret returns null (no node at caret to descend into). Then .Assistant
Now let me trace the state machine. Let me compare with the normal case (paragraph after decorator) to understand the expected behavior:
Assistant
Let me look at
getAdjacentCaretandgetParentCaretfor sibling carets:Assistant
Let me look at
$getChildCaretOrSelf:Assistant
Let me dig into scenario 4 (paragraph + list) to understand the normal merge behavior:
Assistant
Let me check for existing tests related to decorator deletion behavior:
Assistant
There's a regression test
7246-delete-character-backward-list.spec.mjs. Let me look at it:Assistant
Let me look at the caret tests and selection tests for decorator behavior:
Assistant
Let me look at the git blame for this decorator handling section to understand the intent:
Assistant
Let me check
getChildCaretfor sibling carets to fully understand the descent:Assistant
Now I need to understand
$getChildCaretOrSelffor a SiblingCaret.caret.getChildCaret()for a SiblingCaret. Looking at the interface (line 292):getChildCaret: () => null;— that's one interface. But which class? Let me check which one. Line 292 is in some interface. Hmm.Actually, the AbstractSiblingCaret's getChildCaret — let me check. Looking at the grep output, line 494 is
$getChildCaret(origin, this.direction);inside getChildCaret of AbstractSiblingCaret probably. Let me look at lines 530-620.Assistant
Let me look at the regression test for decorator deletion (6974) and check for decorator+paragraph tests:
Assistant
Let me look at the playground's HR node and block cursor to understand the #7319 scenario:
Assistant
Let me look at
$caretFromPoint:Assistant
Let me look at
$getChildCaretAtIndex:Assistant
Let me search more broadly for existing tests involving lists and decorators together:
Assistant
Let me trace the #7319-like scenario (block decorator + cursor at end) to understand it:
Assistant
The #7319 case has
state='initial'when reaching the decorator (cursor directly after it), while the list bug hasstate='merge-next-block'(a block boundary was crossed). Let me verify scenario 3 (decorator + paragraph with text) and check for related tests:Assistant
Let me check the list-followed-by-element-node test and 1730-delete-backword:
Assistant
Let me look at how
modifyis implemented to assess the fallback path risk:Assistant
Let me look at
$modifySelectionAroundDecoratorsAndBlocks:Assistant
I now understand the bug. Let me look at the exact current code section to edit:
Assistant
Let me check
$collapseAtStartdefinition and the imports available in the file:Assistant
Let me check the CollapsibleContainerNode which was modified in the #7248 commit:
Assistant
ParagraphNode has a
collapseAtStart. Let me read it fully:Assistant
Let me check ListItemNode.collapseAtStart to confirm it always returns true:
Assistant
Let me check the imports in LexicalSelection.ts to ensure
$isRootOrShadowRootand others are available:Assistant
All imports are available. Now let me implement the fix. Let me view the exact lines:
Assistant
Now let me apply the fix:
Assistant
No python. Let me use the edit command instead. Let me open the file:
Assistant
Now let me apply the edit to the decorator branch:
Assistant
The edit looks good. Now let me run the repro test to verify:
Assistant
All 5 scenarios pass. The fix works: decorator + list now converts the list to a paragraph while keeping the decorator, and the other behaviors (#7319 HR removal, #7246 list merge, decorator+paragraph) are unchanged.
Now let me run the existing unit tests to make sure nothing breaks. Let me run the lexical-list and lexical unit tests:
Assistant
All lexical-list tests pass. Now let me run the core lexical unit tests, especially the selection and caret tests:
Assistant
Let me run the broader lexical unit tests and the rich-text heading tests (which test deleteCharacter):
Assistant
All 582 lexical unit tests pass. Let me also run the rich-text tests (heading tests use deleteCharacter):
Assistant
Let me verify TypeScript compiles cleanly for the changed file:
Assistant
Let me check the full tsc output for any errors introduced:
Assistant
Let me run eslint on the changed file to ensure no lint issues:
Assistant
No lint errors. Let me test more edge cases to be thorough — nested lists, list items containing paragraphs, and numbered lists:
Assistant
Let me see the actual errors for those two cases:
Assistant
The two "failures" are test-environment artifacts, not fix regressions:
- Nested list: the decorator is never reached (iteration stops at the preceding text node), so it falls to the pre-existing
modifyfallback which needs a real DOM window — unchanged by my fix. - Paragraph-in-listitem: a test-setup issue with list normalization.
Let me verify the nested case is unchanged by my fix (decorator not reached) and test the paragraph-in-listitem case with proper cursor setup:
- Nested list: the decorator is never reached (iteration stops at the preceding text node), so it falls to the pre-existing
Assistant
The paragraph-in-listitem case is normalized by Lexical into text-in-listitem, which my fix handles correctly. Let me verify the nested-list-without-text case (where the decorator IS reached) works and preserves the decorator:
Assistant
The nested-no-text case correctly preserves the decorator and unindents. Now let me verify forward deletion (delete key) scenarios aren't broken:
Assistant
Forward delete behavior changed — let me check what the original (pre-fix) forward-delete behavior was, to decide if I should restrict my fix to backward-only:
Assistant
The OLD forward-delete behavior removed the decorator (list unchanged). My fix changed that, which is out of scope for this issue (which is about backspace). Let me restrict my fix to backward deletion only by adding
isBackward &&to the condition:Assistant
Now let me re-run the comprehensive tests covering both backward (fixed) and forward (unchanged) scenarios:
Assistant
All 7 pass — the fix is targeted: backward deletion of a leading list item preceded by a block decorator is fixed, all other behaviors preserved. Let me run the full relevant test suites, typecheck, and lint once more:
Assistant
Let me also run table and code unit tests since they involve block structures and decorators:
Assistant
All pass. Now let me clean up the repro test file and review the final diff:
Assistant
The diff is clean and minimal. Let me run the complete unit test suite once more to ensure nothing is broken across all packages:
Assistant
All 2962 unit tests pass. Let me also verify the inline-decorator merge scenario (#6974) is unaffected, since the decorator branch is involved:
Assistant
The inline decorator merge (#6974-like) works correctly — text merges into the paragraph, decorator preserved. My change doesn't affect the merge-block path. Let me clean up the repro test and do a final review:
Assistant
Let me do a final review of the complete modified section:
Assistant
Let me verify one more edge case — a CollapsibleContainer (which has
collapseAtStart) before a decorator, to ensure the generic collapseAtStart deferral is safe:Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.