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 start by understanding the issue. The bug is about a Lexical editor where deleting the leading ListItemNode also deletes the previous DecoratorBlockNode. This is in the Lexical JavaScript editor library.
Let me first explore the repository structure to understand how it's organized.
Assistant
This is the Lexical editor repository. The issue is about List and DecoratorBlockNode interaction. Let me look at the packages directory to find the relevant list-related code.
Assistant
Let me look at the test files to understand how lists are tested and how the delete operation might be related:
Assistant
Now let me look at the main LexicalListItemNode.ts file since the issue is about deleting a ListItemNode:
Assistant
Let me search for the delete-related functionality. The issue is about deleting a ListItemNode and how it affects the previous DecoratorBlockNode. Let me look at the core Lexical package for delete handling:
Assistant
Let me search for where the delete backward command is dispatched and how it's handled:
Assistant
Let me search for where DELETE_CHARACTER_COMMAND is handled:
Assistant
<parameter="" ="">Let me look at the lexical-rich-text handling of DELETE_CHARACTER_COMMAND: </tool_call>
Assistant
Let me look at the lexical-rich-text handling of DELETE_CHARACTER_COMMAND to understand how deletion works.
Assistant
Let me look at the
$getSiblingCaretfunction and related caret handling in Lexical, since the issue seems to be related to how the caret is positioned when deleting a list item. The bug mentions that when you delete the first list item, the previous DecoratorBlockNode is also removed.Assistant
Let me search for where the delete behavior is handled when a list item is being deleted. The issue is about deleting the leading ListItemNode and it also deleting the previous DecoratorBlockNode. Let me look at the selection delete behavior.
Assistant
The
deleteCharacterfunction seems to be the key. Let me look at it more closely, especially around lines 1763-1890:Assistant
Let me look at the test file for ListItemNode to understand the expected behavior:
Assistant
No decorator-related tests in the list item tests. Let me search more broadly:
Assistant
Now I need to understand the issue better. Looking at the code around lines 1830-1852, I see the handling for decorators when deleting. The issue is that when deleting the first list item after a DecoratorBlockNode, the decorator is also being removed.
Let me look at the key section more closely - the part that handles removing adjacent decorators:
Assistant
I see the issue. The problem is in lines 1847-1850. When the anchor is not an empty element, the adjacent decorator is removed. But this logic is too aggressive - it removes the decorator even when the element is a ListItemNode (or more specifically, a ListNode wrapped around it).
The issue is that when you have:
- A DecoratorBlockNode
- A ListNode with ListItemNode
And you delete the first list item (backspace at the start of the first list item), the code finds the decorator and removes it.
Let me understand this better by looking at what
$isElementNode(initialRange.anchor.origin)returns for a list item scenario:Assistant
Let me understand the issue better. When the cursor is at the start of a ListItemNode and you press backspace:
- The selection is collapsed at the start of the ListItemNode
- The code looks for a sibling in the "previous" direction (backspace)
- It finds the DecoratorBlockNode
- Since
initialRange.anchor.originis the ListItemNode (which is an ElementNode) and it's NOT empty, the code goes to the else branch and removes the decorator
The problem is that when the ListItemNode itself has content (text), it's not considered empty, so the decorator is removed. But the expected behavior should be that only the list is affected, not the decorator.
Let me look at what the condition should be. The current logic at lines 1847-1850 removes the decorator when:
- The state is NOT 'merge-next-block' (or the other conditions don't match)
- OR when
initialRange.anchor.origin.isEmpty()is false
The fix should ensure that we don't remove the decorator when deleting backward from a list item. Let me check if there's a specific condition for this:
Assistant
Let me look at how tests are run for this project:
Assistant
Let me run the tests related to lists first to understand the current state:
Assistant
The test timed out. Let me try a more targeted approach - first let me look at the existing test infrastructure and understand what I need to fix:
Assistant
Let me look at more tests about delete and decorators:
Assistant
Let me search for tests involving delete with blocks:
Assistant
Let me search for tests related to this behavior:
Assistant
Let me search for tests that might involve the delete behavior with decorators and blocks:
Assistant
Let me look for e2e tests or integration tests that involve this behavior:
Assistant
Let me search for e2e/playwright tests in the lexical packages:
Assistant
Let me understand the issue better by examining the code flow. The problem occurs when:
- There's a DecoratorBlockNode (like TweetNode)
- A ListNode follows with ListItemNodes
- When deleting backward from the first character of the first ListItemNode, the decorator is also removed
Let me look at what happens when we iterate through node carets. The key is understanding when a decorator is encountered:
Assistant
Let me look at the test utilities to understand how tests are structured:
Assistant
Assistant
Let me try the command again with the proper format.
Assistant
Now I understand the issue. The problem is in the else block (lines 1847-1850) where it removes the decorator when the anchor element is not empty. This is incorrect for block-level decorators.
The fix should only remove inline decorators in this case, not block-level decorators. Block-level decorators should be preserved.
Assistant
Now I need to fix the code. The issue is at lines 1847-1850. The current code removes the decorator when the anchor element is not empty. This is wrong for block-level decorators.
The fix should only remove the decorator when it's inline. For block-level decorators, we should just return without removing anything.
Assistant
The sed command didn't work as expected - it replaced each line separately instead of the block. Let me revert and fix this properly.
Assistant
Let me use a different approach to edit the file - I'll use the edit command properly.
Assistant
I need to replace lines 1847-1850 with the fixed code. Let me use the replace command:
Assistant
Let me try a different approach using sed or another available tool:
Assistant
I need to include a proper tool call with function name and arguments.
Assistant
Good, node is available. Let me use it to apply the fix.
Assistant
Let me verify the fix was applied correctly.
Assistant
The fix has been applied. Let me verify the change by looking at the diff:
Assistant
The fix looks correct. Let me now verify by running the relevant tests to make sure I haven't broken anything.
Assistant
The test timed out. Let me try running a smaller subset of tests or check if there's a way to run tests faster.
Assistant
The command is timing out, possibly due to pnpm trying to download something. Let me try a different approach - just check if the TypeScript compiles without errors using tsc directly.
Assistant
Let me verify the fix by reviewing the code structure more carefully to ensure it's syntactically correct.
Assistant
The code structure looks correct. Let me now look at how I can verify the fix works by examining the test utilities and creating a simple test script.
Assistant
Let me look at the test utilities file to understand how TestDecoratorNode works and also check if there are any existing tests for the deleteCharacter behavior with decorators.
Assistant
The user seems to have sent an empty message. Let me continue with my work on the fix. I need to find the test utilities file and verify the fix is working correctly.
Assistant
The user sent an empty message, so I should continue with my work. I was looking for test utilities to understand how TestDecoratorNode works. Let me continue exploring.
Assistant
continuing = True
Assistant
Let me check if TestDecoratorNode is inline or block-level, which is important for verifying my fix.
Assistant
Good, I can see that
TestDecoratorNodeby default is inline (__block: false). My fix should only affect block-level decorators. Let me now check if there are any tests related to decorator deletion behavior that I should verify.Assistant
Let me continue verifying my fix by looking at any existing tests related to this behavior and trying to run them.
Assistant
It seems the search returned no results. Let me try a different approach to verify my fix is working. I should look at how the tests are structured and see if I can find any related test files.
Assistant
Good, I found some tests that use
deleteCharacter. Let me look at this test to understand how it's used and how I can verify my fix.Assistant
Good, I can see how the tests work. Let me look for any existing tests that specifically test the behavior of deleting backward from a list item when there's a previous block. Let me also search for any tests that involve decorator nodes and lists.
Assistant
I found some relevant test files. Let me look at the RichTextInlineDecoratorMoveToStart.test.ts since it mentions "block (non-inline) decorator" which is related to my fix.
Assistant
Let me review my fix again and think about edge cases to make sure the solution is complete and correct.
Assistant
My fix looks correct. Let me now analyze the code flow to understand how my fix handles the bug scenario:
Bug scenario:
- Document has:
[DecoratorBlockNode] [ListNode → ListItemNode(text)] - Cursor is at the start of the text in ListItemNode
- User presses Backspace
Before my fix:
- The code iterates through node carets looking for adjacent blocks
- When it encounters the
DecoratorBlockNodeas a sibling caret:- It's not isolated
state.typeis'merge-next-block'(because we saw the ListItemNode/ListNode block)
- Document has:
Assistant
The diff looks correct. Let me try to run a quick syntax check using node to make sure there are no syntax errors:
Assistant
Good, the syntax check passed. Let me now try to run the tests to verify my fix doesn't break any existing functionality. Let me try running a smaller subset of tests first.
Assistant
Good, the HeadingNode tests pass. Let me run more tests related to decorators and selection to verify my fix doesn't break anything.
Assistant
Let me run more tests to ensure my fix doesn't break anything.
Assistant
Excellent! All 53 selection tests pass! Let me also run the list-related tests to make sure nothing is broken there.
Assistant
All 98 list tests pass. Let me also run some decorator-related tests to make sure my fix doesn't break existing decorator behavior.
Assistant
All rich-text tests pass. Let me run more comprehensive tests to make sure my fix doesn't break anything else.
Assistant
All 582 tests in the lexical package pass. This is great news - my fix doesn't break any existing tests. Let me now verify my understanding of the fix and make sure the change is complete.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.