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 understand the issue first. The bug is about a Lexical editor where deleting a leading
ListItemNodealso deletes the previousDecoratorBlockNode. This is a bug in the Lexical editor library.Let me start by exploring the repository structure to understand the codebase.
Assistant
Let me explore the repository structure more, especially looking for list-related files since the bug is about ListItemNode.
Assistant
Now let me look at the formatList.ts file which handles list operations like indent/outdent and might be related to the deletion behavior:
Assistant
Now let me look at the core lexical package for how deletion/backspace is handled, which is more likely where the bug occurs:
Assistant
The issue is about deleting a ListItemNode and it also deleting the previous DecoratorBlockNode. This is likely related to the deletion/backspace handling. Let me search for backspace or delete related code:
Assistant
Let me look at the LexicalSelection.ts file since deletion is typically handled there:
Assistant
Let me look at the
deleteCharacterfunction more closely:Assistant
Now I need to understand the issue better. The problem happens when:
- A
DecoratorBlockNode(like TweetNode) exists - A list is inserted after the decorator
- When deleting the first list item, the decorator is also removed
Looking at the code around lines 1826-1855, I see the handling for decorator nodes. The issue seems to be in how the deletion handles the case when the anchor is at a list item. Let me look at the
$collapseAtStartfunction:- A
Assistant
Let me also look at the tests for the list package to understand the expected behavior:
Assistant
Let me look at the caret module to understand how the caret range works when deleting:
Assistant
Let me search for where the bug might be. Looking at the issue more carefully - when deleting the first list item that follows a DecoratorBlockNode, the decorator is also deleted. Let me look at how
$removeTextFromCaretRangeworks:Assistant
Let me look at the issue more specifically. I need to understand what happens when backspace/delete is pressed on a ListItemNode. Let me search for existing tests about decorator node deletion:
Assistant
Let me look at the deleteCharacter test cases to understand the expected behavior:
Assistant
Let me search for the test files more broadly:
Assistant
Let me trace through the deletion flow more carefully. Let me check what happens when we delete from a ListItemNode:
Assistant
Let me check what happens specifically when deleting a ListItemNode at the start. The issue description says the problem is that when you delete the first list item, the previous DecoratorBlockNode is also removed. Let me focus on the key area of the
deleteCharacterfunction:Assistant
I see the issue now. When there's a decorator node adjacent to the current anchor (which would be the ListItemNode or ListNode), and the state is not
'merge-next-block'with an empty element, it just removes the decorator node (line 1850:caret.origin.remove()).The problem is that the condition at line 1834-1838 checks if the anchor origin is an empty element (
initialRange.anchor.origin.isEmpty()), but aListItemNodecontaining text would not be empty. In that case, it falls through to the else branch and removes the decorator node.However, this behavior is incorrect when the anchor is at the start of a list item (or element) that contains text - we should not be deleting the previous decorator in this case. The backspace should just remove the list item's content or convert the list item to a paragraph, not delete the previous decorator.
Let me understand the flow better. Let me check what happens with a non-empty list item at the beginning when pressing backspace:
Assistant
Now I understand the bug better. Let me trace through the scenario:
- User has: DecoratorBlockNode -> ListNode -> ListItemNode (with text "test")
- Cursor is at the start of "test" in ListItemNode
- User presses backspace
When
deleteCharacter(true)is called:forwardDeletionreturns false (cursor is at start of text node)initialCaretis created pointing backwards- The code iterates through
initialRange.iterNodeCarets('shadowRoot')looking for what's before - It finds a sibling caret at the ListNode (since ListItemNode's parent is ListNode)
- Since ListNode is not inline, it sets
state = {block: ListNode, type: 'merge-next-block'}and continues - Continuing iteration, it eventually finds the DecoratorNode
- At line 1834, the condition checks if
state.type === 'merge-next-block'(true) ANDinitialRange.anchor.origin.isEmpty()(false, because the text node "test" exists)
Assistant
Let me understand how
$caretFromPointand$extendCaretToRangework:Assistant
Let me write a test script to reproduce the issue first. Let me check the test infrastructure:
Assistant
Let me look at an existing test file to understand how tests are structured:
Assistant
Now let me create a test script to reproduce the issue:
Assistant
Let me write a simpler test script that doesn't use the test utilities:
Assistant
I need to include the ListNode and ListItemNode in the editor's nodes:
Assistant
I've successfully reproduced the bug. The decorator node is incorrectly deleted when pressing backspace at the start of a list item that follows a decorator.
Now let me understand the issue better. The bug occurs in the
deleteCharacterfunction inLexicalSelection.ts. Let me look at the exact code path:Assistant
I see the issue. The problem is in the decorator handling code. When the iteration reaches a sibling caret at a DecoratorNode:
- If the state is
'merge-next-block'AND the anchor origin is an empty element, it removes the paragraph and selects the decorator - Otherwise, it just removes the decorator node (line 1849-1850:
caret.origin.remove())
But the issue is that when there's a non-empty list item (or any non-empty block element), the code falls through to the else branch and deletes the decorator.
The expected behavior should be:
- If the state is
'merge-next-block'(meaning we found a block element that we should merge with), we should NOT delete the decorator. Instead, the deletion should proceed normally via the regular merge/block handling.
Let me look at how this should work. When the user presses backspace at the start of a non-empty list item:
- We find the ListItemNode's parent (ListNode) as a block element (
state.type === 'merge-next-block') - We continue iterating and find the DecoratorNode as a sibling
- The current code deletes the decorator
- But it should instead stop and NOT delete the decorator, letting the normal merge logic handle the case
- If the state is
Assistant
I see. The issue is:
- When we encounter a DecoratorNode, the code handles it specially.
- If the state is
'merge-next-block', the code only preserves the decorator when the anchor origin is an empty element. - But when the anchor is NOT an empty element (like a list item with text), it deletes the decorator.
The fix should be: when the state is
'merge-next-block', we should NOT delete the decorator. Instead, we should just return without doing anything (or continue the normal merge process). The decorator is not directly related to the deletion of content within a block element.Wait, let me think about this more carefully. The purpose of this code seems to be:
- If there's an empty paragraph followed by a decorator, pressing backspace should select the decorator instead of deleting the paragraph
- If there's a non-empty element followed by a decorator, pressing backspace at the start should delete the decorator
But this logic is wrong when the non-empty element is a structured block like a ListNode/ListItemNode. In that case, the backspace should not delete the previous decorator.
Assistant
continue
Assistant
Now I can see exactly what's happening:
-
The anchor is at the text node ("test item") inside the ListItemNode
-
initialRange.anchor.originis the text node (not an ElementNode) -
The iteration finds:
listitemas a sibling caret (non-inline, sostate = 'merge-next-block')listas a sibling caret (continues because it's also non-inline)test-decoratoras a sibling caret
-
When the decorator is found, the condition checks:
state.type === 'merge-next-block'→ true$isElementNode(initialRange.anchor.origin)→ false (it's a text node)- So the entire condition is false, and we fall into the else branch which deletes the decorator!
The bug is that when
state.type === 'merge-next-block'and we encounter a decorator, we should NOT delete the decorator if the anchor is at a non-empty position. The decorator deletion should only happen when we're trying to delete an empty block.-
Assistant
The issue is clear now. The logic has 3 cases:
- If decorator is isolated: do nothing
- If
state.type === 'merge-next-block'AND anchor is empty element: remove the empty element and select the decorator - Otherwise: delete the decorator
The problem is in case 3. When
state.type === 'merge-next-block'but the anchor is NOT an empty element (like a list item with text), we shouldn't delete the decorator. Themerge-next-blockstate means we found a block that could potentially be merged, so we should let the normal merge logic handle it.The fix should be: when
state.type === 'merge-next-block', we should just return without deleting the decorator. The else branch should only delete the decorator when we're in'initial'state (i.e., no block was found to merge with).Let me look at when the decorator deletion should happen. Looking at the comment "When the anchor is not an empty element then the adjacent decorator is removed", this seems to be for cases where you have inline content with a decorator next to it and you want to delete that decorator.
But when
state.type === 'merge-next-block', it means we're dealing with block elements, and we should NOT delete the decorator. Instead, we should just return and let the code fall through to handle the merge normally.Assistant
Let me fix the open command syntax.
Assistant
Let me look at the exact lines to edit:
Assistant
Now I'll make the fix. The key change is to only delete the decorator when
state.type !== 'merge-next-block':Assistant
Let me check if the replacement worked correctly:
Assistant
The sed command went wrong. Let me restore the file and make a clean edit:
Assistant
Good, the file is restored. Now let me use the
editcommand to make the fix:Assistant
The fix looks correct. Now let me run the test again to confirm the bug is fixed:
Assistant
The fix works. The decorator is now preserved. However, I notice that the list item text "test item" is still there, meaning the backspace didn't delete anything from the list item itself. Let me think about what should happen in this case:
- The user is at the start of the text "test item" in the list item
- They press backspace
- The expected behavior according to the issue: "Only ListNode would be deleted" (or the ListItemNode converted to a ParagraphNode)
But wait, looking at the output, the text is still "test item" - which means the backspace didn't do anything. That might not be the expected behavior either. Let me check if there's supposed to be some additional behavior after returning from the decorator handling...
Assistant
continue
Assistant
Hmm, this is a different case. The forward delete (Delete key) should delete the decorator but it's not. Let me check if my fix broke this case. First, let me restore the file and test the original behavior:
Assistant
So the original code correctly deletes the decorator in the forward delete case. Let me restore my changes and analyze this case:
Assistant
Now let me debug the forward delete case to understand what's happening:
Assistant
Assistant
The decorator is preserved, but nothing else changed. Let me understand what SHOULD happen. Looking at the issue description again, when you press backspace at the start of a list item, the expected behavior is that the list item should be converted to a paragraph (similar to how other editors work).
Let me trace what happens after the decorator handling returns:
Assistant
I see. After the decorator handling returns early (line 1853), the code skips the
if (state.type === 'merge-block')block (since state is'merge-next-block') and falls through to the "// Handle the deletion around decorators" section starting at line 1890.At this point:
this.modify('extend', isBackward, 'character')- tries to extend the selection- If selection is not collapsed, it handles segmented nodes
- If selection is collapsed and
isBackward && anchor.offset === 0, it calls$collapseAtStart - Finally, it calls
this.removeText()
In the original bug case, after pressing backspace at the start of the text, the selection is still collapsed at offset 0, so it goes to
$collapseAtStartwhich might handle the list conversion.But wait, the issue is that in my fix, when the decorator is encountered, we return early from the
deleteCharacterfunction. So the code after the for loop (starting at line 1890) is never reached!Let me check if the return statement is causing the issue:
Assistant
I see - there's a
return;statement at line 1852 that says "always stop when a decorator is encountered". This means when a decorator is found, the function returns immediately and doesn't proceed to the merge logic below.So the issue is that when
state.type === 'merge-next-block'and we encounter a decorator, the function returns without doing anything (with my fix). But we should continue to let the merge logic handle the situation.Let me reconsider the fix. The correct behavior should be:
- If decorator is isolated: do nothing and return
- If
state.type === 'merge-next-block'and anchor is empty element: remove empty element and select decorator, return - If
state.type === 'merge-next-block'and anchor is NOT empty element: DON'T return, let the merge logic below handle it - Otherwise: delete decorator and return
So instead of just not deleting the decorator, we should also NOT return. Let me update the fix:
Assistant
edit --file /lexical/packages/lexical/src/LexicalSelection.ts 1830:1855 " } else if ($isDecoratorNode(caret.origin)) { if (caret.origin.isIsolated()) { // do nothing, shouldn't delete an isolated decorator return; } else if ( state.type === 'merge-next-block' && (caret.origin.isKeyboardSelectable() || !caret.origin.isInline()) && $isElementNode(initialRange.anchor.origin) && initialRange.anchor.origin.isEmpty()
Assistant
The edit command seems to have had an issue. Let me try again with a simpler approach.
Assistant
Assistant
The bash command didn't execute properly. Let me try again.
Assistant
OK, I need to test the original behavior without my fix. Let me first confirm the original code and then run the test.
Assistant
Good, I can see the bug confirmed - the decorator is incorrectly deleted. Now let me apply the fix properly using the edit command:
Assistant
I need to modify the code to fix the bug. The issue is in the else branch at lines 1847-1851. When
state.type === 'merge-next-block', we should NOT delete the decorator. Let me replace the else branch with a more specific condition.The fix should change:
} else { // When the anchor is not an empty element then the // adjacent decorator is removed caret.origin.remove(); }To:
} else if (state.type !== 'merge-next-block') { // When no block was found to merge with, the // adjacent decorator is removed caret.origin.remove(); }This way, when state is 'merge-next-block', we won't delete the decorator.
Assistant
Now let me test if the fix works:
Assistant
The fix works for the original bug - the decorator is now preserved. Let me also test the forward delete case to make sure it still works:
Assistant
My fix broke the forward delete case. The issue is that I need to distinguish between backward delete (where we shouldn't delete the decorator) and forward delete (where we should delete the decorator).
Looking at the debug output, the key difference is:
- Backward delete (original bug): The block we found (ListItemNode) has parent ListNode, which has a different parent than the decorator (root)
- Forward delete: The block we found (ParagraphNode) and the decorator share the same parent (root)
So the fix should check if the block and decorator share the same parent. Only delete the decorator if they're at the same level.
Let me update the fix:
Assistant
The user seems to have lost context. Let me check the current state of the file and continue with the fix.
Assistant
I see. Let me continue with the fix. The issue is that my previous fix broke the forward delete case. I need to distinguish between:
- Backward delete with nested structure (original bug): The block and decorator don't share the same parent → DON'T delete decorator
- Forward delete with same-level blocks: The block and decorator share the same parent → DELETE decorator
The key is to check if
state.blockandcaret.origin(the decorator) share the same parent.Assistant
The parent check doesn't help distinguish the cases. Let me dig deeper into what differentiates them.
Assistant
Now I understand the issue better. The key difference is:
- In the list case,
state.block(ListNode) is NOT the direct parent of the anchor origin (text). The ListItemNode is the direct parent. - In the paragraph case,
state.block(ParagraphNode) IS the direct parent of the anchor origin (text).
The fix should be: only delete the decorator when
state.blockis the direct parent ofinitialRange.anchor.origin. When there are intermediate wrappers (like ListItemNode between ListNode and text), we should NOT delete the decorator.- In the list case,
Assistant
Let me also verify the forward delete case:
Assistant
The analysis shows that the fix should check if
state.blockis the direct parent ofinitialRange.anchor.origin. If it is, delete the decorator (normal case). If not (there are intermediate wrappers like ListItemNode), don't delete the decorator.Let me apply this fix:
Assistant
Let me test both the original bug case and the forward delete case to verify the fix works correctly:
Assistant
The previous command executed successfully and showed that the fix works for the original bug case. Let me now test the forward delete case.
Assistant
Both test cases pass now:
- Original bug case (Decorator -> List -> ListItem): Decorator is preserved ✅
- Forward delete case (Paragraph -> Decorator): Decorator is correctly deleted ✅
Let me now run the existing tests in the repository to make sure the fix doesn't break any existing functionality.
Assistant
The user is reminding me about the proper tool usage format. Let me continue testing the fix by running the tests properly.
Assistant
Let me try a different approach to run the tests. Let me check how tests are structured in this repo.
Assistant
The user is showing me the output from my last command. I need to continue running tests to verify my fix. Let me use the vitest.config.mts file at the root to run tests.
Assistant
The test failures are due to DOM environment issues ("document is not defined"), not my code changes. Let me run the list-specific tests which use headless editor:
Assistant
The test failures are all due to
ReferenceError: document is not definedwhich is a test environment setup issue, not related to my code changes. Let me verify by running the same tests on the original code to confirm:Assistant
The test failures are identical with and without my changes - they're environment setup issues (
document is not defined), not caused by my fix. Let me restore my changes and test more edge cases:Assistant
Let me test more edge cases to ensure my fix is robust:
Assistant
All edge case tests pass. Let me also run the headless tests that don't require DOM to verify my fix doesn't break existing functionality:
Assistant
All headless tests pass. Let me clean up the temporary test files and review my final changes:
Assistant
Let me review my changes one final time:
Assistant
The fix looks good. Let me run one final verification test to confirm everything works:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.