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
removemethod in theListItemNodeto seehow deletion is handled.Assistant
Let me see the rest of the file, especially the methodsthat handle the leading edge case.
Assistant
Let me look at the LexicalNode base class to understand how the deletion logic works.
Assistant
Let me look for how
collapseAtStartis called and the surrounding logic.Assistant
Let me look at the
moveSelectionPointToSiblingfunction.Assistant
Let me look at the ElementNode remove logic.
Assistant
Let me look at the existing tests to understand the test infrastructure.
Assistant
Let me look at the tests for
remove():Assistant
Let me write a test script to reproduce the bug.
Assistant
Let me write a temporary script to reproduce the bug. First, let me look at how the existing tests are structured.
Assistant
Let me write a temporary test file to reproduce the bug.
Assistant
I need to register the decorator node. Let me check how nodes are registered in tests.
Assistant
I see there's a
TestDecoratorNodealready in test utilities. Let me update my test to use it.Assistant
The decorator is deleted but no test assertion. Let me add proper assertions:
Assistant
Let me look at how
selectStartworks:Assistant
Let me look at how decorator block nodes behave differently than inline ones.
Assistant
Let me add more debugging to understand the issue:
Assistant
Let me look at
deleteCharacterto understand the flow better:Assistant
Let me try to reproduce with multiple list items:
Assistant
Let me look at
getFirstDescendant:Assistant
So
getFirstDescendantwalks down to the deepest first leaf. For an empty listItem, it returns the listItem itself (because getFirstChild returns null, so the loop doesn't execute).So when I call
item1.selectStart()on an empty listItem, it callslistItem.select()which createsan element-type range selection.Let me look at
select()for ElementNode:Assistant
OK so for an empty listItem,
select()with anchor=0, focus=0:- listItem cannot be empty (canBeEmpty returns false for ListItemNode, since it has a parent ListNode)
- Wait, canBeEmpty for ListItemNode? Let me check.
Assistant
Now let me understand the issue better. The bug is in
deleteCharacterwhen the anchor is a text node. Let me look at the context:Assistant
Let me check the git blame on this section:
Assistant
Let me look at the original PR that added this logic:
Assistant
Let me look at the relevant tests in the e2etests for this commit:
Assistant
Let me search for tests that rely on the else branch:
Assistant
Let me check the Selection tests for HR:
Assistant
Let me look at the unit tests for selection:
Assistant
Let me search for any test that tests the else branch behavior:
Assistant
Let me now understand the right fix by examining the surrounding code:
Assistant
Now I understand the bug. Let me also look at the relevant logic in detail:
Assistant
Now let me apply the fix:
Assistant
Now let me apply the fix. First, let me make sure I understand the structure:
Assistant
Now let me apply the fix:
Assistant
Now let me run the tests to see if the fix works:
Assistant
Let me see how to handle the
domSelection.modifyissue in tests:Assistant
This is a polyfill for Selection.prototype.modify used in tests. Let me see if there's a setup file or testutility I can use.
Assistant
OK there's a polyfill for Selection.prototype.modify in
packages/lexical-selection/src/__tests__/utils/index.ts. Let me see how it's used in tests.Assistant
Let me check how the Selection.prototype.modify polyfill is loaded for tests:
Assistant
Let me update my test to import the polyfill:
Assistant
Excellent! Both tests pass! Now let me run the existing tests to make sure I didn't break anything:
Assistant
All lexical-list tests pass. Now let me run the lexical tests:
Assistant
All lexical tests pass. Let me also run the lexical-rich-text tests (which include backspace at heading start tests):
Assistant
Let me also run a wider set of tests to be sure:
Assistant
Let me think about edge cases and write more comprehensive tests:
Assistant
Let me also run the e2e tests for HorizontalRule to make sure I didn't break anything there:
Assistant
The e2e tests require a browser environment. Let me check the flow types:
Assistant
TypeScript compiles. Let me look at the final fix:
Assistant
Let me also test more edge cases:
Assistant
Let me check the test results more carefully:
Assistant
Now let me clean up my test file since I shouldn't leave test files in the repo:
Assistant
Let me run all the tests once more to make sure everything passes:
Assistant
Let me look at the playground e2e tests for backspace near lists:
Assistant
These tests are about list indentation, not about decorators. Theyshouldn't be affected by my fix.
Let me also search for tests that might involve decorators and lists:
Assistant
Let me also look at the KeyboardShortcuts tests for backspace:
Assistant
The e2e tests are mostly about UI interactions and don't directly test the scenario.
Let me also check if there's anything else I should fix. Let me also look at the fast path cross parent test:
Assistant
OK there's no other deleteCharacter in the caret code. Let me also check the existing fastpath test:
Assistant
Let me verify the FastPathCrossParent test still passes:
Assistant
The FastPathCrossParent test still passes. Let me also run the list extension tests:
Assistant
All table tests pass. Let me also check the markdown and yjs tests:
Assistant
Let me look at the full file to verify my fix:
Assistant
Let me also verify the existing test for "delete empty paragraph after horizontal rule" still works:
Assistant
Let me also run the integration tests:
Assistant
All tests pass. Let me verify my fix is correctly placed by running all the tests one more time:
Assistant
All 2962 tests pass.
Let me also do a sanity check on the diff to make sure my fix is clean:
Assistant
Let me verify the final file content is correct:
Assistant
Let me also check that the file compiles properly:
Assistant
Let me also check if there's a similar bug for
removeText(when the selection is not collapsed):Assistant
removeTextcalls$removeTextFromCaretRange. Let me check if this has similar decorator handling.Assistant
The
removeTextpath is different from the issue scenario. The issue is specifically about thedeleteCharacterpath (cursor at start of listItem, press backspace). My fix addresses this correctly.Let me do one final verification by writing a comprehensive test:
Assistant
The final test confirms:
- Final state:
[ 'test_decorator', 'paragraph' ] - The decorator is preserved!
- The list is replaced by a paragraph!
This is exactly the expected behavior the user described in the issue. Let me clean up the test file:
- Final state:
Assistant
The diff is clean and only contains the fix. Let me run the relevant test suites one more time to confirm everything works:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.