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.)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
Let me analyze the issue. The bug is about deleting a leading ListItemNode also deleting the previous DecoratorBlockNode in Lexical editor. Let me first explore the repository structure to understand the codebase and then find the relevant code that handles list item deletion.
Assistant
Let me explore the list-related files, especially those handling deletion:
Assistant
Now let me look at the formatList.ts file, which might handle the deletion logic:
Assistant
Now let me look at the tests to understand the expected behavior better:
Assistant
The issue is about deleting the first ListItemNode also deleting the previous DecoratorBlockNode. This is likely related to how Lexical handles backspace/delete when the cursor is at the beginning of a list item. Let me search for relevant code:
Assistant
The issue is probably in the core Lexical package, related to how backspace/delete handles node merging. Let me search for the merge-related code:
Assistant
Let me look at the LexicalSelection.ts file, specifically around the merge logic:
Assistant
Now I see the issue. The problem is in the decorator handling section. When we delete from a list item and there's a decorator node before it, the code at lines 1834-1850 handles the decorator case. Specifically, the issue is in this logic:
} else if ($isDecoratorNode(caret.origin)) { if (caret.origin.isIsolated()) { // do nothing, shouldn't delete an isolated decorator } else if ( state.type === 'merge-next-block' && (caret.origin.isKeyboardSelectable() || !caret.origin.isInline()) && $isElementNode(initialRange.anchor.origin) && initialRange.anchor.origin.isEmpty() ) { // If the anchor is an empty element that is adjacent to a // decorator then we remove the paragraph and select the // decorator initialRange.anchor.origin.remove(); const nodeSelection = $createNodeSelection(); nodeSelection.add(caret.origin.getKey()); $setSelection(nodeSelection); } else { // When the anchor is not an empty element then the // adjacent decorator is removed caret.origin.remove(); } // always stop when a decorator is encountered return; }The problem is that when we're in a list item, the code finds the decorator as a sibling and removes it, but it shouldn't do this when the list item is part of a ListNode. Let me explore more:
Assistant
The problem is in the decorator handling. When deleting from a list, the code finds the DecoratorNode as a sibling caret and removes it. But in the case of a list, the anchor origin would be the ListItemNode, not the ListNode itself. Let me understand the structure better:
Assistant
Now I understand the issue better. Let me look at what happens when we encounter a ListNode:
When deleting backward from the first list item (after a DecoratorBlockNode):
- The caret goes from the list item backwards
- It encounters the ListNode as a sibling element (since ListNode is an ElementNode)
- Then it continues and finds the DecoratorNode as another sibling
- The code then removes the DecoratorNode
The problem is that when the previous sibling is a ListNode (a block element), the code sets
state.type = 'merge-next-block'andcontinue, but then the loop continues and eventually hits the DecoratorNode. When it hits the DecoratorNode, it checks ifstate.type === 'merge-next-block'and the anchor is an empty element. If the anchor (ListItemNode) is empty, it removes the ListItemNode and selects the decorator. Otherwise, it removes the decorator!The bug is that when the anchor origin is a ListItemNode (not a ParagraphNode), the code shouldn't be deleting the decorator. The decorator should only be removed when the anchor is something like a ParagraphNode directly adjacent to it.
Let me look at the test file to understand the expected behavior:
Assistant
I need to understand the issue better. Let me look at the existing tests for deleteCharacter to understand the expected behavior around decorators and lists.
Assistant
Let me look at the existing tests in the lexical package for selection-related behaviors:
Assistant
Let me understand the problem more by looking at how iterNodeCarets works and what the sibling caret would be when we have:
Root └── DecoratorNode (TweetNode) └── ListNode └── ListItemNode (empty or with cursor at start)Assistant
Let me understand what happens when iterating through node carets. The key issue is that when deleting from a ListItemNode that follows a DecoratorNode at the root level, the iteration finds:
- The ListItemNode (anchor origin)
- Then moves to parent ListNode
- Then the sibling caret would point to the ListNode
- Then the sibling caret would eventually point to the DecoratorNode
Wait, I need to understand this better. Let me look at what happens in the sibling caret case more carefully:
Assistant
Now I understand the issue. When the user is at the beginning of a ListItemNode and presses backspace:
- The code iterates through carets going backwards
- When it encounters a SiblingCaret pointing to an ElementNode (like ListNode), it sets
state.type = 'merge-next-block'and continues - When it then encounters a SiblingCaret pointing to a DecoratorNode, it falls into the decorator handling logic
- The condition
state.type === 'merge-next-block'is true (because the ListNode was encountered first) - If the anchor origin (ListItemNode) is empty, it removes the ListItemNode and selects the decorator
- If the anchor origin is NOT empty, it removes the decorator (this is the bug!)
The problem is in the
elsebranch at line 1847-1850: when the anchor is not empty, the code removes the decorator node. But this is wrong when the anchor is a ListItemNode, because the ListItemNode is wrapped inside a ListNode.The expected behavior should be:
- When the anchor is a ListItemNode inside a ListNode and there's a decorator before the list, pressing backspace should not delete the decorator. Instead, it should convert the ListItemNode to a paragraph or handle it through the normal list deletion flow.
Assistant
The issue is clear now. The current logic handles the case when
state.type === 'merge-next-block'and the anchor is empty (removes anchor, selects decorator). However, when the anchor is NOT empty, it falls through to theelsebranch which removes the decorator.But wait, let me trace through more carefully:
- When we're at a ListItemNode with content and press backspace at the beginning
- The caret iterates backwards
- First sibling encountered is ListNode (since ListItemNode is inside ListNode)
- ListNode is a block element, so
state.type = 'merge-next-block'and we continue - Next sibling encountered is DecoratorNode
- Since
state.type === 'merge-next-block'and the anchor (ListItemNode) is NOT empty, the condition at line 1834-1838 fails - Falls to
elsebranch and removes the decorator
The problem is that the
elsebranch doesn't account for the case where there's an intermediate block (like ListNode) between the anchor and the decorator. In this case, the decorator should NOT be removed - instead, the list should be handled through the merge-block path.Assistant
Now let me look at how to run the tests and set up a reproduction script:
Assistant
Let me try a different approach - let me look at the specific test file to understand how to run tests:
Assistant
Good, the tests are running. Now let me understand the issue better and create a reproduction script. Let me first look at the formatList.test.ts to understand how tests are structured:
Assistant
Now let me look at how to properly reproduce the issue. The bug is in
deleteCharacterinLexicalSelection.ts. Let me create a test to reproduce it:Assistant
I've successfully reproduced the bug. The test shows that after deleting from the first list item, the decorator is gone and only the list remains. The first child is now a
listnode, not a decorator node.Now let me understand the fix. Looking at the code in
LexicalSelection.ts:The issue is when
state.type === 'merge-next-block'and we encounter a decorator. The current logic is:- If anchor is empty, remove anchor and select decorator
- Otherwise, remove decorator
But the problem is that when
state.type === 'merge-next-block', there's an intermediate block (like ListNode) between the anchor and the decorator. In this case, we shouldn't directly delete the decorator - we should fall through to the normal merge logic.The fix should be: when
state.type === 'merge-next-block'and we encounter a decorator, we should only apply the decorator-specific handling when the anchor is empty. When the anchor is NOT empty, we should break and let the merge-block logic handle it.Let me look at the code again:
Assistant
I see. The issue is in the
elsebranch at lines 1847-1850. Whenstate.type === 'merge-next-block'and the anchor is NOT empty, the code falls through to theelsebranch and removes the decorator. This is incorrect because there's an intermediate block (like ListNode) between the anchor and the decorator.The fix should be: when
state.type === 'merge-next-block'and the anchor is not empty, we should NOT remove the decorator. Instead, we should break and let the code fall through to themerge-blockhandling logic (or just return without deleting anything, letting the default list handling work).Looking at the code flow:
- When we hit a sibling element that's a block (like ListNode), we set
state.type = 'merge-next-block'andcontinue - When we then hit a decorator, we check if it should be handled specially
- The special handling only applies when the anchor is empty
- When the anchor is not empty and
state.type === 'merge-next-block', we should break, not remove the decorator
Let me make the fix:
- When we hit a sibling element that's a block (like ListNode), we set
Assistant
Now let me open the file and make the fix. The issue is that when
state.type === 'merge-next-block'and the anchor is NOT empty, the code should break instead of removing the decorator. The decorator should only be removed when it's directly adjacent to the anchor (i.e., whenstate.type === 'initial').Assistant
Now I understand the issue. The fix should be to add a condition: when
state.type === 'merge-next-block'and the anchor is NOT empty, we should break (or return without deleting the decorator). The decorator should only be removed when:state.type === 'initial'(decorator is directly adjacent to the anchor)- OR
state.type === 'merge-next-block'AND the anchor is empty (in which case we remove the anchor and select the decorator)
Let me make the fix:
Assistant
Now I understand the code. The fix should be to add a check: when
state.type === 'merge-next-block'and the anchor is not empty, we should break instead of removing the decorator. The decorator should only be removed when:state.type === 'initial'(no intermediate block between anchor and decorator)- OR when
state.type === 'merge-next-block'AND the anchor is empty (handled in the existing condition)
Let me make the fix:
Assistant
I see the edit got corrupted - the
!caret.origin.isInline()part was lost. Let me fix this:Assistant
I see, the
!caret.origin.isInline()line got removed. Let me revert and redo the fix properly:Assistant
Now let me use sed to properly insert the new condition:
Assistant
I see the sed command removed some important lines. Let me check what's missing:
Assistant
I see - the
$setSelection(nodeSelection);line was removed. Let me revert and try again with a more careful approach:Assistant
Now I'll add the new condition after line 1847 (the
} else {line):Assistant
I need to replace line 1847
} else {with the new condition block:Assistant
The fix is now in place. Let me verify the full diff:
Assistant
The fix looks correct. Now let me run my test to verify it works:
Assistant
The test now reaches further - it's failing because when we
breakout of the decorator handling, the code falls through to themodify('extend', isBackward, 'character')call which requires DOM. But the important thing is that the decorator is no longer being deleted!The error
domSelection.modify is not a functionhappens because the test environment (jsdom) doesn't have themodifymethod onwindow.getSelection(). This is expected for this test environment - the code is now reaching the fallback path after the fix.Let me update the test to check that the decorator is preserved even if the deletion fails due to the DOM limitation:
Assistant
The test passes now. The decorator is preserved (first child type is
test_decoratorand there are 2 children). Let me also add a test case for the empty list item scenario to make sure both cases are covered:Assistant
Both tests pass. Now let me also run the existing list tests to make sure I haven't broken anything:
Assistant
Current file/reset: CURRENT FILE OR WORKDIR NOT MODIFIED
The user is reminding me about the command usage. Let me check the current state of my work and continue. I was working on fixing a bug in Lexical where deleting the first ListItemNode would also delete a previous DecoratorBlockNode.
Let me check the current state of the fix and run more tests to ensure I haven't broken anything.
Assistant
Good, my fix is still in place. Let me run more tests related to lexical selection to make sure I haven't broken anything:
Assistant
Let me continue running more tests to validate the fix.
Assistant
All list tests pass. Let me also run some more tests related to selection and deletion to verify nothing else is broken:
Assistant
All tests pass. Let me also run the FastPathCrossParent tests which deal with complex scenarios:
Assistant
Let me run a broader set of tests in the lexical package to make sure nothing is broken:
Assistant
All 582 tests pass. Now let me clean up the test file I created (since I shouldn't leave test files in the repository):
Assistant
Let me verify the fix is still in place and summarize what was done.
Assistant
Let me verify the fix handles edge cases correctly by running a few more related tests:
Assistant
Let me verify the fix is complete and all tests pass. I should also make sure I haven't left any test files behind and check the final state of my changes.
Assistant
Let me verify the fix is correct by reviewing the final diff one more time:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.