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.)Interface
No new interfaces are introduced.
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 exploring the repository structure to understand the codebase.
Assistant
Let me look at the rest of this file - particularly the
removemethod that's likelyrelated to deleting.Assistant
Now let me look at theListNode:
Assistant
Now let's look at formatList.ts to understand the list deletion logic:
Assistant
Now let me look at how the deletion works in Lexical core. Let me look at the core code where deletion happens:
Assistant
Now let me look at the
removeTextFromCaretRangefunction:Assistant
Now let me look at the
removemethod of ElementNode and LexicalNode:Assistant
Now I understand the flow. The
removefunction ofLexicalNodecalls$removeNode, which checks ifparent.canBeEmpty()returns false. If so and the parent becomes empty, the parent is also removed.Let me look at the
canBeEmptymethod ofListNode:Looking at
ListNode:canBeEmpty(): false { return false; }So when the
ListItemNodeis removed from theListNode(its parent), if the ListNode becomes empty (no children left), theListNodeis also removed becausecanBeEmpty()returnsfalse.But this shouldnot cause the DecoratorBlockNode to be deleted...
Let me look at the moveSelectionPointToSibling:
Assistant
Let me reproduce the issue first by setting up a test:
Assistant
Let me look at how the bug reproduces in detail. Let me write a script to reproduce:
Assistant
Let me look at the
removeFromParentfunction and$updateElementSelectionOnCreateDeleteNode:Assistant
Let me write a test reproduction now to better understand the issue. First let me look at the existing test setup:
Assistant
Let me look at how existing tests reproduce the deletion scenario:
Assistant
Let me write a test script to reproduce the bug:
Assistant
Let me write a test to reproduce the issue:
Assistant
Let me look at the existing ListItemNode tests and figure out a way to test:
Assistant
Now let me write a reproduction test in the proper place. First let me create a quick reproduction:
Assistant
Looking at the existing tests, the way they import
testanddescribeis:Assistant
I need to use TestElementNode from the test utils:
Assistant
Interesting! With a direct
firstItem.remove()call, the decorator still exists. But the test case in the issue is about pressing backspace on the first list item, not directly removing it. Let me try simulating that behavior:Assistant
Let me look at the LexicalEvents.ts to understand the backspace behavior:
Assistant
Let me directly call deleteCharacter to reproduce:
Assistant
Now let me look at forwardDeletion:
Assistant
Let me add some console logging to trace exactly what's happening:
Assistant
Let me look at how iterNodeCarets works:
Assistant
OK let me look at theLexicalCaret internals more carefully:
Assistant
Let me trace through the code by adding debug logs to the relevant deleteCharacter function:
Assistant
Let me add some debug logs in LexicalSelection.ts to trace what's happening:
Assistant
Let me revert the debug logs and check with an actual DecoratorNode:
Assistant
Good, the LexicalSelection.ts has been reverted. Now let me create a test using an actual DecoratorNode:
Assistant
Need to register the node:
Assistant
Let me add debug logging back to trace what's happening:
Assistant
Let me revert the debug logs and try with selection deletion:
Assistant
Good. Now let me try the test with selection deletion (selecting the leading list item and removing):
Assistant
Let me try with selecting the list item itself:
Assistant
Let me look at
getAdjacentChildCaret:Assistant
Let me re-find the collapseAtStart call:
Assistant
Let me check the imports in LexicalSelection.ts:
Assistant
Let me look at the structure of deleteCharacter more carefully:
Assistant
Let me check if
$isListNodeis in lexical core:Assistant
Let me look at how table handles deletion:
Assistant
Let me check if there's a duck-typing pattern used:
Assistant
Let me check if there are other cross-package dependencies in lexical:
Assistant
The lexical package depends on @lexical/internal only. So I shouldn't add a dependency on lexical-list.
OK so I need to either:
- Use duck typing in core lexical
- Fix this in the list package somehow
Let me think about option 2 more. The list package has
ListItemNode.collapseAtStartwhich is called by$collapseAtStart. The issue is that$collapseAtStartis called AFTER the merge logic in deleteCharacter.Can we modify the list package to override the deletion behavior? Looking at the available hooks:
In Lexical, there's a concept of
editor.registerCommandfor DELETE_CHARACTER_COMMAND. The list package could register a command listener that intercepts the DELETE_CHARACTER_COMMAND and handles it differently.Letme check if there's such a register:
Assistant
The list package has
registerListwhich registers various commands. There's no DELETE_CHARACTER_COMMAND handler, but we could add one.But wait, looking at the issue more carefully, the user expects the list to be removed/replaced with a paragraph. The collapseAtStart code does this correctly. We just need to make sure it's called.
OK let me think about the simplest fix:
- In the list package, register a DELETE_CHARACTER_COMMAND handler
- When the user presses backspace at the start of a non-empty list item text (which would otherwise remove the previous decorator), intercept and call collapseAtStart instead
Wait but the issue is that deleteCharacter is called by the COMMAND, and the COMMAND is dispatched by the events. Let me look at this more carefully.
Actually, looking at LexicalEvents.ts:
dispatchCommand(editor, DELETE_CHARACTER_COMMAND, true);This dispatches a command. The commandis handled by
RangeSelection.deleteCharacter(true)which is called via the default handler.Wait, let me check if there's a default handler for DELETE_CHARACTER_COMMAND:
Assistant
OK so DELETE_CHARACTER_COMMAND is dispatched, and if any listener returns true, it's handled. If no listener returns true, the command is unhandled.
Looking at the code, the default handler for DELETE_CHARACTER_COMMAND is not explicit. Let me search:
Assistant
Let me look at the events again:
Assistant
Hmm interesting. So DELETE_CHARACTER_COMMAND is dispatched, but I don't see a default handler that calls selection.deleteCharacter().Let me search:
Assistant
Let me look at the merge logic one more time to understand the fix point:
Assistant
Let me re-check the test with TestElementNode (non-decorator):
Assistant
OK now I have a clear picture. Let me write the fix. The bug is in
deleteCharacterinLexicalSelection.ts:Assistant
Let me check the DecoratorBlockNode isInline:
Assistant
Let me look at the caret's getAdjacentCaret and getChildCaret:
Assistant
Let me check isEmpty for ElementNode:
Assistant
Now let me write the fix. First, let me look at where to add the helper function:
Assistant
Now let me make the fix. First, let me look at the merge-block logic and the DecoratorNode branch:
Assistant
Now let me write the fix. I'll modify both the DecoratorNode branch and the merge-block logic:
Assistant
Now let me modify the DecoratorNode branch:
Assistant
Now let me apply the patch using a Python script for reliability:
Assistant
Now I need to define
$isListLikeNode. Let me add it. Let me look at the helpers area in LexicalSelection.ts:Assistant
Let me add
$isListLikeNodeafter$isBlockElementNode:Assistant
Now let me also patch the merge-block logic to handle the list case:
Assistant
Now let me run the reproduction test to see if it works:
Assistant
The
breakwas outside the for loop. Let me restructure:Assistant
Good. Now let me run the reproduction test:
Assistant
The fix works for the TestElementNode case. Now let me test with the actual DecoratorBlockNode:
Assistant
Hmm, there's a JSDom issue with the selection.modify API. Let me lookat the code:
Assistant
OK so the issue is that jsdom doesn't support
domSelection.modify. This is a test infrastructure issue, not related to our fix.Let me check how other tests handlethis:
Assistant
The existing tests don't seem to call deleteCharacter directly. Let me check the vitest.setup.mts:
Assistant
The setup doesn't polyfill
Selection.modify. Let me check if there's a polyfill for it:Assistant
OK so the polyfill for Selection.modify is in lexical-selection tests. Let me use that approach. Let me update the test:
Assistant
The fix works! Let me run the existing tests to make sure I didn't break anything:
Assistant
All 53 tests in LexicalSelection.test.ts passed. Let me also run the lexical-list tests:
Assistant
All 99 tests in lexical-list passed. Let me also run the lexical-caret tests:
Assistant
Let me look at the HeadingNode test for backspace behavior:
Assistant
Let me also test the empty listItem case:
Assistant
All three test cases pass with the expected behavior. Let me also run other test suites that might be affected:
Assistant
All 582 tests in lexical pass. Let me also run other package tests:
Assistant
All tests pass. Let me review the fix once more:
Assistant
The fix is in place. Let me also test the edge case where the list is NESTED:
Assistant
The nested list case also works correctly. The decorator is preserved! Now let me clean up:
Assistant
Now let me view the complete diff of my changes:
Assistant
The diff looks clean. Let me also run all the unit tests in the affected packages to make sure nothing is broken:
Assistant
All 867 tests pass. Let me also run the lexical-selection tests:
Assistant
All 298 tests in lexical-selection pass. Let me also do a final sanity check by running ALL unit tests:
Assistant
All 2962 tests pass! Let me also verify the fix by re-reading the diff:
Assistant
The diff is clean and focused on the issue. Let me also verify by writing a simple integration test that uses the actual playground-style DecoratorBlockNode:
Assistant
Wait, this is strange. Let me investigate by looking at the iteration order for TweetNode:
Assistant
Interesting! The iteration order is:
- sibling listitem 3
- sibling list 2
- sibling tweet 1
And the TweetNode is still being removed. Let me check the DecoratorBlockNode's
isKeyboardSelectable:Assistant
Let me re-run my earlier test with TestDecoratorBlockNode (not DecoratorBlockNode):
Assistant
Good, my fix works for TestDecoratorBlockNode. Let me investigate why the TweetNode test failed:
Assistant
Let me add some debug logs to figure out what's different:
Assistant
Assistant
Let me remove the debug log and check the state after modify:
Assistant
Good, the debug log is removed. Now let me add a debug log at a different point to see what's happening.Let me add it after the modify call:
Assistant
Interesting! Let me trace what's happening with more debug logs:
Assistant
Let me look at modify more carefully:
Assistant
Let me look at what's the initial DOM selection:
Assistant
Hmm, the log didn't show. The moveNativeSelection-input log should have been called. Let me check if it's beingcalled at all:
Assistant
Let me check $caretFromPoint:
Assistant
So
$caretFromPoint(focus, 'previous')where focus is text "Hello" at 0 returns a TextPointCaret with the text "Hello" atoffset 0 going previous direction.Then
$isExtendableTextPointCaret(focus)- let me check:Assistant
OK so
$isExtendableTextPointCaretreturns true if the caret is a text point caret AND its offset is NOT at the end (in the direction).For our case:
- caret is text "Hello" at offset 0
- direction is 'previous'
- $getTextNodeOffset returns the appropriate offset for 'previous' direction. For 'previous' direction, the offset would be 0 (you can't go before the start of the text in 'previous' direction).
Wait,let me check $getTextNodeOffset:
Assistant
So
$getTextNodeOffset(text, 'previous')returns 0. And our caret has offset 0. Socaret.offset !== 0is false. So$isExtendableTextPointCaretreturns false.Hmm, that means the code path:
if (!$isExtendableTextPointCaret(focus)) { for (const siblingCaret of focus) { ... } if (checkForBlock) { for (const nextCaret of $extendCaretToRange(initialFocus).iterNodeCarets(...)) { ... } } }Would execute. Let me trace:
siblingCaret of focus- the focus is a TextPointCaret.for (const siblingCaret of focus)- this iterates sibling carets of the focus. But TextPointCaret doesn't have a Symbol.iterator that does this.Wait, actually let me look:
Assistant
The Symbol.iterator on AbstractCaret iterates sibling carets. For a TextPointCaret, getAdjacentCaret calls $getSiblingCaret on the text node (with the caret direction), which gives the adjacent sibling caret.
OK so for our case (TextPointCaret of "Hello" at offset 0 going 'previous'):
- for siblingCaret of focus: iterates through sibling carets of the text node, starting from the adjacent caret in 'previous' direction
- The first sibling caret would be... well, the text is in the listItem, so the previous sibling would be... nothing (text is the only child).
Hmm, let me think. Actually, the adjacent caret of a text point caret is the text caret itself, with the direction. The sibling caret of a text caret would bethe listItem (parent of text).
Wait, let me look at getAdjacentCaret for TextPointCaret:
Assistant
Let me check
$extendCaretToRange:Assistant
OK so
$extendCaretToRange(anchor)returns a range fromanchorto$getSiblingCaret($getRoot(), anchor.direction). The focus is the sibling caret of root going in the direction.For our case, the direction is 'previous'. So the focus is the sibling caret of root going 'previous'. There's no sibling of root in 'previous' direction. So the focus caret is... let me check:
Assistant
Wait, the test output was before my fix. Let me re-run the test with my fix in place:
Assistant
Let me check what's different between TestDecoratorBlockNode and TweetNode:
Assistant
Let me re-add debug logs to trace:
Assistant
The debug log isn't showing. Let me add a log earlier to see all carets:
Assistant
Both show the same iteration. And the DecoratorNode branch should be triggered. Let me check why the debug log isn't showing:
Assistant
The log is in the wrong branch. Let me move it:
Assistant
The DecoratorNode-hit log is not showing. So the DecoratorNode branch is NOT being entered.
But the caret shows sibling test_decorator_block/tweet. So the iteration is reaching the decorator. But the DecoratorNode branch isn't entered?
Let me check what's happening. Maybe the iteration enters another branch first. Let me look at the code:
Assistant
Looking at the iteration output again, both cases show:
- sibling listitem 3
- sibling list 2
- sibling test_decorator_block/tweet 1
So the iteration is reaching the decorator as a sibling caret. The DecoratorNode branch should be entered.
But the DecoratorNode-hit log is not showing. That means the log statement inside the empty element case is not being reached. But the structure of my added log might be in the wrong place.
Let me add a log inside the actual
$isDecoratorNodebranch, at the beginning:Assistant
For TestDecoratorBlockNode, my fix works. For TweetNode, the DecoratorNode branch is entered but the decorator is still removed. Let me add a log for my fix condition:
Assistant
Both cases hit the LIST-BRANCH-HIT. So the break is executed. But for TweetNode, the decorator is still removed. The issue must be after the break.
Let me check $updateCaretSelectionForUnicodeCharacter:
Assistant
Let me try a different approach - skip the modify call when the current block is a list:
Assistant
Let me think about this differently. I need to skip the modify when the merge logic has handled the deletion. Let me modify the approach:
Assistant
Let me modify the approach to skip the modify call when the current block is a list:
Assistant
Now let me run the tests:
Assistant
stateis defined inside the innerifblock. I need to move its declaration. Let me fix:Assistant
Let me restructure. I'll declare a
listHandledflag inside the merge logic:Assistant
Now let me set
currentBlockIsListLikein the DecoratorNode branch:Assistant
Now let me modify the merge-block logic to set the flag too:
Assistant
Let me restore and re-do this properly:
Assistant
Now let me also skip the modify when
currentBlockIsListLikeis true:Assistant
Now let me run the tests:
Assistant
The variable
currentBlockIsListLikeis declared inside theif (all text slices are 0 distance)block but used outside. Let me check:Assistant
Hmm, only 2 instances. Let me check the firstpatch I did:
Assistant
The variable is still inside the inner
ifblock. Let me check the structure:Assistant
The variable is declared inside the inner if block. Let me move it to the outer scope:
Assistant
The sed command left a mess. Let me restore and do it properly:
Assistant
Let me run the tests:
Assistant
Both tests pass now. Let me run the full unit test suite to make sure I didn't break anything:
Assistant
All 2964 tests pass. Let me also verify my fix is in place:
Assistant
Let me remove the test file I created:
Assistant
Let me also make sure there are no other temp files:
Assistant
Only the LexicalSelection.ts file is modified. Let me run all the tests one more time to confirm everything is good:
Assistant
Let me also check the final diff is clean:
Assistant
The fix is clean and minimal. Let me also do one more check by running the linter:
Assistant
No lint errors. Let me also verify the typescript compilation:
Assistant
There are TypeScript errors. Let me check:
Assistant
The issue is TypeScript type narrowing. Let me fix:
Assistant
Still TypeScript errors. Letme check the structure:
Assistant
Hmm, the error says line 1891, 1892, 1900. Let me check those lines. But wait, the structure nowlooks the same as the original. Let me look at the line numbers and content:
Assistant
Let me run the tsc again with more details:
Assistant
The errors are on
caret.origin.isEmptyandcaret.origin.getParent. TypeScript thinkscaret.originis type 'never'.The issue is that the type narrowing is not working. Let me check the state type declaration again:
Assistant
Let me check what changed:
Assistant
The issue is the structure of the if-else. Let me revert the merge-block logic to the simpler form using a separate check:
Assistant
Still the same error. Let me check the structure:
Assistant
The structure looks correct. The error is on
caret.origin.isEmptyetc. Let me check the line numbers:Assistant
Even with the original structure, TypeScript is complaining. Let me check if this error existed before my fix:
Assistant
The error is pre-existing in the original code. Let me re-apply my fix:
Assistant
The errors are pre-existing in the original code. My fix has the same errors (just at different line numbers due to added code).
Let me run the tests to verify the fix works:
Assistant
All tests pass. Let me also run the lexical-selection tests to make sure:
Assistant
All 384 tests pass. The pre-existing TypeScript errors are not new (they were there before my fix).
Let me also look at the fix once more to make sure it's correct:
Assistant
Wait, this diff doesn't match my last patch. Let me check:
Assistant
Let me try a different structure to avoid the TypeScript narrowing issue:
Assistant
These are pre-existing TypeScript errors. Let me re-apply my fix:
Assistant
All 98 tests pass. Let me also run all unit tests to confirm:
Assistant
All 2962 tests pass. Let me also clean up the temp files and the pre-existing TypeScript errors. Let me look at the issue more carefully to understand if I can avoid the errors:
Assistant
Let me verify these are pre-existing errors by looking at the original:
Assistant
The errors are pre-existing. Let me re-apply my fix:
Assistant
The diff is clean. Let me run the tests one final time:
Assistant
All 2962 tests pass.
Let me also verify by creating a final test that mimics the exact scenario from the issue:
Assistant
The fix works as expected! Let me clean up the test file:
Assistant
Let me verify the final diff one more time:
Assistant
The diff is clean and minimal. Let me also make sure the temp files are cleaned up:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.