Skip to content

[Breaking Change][lexical][lexical-playground] Bug Fix: Improve character deletion around shadow roots and decorators - #7155

Merged
etrepum merged 13 commits into
meta:mainfrom
etrepum:delete-character-shadowRoot
Feb 18, 2025
Merged

etrepum merged 13 commits into
meta:mainfrom
etrepum:delete-character-shadowRoot

Conversation

@etrepum

@etrepum etrepum commented Feb 9, 2025 •

Copy link
Copy Markdown
Collaborator

Breaking Change

The heuristic used in RangeSelection.deleteCharacter to handle merging blocks and deleting decorators from a collapsed selection has been refactored for consistency. It now traverses the node tree directly instead of attempting to use browser selection APIs.

This generally has much more predictable results, aligned with how this method is expected to behave, but it is not "bug for bug" compatible with how it worked previously. Notably, it will no longer pierce shadow roots when merging blocks. For example, performing a forward deletion from the end of a block before a CollapsibleContainerNode would pierce the shadow root of its CollapsibleTitleNode and merge its first block child (e.g. the preceding paragraph and the title's paragraph would be merged). Since there was a test that expected this strange behavior, this functionality was moved to the CollapsiblePlugin's DELETE_CHARACTER_COMMAND command listener.

Description

Allowing the native platform to control deletion only really makes sense when the content to be deleted includes a TextNode, otherwise we are better off handling it ourselves. There's also some "interesting" edge cases around nested elements (with or without shadow roots) and decorators that need to be addressed by this sort of manual navigation.

Other fixes:

Closes #7082
Closes #7090
Closes #5686
Closes #7173
Closes #6763
Closes #7184

Test plan

Before

  • Using character deletion from inside of a TextNode at the beginning of a block, when that block has a TableNode or block decorator previous sibling, would affect the TextNode (Bug: Backspace after a table causes unexpected deletion #7090)
  • Removing a TableNode that was the only child of the RootNode would leave the RootNode with no children
  • Various issues with $setBlocksType (see below)
  • RangeSelection.insertNodes (used by paste) could leave existing content under certain conditions

After

  • All existing unit and e2e tests pass unchanged
  • Removing a TableNode that was the only child of the RootNode now creates a ParagraphNode in its place
  • Pressing delete at the block after a TableNode or DecoratorNode behaves predictably
  • $setBlocksType works for table selections
  • $setBlocksType works for normal
  • $setBlocksType for code nodes in the playground works for partially selected blocks
  • insertNodes removes the select text before adding anything

@vercel

vercel Bot commented Feb 9, 2025 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lexical ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 17, 2025 4:40am
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 17, 2025 4:40am

@facebook-github-bot facebook-github-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 Feb 9, 2025
@etrepum etrepum changed the title [WIP][lexical] Bug Fix: Improve character deletion around shadow roots and decorators [lexical] Bug Fix: Improve character deletion around shadow roots and decorators Feb 10, 2025
@etrepum etrepum changed the title [lexical] Bug Fix: Improve character deletion around shadow roots and decorators [Breaking Change][lexical] Bug Fix: Improve character deletion around shadow roots and decorators Feb 10, 2025
@etrepum etrepum changed the title [Breaking Change][lexical] Bug Fix: Improve character deletion around shadow roots and decorators [Breaking Change][lexical][lexical-playground] Bug Fix: Improve character deletion around shadow roots and decorators Feb 10, 2025
return false;
}
if (!isBackward) {
// inherit strange forward delete behavior per e2e election test

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to change the behaviour that forward deletion should not delete the collapsible section, I'm with you.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have strong opinions about it, it's in a playground plug-in after all. I figured that breaking zero tests would maybe speed up review but it's been a week 🤷

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make it better. I'll do my part on the reviewing side this evening.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created a follow-up #7203 to address this separately

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR in #7204

Comment on lines +263 to +267
if (target) {
if ($isLineBreakNode(possibleLineBreak)) {
possibleLineBreak.remove();
}
target.select().insertRawText(textContent);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come this toolbar logic has now become so manual? Is this the final shape? It used to be pretty straightforward to replicate once it was backed by insertNodes

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@etrepum
etrepum added this pull request to the merge queue Feb 18, 2025
Merged via the queue into meta:main with commit 484cd5b Feb 18, 2025
etrepum added a commit to etrepum/lexical that referenced this pull request Feb 19, 2025
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.

This branch was successfully deployed

2 active deployments
Preview – lexical — 03ef5e87 Deployed Feb 17, 2025 by vercel[bot]
Preview – lexical-playground — 03ef5e87 Deployed Feb 17, 2025 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

4 participants