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 first inspect the repository structure and locate list deletion/backspace handling and relevant tests. Then I’ll reproduce the behavior in the closest existing unit-test harness, implement the smallest general fix, and run the affected test suites.
- Assistant
The repository is clean and dependencies are already installed. The initial filename filter found nothing, so I’ll inspect the list package and search for list-item removal, backspace commands, and adjacent sibling deletion logic.
- Assistant
The relevant implementation is isolated in
packages/lexical-list, with node-level unit tests available. I’ll readListItemNode’s collapse behavior and the list registration handlers, plus nearby tests that establish expected first-item deletion behavior. - Assistant
ripgrepis unavailable, but ordinarygrepis sufficient. The likely hook is farther down inLexicalListItemNode.ts(itscollapseAtStartoverride), so I’ll inspect that section and grep tests using standard tools. - Assistant
collapseAtStartalready intends the desired behavior: for a sole top-level item it inserts a paragraph before the list and removes only the list. The reported adjacent decorator loss therefore likely comes from generic node-removal selection repair whenlistNode.remove()runs after inserting the paragraph. I’ll inspect the existingcollapseAtStarttests and test setup, especially “first list item with a non list item node,” to reproduce the decorator adjacency case precisely. - Assistant
The test decorator utility can model a block decorator by setting
isInline(false). Before changing code, I need distinguish directcollapseAtStartfrom command-level backspace handling. I’ll inspect the later list-item tests and Lexical’s generic collapsed-backspace implementation to trace the exact sequence. - Assistant
The generic deletion route calls
RangeSelection.deleteCharacter(true), which then climbs ancestors and invokesListItemNode.collapseAtStart. I’ll read that implementation and the test decorator class. I’ll also inspect repository history aroundcollapseAtStart; local git history can reveal whether this branch already contains partial fixes relevant to preserving selection. - Assistant
Local history shows the paragraph insertion was added in a focused 2022 commit, while the issue targets 0.12.2. I’ll inspect that commit and its tests; it may be the intended regression fix, but current code may have evolved such that only a missing regression assertion remains elsewhere. This is repository-local evidence, not an external search.
- Assistant
The historical change confirms the specific failure mechanism:
listNode.replace(paragraph)could merge/replace across the adjacent decorator, and the fix is exactly “insert paragraph, then remove list,” which current source already contains. To verify current behavior end-to-end rather than assume it, I’ll create a temporary (non-submitted) Vitest reproduction that places a block decorator before a one-item list, backspaces at the item start, and asserts both decorator preservation and paragraph conversion. I’ll remove the temporary file immediately after running it. - Assistant
The first reproduction run did not execute because Corepack attempted an interactive pnpm version download (disallowed in this environment), and the timeout occurred before cleanup. I’ll bypass Corepack using the already-installed local Vitest binary, run the reproduction, capture its status, and remove the temporary test regardless of result.
- Assistant
The reproduction harness mistake was mine: I destructured
editorbeforeinitializeUnitTest’sbeforeEachinitialized it. I’ll rerun with the established callback pattern. If it passes, I’ll still make a focused robustness change in the same path: useListItemNode.replace(paragraph, true)for top-level collapse. That operation atomically inserts the paragraph, transfers children, maps all element-anchored selection points, and removes the now-empty list—avoiding the dangerous parent-list replacement while removing duplicated/manual selection handling. - Assistant
The corrected reproduction genuinely fails: only one root child remains, confirming the decorator is deleted before
ListItemNode.collapseAtStartgets a chance to run. The cause is ordering inRangeSelection.deleteCharacter: browser-stylemodify('extend', backward, 'character')first extends across the block decorator, andremoveText()deletes it; ancestorcollapseAtStartis only attempted if that extension remains collapsed.I’ll inspect
ParagraphNode.collapseAtStartand nearby deletion tests to determine a safe condition for trying structural collapse before extending across a decorator, without changing ordinary paragraph-to-paragraph merges. - Assistant
There is already a newer caret-based cross-block deletion path before the legacy decorator-extension fallback. It explicitly removes an adjacent decorator when the anchor-side block is non-empty. For a list item, however, structural collapse should take precedence at its logical start. I’ll inspect the start of
deleteCharacterand caret state construction so the fix can be narrowly placed when the selection is at offset zero and an ancestor overridescollapseAtStart. - Assistant
A broad pre-collapse would be wrong for later list items (their backspace should merge with the preceding item). The narrow safe point is when caret traversal has actually encountered an adjacent decorator. At that point, for backward deletion at offset zero, I’ll first offer the anchor’s structural ancestors a chance to collapse;
ListItemNodewill convert itself to a paragraph, while ordinary non-empty paragraphs return false and retain existing decorator-deletion semantics. This also handles empty list-item element selections and isolated decorators. - Assistant
Python is not installed, so no file was changed. I’ll apply the same exact replacement with the available Node runtime and inspect the diff.
- Assistant
The change is now limited to the decorator encounter branch. I’ll rerun the exact failed reproduction and expand the temporary script to cover an empty sole list item as well as a two-item list (ensuring only the leading item is converted and the remaining list survives). Then I’ll remove the script.
- Assistant
The reproduction now passes for non-empty, empty, and multi-item cases, and the temporary file is removed. I’ll run the two most directly affected existing unit suites: core
LexicalSelection(decorator/caret deletion semantics) andLexicalListItemNode(all list structure behavior). - Assistant
Both affected suites pass (92 tests). I’ll now verify formatting and linting on the modified file, inspect the final diff/status for accidental artifacts, and consider whether the condition needs any additional guard based on static checks.
- Assistant
Formatting, lint, and whitespace checks all pass; the final diff is one focused nine-line guard. As a final sanity check, I’ll inspect the base
ElementNodeandRootNodecollapse contracts to ensure calling$collapseAtStarthere cannot accidentally consume a decorator deletion for an unrelated node type at offset zero.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.