Skip to content

[lexical-list] Bug Fix: Backspace at start of list item outdents or converts to paragraph - #8829

Merged
etrepum merged 5 commits into
meta:mainfrom
mayrang:fix/list-backspace-outdent
Jul 15, 2026
Merged

etrepum merged 5 commits into
meta:mainfrom
mayrang:fix/list-backspace-outdent

Conversation

@mayrang

@mayrang mayrang commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator

Description

Pressing Backspace at the start of a list item currently falls through to deleteCharacter, which merges the item with the previous block. This deletes the list formatting and, when a DecoratorNode precedes the list, deletes the decorator entirely. The expected behavior (matching Google Docs, Notion, and Apple Pages) is: indented items outdent one level, and top-level items convert to a paragraph, splitting the list if the item is in the middle.

This PR adds a KEY_BACKSPACE_COMMAND handler at COMMAND_PRIORITY_BEFORE_EDITOR that intercepts Backspace when the caret is at offset 0 of a list item. A walk-up loop from the anchor node finds the enclosing ListItemNode, then $handleListBackspaceAtStart either calls $handleOutdent (indented items) or converts the item to a paragraph with $copyNode-based list splitting (top-level items). Empty non-first items are just removed with the cursor placed at the end of the previous sibling, matching the merge behavior of deleteCharacter.

ListItemNode.collapseAtStart is also updated to match, since beforeinput's deleteContentBackward dispatches DELETE_CHARACTER_COMMAND directly — bypassing KEY_BACKSPACE_COMMAND — and reaches collapseAtStart through the $collapseAtStart walk in LexicalSelection.ts.

The existing $handleListItemBackspaceAdjacentToDecorator (#5072) is kept as-is. Its decorator-adjacent guard fires first for that specific case; the new handler covers everything else. A follow-up cleanup is tracked separately.

Closes #6334

Test plan

  • 8 new unit tests for $handleListBackspaceAtStart: single-item list, multi-item first, indented outdent, indented with siblings, middle item split, last item, empty middle (removed), empty indented (outdented)
  • 5 existing decorator-adjacent tests updated with corrected expectations
  • pnpm test-unit — lexical-list 118/118, full suite pass
  • CharacterLimit e2e regression caught and fixed — empty non-first items now defer to merge instead of paragraph conversion
  • Manual playground (Chrome, Firefox, Safari):
    • Top-level first item Backspace → converts to paragraph
    • Indented item Backspace → outdents one level
    • Multi-item list first item → list splits correctly
    • Single-item list → list removed entirely
    • Cursor in middle of text → normal delete, not intercepted
    • DecoratorNode before list → decorator preserved
  • tsc / prettier / eslint clean

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 13, 2026
@vercel

vercel Bot commented Jul 13, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview Jul 15, 2026 9:56am
lexical-playground Ready Ready Preview Jul 15, 2026 9:56am

Request Review

@etrepum

etrepum commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator

Is the KEY_BACKSPACE_COMMAND handler redundant if DELETE_CHARACTER_COMMAND has to be handled anyway?

@mayrang

mayrang commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator Author

Not entirely — deleteCharacter calls $extendSelectionForDeletion before checking collapseAtStart, so for non-first list items the selection gets extended into the previous sibling and collapseAtStart is never reached. That's actually what tripped up manual testing earlier: backspace on a middle item was merging with the previous sibling instead of outdenting.

I moved the conversion/outdent/removal logic into ListItemNode.collapseAtStart (d6b5465) so both entry points produce the same result. The KEY_BACKSPACE_COMMAND handler now just routes to collapseAtStart for two cases deleteCharacter can't reach on its own: decorator-adjacent first items (#5072) and non-first items at offset 0.

@etrepum

etrepum commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator

Claude seems to think that there's some missing pieces here that could be satisfied with DELETE_CHARACTER_COMMAND


Reviewed at 6d025d4. The refactor direction is right — consolidating the policy into ListItemNode.collapseAtStart removes the duplicated logic from the first version, and keeping a KEY_BACKSPACE_COMMAND handler is still necessary (for a non-first item, deleteCharacter takes the merge-block path and never reaches collapseAtStart). But the new getPreviousSibling() !== null gate in the handler is too broad, and it regresses two cases the first version handled.

The previousSibling !== null gate opens behavior gaps

"Has no previous sibling" is not the same as "will reach collapseAtStart via deleteCharacter". The fall-through only works when there is nothing anywhere before the item to merge into — i.e. the list is the first block of the editor (or of a shadow root like a table cell). Two first-item cases now silently keep the old merge behavior:

  1. First item of a nested list. The handler skips it (no previous sibling), and deleteCharacter descends through the nested-list wrapper's previous sibling into the parent item → cross-block merge. collapseAtStart never runs, so the item merges into its parent instead of outdenting. The first version of this PR outdented here. Meanwhile the second nested item outdents — inconsistent within the same list.

  2. First item of a list preceded by another block (paragraph, heading). Same shape: the handler skips it and deleteCharacter merges the item's text into the preceding block. So Backspace on item 2 converts it to a paragraph, while Backspace on item 1 merges it into the block above. Google Docs/Notion convert item 1 to its own paragraph here.

The consistency goal of the refactor isn't achieved yet

The commit message says both the keyboard and delete-command paths now behave identically, but the reachability problem is unchanged: on the paths that dispatch DELETE_CHARACTER_COMMAND without KEY_BACKSPACE_COMMAND (Android/iOS deleteContentBackward in LexicalEvents.ts, macOS Ctrl+H, programmatic dispatch), a non-first item still hits the merge path before collapseAtStart can run. The two paths agree only where they both reach the same code — which is mostly just the document-start case.

Suggestion that resolves all of the above at once: register a DELETE_CHARACTER_COMMAND handler (checking the isBackward payload) at COMMAND_PRIORITY_BEFORE_EDITOR instead of extending KEY_BACKSPACE_COMMAND, containing the at-start walk with no previous-sibling gate. Hardware Backspace already funnels into DELETE_CHARACTER_COMMAND via the rich-text handler, so a single interception point covers hardware keyboards, soft keyboards, Ctrl+H, and programmatic dispatch — and there's no need to predict which cases deleteCharacter would merge. The decorator-adjacent check can stay on KEY_BACKSPACE_COMMAND where it lives on main.

Smaller notes

  • collapseAtStart now opens with:

    if (isNestedListNode(this)) {
      return true;
    }

    Returning true while doing nothing aborts the deletion entirely on the deleteCharacter path ($collapseAtStart returning true makes deleteCharacter return), so Backspace becomes a swallowed no-op for an element-point selection on a nested-list wrapper item. The first version returned false here and correctly deferred to default behavior — this should remain a fall-through rather than claimed-handled.

  • The empty-item branch (getTextContentSize() === 0 with a previous item → remove() + previousSibling.selectEnd()) is a reasonable choice, but note Docs/Notion turn an empty bullet into an empty paragraph rather than merging upward. Worth a deliberate decision since this is now baked into the node API, not just the key handler.

  • The unconditional paragraph.select(0, 0) replaced the earlier conditional anchor/focus remap. That's fine for the Backspace-at-offset-0 flows, but collapseAtStart is a public node API — a caller invoking it with a selection elsewhere in the item now has the caret yanked to the paragraph start. The old remap was more faithful to the method's contract.

@mayrang

mayrang commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator Author

Pushed 6bb51bd.

Dropped the previousSibling gate, so all list items at offset 0 go through collapseAtStart now. isNestedListNode returns false to let the $collapseAtStart walker handle it, empty items convert to paragraphs instead of merging (Docs/Notion behavior), and selectStart() replaces select(0, 0). The handler also respects collapseAtStart's return value.

I kept it on KEY_BACKSPACE_COMMAND rather than moving to DELETE_CHARACTER_COMMAND — hardware Backspace never reaches DELETE_CHARACTER_COMMAND. In onKeyDown:

if (isBackspace(event)) {
  dispatchCommand(editor, KEY_BACKSPACE_COMMAND, event);
} else {
  dispatchCommand(editor, DELETE_CHARACTER_COMMAND, true);
}

The else is Ctrl+H and friends. The rich-text KEY_BACKSPACE_COMMAND handler calls $deleteCharacter directly without dispatching DELETE_CHARACTER_COMMAND, so a handler there wouldn't see hardware Backspace at all. The Ctrl+H path still works because deleteCharacter → $collapseAtStart hits the rewritten collapseAtStart.

Tested in Chrome, Firefox, Safari.

mayrang added 5 commits July 15, 2026 18:53
…AtStart

Consolidates $handleListBackspaceAtStart into ListItemNode.collapseAtStart
so that KEY_BACKSPACE_COMMAND and DELETE_CHARACTER_COMMAND produce identical
behavior. The KEY_BACKSPACE_COMMAND handler now only handles the
decorator-adjacent case (meta#5072).
Extract list items into named variables so .select() calls resolve
against ListItemNode instead of LexicalNode.
- Remove previousSibling gate: all list items at offset 0 now route
  through collapseAtStart, not just non-first items
- isNestedListNode returns false instead of true to defer to default
  deleteCharacter behavior (outdent via $collapseAtStart walker)
- Empty items convert to paragraphs instead of merging into previous
  sibling, matching Google Docs / Notion behavior
- Use paragraph.selectStart() for reliable cursor positioning
- Respect collapseAtStart return value in KEY_BACKSPACE_COMMAND handler
- Add list-type-preservation test for split
…AtStart change

Empty non-first list items now convert to paragraphs instead of merging
into the previous item, so deleting through them requires one extra
Backspace press.
@mayrang
mayrang force-pushed the fix/list-backspace-outdent branch from e94aa2c to a63ca63 Compare July 15, 2026 09:54
@etrepum
etrepum added this pull request to the merge queue Jul 15, 2026
Merged via the queue into meta:main with commit b8d1aaa Jul 15, 2026
48 checks passed
etrepum pushed a commit to etrepum/lexical that referenced this pull request Jul 15, 2026
… merge

## Description

The merge of meta#8829 introduced a new `isNestedListNode` call in
`ListItemNode.collapseAtStart`; update it to the renamed
`$isNestedListNode`.

## Test plan

### After

`pnpm run tsc` passes and all 119 lexical-list unit tests pass. No
references to the old name remain.

Co-Authored-By: Claude Fable 5 <[email protected]>
@etrepum etrepum mentioned this pull request Jul 16, 2026
corinthionia added a commit to corinthionia/lexical that referenced this pull request Oct 1, 2026
…s that cross a block or a decorator

## Description

On iOS, `KEY_BACKSPACE_COMMAND` returns `false` without calling
`event.preventDefault()` so the system keyboard keeps refreshing its
suggestion bar, and the deletion is delegated to the `beforeinput`
`deleteContentBackward` handler (meta#5841, meta#8725). There, the iOS 10-key Korean
IME workaround added by meta#8475 applies any non-collapsed `targetRange` with
`applyDOMRange()` and deletes it with `removeText()`.

That is correct for the IME's jamo deletes, which stay inside one text node,
but WebKit reports a non-collapsed range for an ordinary Backspace too. At the
start of a block it reports the range from the previous editable position to
the caret, skipping any `contenteditable=false` node in between: with
`<p>abc</p><div contenteditable="false">…</div><p></p>` and the caret in the
empty paragraph, the range is `#text(abc)@3 .. P@0`. `removeText()` deletes
everything between the two points and merges the blocks, which skips the rules
`RangeSelection.deleteCharacter` applies there:

- an empty paragraph after a block decorator should be removed and the
  decorator selected (meta#7155); on iOS the decorator is deleted too and the caret
  lands at the end of "abc";
- at the start of a text paragraph after a block decorator only the decorator
  should be deleted (meta#7155); on iOS the two paragraphs are merged as well;
- an empty paragraph before a heading should be removed and the heading kept
  (meta#8493); on iOS the heading is merged into the paragraph;
- an isolated decorator (`isIsolated()`) is never deleted by Backspace (meta#7155),
  but WebKit reports `#text(ab)@2 .. SPAN@0` across an inline
  `contenteditable=false` element inside one paragraph, and on iOS it is
  deleted.

This resolves the range on a clone of the selection and takes the fast path
only when both ends share the same nearest block element and the range contains
no decorator. Anything wider falls through to `DELETE_CHARACTER_COMMAND` from
the untouched caret, which is what every other platform runs for Backspace
(their keydown handler calls `preventDefault()`, so they never see a
`targetRange`). The IME case is unchanged: a jamo delete is a text-only range
inside one block, and so is a range straddling two adjacent text nodes in one
paragraph. The keydown pass-through is untouched, so the suggestion bar keeps
refreshing, and the fallback already calls `event.preventDefault()`, so nothing
is deleted twice. Resolving on a clone also means a range that
`applyDOMRange()` resolves to a collapsed point no longer moves the caret
before the fallback runs.

List items are unaffected: `@lexical/list` handles Backspace at their start on
keydown, ahead of the iOS pass-through (meta#8829).

Out of scope: a text-only range inside one block still takes the `removeText()`
fast path, so segmented and token text nodes delete differently there than on
other platforms. That predates this change (meta#8475) and needs the fast path
itself, which the IME depends on. Backspace at the very start of the first
block, where WebKit fires no `beforeinput` at all, is a separate defect fixed
on its own in the first-block iOS Backspace change.

## Test plan

### Before

```
$ pnpm run test-unit packages/lexical/src/__tests__/unit/LexicalIosKoreanIME.test.ts

     × keeps the block decorator and selects it when backspacing an empty paragraph after it
     × matches DELETE_CHARACTER_COMMAND at the start of a text paragraph after a block decorator
     × keeps an isolated inline decorator that the range skips over
     × keeps the heading when backspacing at its start after an empty paragraph

AssertionError: expected { json: { root: { …(8) } }, …(1) } to deeply equal { json: { root: { …(8) } }, …(1) }   (x4)

 Test Files  1 failed (1)
      Tests  4 failed | 6 passed (10)
```

### After

```
$ pnpm run test-unit packages/lexical/src/__tests__/unit/LexicalIosKoreanIME.test.ts

 Test Files  1 passed (1)
      Tests  10 passed (10)

$ pnpm run test-unit packages/lexical packages/lexical-rich-text packages/lexical-plain-text

 Test Files  345 passed (345)
      Tests  5740 passed | 1 skipped (5741)

$ VITEST_BROWSER=webkit pnpm run test-browser \
    packages/lexical/src/__tests__/browser/BackspaceTargetRangeAcrossDecorator.test.ts

 Test Files  1 passed (1)
      Tests  2 passed (2)

$ pnpm run lint && pnpm run flow && pnpm run tsc

 $ eslint ./
 Running Flow...
 Found 0 errors
 $ tsc

$ npx prettier --check packages/lexical/src/LexicalEvents.ts \
    packages/lexical/src/__tests__/unit/LexicalIosKoreanIME.test.ts \
    packages/lexical/src/__tests__/browser/BackspaceTargetRangeAcrossDecorator.test.ts

 Checking formatting...
 All matched files use Prettier code style!
```

Each new unit test runs the same document through the iOS `targetRange` path
and through `DELETE_CHARACTER_COMMAND` and asserts the results match. The one
missing from Before, a plain paragraph boundary, passes before the fix too
(merging gives the same result either way) and guards that boundary. The existing IME tests pass
unchanged.

The browser test pins the engine behavior the unit tests fake with a
`StaticRange`: it presses a real Backspace in a plain `contenteditable` with no
editor attached, and asserts that WebKit reports a non-collapsed range that
contains the `contenteditable=false` block, from both an empty and a text
paragraph. It runs on WebKit only. Firefox reports a range that does not
contain the block in the empty-paragraph case, and the fix only runs on iOS.
Chromium and Firefox skip it, so its signal comes from the macOS + webkit job
of the extended matrix.

Not yet verified on a physical iOS device. To check there:

- Backspace in an empty paragraph after a block decorator removes the paragraph
  and selects the decorator, and a second Backspace deletes the decorator
  without the page scrolling to the top (see meta#9113);
- Backspace at the start of a text paragraph after a block decorator, and
  between two plain paragraphs, matches desktop;
- Backspace at the start of a heading after an empty paragraph keeps the
  heading;
- 10-key Korean input of "안녕하세요" is correct (meta#8475) and 2-set Korean
  composition is unaffected;
- the suggestion bar still refreshes after Backspace;
- press-and-hold Backspace and Backspace over a range selection delete once.
corinthionia added a commit to corinthionia/lexical that referenced this pull request Oct 1, 2026
…s that cross a block or a decorator

## Description

On iOS, `KEY_BACKSPACE_COMMAND` returns `false` without calling
`event.preventDefault()` so the system keyboard keeps refreshing its
suggestion bar, and the deletion is delegated to the `beforeinput`
`deleteContentBackward` handler (meta#5841, meta#8725). There, the iOS 10-key Korean
IME workaround added by meta#8475 applies any non-collapsed `targetRange` with
`applyDOMRange()` and deletes it with `removeText()`.

That is correct for the IME's jamo deletes, which stay inside one text node,
but WebKit reports a non-collapsed range for an ordinary Backspace too. At the
start of a block it reports the range from the previous editable position to
the caret, skipping any `contenteditable=false` node in between: with
`<p>abc</p><div contenteditable="false">…</div><p></p>` and the caret in the
empty paragraph, the range is `#text(abc)@3 .. P@0`. `removeText()` deletes
everything between the two points and merges the blocks, which skips the rules
`RangeSelection.deleteCharacter` applies there:

- an empty paragraph after a block decorator should be removed and the
  decorator selected (meta#7155); on iOS the decorator is deleted too and the caret
  lands at the end of "abc";
- at the start of a text paragraph after a block decorator only the decorator
  should be deleted (meta#7155); on iOS the two paragraphs are merged as well;
- an empty paragraph before a heading should be removed and the heading kept
  (meta#8493); on iOS the heading is merged into the paragraph;
- an isolated decorator (`isIsolated()`) is never deleted by Backspace (meta#7155),
  but WebKit reports `#text(ab)@2 .. SPAN@0` across an inline
  `contenteditable=false` element inside one paragraph, and on iOS it is
  deleted.

This resolves the range on a clone of the selection and takes the fast path
only when both ends resolve inside the same, non-null nearest block element and
the range contains no decorator. Anything wider falls through to `DELETE_CHARACTER_COMMAND` from
the untouched caret, which is what every other platform runs for Backspace
(their keydown handler calls `preventDefault()`, so they never see a
`targetRange`). The IME case is unchanged: a jamo delete is a text-only range
inside one block, and so is a range straddling two adjacent text nodes in one
paragraph. The keydown pass-through is untouched, so the suggestion bar keeps
refreshing, and the fallback already calls `event.preventDefault()`, so nothing
is deleted twice. Resolving on a clone also means a range that
`applyDOMRange()` resolves to a collapsed point no longer moves the caret
before the fallback runs.

The same-block test requires a non-null block on each end rather than comparing
the two `$findMatchingParent()` results directly. That helper stops before the
`RootNode` and returns null, so two points with no block ancestor would compare
equal and read as "same block". This part is hardening, not a reachable fix: I
could not construct a document where it happens, because `applyDOMRange()`
normalizes a root-level DOM offset down into a child block, so both ends resolve
to a paragraph or heading element point, or to a text point inside one. Four
shapes were probed (two empty paragraphs, an empty paragraph followed by a text
paragraph, a single empty paragraph, and an empty paragraph followed by a
heading) and none produced a point on the `RootNode`. No test pins the null
rejection for that reason; a test that exercises it would pass without it. Both
lookups are hoisted into locals, which also drops a duplicated tree walk.

List items are unaffected: `@lexical/list` handles Backspace at their start on
keydown, ahead of the iOS pass-through (meta#8829).

Out of scope: a text-only range inside one block still takes the `removeText()`
fast path, so segmented and token text nodes delete differently there than on
other platforms. That predates this change (meta#8475) and needs the fast path
itself, which the IME depends on. Backspace at the very start of the first
block, where WebKit fires no `beforeinput` at all, is a separate defect fixed
on its own in the first-block iOS Backspace change.

## Test plan

### Before

```
$ pnpm run test-unit packages/lexical/src/__tests__/unit/LexicalIosKoreanIME.test.ts

     × keeps the block decorator and selects it when backspacing an empty paragraph after it
     × matches DELETE_CHARACTER_COMMAND at the start of a text paragraph after a block decorator
     × keeps an isolated inline decorator that the range skips over
     × keeps the heading when backspacing at its start after an empty paragraph

AssertionError: expected { json: { root: { …(8) } }, …(1) } to deeply equal { json: { root: { …(8) } }, …(1) }   (x4)

 Test Files  1 failed (1)
      Tests  4 failed | 6 passed (10)
```

### After

```
$ pnpm run test-unit packages/lexical/src/__tests__/unit/LexicalIosKoreanIME.test.ts

 Test Files  1 passed (1)
      Tests  10 passed (10)

$ pnpm run test-unit packages/lexical packages/lexical-rich-text packages/lexical-plain-text

 Test Files  345 passed (345)
      Tests  5740 passed | 1 skipped (5741)

$ VITEST_BROWSER=webkit pnpm run test-browser \
    packages/lexical/src/__tests__/browser/BackspaceTargetRangeAcrossDecorator.test.ts

 Test Files  1 passed (1)
      Tests  2 passed (2)

$ pnpm run lint && pnpm run flow && pnpm run tsc

 $ eslint ./
 Running Flow...
 Found 0 errors
 $ tsc

$ npx prettier --check packages/lexical/src/LexicalEvents.ts \
    packages/lexical/src/__tests__/unit/LexicalIosKoreanIME.test.ts \
    packages/lexical/src/__tests__/browser/BackspaceTargetRangeAcrossDecorator.test.ts

 Checking formatting...
 All matched files use Prettier code style!
```

Each new unit test runs the same document through the iOS `targetRange` path
and through `DELETE_CHARACTER_COMMAND` and asserts the results match. The one
missing from Before, a plain paragraph boundary, passes before the fix too
(merging gives the same result either way) and guards that boundary. The existing IME tests pass
unchanged.

The browser test pins the engine behavior the unit tests fake with a
`StaticRange`: it presses a real Backspace in a plain `contenteditable` with no
editor attached, and asserts that WebKit reports a non-collapsed range that
contains the `contenteditable=false` block, from both an empty and a text
paragraph. It runs on WebKit only. Firefox reports a range that does not
contain the block in the empty-paragraph case, and the fix only runs on iOS.
Chromium and Firefox skip it, so its signal comes from the macOS + webkit job
of the extended matrix.

The non-null block requirement was added after the runs above and re-verified on
its own. The file and test counts differ from the run above because the branch
was rebased onto a newer `main` in between:

```
$ pnpm run test-unit packages/lexical packages/lexical-rich-text packages/lexical-plain-text

 Test Files  360 passed (360)
      Tests  5966 passed | 1 skipped (5967)

$ pnpm run tsc

 $ tsc

$ pnpm exec eslint packages/lexical/src/LexicalEvents.ts
$ pnpm exec prettier --check packages/lexical/src/LexicalEvents.ts

 Checking formatting...
 All matched files use Prettier code style!
```

It rejects strictly more ranges than the version the browser run above exercised,
and is behavior-preserving for every document shape that could be constructed, so
those results stand. The browser test was not re-run for it, since it
characterizes the engine rather than this predicate.

Not yet verified on a physical iOS device. To check there:

- Backspace in an empty paragraph after a block decorator removes the paragraph
  and selects the decorator, and a second Backspace deletes the decorator
  without the page scrolling to the top (see meta#9113);
- Backspace at the start of a text paragraph after a block decorator, and
  between two plain paragraphs, matches desktop;
- Backspace at the start of a heading after an empty paragraph keeps the
  heading;
- 10-key Korean input of "안녕하세요" is correct (meta#8475) and 2-set Korean
  composition is unaffected;
- the suggestion bar still refreshes after Backspace;
- press-and-hold Backspace and Backspace over a range selection delete once.
corinthionia added a commit to corinthionia/lexical that referenced this pull request Oct 1, 2026
…s that cross a block or a decorator

## Description

On iOS, `KEY_BACKSPACE_COMMAND` returns `false` without calling
`event.preventDefault()` so the keyboard keeps refreshing its suggestion bar,
and Backspace is delegated to the `beforeinput` `deleteContentBackward` handler
(meta#5841, meta#8725). The Korean 10-key IME workaround added by meta#8475 handles a
non-collapsed `targetRange` with `removeText()`.

WebKit also reports non-collapsed ranges for ordinary Backspace. At block or
decorator boundaries, deleting those ranges directly can bypass
`RangeSelection.deleteCharacter` rules, causing decorators to be deleted or
blocks to be merged incorrectly (meta#7155, meta#8493).

This change resolves the range on a cloned selection and uses the `removeText()`
fast path only when both ends belong to the same block and the range contains no
decorator. Other ranges fall through to `DELETE_CHARACTER_COMMAND` from the
original caret, matching Backspace behavior on other platforms. The fallback
already calls `event.preventDefault()`, so nothing is deleted twice.

The same-block check also requires a non-null block, since two failed
`$findMatchingParent()` lookups would otherwise compare equal. This is defensive
hardening: `applyDOMRange()` normalized all probed root-level offsets into child
blocks, so no reachable case was found to test separately.

The Korean IME path remains unchanged because its jamo deletion stays within a
text-only range inside one block. List-item Backspace is also unaffected, since
`@lexical/list` handles it on keydown ahead of the iOS pass-through (meta#8829).

Out of scope: text-only ranges within one block still use the existing
`removeText()` path, so segmented and token text nodes delete differently there
than on other platforms (meta#8475).

## Test plan

### Before

```
$ pnpm run test-unit packages/lexical/src/__tests__/unit/LexicalIosKoreanIME.test.ts

     × keeps the block decorator and selects it when backspacing an empty paragraph after it
     × matches DELETE_CHARACTER_COMMAND at the start of a text paragraph after a block decorator
     × keeps an isolated inline decorator that the range skips over
     × keeps the heading when backspacing at its start after an empty paragraph

 Test Files  1 failed (1)
      Tests  4 failed | 6 passed (10)
```

### After

```
$ pnpm run test-unit packages/lexical packages/lexical-rich-text packages/lexical-plain-text

 Test Files  360 passed (360)
      Tests  5966 passed | 1 skipped (5967)

$ VITEST_BROWSER=webkit pnpm run test-browser \
    packages/lexical/src/__tests__/browser/BackspaceTargetRangeAcrossDecorator.test.ts

 Test Files  1 passed (1)
      Tests  2 passed (2)

$ pnpm run lint && pnpm run flow && pnpm run tsc

 $ eslint ./
 Running Flow...
 Found 0 errors
 $ tsc
```

The new unit tests compare the iOS `targetRange` path with
`DELETE_CHARACTER_COMMAND` for the same document. The browser test confirms that
a real Backspace can produce a non-collapsed target range spanning a
`contenteditable=false` block. It runs on WebKit only and is skipped on Chromium
and Firefox, so its signal comes from the macOS + webkit job of the extended
matrix.

Verified on a physical iOS device (iPhone 15 Pro, iOS 26.6.2). The device
verification predates the non-null defensive hardening described above, which
has not been re-verified on device.
corinthionia added a commit to corinthionia/lexical that referenced this pull request Oct 3, 2026
…s that cross a block or a decorator

## Description

On iOS, `KEY_BACKSPACE_COMMAND` returns `false` without calling
`event.preventDefault()` so the keyboard keeps refreshing its suggestion bar,
and Backspace is delegated to the `beforeinput` `deleteContentBackward` handler
(meta#5841, meta#8725). The Korean 10-key IME workaround added by meta#8475 handles a
non-collapsed `targetRange` with `removeText()`.

WebKit also reports non-collapsed ranges for ordinary Backspace. At block or
decorator boundaries, deleting those ranges directly can bypass
`RangeSelection.deleteCharacter` rules, causing decorators to be deleted or
blocks to be merged incorrectly (meta#7155, meta#8493).

This change resolves the range on a cloned selection and uses the `removeText()`
fast path only when both ends belong to the same block and the range contains no
decorator. Other ranges fall through to `DELETE_CHARACTER_COMMAND` from the
original caret, matching Backspace behavior on other platforms. The fallback
already calls `event.preventDefault()`, so nothing is deleted twice.

The same-block check also requires a non-null block, since two failed
`$findMatchingParent()` lookups would otherwise compare equal. This is defensive
hardening: `applyDOMRange()` normalized all probed root-level offsets into child
blocks, so no reachable case was found to test separately.

The Korean IME path remains unchanged because its jamo deletion stays within a
text-only range inside one block. Backspace at the start of a list item is also
unaffected, since `@lexical/list` consumes it on keydown ahead of the iOS
pass-through (meta#8829).

Out of scope: text-only ranges within one block still use the existing
`removeText()` path, so segmented and token text nodes delete differently there
than on other platforms (meta#8475).

## Test plan

### Before

```
$ pnpm run test-unit packages/lexical/src/__tests__/unit/LexicalIosKoreanIME.test.ts

     × keeps the block decorator and selects it when backspacing an empty paragraph after it
     × matches DELETE_CHARACTER_COMMAND at the start of a text paragraph after a block decorator
     × keeps an isolated inline decorator that the range skips over
     × keeps the heading when backspacing at its start after an empty paragraph

 Test Files  1 failed (1)
      Tests  4 failed | 6 passed (10)
```

### After

```
$ pnpm run test-unit packages/lexical packages/lexical-rich-text packages/lexical-plain-text

 Test Files  360 passed (360)
      Tests  5966 passed | 1 skipped (5967)

$ VITEST_BROWSER=webkit pnpm run test-browser \
    packages/lexical/src/__tests__/browser/BackspaceTargetRangeAcrossDecorator.test.ts

 Test Files  1 passed (1)
      Tests  2 passed (2)

$ pnpm run lint && pnpm run flow && pnpm run tsc

 $ eslint ./
 Running Flow...
 Found 0 errors
 $ tsc
```

The new unit tests compare the iOS `targetRange` path with
`DELETE_CHARACTER_COMMAND` for the same document. The browser test confirms that
a real Backspace can produce a non-collapsed target range spanning a
`contenteditable=false` block. It runs on WebKit only and is skipped on Chromium
and Firefox, so its signal comes from the macOS + webkit job of the extended
matrix.

Verified on a physical iOS device (iPhone 15 Pro, iOS 26.6.2). The device
verification predates the non-null defensive hardening described above, which
has not been re-verified on device.
corinthionia added a commit to corinthionia/lexical that referenced this pull request Oct 3, 2026
…s that cross a block or a decorator

## Description

On iOS, `KEY_BACKSPACE_COMMAND` returns `false` without calling
`event.preventDefault()` so the keyboard keeps refreshing its suggestion bar,
and Backspace is delegated to the `beforeinput` `deleteContentBackward` handler
(meta#5841, meta#8725). The Korean 10-key IME workaround added by meta#8475 handles a
non-collapsed `targetRange` with `removeText()`.

WebKit also reports non-collapsed ranges for ordinary Backspace. At block or
decorator boundaries, deleting those ranges directly can bypass
`RangeSelection.deleteCharacter` rules, causing decorators to be deleted or
blocks to be merged incorrectly (meta#7155, meta#8493).

This change resolves the range on a cloned selection and uses the `removeText()`
fast path only when both ends belong to the same block and the range contains no
decorator. Other ranges fall through to `DELETE_CHARACTER_COMMAND` from the
original caret, matching Backspace behavior on other platforms. The fallback
already calls `event.preventDefault()`, so nothing is deleted twice.

The same-block check also requires a non-null block, since two failed
`$findMatchingParent()` lookups would otherwise compare equal. This is defensive
hardening: `applyDOMRange()` normalized all probed root-level offsets into child
blocks, so no reachable case was found to test separately.

The Korean IME path remains unchanged because its jamo deletion stays within a
text-only range inside one block. Backspace at the start of a list item is also
unaffected, since `@lexical/list` consumes it on keydown ahead of the iOS
pass-through (meta#8829).

Out of scope: text-only ranges within one block still use the existing
`removeText()` path, so segmented and token text nodes delete differently there
than on other platforms (meta#8475).

## Test plan

### Before

```
$ pnpm run test-unit packages/lexical/src/__tests__/unit/LexicalIosKoreanIME.test.ts

     × keeps the block decorator and selects it when backspacing an empty paragraph after it
     × matches DELETE_CHARACTER_COMMAND at the start of a text paragraph after a block decorator
     × keeps an isolated inline decorator that the range skips over
     × keeps the heading when backspacing at its start after an empty paragraph

 Test Files  1 failed (1)
      Tests  4 failed | 6 passed (10)
```

### After

```
$ pnpm run test-unit packages/lexical packages/lexical-rich-text packages/lexical-plain-text

 Test Files  360 passed (360)
      Tests  5966 passed | 1 skipped (5967)

$ VITEST_BROWSER=webkit pnpm run test-browser \
    packages/lexical/src/__tests__/browser/BackspaceTargetRangeAcrossDecorator.test.ts

 Test Files  1 passed (1)
      Tests  2 passed (2)

$ pnpm run lint && pnpm run flow && pnpm run tsc

 $ eslint ./
 Running Flow...
 Found 0 errors
 $ tsc
```

The new unit tests compare the iOS `targetRange` path with
`DELETE_CHARACTER_COMMAND` for the same document. The browser test confirms that
a real Backspace can produce a non-collapsed target range spanning a
`contenteditable=false` block. It runs on WebKit only and is skipped on Chromium
and Firefox, so its signal comes from the macOS + webkit job of the extended
matrix.

Verified on a physical iOS device (iPhone 15 Pro, iOS 26.6.2). The device
verification predates the non-null defensive hardening described above, which
has not been re-verified on device.
corinthionia added a commit to corinthionia/lexical that referenced this pull request Oct 3, 2026
…s that cross a block or a decorator

## Description

On iOS, `KEY_BACKSPACE_COMMAND` returns `false` without calling
`event.preventDefault()` so the keyboard keeps refreshing its suggestion bar,
and Backspace is delegated to the `beforeinput` `deleteContentBackward` handler
(meta#5841, meta#8725). The Korean 10-key IME workaround added by meta#8475 handles a
non-collapsed `targetRange` with `removeText()`.

WebKit also reports non-collapsed ranges for ordinary Backspace. At block or
decorator boundaries, deleting those ranges directly can bypass
`RangeSelection.deleteCharacter` rules, causing decorators to be deleted or
blocks to be merged incorrectly (meta#7155, meta#8493).

This change resolves the range on a cloned selection and uses the `removeText()`
fast path only when both ends belong to the same block and the range contains no
decorator. Other ranges fall through to `DELETE_CHARACTER_COMMAND` from the
original caret, matching Backspace behavior on other platforms. The fallback
already calls `event.preventDefault()`, so nothing is deleted twice.

The same-block check also requires a non-null block, since two failed
`$findMatchingParent()` lookups would otherwise compare equal. This is defensive
hardening: `applyDOMRange()` normalized all probed root-level offsets into child
blocks, so no reachable case was found to test separately.

The Korean IME path remains unchanged because its jamo deletion stays within a
text-only range inside one block. Backspace at the start of a list item is also
unaffected, since `@lexical/list` consumes it on keydown ahead of the iOS
pass-through (meta#8829).

Out of scope: text-only ranges within one block still use the existing
`removeText()` path, so segmented and token text nodes delete differently there
than on other platforms (meta#8475).

## Test plan

### Before

```
$ pnpm run test-unit packages/lexical/src/__tests__/unit/LexicalIosKoreanIME.test.ts

     × keeps the block decorator and selects it when backspacing an empty paragraph after it
     × matches DELETE_CHARACTER_COMMAND at the start of a text paragraph after a block decorator
     × keeps an isolated inline decorator that the range skips over
     × keeps the heading when backspacing at its start after an empty paragraph

 Test Files  1 failed (1)
      Tests  4 failed | 6 passed (10)
```

### After

```
$ pnpm run test-unit packages/lexical packages/lexical-rich-text packages/lexical-plain-text

 Test Files  360 passed (360)
      Tests  5986 passed | 1 skipped (5987)

$ VITEST_BROWSER=webkit pnpm run test-browser \
    packages/lexical/src/__tests__/browser/BackspaceTargetRangeAcrossDecorator.test.ts

 Test Files  1 passed (1)
      Tests  2 passed (2)

$ pnpm run lint && pnpm run flow && pnpm run tsc

 $ eslint ./
 Running Flow...
 Found 0 errors
 $ tsc
```

The new unit tests compare the iOS `targetRange` path with
`DELETE_CHARACTER_COMMAND` for the same document. The browser test confirms that
a real Backspace can produce a non-collapsed target range spanning a
`contenteditable=false` block. It runs on WebKit only and is skipped on Chromium
and Firefox, so its signal comes from the macOS + webkit job of the extended
matrix.

Verified on a physical iOS device (iPhone 15 Pro, iOS 26.6.2). The device
verification predates the non-null defensive hardening described above, which
has not been re-verified on device.
zurfyx added a commit that referenced this pull request Nov 2, 2026
…s in CHANGELOG.md by the call shapes they break, and add the missing v0.48 list Backspace behavior change

## Description

Three backfilled CHANGELOG.md entries did not tell people upgrading who is affected or what to change:

- v0.46.0, #8661: the breaking entry said traversal methods "return the base node type" and only named annotated assignments. It did not say that the methods (and `$getNodeByKey` / `$getNodeByKeyOrThrow`) gained a non-generic first overload, so a call without a type argument that used to infer `T` from where its result went (a function argument, a return value, an annotated variable or array) now returns `LexicalNode` and stops compiling. Detect now names those call shapes and the TypeScript error, Migration shows the function-argument case, and the matching deprecation entry now has its own Detect and says the overloads were removed from TypeScript in v0.53.0 (#9479), while the Flow declarations keep them after #9529, instead of "Removal: not scheduled".
- v0.49.0, #8877: the entry said `LexicalCommand<T>` is invariant, but Detect only named `LexicalCommand<unknown>` and explicit generics. A `registerCommand` listener whose parameter annotation is narrower or wider than the payload (for example `(event: KeyboardEvent) => ...` for `KEY_ENTER_COMMAND`, whose payload is `KeyboardEvent | null`), or a helper typed `LexicalCommand<KeyboardEvent>` called with that command, also stops compiling. Detect lists these shapes and the error, and Migration shows inferred listeners and a generic helper.
- v0.48.0, #8829: not listed at all. `registerList` handles Backspace at `COMMAND_PRIORITY_BEFORE_EDITOR`, before rich text: at the start of a top-level item it turns the item into a paragraph and splits the list, where it used to join it to the item above. It is now a behavior change with a Verify line. `ListItemNode.collapseAtStart` also widened its return type from `true` to `boolean`; the entry says why that is not filed as breaking.

To keep this from recurring, `.changeset/README.md` gets a "Type signature changes" section: a type change that can make existing call sites stop compiling is `[breaking]` even when it only deprecates something, and its Detect names the call shapes, including callers that rely on an inferred type argument. AGENTS.md and CONTRIBUTING.md, which already summarize the changeset format, get one paragraph each. `pnpm run check-changesets` now warns (without failing) when a `[deprecation]` or `[chore]` entry mentions a type parameter, overload, generic, return or payload type, or signature and has no `Detect:` paragraph. The pattern also covers "type params", "branded", "invariant", "narrowed return/payload" and "now returns". Neither the pending changesets nor the curated `[deprecation]` and Other entries in CHANGELOG.md trigger it.

Out of scope: `packages/lexical-list/flow/LexicalList.js.flow` still declares `collapseAtStart(selection: RangeSelection): true`. Changing it is a published Flow type change that needs its own changeset.

## Changeset

None needed: documentation and changeset tooling only.

## Test plan

The new CHANGELOG.md wording was checked against the declarations at the v0.45.0, v0.46.0, v0.48.0 and v0.49.0 tags, and the TypeScript errors and the Migration snippets were compiled against the current sources. The list Backspace change was checked with a scratch unit test (not committed) that presses Backspace at the start of the second of three items, with and without `ListExtension`: `["list:first","paragraph:second","list:third"]` with it, and `["list:firstsecond|third"]` with rich text alone, which is what v0.47 did.

### Before

With the test changes applied but not the CHANGELOG.md and `entry.mjs` changes:

```
$ pnpm vitest run scripts/__tests__/unit/changesets.test.ts scripts/__tests__/unit/changelog.test.ts
     × warns when a deprecation or chore changes types without Detect 4ms
     × parses the repository CHANGELOG.md 15ms
 FAIL  |scripts-unit| scripts/__tests__/unit/changelog.test.ts > parseChangelog > parses the repository CHANGELOG.md
AssertionError: expected { behavior: 43, breaking: 35, …(1) } to deeply equal { behavior: 44, breaking: 35, …(1) }
 FAIL  |scripts-unit| scripts/__tests__/unit/changesets.test.ts > changeset entry format > warns when a deprecation or chore changes types without Detect
AssertionError: expected { errors: [], warnings: [] } to deeply equal { errors: [], warnings: [ …(1) ] }
 Test Files  2 failed (2)
      Tests  2 failed | 63 passed (65)
```

### After

```
$ pnpm vitest run scripts/__tests__/unit/changesets.test.ts scripts/__tests__/unit/changelog.test.ts
 Test Files  2 passed (2)
      Tests  65 passed (65)

$ pnpm run check-changesets
Changeset check passed: 80 pending changeset(s) valid.
```

Prettier and ESLint pass on the changed files.
zurfyx added a commit that referenced this pull request Nov 4, 2026
…raversal and v0.48 list Backspace changelog entries

## Description

Review follow-ups for the changelog entries this pull request rewrites:

- v0.48.0, #8829: nested list items were already outdented by `@lexical/rich-text` before #8829 (a nested `ListItemNode` reports its depth as its indent). The entry now says what changed: Backspace at the start of a top-level item used to join it to the block above, either the previous item or a paragraph before the list, and now turns it into a paragraph.
- v0.46.0, #8661: Detect quotes the TypeScript errors each call shape produces (TS2322, TS2345, and TS2740 for a return value).
- `.changeset/README.md`: the type-signature example uses `getFirstChildOrThrow()`, since `getFirstChild()` returns `T | null` and its argument case already failed under `strict`.

## Test plan

The quoted errors come from compiling the four call shapes (annotated `getFirstChild()`, annotated `getChildren()`, `getFirstChildOrThrow()` as an argument, and as a return value) against the current declarations, whose non-generic overloads are the ones v0.46.0 added:

```
t.ts(5,7): error TS2322: Type 'LexicalNode | null' is not assignable to type 'ParagraphNode | null'.
t.ts(6,7): error TS2322: Type 'LexicalNode[]' is not assignable to type 'ListItemNode[]'.
t.ts(7,12): error TS2345: Argument of type 'LexicalNode' is not assignable to parameter of type 'ListNode'.
t.ts(9,3): error TS2740: Type 'LexicalNode' is missing the following properties from type 'ListNode': __tag, __start, __listType, getTag, and 68 more.
```

A scratch unit test (not committed) pressing Backspace at the start of the first item of a list that follows a paragraph prints `["paragraph:para","paragraph:first","list:second"]` with `ListExtension` and `["paragraph:parafirst","list:second"]` with rich text alone.

### Before

Same tests as the previous commit:

```
$ pnpm vitest run scripts/__tests__/unit/changesets.test.ts scripts/__tests__/unit/changelog.test.ts
 Test Files  2 passed (2)
      Tests  65 passed (65)
```

### After

```
$ pnpm vitest run scripts/__tests__/unit/changesets.test.ts scripts/__tests__/unit/changelog.test.ts
 Test Files  2 passed (2)
      Tests  65 passed (65)

$ pnpm run check-changesets
Changeset check passed: 80 pending changeset(s) valid.
```

This branch was successfully deployed

2 active deployments
Preview – lexical — a63ca63d Deployed Jul 15, 2026 by vercel[bot]
Preview – lexical-playground — a63ca63d Deployed Jul 15, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Pressing backspace (delete) in an empty list item, should remove the list formatting, before removing the entire line

2 participants