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 test harness, implement a focused fix, and run the affected test suites.
- Assistant
rgis unavailable, so I’ll usegrep/find. I’m narrowing the search to the list package’s source and tests, especially keyboard deletion commands and list-item collapse logic. - Assistant
The likely path is
ListItemNode.collapseAtStart, invoked by core range-selection backward deletion. I’ll inspect that implementation, its surrounding helpers, and list unit tests to understand intended restructuring behavior before changing it. - Assistant
collapseAtStartalready converts a sole top-level list item into a paragraph, matching the preferred expected behavior. The bug therefore likely occurs earlier in core backward deletion: it may treat the preceding decorator as an empty block to remove while searching for a merge target. I’ll inspect that control flow and existing decorator-adjacent deletion tests. - Assistant
The control flow calls
selection.modify('extend', backward, 'character')beforecollapseAtStart. If that extension selects the preceding decorator, the selection is no longer collapsed and the list-specific collapse never runs;removeText()then deletes both selected structural boundaries. I’ll inspect the full preconditions and test utilities for creating a block decorator, then reproduce this directly without modifying repository tests. - Assistant
The newer caret-based deletion logic confirms the failure: when traversal reaches a non-isolated adjacent decorator and the anchor-side list structure is non-empty, it explicitly removes the decorator and returns. The list’s own
collapseAtStarthook is only attempted later, so it never gets a chance.I’ll examine the method’s entry conditions and commit history if available. The safest general fix is likely to invoke rich-text
collapseAtStartbefore cross-block/decorator traversal when backward-deleting at offset zero, while preserving character deletion inside text and decorator selection behavior for ordinary paragraphs. - Assistant
Because this repository retains extensive history, I’ll search commit messages and patches for the reported regression wording. If the upstream project already fixed this exact issue, matching that solution is preferable to inventing broader deletion semantics.
- Assistant
No exact commit message is present. I’ll use blame/history around
collapseAtStart; this can reveal whether an earlier implementation included adjacency guards that were lost during later selection refactors. - Assistant
The history shows a 2024 list change/revert centered on backspace selection mapping. I’ll inspect those patches and the old 2023-era core deletion implementation corresponding to the reported version. This should clarify whether the robust fix belongs in core ordering or list selection remapping.
- Assistant
The 2024 changes are unrelated to decorator adjacency. I’ll inspect all historical patches that changed decorator deletion logic and the list’s collapse hook around Lexical 0.12.x. One of those may encode the exact intended precedence.
- Assistant
Commit
c478319e0(“fix #4526”) falls exactly in the 0.12 timeframe and changescollapseAtStart; I’ll inspect it and its tests. - Assistant
A global early collapse would regress normal behavior: backspace at the first list item after a paragraph should merge with the paragraph, whereas this issue only requires avoiding destructive decorator deletion. I’ll inspect
ParagraphNode.collapseAtStartand decorator branch tests, then add the narrow precedence rule at the point where core is about to remove an adjacent decorator: give the anchor-side rich block its collapse hook first. - Assistant
The narrow rule is clear: only when backward traversal encounters a decorator, call
$collapseAtStarton the original anchor hierarchy before removing that decorator. Nonempty paragraphs returnfalseand retain existing decorator deletion semantics; list items returntrueand convert/unindent themselves.Before editing, I’ll reproduce the bug with a temporary Vitest script (removed afterward): a block decorator followed by a one-item list, caret at the item’s start, then
deleteCharacter(true). - Assistant
The first reproduction command hit Corepack’s interactive download prompt (unsupported here) before executing tests, and timed out, leaving the temporary file. I’ll bypass Corepack with the repository’s installed Vitest binary and then clean up explicitly.
- Assistant
The reproduction confirmed the bug: after backward deletion the root contains only the list—the block decorator was removed—and the list item did not collapse. (The editor’s default error handler logs assertions rather than making this ad hoc test fail, but the observed node types are definitive.)
I’ll now add the narrow core guard in the decorator encounter branch. It runs only for backward deletion and returns only if an anchor ancestor handles
collapseAtStart; thus a list item converts to a paragraph before the decorator can be removed, while ordinary nonempty blocks preserve current behavior. - Assistant
Python is not installed in the environment, so no source change occurred. I’ll use the provided literal replacement utility instead.
- Assistant
The editor utility found the target but requires its display window to include it. Since the file is now open, I’ll navigate there and apply the same exact replacement.
- Assistant
The source change is in place. I’ll rerun a stronger temporary reproduction with an
onErrorhandler that throws, covering both a text-point caret and an element-point caret in an empty list item. I’ll also verify a multi-item list splits correctly: decorator, paragraph from the leading item, then the remaining list. - Assistant
All reproduction variants now pass: the decorator survives; a sole list becomes a paragraph (including empty items); and a multi-item list becomes decorator + paragraph + remaining list.
Next I’ll run the existing core selection and list-item unit suites, which are the two areas directly affected by this control-flow change.
- Assistant
The command unintentionally ran unit test files under all configured Vitest projects, including the Node-only
benchproject wheredocumentis undefined. Those environment-mismatch runs failed, but both intended DOM/unit project runs passed completely (53/53 core selection and 39/39 list-item tests). I’ll rerun with--project unitto obtain an unambiguous successful exit code, then run formatting/type validation for the changed package. - Assistant
The targeted unit run is clean: 92/92 tests pass. I’ll inspect available validation scripts and run the repository formatter/type checker in check mode against the source change, avoiding any command that rewrites unrelated files.
- Assistant
git diff --checkis clean and only the intended source file is modified. I’ll run Prettier and ESLint on that file, followed by the repository TypeScript check to ensure the added call is type-safe across the monorepo. - Assistant
One edge case deserves tightening before finalizing: isolated decorators explicitly make deletion a no-op. The new collapse hook currently runs before that isolation check, which could change list structure even though the decorator boundary is meant to block deletion. I’ll preserve existing isolation semantics by moving the collapse attempt into the non-isolated path, then rerun the reproduction and targeted tests.
- Assistant
The guard now preserves isolated-decorator no-op behavior while still handling the reported non-isolated
DecoratorBlockNodecase. Since this only moved the already validated condition one branch lower, I’ll perform the final targeted test, static checks, and repository cleanliness review.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.