Skip to content

[lexical] Bug Fix: Don't move the caret past the acceptance boundary for Enter on iOS - #8941

Merged
etrepum merged 2 commits into
meta:mainfrom
SujinKim1127:fix/ios-acceptance-boundary-caret
Aug 9, 2026
Merged

etrepum merged 2 commits into
meta:mainfrom
SujinKim1127:fix/ios-acceptance-boundary-caret

Conversation

@SujinKim1127

Copy link
Copy Markdown
Contributor

Description

On iOS, accepting an autocorrect suggestion with Enter moves the caret one
block further than the single Enter should have taken it.

$maybeMoveSelectionPastTrailingAcceptanceBoundary exists because, as its comment says,
Chrome and Firefox fire the input event for the key press that triggered the acceptance
before the one for the replacement text, leaving the caret before the acceptance
boundary.

iOS fires them in the opposite order:

keydown     key=Enter shift=false
beforeinput insertReplacementText   data=null  dataTransfer="ABC"
beforeinput insertParagraph

Because the replacement is handled first, the paragraph has not been split yet, so
block.getNextSibling() still returns the block that already followed. The correction
moves the caret into that block, and the insertParagraph that arrives next splits
it instead.

Note that the resulting block structure is the same either way — exactly one paragraph is
inserted. Only the caret position differs, which is why this is easy to miss.

This only reproduces when the paragraph has a following sibling block — with nothing
after it getNextSibling() is null and the correction is a no-op, which is likely why
it went unnoticed.

This PR skips the '\n' branch on iOS. Desktop WebKit keeps the correction, so the
existing webkit coverage in Events.spec.mjs still passes — note that guarding on
IS_APPLE_WEBKIT instead does fail that test, since it asserts the correction fires on
webkit.

Test plan

Reproduced on an iPhone 17 simulator (iOS 26.1), Safari, software keyboard:

  1. Type abc in a paragraph that has another paragraph below it
  2. Wait for the autocorrect candidate to be selected
  3. Press Enter once

Before

Simulator.Screen.Recording.-.iPhone.17.-.2026-08-06.at.15.34.04.mov

The caret skips the paragraph that was just inserted and lands at the start of the block
that already followed, so it ends up two lines below the text instead of one.

After

Simulator.Screen.Recording.-.iPhone.17.-.2026-08-07.at.09.22.04.mov

The caret stays in the newly inserted paragraph, directly below the text.

Automated

Adds packages/lexical/src/__tests__/unit/IosEnterAcceptanceBoundary.test.ts, which
mocks IS_IOS and dispatches the events in the order iOS produces them. It follows the
same vi.mock('lexical/src/environment') pattern already used for browser-specific
fixes in LexicalEvents.ts (#8769, #8680, #8475, #8725).

Two cases:

  • a following block exists — fails without this change (the caret lands in the
    following block instead of the new paragraph)
  • the paragraph is the last block — passes either way; pinned because that is the case
    where the correction was already a no-op
$ npx vitest run --project unit packages/lexical/src/__tests__/unit/
  46 files, 834 tests passed

$ E2E_BROWSER=webkit npx playwright test --project=webkit \
    packages/lexical-playground/__tests__/e2e/Events.spec.mjs
  4 passed

$ npx tsc --noEmit
  clean

Happy to move the test somewhere else or restructure it if you'd prefer different
coverage here.

…for Enter on iOS

$maybeMoveSelectionPastTrailingAcceptanceBoundary compensates for Chrome and
Firefox, which fire the input event for the key press that triggered a text
replacement *before* the one for the replacement text, leaving the caret before
the acceptance boundary.

iOS fires them in the opposite order — insertReplacementText first, then
insertParagraph. The paragraph has not been split yet at that point, so the
block the caret sits in still has its original next sibling. The correction
moves the caret into that block and the following insertParagraph splits it
instead, leaving the caret one block further down than a single Enter should
have taken it.

This only reproduces when the paragraph has a following sibling block; with
nothing after it getNextSibling() is null and the correction is a no-op, which
is likely why it went unnoticed.

Skip the Enter branch on iOS. Desktop WebKit keeps the correction, so the
existing webkit coverage in Events.spec.mjs still passes — guarding on
IS_APPLE_WEBKIT instead fails it.

Adds IosEnterAcceptanceBoundary.test.ts, which mocks IS_IOS following the
existing pattern in IosKeyboardSuggestions.test.ts and dispatches the events in
the order iOS produces them. It fails without this change.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@vercel

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

Request Review

@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 7, 2026
@vercel

vercel Bot commented Aug 8, 2026

Copy link
Copy Markdown

@SujinKim1127 is attempting to deploy a commit to the Meta Open Source Team on Vercel.

A member of the Team first needs to authorize it.

@ivailop7 ivailop7 added the extended-tests Run extended e2e tests on a PR label Aug 8, 2026
@etrepum
etrepum added this pull request to the merge queue Aug 9, 2026
Merged via the queue into meta:main with commit 48685cd Aug 9, 2026
106 checks passed
@etrepum etrepum mentioned this pull request Aug 31, 2026
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.

3 participants