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 the leading
ListItemNodein aListNodewhich also deletes the previousDecoratorBlockNode - The expected behavior is that only the
ListItemNodeshould be deleted, or theListNodeshould be replaced by aParagraphNode
Let me start by exploring the repository structure to understand the codebase.
- The bug is about deleting the leading
Assistant
This is a monorepo. Let me explore the packages directory since this is a Lexical editor bug.
Assistant
The issue is about deleting a
ListItemNodewhich also deletes the previousDecoratorBlockNode. This is likely in thelexical-listpackage. Let me explore itAssistant
Let me look at the relevant files. First, let me understand how list items are removed:
Assistant
Let me look at the
$removeListItemfunction informatList.ts:Assistant
Let me see the full
$removeListfunction and check the utilsAssistant
Now let me look at
utils.tsfor the list utility functions:Assistant
Let me search for the deletion handling in list code:
Assistant
The deletion logic might be in the core
lexicalpackage. Let me look at the Lexical core for backspace/delete handlingAssistant
Let me look at
$removeNodefunction in LexicalNodeAssistant
The
line_numberparameter may not be accepted. Let me try opening with just the file path.Assistant
Let me look at
removeFromParentand the merge logic:Assistant
Let me look at the
removeFromParentfunction in LexicalNode.ts:Assistant
Let me search more broadly for where
removeFromParentis defined:Assistant
Let me look at
removeFromParentinLexicalUtils.ts:Assistant
Let me see the full
removeFromParentfunction:Assistant
Let me look at the
deleteCharacterfunction in LexicalSelection.ts:Assistant
Let me look at the
insertAfterfunction:Assistant
Let me look at
insertAfterAssistant
Let me look at the list tests to understand the test structure and potentially find related tests:
Assistant
Let me look at the unit tests:
Assistant
Let me search for tests related to decorator and list interaction
Assistant
Let me search more broadly for tests involving decorators and lists
Assistant
Let me look at
forwardDeletionAssistant
Let me look at
forwardDeletionAssistant
Let me look at the
LexicalListItemNodeto check for custom removeAssistant
Let me look at the
removemethod onListItemNodeAssistant
Let me look at the
$collapseAtStartfunction referenced in deleteCharacterAssistant
The grep syntax might be wrong. Let me fix it.
Assistant
Let me look at the
$collapseAtStartfunction:Assistant
Let me look at how tests are structured, particularly the formatList test:
Assistant
Let me look at the
$createTestDecoratorNodeutility in the test utilitiesAssistant
Let me find where the test decorator node is defined:
Assistant
Let me find the source of
$createTestDecoratorNode:Assistant
Let me search more broadly
Assistant
Let me look at the test utilities file
Assistant
The file might be at a different location. Let me search for it
Assistant
Hmm, that didn't find anything. Let me check where the imports are coming from.
Assistant
Let me find where
$createTestDecoratorNodeis definedAssistant
Found it. Let me look at the TestDecoratorNode:
Assistant
Let me look at
moveSelectionPointToSibling:Assistant
Let me look at `moveSelectionPointToSibling
Assistant
Let me check the current version of the project
Assistant
Let me look at the selection tests to see how deletion tests are structured:
Assistant
Let me look at the LexicalSelection tests for deletion-related tests
Assistant
Let me look at the broader test directory to find any tests related to backspace with lists
Assistant
Let me look at this from a different angle. Let me search for tests that combine DecoratorNode and ListNode or ListItemNode
Assistant
Let me look at the LexicalCaret test that has a decorator node in a list item:
Assistant
Let me look at the formatList test for patterns:
Assistant
Let me now try to create a script to reproduce the issue. First, let me understand the
initializeUnitTestutility:Assistant
Let me look at the
initializeUnitTestfunction:Assistant
Let me see how to run tests:
Assistant
Let me look at the package.json scripts:
Assistant
Let me check the package.json differently:
Assistant
Let me try a more direct approach. Let me create a test script to reproduce the bug:
Assistant
Let me try to run this test
Assistant
Let me try running an existing test first
Assistant
Let me run with the
unitprojectAssistant
Let me look more at the caret iteration code to understand the flow
Assistant
Let me look at the
iterNodeCaretsimplementation:Assistant
Let me look at
$extendCaretToRange:Assistant
Let me fix the grep command
Assistant
Let me look at the
$extendCaretToRangefunction:Assistant
Let me look at
getParentCaret:Assistant
Let me look at the ChildCaret's
getParentCaret:Assistant
Now I have a much better understanding of the issue. Let me look at the specific code in
deleteCharacterthat handles decoratorsAssistant
Let me look at registerList.ts to see what commands are registered:
Assistant
Let me look at
selectPrevious:Assistant
Let me look at
selectPrevious:Assistant
Let me search for any related fixes or mentions of this issue:
Assistant
Let me search for relevant commits
Assistant
There's a commit related to
deleteCharacter through ListNode->ListItemNode. Let me look at it:Assistant
Let me see the full diff for this commit:
Assistant
Let me try with different options:
Assistant
Let me look at the commit about "Improve character deletion around shadow roots and decorators":
Assistant
Let me re-read the current
deleteCharactermethod to understand the current state:Assistant
Assistant
I've identified the bug. Let me now implement the fix. The issue is in
packages/lexical/src/LexicalSelection.tsin thedeleteCharactermethod, specifically in the decorator handling section (around lines 1831-1854).When a DecoratorBlockNode is encountered during caret iteration and the anchor origin is not an empty element, the decorator incorrectly gets removed. The fix should prevent this by falling through to the
$collapseAtStartlogic.Let me first look at the exact lines I need to modify
Assistant
Let me look at
$caretFromPointto understand what kind of caret is returnedAssistant
Let me look at `$caretFromPoint
Assistant
Now I understand the bug clearly. Let me implement the fix. The issue is in
packages/lexical/src/LexicalSelection.ts. Whenstate.type === 'merge-next-block'(meaning we found a block between the cursor and the decorator) but the anchor origin is NOT an empty element (it has content), the decorator should NOT be removed. Instead, we should just return and let$collapseAtStarthandle the list.Assistant
The edit looks correct. Let me verify by checking the modified code:
Assistant
Now let me run the existing tests to make sure I haven't broken anything
Assistant
There's a parse error at line 1956. Let me check what's there
Assistant
Let me check the full context around my edit to ensure there's no syntax issue
Assistant
Let me look at the original code structure more carefully around the break statement
Assistant
Let me check my current code more carefully for the break statement:
Assistant
I see the issue - my edit accidentally removed the
break;statement. Let me fix itAssistant
Good, the code looks correct now. Let me verify with
Assistant
Let me double-check the brace structure:
Assistant
I see the issue - I'm missing the closing brace for the
forloop. My edit accidentally removed it. Let me fix thisAssistant
I have a duplicate line now. Let me fix that
Assistant
Let me verify the diff
Assistant
The diff looks clean. Let me now run the tests
Assistant
All 53 selection tests pass. Let me run more related tests
Assistant
All 98 list tests pass. Let me also run broader tests for the lexical core
Assistant
All 582 lexical core tests pass. Let me also run the caret tests and rich-text tests
Assistant
All caret tests pass too. Let me also run the rich-text tests
Assistant
All tests pass. Let me also run the history and other extension tests
Assistant
All tests pass. Let me verify the final change 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.