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 the file to understand more about the ListItemNode:
Assistant
Let me look at the formatList.ts file for the outdent handling:
Assistant
Let me look at the existing tests for this scenario:
Assistant
Let me look more carefully at how
remove()works on aListItemNode:Assistant
Let me look at the base
removemethod onElementNode:Assistant
Let me check
moveSelectionPointToSibling:Assistant
Let me check
removeFromParent:Assistant
Let me check
$maybeMoveChildrenSelectionToParent:Assistant
Let me check the playground's TweetNode and the issue more carefully:
Assistant
Let me look at
DecoratorBlockNode:Assistant
Let me look at the playground tests to see if there's a similar scenario:
Assistant
Let me first set up a reproduction script to test this:
Assistant
Now let me write a reproduction script:
Assistant
Let me look for deletion handling:
Assistant
Now let me see how the deletion is triggered in lexical core. Let me find the coredeletion logic:
Assistant
Let me look at
collapseAtStart:Assistant
Let me look at what
collapseAtStartis doing - this might be the relevant function:Assistant
Let me check the ParagraphNode's collapseAtStart:
Assistant
Let me actually write a reproduction script to test this:
Assistant
Let me create a reproduction test:
Assistant
Let me find existing tests that use the structure I want:
Assistant
Let me look at
$removeTextAndSplitBlockto understand block deletion:Assistant
Let me look at how
remove()is called for the list item in the test framework:Assistant
Let me create a reproduction test:
Assistant
Let me look at how toproperly register a custom node in a test:
Assistant
Let me create a proper reproduction test using the TestDecoratorNode:
Assistant
The simple reproduction with
.remove()passes. The bug must be in the backspace/deleteCharacter path. Let me write a more realistic reproduction:Assistant
Let me check
$updateElementSelectionOnCreateDeleteNode:Assistant
Let me add more debugging to understand the exact behavior:
Assistant
Let me re-read the deleteCharacter code:
Assistant
Let me check
$caretFromPointand$extendCaretToRange:Assistant
OK so
$extendCaretToRangeextends the caret to a range from the current caret to the end of the document (in the direction of the caret). For our case:- direction = 'previous'
- getRoot(), 'previous') - this gets a sibling caret at the root with direction 'previous' - which is the END of the root.
Assistant
Let me look at $getAdjacentChildCaret:
Assistant
Let me add console.log statements to the deleteCharacter function to trace it:
Assistant
Now I understand the bug! Let me look at the full deleteCharacter flow:
Assistant
Let me check
isEmptyon LexicalNode:Assistant
Let me check the inheritance of DecoratorNode:
Assistant
Let me add console.log statements to the deleteCharacter code to trace:
Assistant
Let me add debug logging temporarily:
Assistant
Let me search for the actual line:
Assistant
The marker is matching the wrong location. There must be multiple occurrences. Let me search:
Assistant
The issue is the
indexOffinds the WRONGif (state.type === 'merge-block')since there are 3 occurrences. Let me find the right one (line 1858):Assistant
Let me check if Root is detected as ElementNode:
Assistant
Let me check
$getSiblingCaret:Assistant
Let me undo the debug change first:
Assistant
Let me check the imports in LexicalSelection.ts:
Assistant
Let me check if there's a node-level way to handle this:
Assistant
Let me look at what other methods ElementNode has:
Assistant
Let me look at the full deleteCharacter function:
Assistant
Now I understand the full structure. The
$collapseAtStartis only called when:- The selection is collapsed (
this.isCollapsed()is true) isBackward && anchor.offset === 0- The iterNodeCarets loop didn't return (no decorator or shadow root was found)
In our case, the decorator IS encountered and the decorator removal returns, so
$collapseAtStartis never called.The bug is in this specific code path: when we encounter a decorator and the anchor is in a list item (which is part of a list that's the state.block), we remove the decorator. We should instead handle the list (like collapseAtStart does).
Now, since we can't import ListNode into LexicalSelection.ts, we need a different approach. Let me think about this.
One approach: introduce a method on ElementNode like
removeForBackspaceAtStartor similar that:- The selection is collapsed (
Assistant
Let me look at how the list code identifies list nodes:
Assistant
Let me check the dependencies between packages:
Assistant
Now I understand the fix. Let me think about how to implement it cleanly. The simplest approach is to check if the anchor is in a list item (by checking node type strings) and handle it like collapseAtStart does. Let me first understand how to access this through the right channels:
Assistant
Let me check
$getChildCaretOrSelf:Assistant
Let me check what happens when we call $collapseAtStart in this context. First, let me look at the whole flow:
Assistant
Now let me look at the implementation. I'll think about the cleanest fix.
Let me look at how to add the fix:
Assistant
Now I'll implement the fix. The approach is to call
$collapseAtStartin the decorator branch when the anchor is not in an empty element. This allowsListItemNode.collapseAtStartto handle the list-to-paragraph conversion (which is already implemented).Let me look at the current code once more:
Assistant
Let me search for other
collapseAtStartimplementations:Assistant
Let me first remove the repro test and implement the fix:
Assistant
Now let me think about the best approach. Let me look at the existing tests for this:
Assistant
Let me search for existing tests with decorator and heading:
Assistant
Let me check
isIsolated:Assistant
Let me now implement the fix. The approach is to add a check in the decorator branch in
deleteCharacterto handle the list case:Assistant
Let me check if registering a backspace command listener in the list package is feasible:
Assistant
Let me now implement the fix:
Assistant
Now let me implement the fix:
Assistant
Let me check what the file currently looks like:
Assistant
Hmm, the decorator branch wasn't patched. Let me check what'sthere now:
Assistant
Now let me create the reproduction test again to verify the fix:
Assistant
Both tests pass! Let me run the existing tests to make sure I didn't break anything:
Assistant
Let me also run a few more relevant tests to ensure no regressions:
Assistant
All tests pass. Let me also run all the unit tests to make sure nothing else is broken:
Assistant
I need to fix a bug in my implementation. Let me trace through nested list case:
For nested lists, my function would incorrectly return true. Let me fix the helper function:
Assistant
I need to update the function to use the IMMEDIATE listitem ancestor, not any ancestor. Let me fix this:
Assistant
Now let me also add a test for the nested list edge case to make sure my fix doesn't accidentally trigger:
Assistant
Let me check why the nested list case is going through
modify:Assistant
Let me remove the nested list test since it requires the modify path which needs a window:
Assistant
Now let me delete the reproduction test file (as per instructions):
Assistant
Let me also write a temporary verification script to confirm the fix works:
Assistant
Let me remove the test that requires a window:
Assistant
The fix works correctly! Let me remove the verification test:
Assistant
Let me check git status to see all my changes:
Assistant
Let me clean up the extra blank line:
Assistant
Let me run all the unit tests one more time to make sure nothing is broken:
Assistant
Let me create a temp test one more time to verify the fix:
Assistant
Let me clean up the final verification test:
Assistant
Let me also verify that all the changes are clean:
Assistant
Let me do a final check to make sure everything is correct:
Assistant
Both tests pass. Now let me clean up the temporary test:
Assistant
Only the fix file is modified. Let me run all tests one final time to make sure nothing is broken:
Assistant
All tests pass. Let me submit the fix:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.