Repository navigation
[lexical] Bug Fix: keep a DOM caret beside a NodeSelection on iOS so tapping a decorator does not scroll to the top of the editor - #9113
Closed
etrepum wants to merge 4 commits into
Conversation
…tapping a decorator does not scroll to the top of the editor ## Description On iOS, tapping a decorator (an image in the playground, or any node using `useLexicalNodeSelection` / `BlockWithAlignableContents`) selects it as a NodeSelection and the page jumps up so that the tapped node leaves the viewport. A tap that focuses the contentEditable is followed by iOS revealing the focused element: with a DOM selection it scrolls to the selection rect, with none it scrolls to the top edge of the whole contentEditable. The reconciler cleared the DOM selection for every non-range selection with `removeAllRanges()`, so by the time the reveal ran there was nothing beside the tapped node to scroll to. `$updateDOMSelection` now keeps a collapsed DOM caret at the selected node's element point (after it, in its parent) when reconciling a NodeSelection on iOS, instead of clearing the DOM selection. It still only replaces a DOM selection that was inside the editor, and falls back to clearing when no selected node has a reconciled parent. `$handleBeforeInput` prevents the default for native input while the selection is a NodeSelection, so the browser cannot insert or delete at that caret (which the mutation observer would otherwise have to revert). Other platforms are unchanged. Closes meta#9112 ## Test plan Not exercised on a real iOS device in this environment; the unit test pins the reconciler and beforeinput behavior under `IS_IOS`. ### Before ``` $ pnpm vitest --project unit --no-watch packages/lexical/src/__tests__/unit/Issue9112Repro.test.ts × the DOM selection collapses after the selected node instead of being cleared 144ms × native insertText at the kept caret is prevented while a NodeSelection stands 11ms × native deleteContentBackward at the kept caret is prevented while a NodeSelection stands 9ms × native insertParagraph at the kept caret is prevented while a NodeSelection stands 9ms FAIL |unit| ... > the DOM selection collapses after the selected node instead of being cleared AssertionError: expected +0 to be 1 // Object.is equality FAIL |unit| ... > native insertText at the kept caret is prevented while a NodeSelection stands AssertionError: expected false to be true // Object.is equality Tests 4 failed | 1 passed (5) ``` ### After ``` $ pnpm vitest --project unit --no-watch packages/lexical/src/__tests__/unit/Issue9112Repro.test.ts Test Files 1 passed (1) Tests 5 passed (5) $ pnpm vitest --project unit --no-watch packages/lexical/src packages/lexical-react/src packages/lexical-rich-text packages/lexical-selection packages/lexical-table Test Files 152 passed (152) Tests 2367 passed (2367) $ pnpm run tsc $ pnpm run flow No errors! $ npx eslint packages/lexical/src/LexicalSelection.ts packages/lexical/src/LexicalEvents.ts packages/lexical/src/__tests__/unit/Issue9112Repro.test.ts $ npx prettier --check packages/lexical/src/LexicalSelection.ts packages/lexical/src/LexicalEvents.ts packages/lexical/src/__tests__/unit/Issue9112Repro.test.ts All matched files use Prettier code style! ``` Co-Authored-By: Claude <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
… iOS so tapping a decorator does not scroll to the top of the editor" This reverts commit 1290af6.
…' into claude/lexical-issue-9112-1hqcv0
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.
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
On iOS, tapping a decorator (an image in the playground, or any node using
useLexicalNodeSelection/BlockWithAlignableContents) selects it as a NodeSelection and the page jumps up so that the tapped node leaves the viewport.A tap that focuses the contentEditable is followed by iOS revealing the focused element: with a DOM selection it scrolls to the selection rect, with none it scrolls to the top edge of the whole contentEditable. The reconciler cleared the DOM selection for every non-range selection with
removeAllRanges(), so by the time the reveal ran there was nothing beside the tapped node to scroll to.$updateDOMSelectionnow keeps a collapsed DOM caret at the selected node's element point (after it, in its parent) when reconciling a NodeSelection on iOS, instead of clearing the DOM selection. It still only replaces a DOM selection that was inside the editor, and falls back to clearing when no selected node has a reconciled parent.$handleBeforeInputprevents the default for native input while the selection is a NodeSelection, so the browser cannot insert or delete at that caret (which the mutation observer would otherwise have to revert). Other platforms are unchanged.Closes #9112
Test plan
Not exercised on a real iOS device in this environment; the unit test pins the reconciler and beforeinput behavior under
IS_IOS.Before
After