Skip to content

[lexical] Bug Fix: Backspace at a soft line-wrap boundary in Firefox - #9101

Merged
etrepum merged 1 commit into
meta:mainfrom
etrepum:claude/backspace-line-wrap-cursor-gdrmmd
Aug 27, 2026
Merged

etrepum merged 1 commit into
meta:mainfrom
etrepum:claude/backspace-line-wrap-cursor-gdrmmd

Conversation

@etrepum

@etrepum etrepum commented Aug 27, 2026

Copy link
Copy Markdown
Collaborator

Description

Where a line soft-wraps, one DOM offset has two visual caret positions: the end of the wrapped line, and the start of the next. The DOM selection API does not expose these two positions as distinct points. Firefox spends the first Selection.modify(alter, 'backward', 'character') call flipping between those two positions rather than moving, when this happens in our "controlled" selection model the caret can get stuck.

moveNativeSelection now asks for the movement a second time when this condition is detected in a DOM Text node that has more code units in that direction for character granularity.

While investigating this issue, a platform inconsistency in DecoratorNode.isIsolated() behavior was surfaced and made consistent.

Closes #9100

Test plan

New browser-mode tests in
packages/lexical/src/__tests__/browser/Issue9100WrappedLineDeletion.test.ts. The first group lays out text in a fixed-width monospace contenteditable so it soft-wraps, asserts the fixture really does wrap, then deletes backward and forward from every offset asserting exactly one character goes each time. The second pins the isolated-decorator behavior on both engines, including that the native measurement is not consulted for it at all.

## Description

Where a line soft-wraps, one DOM offset has two visual caret positions — the
end of the wrapped line and the start of the next — and which one the caret
holds is engine state the DOM selection API does not expose. Firefox spends
the first `Selection.modify(alter, 'backward', 'character')` call flipping
between those two positions rather than moving, so it reports the same
boundary point it started from.

`RangeSelection.deleteCharacter` measures a collapsed native caret move to
find the grapheme boundary to delete to. With the caret parked on a wrap
boundary that measurement said the caret had not moved, the model selection
stayed collapsed, and Backspace deleted nothing. Chromium does not reproduce
it; the pre-existing `modify('extend')` measurement was stuck at the same
offset, so this is not a regression from the collapsed-move rework in meta#8766.

`moveNativeSelection` now asks for the movement a second time when, for
`character` granularity only, the boundary point is unchanged *and* the DOM
Text node it sits in still has a code unit on the side being crossed — so a
movement was definitely available and cannot have left that text node. A caret
that genuinely cannot move stays put on the second call too, and non-character
granularities are untouched, where a no-op is meaningful (backward
`lineboundary` from the start of a line).

That guard is sized from a scan of every caret position across 18 fixtures on
both engines, reporting only positions where a first call is consumed:

- Firefox: 14 such positions — space wraps, `word-break: break-all`,
  `white-space: normal`, RTL, mixed bidi, emoji and combining marks,
  decorator-adjacent wraps, multiple paragraphs. All 14 are interior to a text
  node, so the guard admits every one. Fixtures whose wrap lands exactly on a
  node boundary have none at all: the two visual positions there already have
  two distinct DOM representations, so nothing is ambiguous to flip.
- Chromium: only element points beside an inline decorator, where a second
  call does not cross a character but lands on the far side of the decorator.

Chasing that Chromium case down surfaced a second, older bug.
`DecoratorNode.isIsolated()` had no documented behavior for caret movement and
the code supplied none: `$modifySelectionAroundDecoratorsAndBlocks` hops the
caret over an adjacent decorator but declined to for an isolated one and
returned false, leaving the movement to the native measurement. Three
successive `modify('move', backward, 'character')` calls from the element
point between an isolated inline decorator and the text after it:

```
chromium  def:0 -> def:0 -> def:0 -> def:0   (trapped; no way past by keyboard)
firefox   def:0 -> abc:3 -> abc:2 -> abc:1   (steps over it)
```

Resolve it in the model instead: an adjacent isolated decorator stops the
caret, and the pre-pass reports the movement handled with the selection left
where it is. That matches what `isIsolated()` already means at its other call
sites — `deleteCharacter` refuses to delete such a decorator and
`$isSelectableBlockDecorator` in `@lexical/rich-text` refuses to select one —
and it is now written down on the method, which carried no doc comment at all.
This does change Firefox, which could previously step over an isolated
decorator; there was no defined behavior to preserve, since the engines never
agreed, and the only node in the repo that returns true is the playground's
portal-rendered `StickyNode`, which has no E2E coverage of caret movement.

Closes meta#9100

## Test plan

New browser-mode tests in
`packages/lexical/src/__tests__/browser/Issue9100WrappedLineDeletion.test.ts`.
The first group lays out text in a fixed-width monospace contenteditable so it
soft-wraps, asserts the fixture really does wrap, then deletes backward and
forward from every offset asserting exactly one character goes each time. The
second pins the isolated-decorator behavior on both engines, including that
the native measurement is not consulted for it at all.

### Before

```
$ VITEST_BROWSER=firefox,chromium npx vitest run \
    --project 'browser (chromium)' --project 'browser (firefox)' Issue9100

 FAIL  |browser (chromium)| .../Issue9100WrappedLineDeletion.test.ts > an isolated decorator stops the caret on every engine > the caret does not cross it, and the engine is not asked
AssertionError: expected 'text text:0' to be 'paragraph element:2' // Object.is equality
 FAIL  |browser (firefox)| .../Issue9100WrappedLineDeletion.test.ts > deletion at a soft line-wrap boundary (meta#9100) > backspace deletes exactly one character at every offset
AssertionError: backspace at offset 10: expected 'aaaa bbbb cccc dddd' to be 'aaaa bbbbcccc dddd' // Object.is equality
 FAIL  |browser (firefox)| .../Issue9100WrappedLineDeletion.test.ts > an isolated decorator stops the caret on every engine > the caret does not cross it, and the engine is not asked
AssertionError: expected 'text text:1' to be 'paragraph element:2' // Object.is equality

 Test Files  2 failed (2)
      Tests  3 failed | 5 passed (8)
```

### After

```
$ VITEST_BROWSER=firefox,chromium pnpm run test-browser

 Test Files  38 passed (38)
      Tests  270 passed | 2 skipped (272)

$ pnpm run test-unit

 Test Files  271 passed (271)
      Tests  5424 passed | 1 skipped (5425)

$ pnpm run lint && pnpm run ci-check

No errors!
```

WebKit could not be exercised: the Playwright WebKit build fails its host
requirement check in this Linux container. Firefox and Chromium were both run
for the full browser suite above.

Co-Authored-By: Claude <[email protected]>
@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 Aug 27, 2026
@vercel

vercel Bot commented Aug 27, 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 Aug 27, 2026 1:02am
lexical-playground Ready Ready Preview Aug 27, 2026 1:02am

Request Review

@etrepum
etrepum added this pull request to the merge queue Aug 27, 2026
Merged via the queue into meta:main with commit 746bf3b Aug 27, 2026
48 checks passed
unix-max pushed a commit to unix-max/lexical that referenced this pull request Aug 28, 2026
@etrepum etrepum mentioned this pull request Aug 31, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — 79d334ba Deployed Aug 27, 2026 by vercel[bot]
Preview – lexical-playground — 79d334ba Deployed Aug 27, 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: At the end of a wrapped line in Firefox, Backspace does not work

3 participants