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.
Assistant
Let me explore the packages directory to understand the structure.
Assistant
The list-related code is likely in
lexical-list. Let me explore that.Assistant
Let me see the full file including the truncated middle portion.
Assistant
I found the key method
collapseAtStart. Let me search for where it's called to understand the deletion flow.Assistant
Let me look at the
deleteCharactermethod andforwardDeletionfrom the beginning.Assistant
Let me look at the full
deleteCharactermethod from the start.Assistant
Let me check the git history for the relevant files to understand recent changes.
Assistant
Let me look at the existing tests for the ListItemNode to understand the test setup.
Assistant
Let me look for tests related to
collapseAtStartwith decorators.Assistant
Let me look at how existing tests use deleteCharacter or simulate backspace deletion.
Assistant
Let me look at the formatList.test.ts to see how it tests the decorator node interaction with lists.
Assistant
Let me look at the initOptions used in the formatList test and how to set up the editor with decorator nodes.
Assistant
Now let me write a reproduction test. Let me create a temporary test file.
Assistant
Let me check what nodes are registered by default in
createTestEditor.Assistant
DEFAULT_NODES includes TestDecoratorNode. The default nodes also include ListNode and ListItemNode? Let me check.
Assistant
Now let me run the reproduction test.
Assistant
The console shows
children: [ 'list' ]- the decorator was deleted! Let me add more detailed logging to understand the full before/after state.Assistant
I've reproduced the bug. The decorator was deleted but the list remained. Let me trace through the deletion logic. Let me look at the
forwardDeletionfunction.Assistant
Let me understand the caret API used in the deletion logic.
Assistant
Let me add temporary debug logging to
deleteCharacterto trace the flow.Assistant
The debug shows the merge logic descends through
listitem -> list -> decoratorand then deletes the decorator. Let me compare with the behavior when the previous block is a paragraph instead of a decorator.Assistant
Let me see the full STRUCTURE outputs more clearly.
Assistant
The behavior is now clear:
- Paragraph before list: content merges into paragraph →
paragraph: "paraitem 1"(list removed). - Decorator before list: decorator gets deleted, list remains → BUG.
Let me look at the git history of
deleteCharacterto understand the original intended behavior.- Paragraph before list: content merges into paragraph →
Assistant
Let me check the old version of
deleteCharacterbefore the caret-based rewrite.Assistant
Let me look at an older version of deleteCharacter before the caret-based rewrite to understand the original behavior.
Assistant
The issue was reported against version 0.12.2. Let me find that tag.
Assistant
The old (0.12.2) version also had the same bug - the decorator removal. Now let me test more scenarios to fully understand the behavior, including the paragraph-followed-by-decorator case.
Assistant
Let me simplify the test using async/await directly.
Assistant
Let me run the paragraph-after-decorator case with the debug logging still in place to compare the caret traversal.
Assistant
Let me look at
ParagraphNode.collapseAtStartto understand the fallback behavior.Assistant
Let me look at
$caretFromPointto understand caret traversal.Assistant
Let me look at existing tests related to decorator deletion to understand the expected behavior.
Assistant
Let me look at the full heading deleteCharacter test suite to understand the tested scenarios.
Assistant
Let me check
HeadingNode,QuoteNode, andCodeNodecollapseAtStartimplementations.Assistant
So:
- HeadingNode.collapseAtStart(): returns true always (converts heading to paragraph, moves children). Wait, line 203 is in the QuoteNode? Let me check. Actually line 195-215 is the QuoteNode (collapseAtStart returns true, replaces with paragraph). And line 403 is HeadingNode.collapseAtStart which also returns true.
Wait, let me re-examine. The first block (line 203) is in QuoteNode (the
insertNewAfterthencollapseAtStartthencanMergeWhenEmpty). And line 403 is HeadingNode.collapseAtStart.HeadingNode.collapseAtStart (line 403):
javascriptcollapseAtStart(): true { if (this.isEmpty()) { const paragraph = $createParagraphNode(); const children = this.getChildren(); children.forEach(child => paragraph.append(child)); this.replace(paragraph); } return true; }Wait, that only replaces if empty? That seems odd for a heading. Let me re-read. Actually let me look at the full HeadingNode.collapseAtStart.
Assistant
Let me search for existing tests that test decorator deletion behavior that my change might affect.
Assistant
Let me search for e2e/playwright tests that involve backspace near decorators.
Assistant
Let me search broadly for any test that asserts a decorator gets removed via backspace/deleteCharacter.
Assistant
Let me look at the LexicalSelection tests for decorators + deleteWordBackward which might exercise the merge logic. Let me look at lines around 1140-1300.
Assistant
Let me check the HeadingsBackspaceAtStart e2e test and search for decorator-related e2e backspace tests.
Assistant
Let me search the e2e Selection spec for backspace scenarios with images/decorators.
Assistant
Let me check the base
ElementNode.collapseAtStartdefault and reconsider.Assistant
Let me search the CHANGELOG for related fixes.
Assistant
Let me establish the baseline behavior for various block types preceded by a decorator. Let me write a comprehensive repro test.
Assistant
Let me fix the imports in the test file.
Assistant
Let me look at
$getTextPointCaretto understand why the first caret is a sibling caret of listitem.Assistant
OK, the iterNodeCarets uses
anchor.getSiblingCaret()for text point carets. Let me look at the TextPointCaret.getSiblingCaret. Line 400-403.Assistant
Let me look at
$getSiblingCaretto understand what origin it returns.Assistant
So
$getSiblingCaret(text "item 1", 'previous')creates a PreviousSiblingCaret with origin = text "item 1". TheiterNodeCaretsusesanchor.getSiblingCaret()= sibling caret of the text node. Then steps:$getAdjacentChildCaret(state) || state.getParentCaret(rootMode).Let me look at the PreviousSiblingCaret's getAdjacentCaret / getNodeAtCaret / getParentCaret to understand the traversal. Actually, let me look at SIBLING_CTOR and the SiblingCaret class.
Assistant
Let me look at
$getAdjacentChildCaret.Assistant
I now fully understand the traversal. Let me search e2e tests for heading/quote/code preceded by a block decorator (tweet, excalidraw, equation) to ensure my fix won't break them.
Assistant
This regression test #6974 uses an INLINE decorator inside a paragraph (descended via childCaret), so my planned fix (only changing the sibling-decorator else-branch) won't affect it. Let me verify the no-previous-block multi-item list behavior (the behavior I want to match), then implement the fix.
First, let me remove the debug logging I added.
Assistant
Now let me verify the no-previous-block multi-item list behavior (the behavior I want to match for the decorator case).
Assistant
The inline #6974-like case works (decorator preserved, "test" merges). Let me see the NO-PREV list cases.
Assistant
The NO-PREV list cases fail in jsdom because
Selection.modifyisn't implemented (the fallthrough path). My fix will call$collapseAtStartdirectly, avoiding that. Now let me implement the fix inLexicalSelection.ts.Let me view the exact decorator branch to edit.
Assistant
Now let me implement the fix. Let me confirm
anchorand$collapseAtStartare in scope.Assistant
Now let me apply the fix:
Assistant
Let me open the file at the relevant line and use the edit command.
Assistant
The fix is applied. Now let me run the comprehensive repro test to verify the behavior.
Assistant
The fix works correctly:
- LIST-SINGLE: decorator preserved, list → paragraph "item1" ✓ (exactly what the issue wanted)
- LIST-MULTI: decorator preserved, first item → paragraph "item1", rest of list remains ✓
- PARAGRAPH: unchanged (decorator removed) ✓
- QUOTE/HEADING: decorator now preserved (consistent with their no-previous-sibling backspace behavior) ✓
- INLINE #6974: unchanged ✓
Now let me run the existing test suites to ensure nothing breaks. Let me start with lexical-list, lexical-rich-text, and lexical-selection.
Assistant
The
|bench|failures are allReferenceError: document is not defined(environment issues, not my change). Let me check if these are pre-existing by stashing my change and comparing, and also run just theunitproject.Assistant
The same 80 failures occur without my change — they're pre-existing
|bench|environment issues. Let me run only theunitproject to confirm my change doesn't break unit tests.Assistant
All lexical-list unit tests pass. Now let me run the lexical-core, lexical-rich-text, and lexical-selection unit tests.
Assistant
All pass. Let me run the broader lexical core unit tests to be safe.
Assistant
All lexical core unit tests pass. Let me also run other packages with
collapseAtStart/deletion logic (code, table, playground nodes) to be safe.Assistant
All pass. Let me verify a few edge cases: forward deletion (should be unchanged) and a nested/indented list. Let me write a quick test.
Assistant
Edge cases confirmed:
- FORWARD deletion: unchanged (decorator removed as expected). ✓
- Two decorators backward: both preserved, list → paragraph. ✓
- Nested: unaffected by my change (pre-existing jsdom
modifylimitation for the within-list merge path — not my fix's concern).
Let me verify the nested case also fails without my change (confirming it's pre-existing), then clean up.
Assistant
Confirmed the nested case is pre-existing (fails in both with/without my change due to jsdom's
modifylimitation). The✓/×only reflects throw/no-throw since my repro tests have no assertions; the actual behavior difference was verified via console output earlier.Let me remove the repro test and review the final diff.
Assistant
The diff is clean and minimal. Let me verify the change type-checks correctly. Let me run a TypeScript check on the lexical package.
Assistant
No type errors in LexicalSelection. Let me run the full type-check to be sure there are no errors introduced.
Assistant
Type-check passes. Let me run ESLint on the changed file to ensure no lint issues.
Assistant
ESLint passes. Let me do a final review of the complete decorator branch to ensure correctness.
Assistant
The change is correct and complete. Let me run the playground unit tests (which test decorator nodes) to be extra safe.
Assistant
All pass. Let me do one final assertion-based verification of the exact issue scenario, then clean up.
Assistant
Both assertion-based tests pass. The fix definitively resolves the issue. Let me remove the temporary test file and confirm the final state.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.