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.
- Assistant
Let me understand the issue better. It's about deleting a list item that is the first item in a list, and it also deletes the previous DecoratorBlockNode. This sounds like a bug in the list handling code in Lexical.
Let me first look at the packages directory structure.
- Assistant
The issue is about deleting a leading ListItemNode in a list, which also deletes the previous DecoratorBlockNode. Let me look at the lexical-list package since it's likely where the list-related code lives.
- Assistant
Let me look at the LexicalListItemNode.ts file, especially any deletion-related code:
- Assistant
Now let me look at the registerList.ts file which likely handles keyboard events for lists:
- Assistant
Let me look at the deletion handling in the core lexical package. The issue is about deleting a ListItemNode and its interaction with the previous DecoratorBlockNode:
- Assistant
Let me look at the DecoratorBlockNode implementation:
- Assistant
Let me search for how deletion works for list items, especially the first list item in a list:
- Assistant
Let me look at the formatList.ts file, which likely contains the deletion handling logic:
- Assistant
Now let me look at the utils.ts file:
- Assistant
Now let me look at how deletion works in the core lexical selection module. Let me search for relevant deletion handling:
- Assistant
Let me look at the tests directory for list-related tests:
- Assistant
Let me look at how backspace/delete is handled in the core lexical editor:
- Assistant
Let me look at the LexicalSelection.ts file since it likely contains the deletion logic.
- Assistant
Let me look at LexicalSelection.ts to understand how deletion works:
- Assistant
Now let me look at how deletion of the first list item is handled. Let me check the LexicalEvents.ts:
- Assistant
Let me look at how deletion works in the events file:
- Assistant
Let me look at the context around where the backspace command is dispatched:
- Assistant
Let me search for where DELETE_CHARACTER_COMMAND is handled (which handles backspace/delete):
- Assistant
Let me look at the deleteCharacter method:
- Assistant
Let me look at more of this function to understand the deletion logic:
- Assistant
Now let me look at the
$collapseAtStartfunction which handles backspace at the start of a node: - Assistant
Let me look at the
$collapseAtStartfunction and the backspace handling at offset 0: - Assistant
Let me look at the
$collapseAtStartfunction: - Assistant
Now let me look at the
collapseAtStartmethod in the ListItemNode. This is likely where the issue is: - Assistant
Let me look at the collapseAtStart method in the ListItemNode:
- Assistant
Let me look at the full collapseAtStart flow and the forwardDeletion method:
- Assistant
Now I need to understand the actual deletion flow. Let me look at the
removeTextmethod and the$collapseAtStartflow: - Assistant
Now I'm understanding the issue. Let me look at the actual test cases related to this:
- Assistant
Let me look more closely at the collapseAtStart flow. The issue is likely in how the parent node (ListNode) handles collapseAtStart when its first child (ListItemNode) is being collapsed:
- Assistant
The ListNode doesn't have a
collapseAtStartmethod. Let me look at thecollapseAtStartmethod more carefully. - Assistant
Let me understand the issue better by looking at how the deletion of a list item interacts with its surrounding nodes. Let me check if there's a specific handler for backspace in list context:
- Assistant
Now let me write a test script to understand the issue. First, let me look at how existing tests are structured:
- Assistant
Now I understand the issue better. Let me look at the full flow when backspace is pressed at the beginning of a list item. Let me look at how
deleteCharacterhandles the case when the selection is at the start of a ListItemNode: - Assistant
Let me understand the flow better. Let me look at the
$collapseAtStartfunction and how it interacts with the caret range traversal: - Assistant
Let me look at the collapseAtStart method in ElementNode.
- Assistant
Let me understand the
$collapseAtStartfunction. It returnsfalseby default. OnlyListItemNode.collapseAtStartreturnstrue. Now let me think about what happens during deletion when the cursor is at the beginning of a list item with a DecoratorBlockNode before it.Let me look at the code flow more carefully:
- Assistant
Let me check ParagraphNode.collapseAtStart:
- Assistant
Now I understand the issue. Let me trace through the
deleteCharacterflow when backspace is pressed at the start of a list item:- The selection is collapsed at offset 0 of the text in the first ListItemNode
deleteCharacterenters the "no text in the direction" path- The iteration through carets traverses out of TextNode → ListItemNode → ListNode → DecoratorBlockNode (as sibling)
- When the DecoratorBlockNode is found as a
SiblingCaret, the code hits the decorator removal logic - Since
state.typeis 'initial' (not 'merge-next-block'), it falls to the else branch and removes the DecoratorBlockNode
The bug is that the code removes the DecoratorBlockNode without checking if the anchor is inside a structure like a list. Let me verify this understanding:
- Assistant
Now I understand the issue. The problem is in the decorator removal logic. When backspace is pressed at offset 0 of the first list item, the code traverses through carets and encounters the DecoratorBlockNode. Since
state.typeis 'initial' (not 'merge-next-block'), and the anchor is a TextNode (not an empty ElementNode), the code falls to theelsebranch and removes the DecoratorBlockNode.The fix should check if the anchor is inside a node with
isParentRequired()(like a ListItemNode inside a ListNode) and, if so, NOT remove the decorator. Instead, the code should break and let$collapseAtStarthandle the list deletion.Let me first create a reproduction test to confirm the issue:
- Assistant
Now let me understand the exact issue. When backspace is pressed at offset 0 of a list item that follows a DecoratorBlockNode:
- The selection enters the
deleteCharactercode path for "no text in direction" - The caret iteration traverses: TextNode → ListItemNode (ChildCaret) → ListNode (ChildCaret) → DecoratorBlockNode (SiblingCaret)
- Since
state.typeis still 'initial' (ChildCarets for non-inline, non-shadow elements don't update state), and the anchor is a TextNode (not empty ElementNode), the code falls to theelsebranch and callscaret.origin.remove()on the DecoratorBlockNode
The fix should be: when the anchor node has a parent with
isParentRequired() === true(like a ListItemNode), don't remove the DecoratorBlockNode. Instead, break out of the loop so the$collapseAtStartpath can handle the list item deletion properly.Let me check what
isParentRequiredreturns for relevant nodes: - The selection enters the
- Assistant
Now let me check
isParentRequiredin more detail: - Assistant
Now I have a clear understanding of the issue and the fix. Let me implement it.
The Root Cause: In the
deleteCharactermethod's caret iteration (lines 1844-1856), when the anchor is inside a list item and the iteration encounters a DecoratorBlockNode as a sibling caret, the code falls to theelsebranch and removes the decorator. The state is 'initial' because ChildCarets for ListItemNode/ListNode don't update state.The Fix: Before removing the decorator, check if the anchor node is inside a structure with
isParentRequired() === true(like ListItemNode). If so, break out of the loop instead of removing the decorator, allowing$collapseAtStartto handle the list deletion. - Assistant
Now let me look at the full
deleteCharacterflow. I need to trace the execution path carefully: - Assistant
Let me trace the exact flow. When backspace is pressed at offset 0 of a text node inside the first list item:
- Assistant
Now I understand the issue clearly. Let me trace through the code path:
- Cursor at offset 0 of TextNode inside first ListItemNode
$caretFromPoint(anchor, 'previous')creates a caret going backwardgetTextSlices()returns all zero distances → enters the exploration block- The caret traversal goes: TextNode → (ChildCaret) ListItemNode → (ChildCaret) ListNode → (SiblingCaret) DecoratorBlockNode
- For ChildCaret entries of ListItemNode and ListNode: they're not inline, not shadow root, and state is 'initial', so nothing happens
- When DecoratorBlockNode is found as a SiblingCaret,
stateis still 'initial',initialRange.anchor.originis a TextNode (not an ElementNode), so it falls to theelsebranch and callscaret.origin.remove()- removing the DecoratorBlockNode!
The fix: before removing the decorator, check if the anchor origin is inside a structure that requires a parent (like a ListItemNode). If so, don't remove the decorator and let
$collapseAtStarthandle it. - Assistant
Now I understand the exact issue. Let me trace the code flow:
- Cursor at offset 0 of TextNode inside first ListItemNode, inside a ListNode, after a DecoratorBlockNode
getTextSlices().every(slice => slice === null || slice.distance === 0)is true (no text backward at offset 0)- The caret iteration goes: TextNode → (ChildCaret) ListItemNode → (ChildCaret) ListNode → (SiblingCaret) DecoratorBlockNode
- For ChildCarets of ListItemNode and ListNode: they're not inline, not shadow root, and state is 'initial', so nothing happens
- DecoratorBlockNode is found as SiblingCaret: state is still 'initial', anchor origin is a TextNode (not an ElementNode), so it falls to the
elsebranch and removes the DecoratorBlockNode - The code returns, never reaching the
$collapseAtStartat line 1924
The fix: before removing the decorator in the
elsebranch, check if the anchor is inside a node withisParentRequired() === true(like a ListItemNode). If so, call$collapseAtStartinstead of removing the decorator. - Assistant
Now I have a clear understanding of the issue. Let me write a test to reproduce it first, then implement the fix:
- Assistant
Let me look at how existing tests are structured for list deletion:
- Assistant
Let me look at the existing tests to understand how they're structured:
- Assistant
Let me search for the right test file:
- Assistant
Let me search for test files related to DecoratorBlockNode or deletion tests.
- Assistant
Let me look at existing deletion tests with decorators:
- Assistant
Let me look at how
$createTestDecoratorNodeis defined: - Assistant
Let me look at the test utilities to understand how to create a decorator node for testing.
- Assistant
Let me look at the full test decorator node to understand the
isInline()method: - Assistant
Now I understand the exact issue. Let me trace through the code flow:
- Cursor at offset 0 of TextNode inside first ListItemNode
deleteCharacter(true)enters the "no text in direction" exploration- The caret iteration goes: TextNode → (ChildCaret) ListItemNode → (ChildCaret) ListNode → (SiblingCaret) DecoratorBlockNode
- For the ChildCaret entries of ListItemNode and ListNode, the state remains 'initial'
- When the DecoratorBlockNode is found as a SiblingCaret with state still 'initial', the code hits the
elsebranch at line 1847 and removes the DecoratorBlockNode
The fix: when the anchor is inside a node with
isParentRequired()(like a ListItemNode), we should not remove the adjacent decorator. Instead, we should handle the list deletion through$collapseAtStart. - Assistant
CodeNode does have a
collapseAtStartmethod. Let me check what it does: - Assistant
Let me find the right file path.
- Assistant
Let me look at the CodeNode's collapseAtStart method:
- Assistant
Now I have a clear understanding of the issue. Let me implement the fix. The key insight is:
- When backspace is pressed at the start of a list item that follows a DecoratorBlockNode, the caret traversal finds the DecoratorBlockNode and removes it
- The fix should check if the anchor is inside a node with
isParentRequired() === true(like a ListItemNode) before removing the decorator - If it is, we should call
$collapseAtStartinstead, which correctly handles the list item conversion
- Assistant
Now let me implement the fix. The key change is in the
elsebranch of the decorator handling. When the anchor is inside a node withisParentRequired() === true(like a ListItemNode), we should not remove the DecoratorBlockNode. Instead, we should call$collapseAtStartto properly convert the list item to a paragraph. - Assistant
I see a problem - I'm using
anchorNodewhich is already defined in the function scope. Let me fix the variable name: - Assistant
Good, now let me verify the imports include
$isRootOrShadowRootsince I'm using it: - Assistant
Let me check more carefully:
- Assistant
Good,
$isRootOrShadowRootis already imported. Let me also verify$isElementNodeand$collapseAtStartare accessible: - Assistant
Good. Now let me check if
$collapseAtStartis a function defined in the file and accessible: - Assistant
Good,
$collapseAtStartis defined later in the file. It's a module-level function so it will be hoisted and accessible. Now let me run the existing tests to make sure my changes don't break anything: - Assistant
Let me check how to run tests in this repo:
- Assistant
Let me look for the test command.
- Assistant
Let me run the unit tests for the list package and the core selection tests to verify.
- Assistant
All list tests pass. Let me also run the core selection tests:
- Assistant
All tests pass. Let me also verify that my changes compile correctly by running a broader test:
- Assistant
All 582 tests pass. Let me also run the rich-text tests since they involve selection and decorator behavior:
- Assistant
Let me also run the extension tests which may have decorator-related tests:
- Assistant
Good. Now let me verify that my change properly handles the
$collapseAtStartfunction's interaction with list items. Let me look at the$collapseAtStartfunction to make sure it will work correctly with my change: - Assistant
The
$collapseAtStartfunction walks up fromstartNodeand callscollapseAtStarton each element. For a TextNode at offset 0 inside the first ListItemNode:- TextNode → not an element, no previous sibling, continue
- ListItemNode → is an element,
collapseAtStartreturns true → converts list item to paragraph
This is correct. But there's an edge case - what if
$collapseAtStartreturns false (e.g., when the TextNode has a previous sibling within the ListItemNode)? In that case, we should still NOT remove the DecoratorBlockNode. Let me update the fix: - Assistant
I see the issue with my current fix - when
$collapseAtStartreturns false (e.g., when the TextNode has a previous sibling within the ListItemNode), the code falls through tocaret.origin.remove()which still removes the decorator. The fix should be: if we're inside a required parent, don't remove the decorator at all, regardless of$collapseAtStart's result. - Assistant
Now let me run the tests again to make sure the updated fix works:
- Assistant
All list tests pass. Let me run the full unit test suite:
- Assistant
All 582 tests pass. Let me run the full unit test suite to make sure nothing is broken across all packages:
- Assistant
All 2962 tests pass (1 skipped). Let me now review the diff to make sure my changes are correct:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.