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
I'll start by exploring the repository structure to understand the codebase and locate relevant files. Let me first check the overall layout.
- Assistant
Let me explore the repository structure more broadly to understand what we're working with.
- Assistant
Let me look at the packages directory, particularly the lexical-list package since the issue is about lists.
- Assistant
Let me explore the lexical-list package to understand how lists are handled.
- Assistant
The issue is about deleting the first ListItemNode in a list also deleting the previous DecoratorBlockNode. Let me understand the bug better and find the relevant code. This seems to be related to how backspace/delete handles list items at the beginning of a list when there's a DecoratorBlockNode before it.
Let me look at the key files:
- LexicalListItemNode.ts - for the list item node behavior
- registerList.ts - for list-related event handling
- LexicalListNode.ts - for list node behavior
- Assistant
Let me explore the relevant files to understand the issue better. The bug is about deleting the first ListItemNode in a list also deleting the previous DecoratorBlockNode. Let me look at the list-related code and the deletion handling.
- Assistant
Now let me look at the ListItemNode and ListNode to understand how deletion works.
- Assistant
Let me look at how deletion of list items is handled, particularly at the beginning of a list.
- Assistant
Now let me look at the
formatList.tsfile, which seems to contain the$handleListInsertParagraphfunction that handles the list insert paragraph command. This is likely where the issue is. - Assistant
Let me look at the utils.ts file in the list package to understand the helper functions:
- Assistant
Let me search for how list item deletion is handled in the codebase:
- Assistant
Let me look at how backspace/delete is handled in the Lexical core to understand the deletion flow:
- Assistant
Let me look at
deleteCharacterin LexicalSelection.ts as it seems to handle backspace/delete key behavior. Let me also check if there's any specific handling related to decorator nodes and list items.Let me look at the relevant part of LexicalSelection.ts:
- Assistant
Let me examine the deleteCharacter method more closely to understand how deletion is handled:
- Assistant
Now let me understand the issue better. Let me look at the test files for list operations to understand the expected behavior:
- Assistant
Let me trace through the exact scenario. Let me look at how the carets are traversed:
- Assistant
Let me look at how the issue manifests. Let me search for how the list handles the case when the first list item is empty and should be removed:
- Assistant
Now I understand the issue. Let me look at the
deleteCharactermethod more carefully to see what happens when the first list item is empty: - Assistant
Let me look at the
$collapseAtStartfunction to understand what happens when pressing backspace at the start of a list item: - Assistant
Let me look at the
$collapseAtStartfunction to understand what happens when backspace is pressed at the start of a list item. - Assistant
Now let me look at what happens when the first list item is empty. Let me trace through
$handleListInsertParagraph: - Assistant
Let me look at what happens when a ListItemNode becomes empty. I need to search for transforms or handlers that might clean up empty list items:
- Assistant
Let me look at what
$collapseAtStartdoes for a ListItemNode - this is called when pressing backspace at offset 0: - Assistant
Let me look at the collapseAtStart method for ListItemNode - this is likely the key method that handles what happens when backspace is pressed at the start of a list item.
- Assistant
Let me check if ListNode is considered a shadow root:
- Assistant
Let me look at what
isShadowRootdoes for ListNode and what happens in the caret traversal: - Assistant
Let me look at the
forwardDeletionmethod to see if it returns true when at the beginning of a list item and pressing backspace: - Assistant
Let me look at the
forwardDeletionmethod to understand what it does. - Assistant
Let me understand the issue better. Let me look at what happens when you press backspace at the start of a list item when it's empty. Let me check the caret navigation code:
- Assistant
Let me look at how
iterNodeCaretsworks to understand the caret traversal: - Assistant
Let me look at
$removeListto understand how it works: - Assistant
Let me look at how the caret navigation works when pressing backspace at the beginning of a list item. Let me check if there's special handling for the cursor at offset 0 in a list item:
- Assistant
Let me understand the issue better. I think the problem might be in the
deleteCharactermethod where it handles decorator nodes. Let me look at the relevant section more carefully: - Assistant
Let me look at how the caret navigation works in the scenario described. The key question is: when the user is at offset 0 of the first ListItemNode and presses backspace, what does the caret traversal look like?
- Assistant
So
ListNodedoesn't overrideisShadowRoot(). Let me check whatElementNode.isShadowRoot()returns by default. - Assistant
Let me check what
isShadowRootreturns for ElementNode by default: - Assistant
Let me look at what happens when the user selects a list item and presses backspace. Let me check if there's any special handling for list item selection:
- Assistant
Let me understand the flow more carefully. When the user has their cursor at the beginning of the first list item and presses backspace, what happens? Let me look at the full flow in
deleteCharacter: - Assistant
Now I need to understand the issue better. Let me trace through the flow when the first list item is empty and user presses backspace. Let me look at the exact caret traversal logic:
- Assistant
Let me look at how the list handles deletion when the first list item is empty. Let me check if there's special handling in the
collapseAtStartof ListItemNode: - Assistant
Let me understand the issue by looking at the caret system to understand the traversal when pressing backspace at the start of the first list item:
- Assistant
Let me now write a script to reproduce the issue. First, let me check what testing infrastructure is available:
- Assistant
Let me check what makes a node a shadow root in Lexical:
- Assistant
Let me look at how
$caretFromPointworks with element selection: - Assistant
Let me look at how the caret system works with
$caretFromPointwhen the anchor is an element node: - Assistant
Let me now look at the specific code flow. The issue seems to be in the
deleteCharactermethod when pressing backspace at the beginning of a list item that's preceded by a DecoratorBlockNode. Let me examine the flow more carefully: - Assistant
Now I understand the issue. Let me look at how the caret traversal works when pressing backspace at the beginning of the first list item. The key is that the ListNode is NOT a shadow root, so the traversal goes through it and reaches the DecoratorBlockNode. Let me verify this understanding:
- Assistant
Let me look at how
$extendCaretToRangeworks: - Assistant
Now I understand the core issue. Let me trace through the scenario more carefully. When the user is at the beginning of the first list item (which has content) and presses backspace:
- Assistant
Now I understand the issue. Let me trace through the exact flow when the user has a DecoratorBlockNode followed by a ListNode with a ListItemNode, and presses backspace at the beginning of the first list item.
The key issue is in the
deleteCharactermethod inLexicalSelection.ts. When pressing backspace at the beginning of the first list item, the caret traversal goes backward and encounters the DecoratorBlockNode. The code then removes the DecoratorBlockNode because:- The anchor (ListItemNode or TextNode inside it) is not empty
- The state is 'initial' (not 'merge-next-block')
- The code falls into the
elsebranch which unconditionally removes the decorator
Let me now look at the ListItemNode's collapseAtStart method again:
- Assistant
Now I understand the issue! Let me trace through the flow:
When the user presses backspace at the beginning of the first list item (which is inside a ListNode, and the ListNode has a DecoratorBlockNode as its previous sibling):
- The caret traversal goes backward from the anchor (inside the ListItemNode)
- It first encounters the ListNode as a ChildCaret
- Since ListNode is NOT a shadow root (isShadowRoot() returns false), the traversal continues
- It then encounters the DecoratorBlockNode as a SiblingCaret
- At this point,
state.typeis still 'initial' (not 'merge-next-block') - The code falls into the
elsebranch at line 1849-1852:caret.origin.remove()- THIS REMOVES THE DECORATOR!
The fix needs to prevent the DecoratorBlockNode from being removed when the anchor is inside a list (or more generally, inside a non-empty container).
Let me look at how to fix this. The key issue is that the code doesn't check if the anchor is inside a list structure before removing the decorator.
- Assistant
Let me now look at the test structure to understand how to create a test case. Let me first check if there are any existing tests for the decorator node behavior:
- Assistant
Let me look at the formatList test to understand how decorators are used in tests:
- Assistant
Let me look at the existing test for decorator handling to understand the test pattern:
- Assistant
Now I understand the issue. Let me write a test to reproduce it first. Let me look at how tests are structured in this project:
- Assistant
Now I understand the issue clearly. Let me trace through the exact scenario:
- Document structure: Root -> [DecoratorBlockNode, ListNode -> ListItemNode]
- User is at offset 0 of the ListItemNode and presses backspace
- The caret traversal goes backward: ListItemNode -> ListNode -> DecoratorBlockNode
- Since ListNode is NOT a shadow root, the traversal continues past it
- When the DecoratorBlockNode is encountered as a SiblingCaret,
state.typeis still 'initial' - The code falls into the
elsebranch which unconditionally removes the decorator
The fix needs to check if the anchor is inside a list structure before removing the decorator. Let me implement this fix:
- Assistant
Now I understand the issue. Let me trace through the exact flow:
- Document structure: Root -> [DecoratorBlockNode, ListNode -> ListItemNode -> TextNode]
- User is at offset 0 of the TextNode and presses backspace
- The caret traversal goes backward: TextNode -> ListItemNode (ChildCaret) -> ListNode (ChildCaret) -> DecoratorBlockNode (SiblingCaret)
- When ListNode is encountered as a ChildCaret, it's not inline and not a shadow root, but state is still 'initial'
- When DecoratorBlockNode is encountered as a SiblingCaret,
state.typeis still 'initial' - The code falls into the
elsebranch which unconditionally removes the decorator
The fix should prevent the decorator from being removed when the anchor is inside a list structure. Let me look at how to detect this:
- Assistant
Let me think about this more carefully. The issue is that when pressing backspace at the beginning of the first list item (which is empty), the code removes the DecoratorBlockNode because:
- The caret traversal goes backward and encounters the DecoratorBlockNode
state.typeis still 'initial' (no block element to merge with)- The code falls into the
elsebranch which unconditionally removes the decorator
The fix should check if the anchor is inside a list structure. If it is, we should NOT remove the decorator. Instead, we should break out of the loop and let the code fall through to
$collapseAtStart.Let me look at the full flow once more to understand where to add the check:
- Assistant
Now I understand the issue clearly. The problem is in the
deleteCharactermethod. When the cursor is at the beginning of the first list item and the user presses backspace:- The caret traversal goes backward from the anchor
- It descends into the ListNode (a non-inline, non-shadow-root ChildCaret) - but the state stays 'initial'
- It then encounters the DecoratorBlockNode as a SiblingCaret
- Since
state.typeis still 'initial', the code falls into theelsebranch which removes the decorator
The fix should be: when we've descended into a non-shadow-root block (like a ListNode), we should not remove the decorator. Let me implement this:
- Assistant
Now I have a clear understanding of the issue. Let me trace through the exact scenario:
- Document:
Root -> [DecoratorBlockNode, ListNode -> ListItemNode] - Cursor at offset 0 of first ListItemNode, pressing backspace
- Caret traversal: ChildCaret(ListNode) → SiblingCaret(DecoratorBlockNode)
- When encountering ChildCaret(ListNode), it's NOT inline, NOT shadow root, but state is 'initial' so nothing happens
- When encountering SiblingCaret(DecoratorBlockNode), state is still 'initial', code removes the decorator
The fix: when we've descended into a non-inline, non-shadow-root block (like a ListNode), we should NOT remove an adjacent decorator. Let me implement this:
- Document:
- Assistant
Let me now create a test to reproduce the issue:
- Assistant
Let me first try to understand the actual issue better by looking at how the caret system works in this case. Let me check what happens during the caret traversal:
- Assistant
Let me try to run the test first to see the actual behavior, and then trace through the logic more carefully:
- Assistant
The test file needs to have
.test.tsextension. Let me rename it. - Assistant
Let me take a different approach and try to understand the caret traversal better. Let me look at how it works in this specific case:
- Assistant
Now I understand the issue. Let me trace through the exact flow:
- Structure:
Root -> [DecoratorBlockNode, ListNode -> ListItemNode(empty)] - Cursor at offset 0 of empty ListItemNode, press backspace
deleteCharacter(true)is calledgetTextSlices()returns all distance 0 (no text)- Enters caret iteration
- First caret: ChildCaret(ListNode) - not inline, not shadow root, state is 'initial', nothing happens
- Second caret: SiblingCaret(DecoratorBlockNode) - state is 'initial', falls to
elsebranch, removes decorator!
The fix: when we encounter a non-inline, non-shadow-root ChildCaret (like ListNode) and state is 'initial', we should not continue past it. Let me implement this:
- Structure:
- Assistant
Let me look at the existing tests to understand the expected behavior better:
- Assistant
Let me look at the selection tests to understand expected behavior:
- Assistant
Let me look at the actual implementation of
getParentCaretto understand how the caret traversal works: - Assistant
Let me look at the getParentCaret implementation to understand how the caret traversal works:
- Assistant
Let me look at what
ParagraphNode.collapseAtStartdoes: - Assistant
Now I understand the issue clearly. Let me trace through the exact flow:
- Document:
Root -> [DecoratorBlockNode, ListNode -> ListItemNode(empty)] - Cursor at offset 0 of empty ListItemNode, press backspace
- Caret traversal: ChildCaret(ListNode) → SiblingCaret(DecoratorBlockNode)
- When ChildCaret(ListNode) is encountered, it's NOT inline, NOT shadow root, state is 'initial'
- Nothing happens, traversal continues to DecoratorBlockNode
- DecoratorBlockNode is removed!
The fix: when we encounter a non-inline, non-shadow-root ChildCaret and state is 'initial', we should not continue past it. The list acts as a boundary.
Let me implement the fix:
- Document:
- Assistant
Now I understand the issue and the fix. The problem is that when the caret traversal encounters a non-inline, non-shadow-root ChildCaret (like a ListNode) and the state is 'initial', it doesn't stop the traversal. This allows the traversal to continue past the ListNode and reach the DecoratorBlockNode, which is then incorrectly removed.
The fix: when we encounter a non-inline, non-shadow-root ChildCaret and the state is 'initial', we should return (stop the traversal). This is because the list acts as a boundary, similar to a shadow root.
Let me implement the fix:
- Assistant
Wait, my fix might not be correct. Let me trace through the exact caret traversal more carefully. Let me revert my change and think about this differently:
- Assistant
Let me look at
$caretFromPointto understand what caret is created for an element selection: - Assistant
Let me look at
$filterByModeto understand how it works: - Assistant
Let me understand the issue better by writing a simpler test. Let me look at what
$createTestDecoratorNodedoes and whether it returns an isolated decorator. - Assistant
Let me find the test utilities.
- Assistant
Let me find the $createTestDecoratorNode function.
- Assistant
Let me find the test decorator node utility.
- Assistant
Let me look at the TestDecoratorNode to understand its properties:
- Assistant
Let me find the utils file.
- Assistant
Let me look at the TestDecoratorNode to understand its properties:
- Assistant
Let me look at the TestDecoratorNode more carefully:
- Assistant
Let me understand how
getParentCaretworks for SiblingCaret: - Assistant
Now I see -
getParentAtCaret()for SiblingCaret returnsthis.origin.getParent(), notthis.origin. Let me look atgetAdjacentCaretfor SiblingCaret to understand the traversal: - Assistant
Let me look at
getAdjacentCaretfor SiblingCaret: - Assistant
Let me look at the
getAdjacentCaretfor SiblingCaret: - Assistant
Let me look at how
isIsolatedworks for DecoratorNode: - Assistant
Let me find the DecoratorNode implementation.
- Assistant
Let me focus on the actual issue. Let me trace through what happens when the user has a non-empty list item at the beginning and presses backspace at offset 0. Let me check what
getTextSlicesreturns: - Assistant
Let me look at
getAdjacentCaretfor TextPointCaret: - Assistant
Let me take a different approach. Let me write a comprehensive test that actually runs to reproduce the issue:
- Assistant
Let me run the test to see what happens:
- Assistant
The test confirms the issue! The decorator is being removed. Now let me trace through the exact code path. Let me look at what caret is created for a TextPointCaret at offset 0:
- Assistant
Let me look at
getNodeAtCaretfor SiblingCaret to understand the traversal: - Assistant
Now I need to understand the
getNodeAtCaret()for SiblingCaret. Let me look at it: - Assistant
Let me look at
getNodeAtCaretfor SiblingCaret: - Assistant
Let me search for
getNodeAtCaretin the SiblingCaret class: - Assistant
Let me look at the full SiblingCaret implementation to find
getNodeAtCaret: - Assistant
Let me find the concrete SiblingCaret classes:
- Assistant
Let me look at the concrete SiblingCaret classes to find
getNodeAtCaret: - Assistant
Now I understand the issue completely. Let me trace through the exact scenario:
- The caret traversal goes: SiblingCaret(ListItemNode) → SiblingCaret(ListNode) → SiblingCaret(DecoratorBlockNode)
- When encountering DecoratorBlockNode: state is 'merge-next-block',
$isElementNode(anchor.origin)is false (TextNode), so we fall toelsebranch which removes the decorator - The decorator is a sibling of the ListNode (which wraps the anchor), NOT adjacent to the anchor itself
The fix: when we encounter a DecoratorNode and state.block is a parent/ancestor of the anchor, we should NOT remove the decorator. Let me implement this:
- Assistant
Now I understand the issue completely. The fix needs to check if the anchor is inside a structural wrapper (like a ListNode) when deciding whether to remove the decorator. Let me implement the fix:
- Assistant
Now let me run the test to see if the fix works:
- Assistant
The test passes in browser environments! Let me check the full output:
- Assistant
The test passes in the unit and bench-dom environments! The bench environment failure is expected (no DOM available). Let me clean up the test file and run the existing list tests:
- Assistant
Now let me run the existing list tests to make sure I didn't break anything:
- Assistant
All failures are only in the
benchenvironment (no DOM). Let me check if the unit tests all pass: - Assistant
Let me check just the unit and bench-dom tests:
- Assistant
Let me check the unit and bench-dom test results specifically.
- Assistant
No failures in unit or bench-dom. Let me also run the core selection tests to make sure my changes don't break anything:
- Assistant
Let me verify the unit and bench-dom tests pass:
- Assistant
All unit and bench-dom tests pass! Only bench tests fail (expected - no DOM). Let me verify the changes I made:
- Assistant
The diff looks clean. Let me now verify my fix once more and also check that it handles edge cases correctly. Let me also check if the
isParentOfmethod is available: - Assistant
Let me check if
isParentOfis available on ElementNode. - Assistant
Good,
isParentOfis available onLexicalNode. Let me also run the rich-text tests since they involve decorator node interactions: - Assistant
No failures in unit or bench-dom tests. Let me also run the formatList tests specifically:
- Assistant
All unit and bench-dom tests pass. Let me now review the changes one more time and create the final checklist:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.