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 what we're working with.
Assistant
Let me look at the
LexicalListItemNode.tsto see howremove()is implemented:Assistant
Now let me check the
LexicalListNode.tsto understand itsremove()method:Assistant
Let me look at the formatList.ts file:
Assistant
Let me look at the utils.ts file to understand
$removeHighestEmptyListParent:Assistant
Let me see how ElementNode's
remove()works:Assistant
Let me look at how the deletion happens when at the start of a list item:
Assistant
Let me run the existing tests first to understand the current behavior:
Assistant
Now let me write a temporary test to reproduce the issue:
Assistant
Let me look at
$maybeMoveChildrenSelectionToParentto understand selection handling:Assistant
Let me write a test script to reproduce the issue:
Assistant
Let me create a test script to reproduce the issue:
Assistant
Let me create a temporary script using the existing test infrastructure to reproduce the bug:
Assistant
Now let me create a temporary test file to reproduce the bug:
Assistant
Looking at the code more carefully, I need to find the test setup that registers DecoratorBlockNode. Let me searchfor tests that use DecoratorBlockNode:
Assistant
Let me look at how e2e tests for lists work:
Assistant
Now let me look at the test file structure for testing the bug directly. Let me check what happens when we use acustom node in tests:
Assistant
Let me use a custom decorator block node for the test:
Assistant
Let me check root's canBeEmpty:
Assistant
Assistant
Let me look at deleteCharacter to understand the bug:
Assistant
Let me write a more comprehensive test that simulates the actual backspace behavior:
Assistant
I successfully reproduced the bug! Now let me look at what happens more carefully. The list AND the paragraph are removed, but the decorator stays. Let me trace through this carefully:
Assistant
OK so my manual trace gives the correct result
[decorator, paragraph], but when I usedeleteCharacter, the paragraph is also removed. Let me trace throughdeleteCharacter:Assistant
Let me trace through
deleteCharacterto understand exactly what happens:Assistant
OK so when I directly call
collapseAtStarton the list item with the selection, the result is:- root: [decorator, paragraph]
- selection points to paragraph
But when I call
deleteCharacter, the paragraph is also removed!So the difference must be in
deleteCharacter. Let me trace through it:deleteCharacter(isBackward: boolean): void { const wasCollapsed = this.isCollapsed(); if (this.isCollapsed()) { const anchor = this.anchor; let anchorNode: TextNode | ElementNode | null = anchor.getNode(); if (this.forwardDeletion(anchor, anchorNode, isBackward)) { return; } const direction = isBackward ? 'previous' : 'next'; const initialCaret = $caretFromPoint(anchor, direction); const initialRange = $extendCaretToRange(initialCaret); if ( initialRange .getTextSlices() .every(slice => slice === null || slice.distance === 0) ) { ... } } if (isBackward && anchor.offset === 0) { // Special handling around rich text nodes if ($collapseAtStart(this, anchor.getNode())) { return; } } this.removeText(); ... }Assistant
Let me add some debugging to trace through deleteCharacter:
Assistant
Let me look at the actual iteration order by examining the iterNodeCarets:
Assistant
Looking at
iterNodeCarets, it starts at the anchor and steps through carets until reaching the focus. Since anchor and focus are the same (collapsed selection), it doesn't iterate.Wait, that's not right. Let me look again:
initial: anchor.isSameNodeCaret(focus) ? null : step(anchor),If anchor and focus are the same node caret, the iteration is empty.
But for an element-type anchor, what's the NodeCaret? It might be a ChildCaret, and the focus is also a ChildCaret at the same position. So they're "same".
Hmm, this is getting complex. Let me look at what $caretFromPoint does:
Assistant
So $caretFromPoint returns a ChildCaret at the position of the anchor.For our case (item at offset 0, direction 'previous'):
- ChildCaret at item, direction 'previous'
Then $extendCaretToRange:
Assistant
So $extendCaretToRange creates a CaretRange from the anchor to the end of the root in the given direction.
So for our case (anchor at item offset 0, direction 'previous'):
- anchor = ChildCaret at item, direction 'previous'
- focus = SiblingCaret at root, direction 'previous'
The iterNodeCarets iterates from anchor to focus:
- ChildCaret at item (origin = item, direction 'previous')
- Then it steps up to the next caret: since item is the first child, the next caret would be SiblingCaret at item (going to find previous sibling), or it goes up to listNode
- The step function:
step = (state) => state.isSameNodeCaret(focus) ? null : $getAdjacentChildCaret(state) || state.getParentCaret(rootMode);
So it tries
$getAdjacentChildCaret(state)first. Adjacent child caret would be the previous sibling's child caret. But the listNode is the parent of item.Let me look at $getAdjacentChildCaret:Assistant
Let me look at how $getAdjacentCaret works for a ChildCaret. Actually, the iteration is in the direction of the caret, so:
- Anchor is at item, going previous
- Step: get adjacent child caret or parent caret
For a ChildCaret at item going 'previous', the adjacent caret would be... let me think. The ChildCaret is "before" the first child. Going previous from there, we go to the previous sibling's caret. But there's no previous sibling. So getAdjacentCaret returns the parent caret (or null)?
Actually, I think the iteration goes:
Assistant
Let me check DecoratorNode.isIsolated:
Assistant
Let me check the ParentCaret type:
Assistant
Now I understand the issue. The
deleteCharacterfunction has special logic to handle decorators adjacent to empty blocks - but it incorrectly treats list items as paragraphs, removing the list without replacing it with a paragraph.Let me verify with a test for the normal paragraph case first:
Assistant
Now I understand the bug. The fix is to prevent the
deleteCharacterfunction from incorrectly removing list items. Let me look at how to fix this:Assistant
Let me think about this differently. The lexical-list package's
ListItemNode.collapseAtStarthandles the case where the list is the only block (no decorator before). The issue is that the deletion path in the core packagedeleteCharacterhandles the decorator case but doesn't account for lists.Let me check if lexical core already has knowledge of ListItemNode:
Assistant
Let me test more scenarios to understand the bug fully:
Assistant
Let me look at the DecoratorBlockNode isKeyboardSelectable:
Assistant
Let me trace through multiple backspace operations to see if both get removed:
Assistant
Let me look at the issue more carefully and understand what the user expects. Looking at the issue again, the title is:
List: delete the leading ListItemNode will also delete the previous DecoratorBlockNode
So the user is talking about deleting the leading list item, and that also deletes the decorator. The current behavior in my tests:
- Case B: empty list + decorator -> only decorator (list removed) - decorator is preserved
- Case C: list with text + decorator -> only list (decorator removed) - list is preserved
Hmm so actually the user's bug is the OPPOSITE of what I found. They say the decorator is removed. In Case C (with text), my test shows the decorator IS removed. Maybe that's the user's scenario.
But they also mention "delete the leading ListItemNode". This is ambiguous. Let me look at the E2E test setup more carefully:
Assistant
Let me check how the playground creates the TweetNode and check if it's isolated:
Assistant
TweetNode extends DecoratorBlockNode which extends DecoratorNode. Let me check if itoverrides isIsolated:
Assistant
Let me look at the actual playground behavior and check what happens for the actual
TweetNode:Assistant
Let me look at the playground E2E tests for list behavior:
Assistant
Let me try one more scenario to find the exact bug:
Assistant
Let me check if there's a case where both are deleted. Let me look at how deleteCharacter handles the "no text in direction" case with a list item having text:
Assistant
Let me now implement the fix. The bug is in
deleteCharacterinpackages/lexical/src/LexicalSelection.ts. The fix should prevent the special decorator handling from applying to list items:Assistant
Let me look at $getSiblingCaret:
Assistant
Let me check $filterByMode:
Assistant
Let me focus on making the fix work rather than fully understanding the caret iteration. Let me write the fix:
Assistant
Let me now apply the fix to LexicalSelection.ts:
Assistant
Now let me apply the fix:
Assistant
Let me use Python to apply the patch to ensure correctness:
Assistant
Now let me run the test to see if the fix works:
Assistant
The decorator is now preserved but the list is not being converted to paragraph. Let me restructure the fix - we need to not return when the anchor is in a nested block, so that collapseAtStart gets called:
Assistant
Let me revert and try a different approach:
Assistant
Let me check what
modify('extend', ...)does:Assistant
Let me re-apply the fix and add some debug output:
Assistant
Let me add logging to understand what's happening:
Assistant
Let me check what modify does in this case:
Assistant
Let me add debug output to understand the flow:
Assistant
Interesting! After modify, the selection is NOT collapsed! That's why we don't call collapseAtStart.
Let me look at what modify does. It extends the selection backward, but apparently it doesn't collapse. So
isCollapsed()returns false.But the anchor is still the same. So the selection must have extended in some way. Let me check the focusafter modify:
Actually let me also log the focus:
Assistant
Let me check
$updateCaretSelectionForUnicodeCharacter:Assistant
Let me look at
removeText:Assistant
Let me revert the debug changes and try a different approach:
Assistant
Let me now apply a different approach - call collapseAtStart directly for the list case:
Assistant
Let me add more debug to understand the flow for Case C:
Assistant
Let me check AbstractChildCaret.getNodeAtCaret:
Assistant
Let me check $getChildCaretAtIndex:
Assistant
Let me check more carefully what's happening in Case C:
Assistant
Let me add more debug logging to the post-loop logic:
Assistant
Hmm, no debug logs for "before modify" / "after modify". That's strange. Let me check if my debuglogs are actually being added:
Assistant
Hmm, no DEBUG output! Let me check if the test is running the right test:
Assistant
Let me add a debug log at the very beginning of deleteCharacter to see what's happening:
Assistant
Let me check my debug log was actually in place:
Assistant
Let me add more debug logs at key points:
Assistant
Let me add debug logs at the start and end of the iteration:
Assistant
Let me check the actual code structure:
Assistant
Let me add more specific debug logging:
Assistant
Now I see the issue! For Test C,
anchor.origin.type = text, notlistitem. Let me check what initialRange.anchor.origin is:Assistant
Let me add more debug logging:
Assistant
Let me check the actual $caretFromPoint implementation:
Assistant
OK so $getChildCaretAtIndex walks through the children. For our case (offset 0, 'previous' direction):
- Starts with ChildCaretFirst(parent, 'next')
- No iteration(index 0)
- Returns $getCaretInDirection(caret, 'previous') which converts to previous direction
Let me check $getCaretInDirection:
Assistant
Now I understand the issue. For Test C, the anchor.origin is a text node (not the listItem), because the anchor is at the start of the text. Let me fix the condition to handle this:
Assistant
Let me check the paragraph content in Test C:
Assistant
Excellent! Test C result is correct - decorator preserved, list converted to paragraph with "Hello".
Let me remove debug logs and verify other tests:
Assistant
Debug logs removed. Now let me run all the tests:
Assistant
All three tests work correctly:
- Test A (paragraph + decorator): paragraph removed (existing behavior)
- Test B (empty list + decorator): list converted to paragraph, decorator preserved (FIXED)
- Test C (list with text + decorator): list converted to paragraph (with text), decorator preserved (FIXED)
Let me run existing tests to make sure nothing is broken:
Assistant
All 986 tests pass. Let me run more tests to make sure:
Assistant
All tests pass. Let me clean up the temporary test file:
Assistant
Let me look at the final fix:
Assistant
Let me check the existing tests for the heading backspace behavior:
Assistant
Let me look at the final fix one more time:
Assistant
Let me check ElementNode.collapseAtStart:
Assistant
Let me check TableCellNode:
Assistant
Let me check the table tests to see if there's any test for the backspace at start of paragraph inside table cell with decorator:
Assistant
Let me think about this differently. Let me make the fix more targeted by tracking the iteration:
Assistant
Let me check TableCellNode.collapseAtStart:
Assistant
Let me check what TableCellNode.collapseAtStart does:
Assistant
Let me make sure my fix is complete. Let me look at the current state of the fix and the original:
Assistant
Oh, my fix was reverted. Let me re-apply:
Assistant
Hmm, both files have the same MD5. So my .bak is actually the fixed version. Wait, that means my fix is intact.
Wait, that's strange. Let me re-check. I think the issue is that when I created the .bak file, the original was already gone (I overwrote it). So .bak now contains the fixed version.
Let me check gitto see what's going on:
Assistant
OK so my fix is intact. There's just a blank line that was removed. Let me restore the blank line and clean up:
Assistant
Now let me run the tests to make sure everything works:
Assistant
All 2962 tests pass! Let me create a final test to verify the fix:
Assistant
All three test cases pass. Let me clean up the temporary test file:
Assistant
Let me check the final state of my fix:
Assistant
Let me run all the tests one more time to make sure everything works:
Assistant
All 1712 tests pass. Let me also verify typecheck:
Assistant
TypeScript compiles cleanly. Let me also check that the specific lines work with the eslint linter:
Assistant
No lint errors. Let me also run the regression test that was specifically related:
Assistant
All playground unit tests pass too. Let me view the final fix one more time:
Assistant
The fix is working. Let me also verify by checking the playground E2E behavior would be correct. Let me also run a quick test with the actual TweetNode:
Assistant
The fix works correctly with TweetNode-like decorators too. Let me clean up the verification test:
Assistant
Let me run all the relevant tests one final time:
Assistant
All 2962 tests pass with my fix. Let me show the final diff:
Assistant
The fix is complete. Let me also verify by running the playground tests one more time to make sure nothing is broken:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.