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
I'll start by exploring the repository structure to understand the codebase and locate relevant files. Let's first get an overview.
- Assistant
Let me explore the repo structure more broadly.
- Assistant
Let me look at the packages directory to find relevant packages.
- Assistant
The issue is about deleting a list item that's after a DecoratorBlockNode. This likely involves the
lexical-listpackage. Let me explore it. - Assistant
Let me look at the lexical-list package to understand the list implementation and find where the deletion logic might be.
- Assistant
Let me look at the core lexical package for delete handling code. The issue likely relates to how deletion handles selection at the boundaries of block nodes.
- Assistant
Let me search for delete-related code in the lexical package.
- Assistant
Let me search for relevant code related to list deletion and decorator nodes.
- Assistant
Let me search for the deletion-related code in LexicalSelection.ts and the registerList.ts
- Assistant
Let me understand the issue better. When you delete the leading ListItemNode, the previous DecoratorBlockNode is also being deleted. Let me look at the selection/deletion handling for list items.
Let me search for relevant code in the lexical-list and lexical packages.
- Assistant
Let me search for the
insertBeforeandinsertAftermethods ofDecoratorBlockNodeand the list node removal logic. The issue seems to be that when the first list item is deleted, the list node itself gets removed, and somehow that also removes the decorator block node.Let me look at the
LexicalListItemNodemore carefully, especially theremovemethod and any methods that handle what happens when a list item is deleted. - Assistant
Let me look at the LexicalListItemNode's remove method more closely.
- Assistant
Let me look at the remove method of ListItemNode specifically.
- Assistant
Let me look at the remove method and the insertAfter method of ListItemNode.
- Assistant
Now let me look at the
insertNewAftermethod and the deletion flow. - Assistant
Let me search for how
collapseAtStartis called to understand the deletion flow. - Assistant
Let me look at the
collapseAtStartin LexicalElementNode and the calling code in LexicalSelection. - Assistant
Let me look at the
$collapseAtStartfunction: - Assistant
Let me look at the
removeTextmethod which is called after thecollapseAtStartreturns false: - Assistant
Let me look at the
deleteCharactermethod and understand how the deletion of the first list item works: - Assistant
Let me look at the ListNode and its insertAfter/remove methods:
- Assistant
Let me understand the full flow of the deletion. Let me look at the test files for relevant tests:
- Assistant
Let me look at the
deleteCharactermethod more carefully. The issue seems to involve the caret-based deletion path when the first list item is deleted: - Assistant
Now I understand the issue. Let me trace through the flow when backspace is pressed at the beginning of the first list item. Let me look at how the caret-based deletion works:
- Assistant
Let me understand how the caret system works. Let me search for the specific caret types and understand how the iteration traverses:
- Assistant
Let me look at the caret utilities to understand how caretFromPoint and extendCaretToRange work.
- Assistant
Let me focus on reproducing the issue first. Let me look at the existing tests and create a reproduction script.
- Assistant
Let me look at the relevant code more carefully. I need to understand how caret-based deletion works when pressing backspace at the start of a list item that follows a DecoratorBlockNode:
- Assistant
Now let me understand the issue better. Let me look at how the
collapseAtStartinListItemNodeworks and how it interacts with the caret-based deletion: - Assistant
Let me look at the caret iteration more carefully:
- Assistant
Let me look more carefully at the caret traversal. Let me understand what happens when we delete at the start of a list item:
- Assistant
Let me look at how the existing tests handle list deletion. Let me check if there's a test for this scenario:
- Assistant
Let me trace through the exact flow for the bug scenario. Let me look at how
$caretFromPointworks when the cursor is at the beginning of a list item: - Assistant
Let me look at
$extendCaretToRangeto understand how the range is formed: - Assistant
Let me search for $extendCaretToRange:
- Assistant
Let me now trace through the exact flow when pressing backspace at the start of the first list item. Let me understand how the caret traversal works:
- Assistant
Let me understand the exact scenario. I need to trace what happens when backspace is pressed at the beginning of a list item that follows a DecoratorBlockNode. Let me look at the caret iteration:
- Assistant
Let me look at the caret iteration behavior more carefully. Let me write a test to understand the issue:
- Assistant
Let me look at how
ParagraphNode.collapseAtStartworks: - Assistant
Now I have a good understanding of the issue. Let me trace through the exact flow:
When pressing backspace at offset 0 of the first text node inside the first ListItemNode that follows a DecoratorBlockNode:
- The caret-based deletion code in
deleteCharacteriterates backward - It encounters ChildCaret(ListItemNode) - state stays 'initial' (not 'merge-next-block')
- It encounters ChildCaret(ListNode) - state stays 'initial'
- It encounters SiblingCaret(DecoratorBlockNode) - it's a DecoratorNode
- Since
state.typeis still 'initial' (not 'merge-next-block'), theelsebranch is taken caret.origin.remove()- the DecoratorBlockNode is removed!
The bug is in the
elsebranch of the decorator handling at line 1874. Whenstate.typeis 'initial' (meaning we're inside a nested block like a list), the decorator should NOT be removed. Instead, we should break out of the loop and letcollapseAtStarthandle it.Let me implement the fix:
- The caret-based deletion code in
- Assistant
Let me see the exact decorator handling block:
- Assistant
Now I understand the full issue. Let me trace through the exact flow:
- Structure:
DecoratorBlockNode → ListNode → ListItemNode → TextNode (cursor at offset 0) - Pressing backspace triggers
deleteCharacter(true) - The caret-based deletion iterates backward through the tree
- ChildCaret(ListItemNode): state stays
'initial'(not'merge-next-block') - ChildCaret(ListNode): state stays
'initial' - SiblingCaret(DecoratorBlockNode):
$isDecoratorNodeis true - Since
state.typeis'initial'(not'merge-next-block'), theelsebranch removes the decorator!
The fix: when
state.typeis not'merge-next-block', we shouldn't remove the decorator. Instead, break out of the loop and let$collapseAtStarthandle it (which will correctly convert the list item to a paragraph). - Structure:
- Assistant
Now let me apply the fix:
- Assistant
Now let me apply the fix:
- Assistant
Good, the fix is applied. Let me verify the complete changed block looks correct:
- Assistant
Now let me create a test to verify the fix. First, let me look at existing test patterns:
- Assistant
Let me look at the
$createTestDecoratorNodeand existing test patterns: - Assistant
Let me find the test utils.
- Assistant
Let me find the test utils:
- Assistant
Let me look at the test utils to find the
initializeUnitTestand$createTestDecoratorNodeutilities. - Assistant
Let me look at the test for deleting decorator nodes:
- Assistant
Let me search for tests related to this issue or to the deleteCharacter method.
- Assistant
Let me look for existing tests about decorator deletion:
- Assistant
Let me first run the existing tests to make sure they pass:
- Assistant
The tests are failing with "ReferenceError: document is not defined" which suggests we need a jsdom environment. Let me check the proper way to run these tests.
- Assistant
Let me check the vitest configuration to understand the test environment:
- Assistant
Let me check how the tests are actually run:
- Assistant
Let me try running just the unit tests specifically:
- Assistant
All existing LexicalSelection tests pass. Let me also run the list tests:
- Assistant
All tests pass. Now let me write a test to verify the fix. Let me create a reproduction script:
- Assistant
Let me look at the existing test structure and write a proper test. Let me look at how
initializeUnitTestworks: - Assistant
Let me look at the existing
initializeUnitTestand$createTestDecoratorNodeto understand how to use them in the test: - Assistant
Let me look at how
$createTestDecoratorNodeis used: - Assistant
Let me look at the
$createTestDecoratorNodefunction: - Assistant
Let me look at the
$createTestDecoratorNodefunction: - Assistant
Now let me write a test script to verify the fix. I'll create a temporary test file:
- Assistant
Let me write a simpler test that directly uses the editor API:
- Assistant
Let me copy the test to the right location and run it:
- Assistant
Let me run the test to see if my fix works.
- Assistant
Let me fix the test - the list type is 'list' not 'bullet'. Also, the second test is failing because the decorator is still being deleted. Let me investigate the issue further.
- Assistant
Let me look at the ListNode to see its type:
- Assistant
Let me fix the test:
- Assistant
Let me run the tests again:
- Assistant
The test shows the decorator is still being removed. After the
break, the code continues tothis.modify('extend', isBackward, 'character')which extends the selection into the decorator, and thenremoveTextdeletes it. I need to call$collapseAtStartdirectly instead of just breaking: - Assistant
The
breakalone doesn't work because after the loop, the code continues tothis.modify('extend', isBackward, 'character')which extends the selection into the decorator and then removes it. I need to directly call$collapseAtStartinstead: - Assistant
Now let me run the test again:
- Assistant
The output still shows the decorator being removed. Let me debug this by checking what's actually happening in the flow. Let me add some logging:
- Assistant
The issue is still occurring. Let me add some debug logging to understand what's happening:
- Assistant
Let me understand the caret iteration better. Let me look at
$getAdjacentChildCaret: - Assistant
I see the issue now. The
$collapseAtStartis never reached because theelse if (state.type === 'merge-next-block')branch fires first. The state IS'merge-next-block'because the ListNode was encountered as a SiblingCaret before the DecoratorBlockNode. ButinitialRange.anchor.originis the TextNode (not an ElementNode), so the empty-element check fails.Let me understand this better. The state becomes
'merge-next-block'when iterating backward from the text node:- TextNode → ChildCaret(ListItemNode) → doesn't change state
- ListItemNode → ChildCaret(ListNode)? No, let me trace more carefully.
Let me look at how
ChildCaret.getParentCaretworks: - Assistant
Let me understand the iteration better. Let me check the SiblingCaret.getParentCaret:
- Assistant
Now I understand! The state IS
'merge-next-block'because the ListNode was encountered as a SiblingCaret. But$isElementNode(initialRange.anchor.origin)fails because the anchor origin is a TextNode, not an ElementNode. So the first condition fails and theelsebranch removes the decorator.The fix should check if the anchor is at text level (inside a nested block like a list) rather than at element level. Let me update the fix:
- Assistant
Now let me run the test again:
- Assistant
Both tests pass now! The fix works:
- First test:
[ 'test_decorator', 'paragraph' ]- decorator preserved, list converted to paragraph ✓ - Second test:
[ 'test_decorator', 'paragraph', 'list' ]- decorator preserved, first item converted to paragraph ✓
Now let me run the existing tests to make sure nothing is broken:
- First test:
- Assistant
All existing LexicalSelection tests pass. Let me run the list tests too:
- Assistant
All existing tests pass. Let me run a broader set of tests to make sure nothing is broken:
- Assistant
All 584 tests pass. Let me also check the rich-text tests:
- Assistant
Let me also check the Issue8563Repro test and run a few more test suites:
- Assistant
Let me now clean up the temporary test file and run a comprehensive set of tests to ensure nothing is broken:
- Assistant
Let me run the full unit test suite to make sure nothing is broken:
- Assistant
All 2962 tests pass (1 skipped). Let me review the final state of my changes:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.