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:
- There's a bug in Lexical (a text editor framework) where deleting the leading ListItemNode in a list also deletes the previous DecoratorBlockNode.
- The expected behavior is that only the ListItemNode should be deleted (or ideally, the ListNode should be replaced by a ParagraphNode).
Let me explore the repository structure first to understand the codebase.
Assistant
Let me explore the packages directory and find the relevant code for list handling.
Assistant
The issue is about list handling. Let me look at the
lexical-listpackage and also search for relevantAssistant
Let me look at the core lexical package for deletion handling, and also look at the list-related
Assistant
Let me look at the LexicalSelection.ts file which likely handles backspace/delete logic, and also search for "decorator" handling:
Assistant
Let me look at the key files. The LexicalSelection.ts is likely where the backspace/delete logic lives:
Assistant
Let me search for relevant code in LexicalSelection.ts related to deletion:
Assistant
Let me look at the
deleteCharactermethod and surrounding code in detail:Assistant
Let me also look at the LexicalSelection test file and search for any existing tests related to this
Assistant
Let me look at the
$collapseAtStartfunction and search for list-specific backspace handlingAssistant
Let me look at the
$collapseAtStartfunction:Assistant
Let me look at the LexicalListNode's
collapseAtStartmethodAssistant
Let me look at the ListItemNode:
Assistant
Let me look at the
forwardDeletionmethod:Assistant
Let me look at the
forwardDeletionmethod:Assistant
Let me look at the
iterNodeCaretsmethod and related caretAssistant
Let me look at the caret utilities:
Assistant
Let me look at LexicalCaret.ts for the
iterNodeCaretsmethod.Assistant
Let me look at the
iterNodeCaretsimplementation in LexicalCaret.ts:Assistant
Let me look at the
iterNodeCaretsimplementationAssistant
Let me look at
$getAdjacentChildCaret:Assistant
Let me look at
$getAdjacentChildCaretAssistant
Let me look at test utilities to understand how to create a test that reproduces this:
Assistant
Let me look at the TestDecoratorNode to understand how it works:
Assistant
Let me look at the
TestDecoratorNodeclass definitionAssistant
Let me look at the TestDecoratorNode class in
Assistant
Let me look at how
isShadowRootworks and what nodes are shadow roots:Assistant
Let me search for
isShadowRootacross the codebase:Assistant
Let me look at the
isShadowRootmethodAssistant
Let me search for all nodes overriding `isShadowRoot
Assistant
Let me look at an existing test to understand the test structure for LexicalSelection
Assistant
Let me look at the LexicalSelection test around line 940-1040 for backspace/delete tests with
Assistant
Let me look at the deleteCharacter tests:
Assistant
Let me look at the LexicalListPlugin test:
Assistant
Let me look at the
deleteCharactermethod from line 1885 onwards:Assistant
Let me look at the
collapseAtStartmethod in the base ElementNode:Assistant
Let me look at the
collapseAtStartAssistant
Let me look at git log for any recent changes to list-related code:
Assistant
Let me look at
$getParentCaret:Let me look at$getParentCaretAssistant
Let me search for
getParentCaret:Assistant
Let me look
Assistant
Let me look
Assistant
Let me search for
filterByMode:Assistant
Let me see
$filterByMode:Assistant
Let me look at the
removeTextmethod:Assistant
Let me look at
removeTextand$removeTextFromCaretRange:Assistant
Let me search for
$removeTextFromCaretRangeAssistant
Let me find where
$removeTextFromCaretRangeis defined:Assistant
Let me look at the
$removeTextFromCaretRangefunction:Assistant
Let me look at
$getBlockMergeTargets:Assistant
Let me search for any mention of this issue in the codebase or changelog:
Assistant
Let me look at the formatList test that involves decorator nodes:
Assistant
Let me look at the
modifymethod of RangeSelection:Assistant
Let me look at the
modifymethod at line 1582:Assistant
Let me look at
$modifySelectionAroundDecoratorsAndBlocks:Assistant
Let me search for
INTERNAL_$isBlock:Assistant
Let me look
Assistant
Let me create a reproduction script to understand the issue
Assistant
Let me look at how to run tests:
Assistant
Let me search for e2e tests related to list deletion:
Assistant
Let me find e2e tests for lists:
Assistant
Let me search for relevant test cases in the List e2e spec:
Assistant
Let me look at the LexicalCaret test for removeTextFromCaretRange tests
Assistant
Let me look at how PointCaret iteration works (the
[Symbol.iterator]for caret):Assistant
Let me look
Assistant
Let me look at
$getSiblingCaret:Assistant
Let me search for
function $getSiblingCaret:Assistant
Let me search for the function definition:
Assistant
Let me look at
$getSiblingCaret:Assistant
Let me look for
$extendCaretToRange:Assistant
Let me look at
$extendCaretToRange:Assistant
Now let me understand the issue better. Let me search for relevant code in the
deleteCharacterflow and the$modifySelectionAroundDecoratorsAndBlocksfunction:Assistant
Let me look at the
getNodeAtCaretfor a SiblingCaret:Assistant
Let me look at the SiblingCaret
getNodeAtCaretimplementation:Assistant
Let me search for the
getNodeAtCaretin the SiblingCaret class:Assistant
Let me find the
getNodeAtCaretimplementation for sibling caret:Assistant
Let me look at lines 542 and 556:
Assistant
Let me look at the sibling caret
getNodeAtCaretAssistant
Let me search for tests related to `$modifySelectionAroundDecoratorsAnd
Assistant
Let me check both call sites
Assistant
Let me check line 1714:
Assistant
Now I understand the issue. Let me trace through the code to confirm my understanding and then implement the fix.
The issue is in the
$modifySelectionAroundDecoratorsAndBlocksfunction. WhendeleteCharacteris called with a non-collapsed selection (e.g., user selects the first list item text and presses backspace), it callsthis.modify('extend', isBackward, 'character')which calls$modifySelectionAroundDecoratorsAndBlocks. The second loop in that function climbs through all element node ancestors (ParagraphNode → ListItemNode → ListNode) to eventually find the DecoratorBlockNode, and extends the selection focus to include it. ThenremoveText()deletes everything including the decorator.The fix: in the second loop, when iterating with
alter === 'extend', we should stop at non-inline element boundaries instead of climbing through them. Foralter === 'move'(arrow key navigation), the existing behavior of climbing through all elements is correct.Let me implement the
Assistant
Let me open the file at the right line
Assistant
Now I can see the exact issue. Let me implement the fix:
Assistant
Let me check the correct syntax for the edit command
Assistant
Good, the edit is applied. Now let me verify it looks correct in context and run the existing tests:
Assistant
Now let me run the relevant unit tests to verify the fix doesn't break anything:
Assistant
All tests pass. Let me also run the caret tests and list tests:
Assistant
Let me also run the list tests and any tests related to decorators:
Assistant
Let me also run the LexicalEditor tests and normalization tests that involve decorators:
Assistant
Let me run the broader unit test suite to catch any regressions:
Assistant
All 2962 tests pass. Let me now create a reproduction script to verify the fix handles the specific
Assistant
Let me try to run this test. First, let me check how other tests are structured for LexicalSelection to see if I need to adjust my approach:
Assistant
The fix looks good. Let me verify the change one more time and check the git diff:
Assistant
Now I need to think about whether the big loop in
deleteCharacter(for collapsed selections) has the same issue. Let me trace through the collapsed case:The big loop at line 1791 also climbs through non-inline element nodes to find decorators. Let me examine and fix it too.
Assistant
The big loop has the same issue - it climbs through all non-inline ancestor elements. Let me fix it similarly. The key insight: when we already have a
merge-next-blockstate and encounter another non-inline element, we should only update if it's a true sibling (not a parent of the current block):Assistant
Let me try the edit command with the correct syntax:
Assistant
I see there's a problem with the edit - the closing brace and
continueare missing. Let me fixAssistant
Let me fix it using the proper heredoc syntax:
Assistant
I see a duplicate line. Let me fix it:
Assistant
Let me remove the duplicate line:
Assistant
Let me verify the full big loop looks correct:
Assistant
Let me run the tests to verify the changes compile and pass:
Assistant
There's a failing test. Let me look at it:
Assistant
Let me see more of the test context:
Assistant
Let me check what
deleteWordBackwarddoes:Assistant
Let me look at the
deleteWordBackwardfunction:Assistant
Let me find how
delete_word_backwardis handled:Assistant
Let me look at the handler:
Assistant
Let me check if the test was already failing before my changes by temporarily reverting:
Assistant
So the test was passing before. Let me restore my changes and investigate more carefully:
Assistant
Let me check if ListItemNode overrides
collapseAtStart:Assistant
ListItemNodeoverridescollapseAtStart! Let me look at it:Assistant
Let me isolate which change is causing the failure. Let me revert only the big loop fix temporarily:
Assistant
Let me test with only the
$modifySelectionAroundDecoratorsAndBlocksfix (reverting the big loop change):Assistant
Let me apply only the
$modifySelectionAroundDecoratorsAndBlocksfix:Assistant
Let me apply only the
$modifySelectionAroundDecoratorsAndBlocksfix:Assistant
Now let me test with only this change:
Assistant
I need to re-examine my approach. The
$modifySelectionAroundDecoratorsAndBlocksfix is correct and passes all tests. The big loop fix broke the test. Let me revert the big loop change and instead fix only the decorator handling in the big loop:Assistant
Now let me fix the big loop decorator handling. I need to add an adjacency check so decorators separated by structural nodes (like ListNode) aren't affected:
Assistant
Let me look at the full big loop context to find where to declare the
firstBlockvariable:Assistant
Let me look at the state type definition:
Assistant
Now I'll add a
firstBlocktracker and use it to validate decorator adjacency. Let me implement this:Assistant
Now let me set
firstBlockwhen the first non-inline element is found:Assistant
Now let me modify the decorator handling to check adjacency using
firstBlock:Assistant
There's an indentation/brace issue. Let me fix it:
Assistant
Let me view the current state around line 1833:
Assistant
I see the issue - a closing brace is missing. Let me fix it:
Assistant
Good, the structure looks correct now. Let me run all unit tests:
Assistant
All 2962 tests pass. Let me verify the complete diff:
Assistant
The diff looks clean. Let me run the full test suite once more to confirm everything passes:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.