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 then find the relevant code that handles list item deletion.
Assistant
I'll start by exploring the repository structure.
Assistant
Let me explore the packages directory to find the list-related code.
Assistant
Let me look at the LexicalListItemNode.ts file, which likely contains the deletion logic.
Assistant
Let me look at the
collapseAtStartmethod more closely and understand the deletion flow. Let me also check the core deletion logic.Assistant
Now let me look at how
collapseAtStartis called inLexicalSelection.tsduringdeleteBackward.Assistant
Now let me understand the
deleteBackwardflow more, especially the part aroundcollapseAtStart:Assistant
Let me look at the full
deleteCharactermethod to understand the flow:Assistant
Let me look at the existing tests to understand the test setup and write a reproduction script.
Assistant
Let me search for tests related to deleting list items and decorators:
Assistant
No existing tests for deleteCharacter in list tests. Let me look at how the tests are set up in the LexicalSelection tests, particularly tests for deleting list items with decorators.
Let me check the selection tests for relevant scenarios.
Assistant
Let me look at the test helper utilities for creating a test decorator node:
Assistant
Now let me look at how the selection tests set up the editor and look at relevant delete tests:
Assistant
Let me look at the
deleteBackwardtest helper:Assistant
Let me look at the
forwardDeletionmethod:Assistant
This is getting complex. Let me write a reproduction script using the test infrastructure. First, let me check how tests are run:
Assistant
Let me look at the
initializeUnitTesthelper to understand the test setup for the list package:Assistant
Now let me write a reproduction test. Let me create a temporary test file based on the existing test setup in the list package:
Assistant
TestDecoratorNodeis included by default. Now let me write a reproduction test file:Assistant
Interesting! The decorator was deleted (the bug). The root only has the 'list' after delete. Let me add more logging to understand the flow:
Assistant
The anchor is at text offset 0. After
modify('extend', backward, 'character'), the selection likely extends back into the decorator. Let me look at themodifymethod:Assistant
Let me look at
$modifySelectionAroundDecoratorsAndBlocks:Assistant
Let me get more detail on the final state:
Assistant
Now I understand the scenarios. The bug is in Scenario B (first list item with text content, backspace at start removes the decorator). Let me compare with how a regular paragraph behaves:
Assistant
Interesting - the paragraph case also removes the decorator. Let me test the list-after-paragraph case and the list-after-decorator with content to understand the intended merge behavior:
Assistant
Let me look at the git history of the deletion logic to understand recent changes:
Assistant
The commit "Backspace at block start preserves the current block (#8493)" looks very relevant. Let me examine it:
Assistant
Let me look at the test changes in that commit to understand the intended behavior:
Assistant
Let me test heading-after-decorator and other block-after-decorator scenarios to understand if the decorator-removal is list-specific:
Assistant
Let me check the ListNode for any deletion/collapse overrides:
Assistant
Let me look at the playground's regression tests for decorator+list deletion, which might hint at the expected behavior:
Assistant
Let me look at the existing list+decorator-related tests in the selection test suite to understand expected behavior:
Assistant
Let me look at the e2e test added in #8493 to understand the expected backspace behavior near decorators:
Assistant
Let me add temporary logging to
deleteCharacterto understand the exact caret state during the bug:Assistant
Let me add temporary logging to understand the caret iteration and the anchor origin.
Assistant
The sed command corrupted the file. Let me revert it:
Assistant
Now let me add the debug log properly using the edit command:
Assistant
Now I understand the iteration. The anchor origin is the text node, so the empty-element check fails and the decorator gets removed. Let me check the existing tests that expect decorator-removal behavior to make sure I don't break them:
Assistant
Let me search all tests for the "decorator removed when adjacent block is non-empty" behavior:
Assistant
Let me check the #5583 regression test about list followed by element node:
Assistant
Not directly related. Let me search for the actual upstream fix. Let me look at the ListItemNode's
collapseAtStartand think about the right approach.Actually, let me reconsider. I recall that the actual Lexical fix for this issue (#5583 or similar) was in the
LexicalSelection.tsdeleteCharacter— specifically, they made the decorator branch NOT delete the decorator when the merge target is a block decorator and instead let the merge proceed differently. OR they added handling for list.Actually, let me reconsider. Let me look at the actual current behavior more. Let me reconsider whether the fix should be: when the previous block is a block-level decorator (DecoratorBlockNode) and the current block is non-empty, instead of removing the decorator, we should NOT remove it and instead the deletion should be a no-op (or convert current block).
Hmm, but that changes the paragraph-after-decorator behavior too (which might be intended/correct).
Wait, let me reconsider. Maybe the actual fix is: the decorator removal should only happen when the anchor is an empty element OR... no.
Let me reconsider by testing: what does the playground ACTUALLY do for backspace at start? Does it delete the image? Let me check if there's a regression test asserting that.
Assistant
Let me check if there's a test asserting the "backspace at start of paragraph after block decorator removes the decorator" behavior:
Assistant
Let me search the LexicalSelectionHelpers test for deleteCharacter with decorator scenarios:
Assistant
Let me look at how the list package registers behavior, to see if there's a hook for deletion:
Assistant
Let me check how backspace triggers
deleteCharacter- whether it goes through a command the list could intercept:Assistant
Let me test the merge behavior with a multi-item list after a paragraph:
Assistant
Let me check what ParagraphNode's collapseAtStart does:
Assistant
Let me check the ListItemNode type string:
Assistant
Let me look at the full merge-block handling:
Assistant
Let me search broadly for tests mentioning both list and decorator deletion:
Assistant
Let me check the CHANGELOG for any mention of this fix:
Assistant
The CHANGELOG mentions #7248 "deleteCharacter through ListNode-ListItemNode" - very relevant. Let me examine that commit:
Assistant
Let me look at the CollapsibleContainerNode change in #7248, which added handling for a special block:
Assistant
Let me check the e2e and regression tests for block-level decorator + backspace behavior:
Assistant
The HorizontalRule is a DecoratorBlockNode. Let me look at the relevant tests around line 141 and 436-475 to understand the expected backspace behavior near an HR.
Assistant
Let me look at the tests around lines 420-480 where backspace is pressed near the HR:
Assistant
Let me search the full git history for commits mentioning this issue or decorator+list deletion:
Assistant
Let me check which nodes override
canBeEmpty()to return false (to use as a discriminator):Assistant
Let me check the command priority constants to see if the list package could register a higher-priority handler:
Assistant
Let me verify whether TableNode is a shadow root (which would mean the decorator branch is never reached with a table as
state.block):Assistant
I now understand the issue. The fix should be in the decorator branch of
deleteCharacter: when the anchor-side block is a list (which can't be empty and can't merge into a decorator), instead of removing the decorator, collapse the list item into a paragraph. Let me first remove the debug log:Assistant
Now let me view the decorator branch to implement the fix:
Assistant
Now let me implement the fix. I'll add a new branch that handles the list case - when the anchor-side block can't be empty (a ListNode), instead of removing the decorator, collapse the list item into a paragraph:
Assistant
Now let me run my reproduction tests to verify the fix:
Assistant
All scenarios pass. The fix works:
- B (non-empty first list item after decorator): now
[decorator][paragraph "foo"]— decorator preserved, list → paragraph ✓ - B2 (multi-item):
[decorator][paragraph "foo"][list "baz"]✓ - A, C (empty cases): unchanged, decorator preserved ✓
- P (paragraph after decorator): unchanged ✓
- LP (list+paragraph merge): unchanged ✓
Now let me run the existing test suites to ensure no regressions. Let me remove my repro file first and run the relevant suites:
- B (non-empty first list item after decorator): now
Assistant
All list tests pass. Now let me run the selection tests and rich-text (heading) tests:
Assistant
All pass. Let me run the core lexical unit tests and the selection helpers tests, since I modified core
LexicalSelection.ts:Assistant
Let me run the caret unit tests since the deletion logic is caret-based, plus the core reconciler and editor state tests:
Assistant
Let me test the forward-delete (Delete key) scenario - list before a decorator:
Assistant
Let me verify the forward case was also a bug before my fix (to confirm the fix improves it), by temporarily stashing the source change:
Assistant
The forward case was also a bug before and is now fixed. Let me clean up and run a broader set of core tests:
Assistant
Let me run the full core lexical unit test suite to be thorough:
Assistant
All 582 core tests pass. Let me also run the rich-text and table tests (tables have
canBeEmpty=falselike lists, so I want to verify no regression):Assistant
All pass. Let me review my final change:
Assistant
Let me test an edge case: backspace at the start of the SECOND list item (should merge within the list, not touch the decorator):
Assistant
Let me clean up the test file and verify there are no TypeScript errors in my change:
Assistant
No TypeScript errors related to LexicalSelection.ts. Let me also check the overall tsc output count to make sure I didn't introduce errors (but there might be pre-existing errors). Let me check the full output count.
Assistant
Let me investigate the tsc exit code - capture all output:
Assistant
TypeScript typecheck passes cleanly (EXITCODE=0, no errors). Let me do a final review of the complete change and check git status:
Assistant
The change is clean and minimal. Let me verify no temporary test files remain:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.