Repository navigation
[Breaking Change][lexical] Bug Fix: Backspace at block start preserves the current block - #8493
Conversation
|
I think this does make sense to scope down, but since it's more of a behavior change than a bug fix we should call it out with a |
|
Updated — |
|
For some reason the deployments aren't showing up for this branch, I don't have access to introspect vercel but I can't merge without some final QA just in case |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
The Vercel deployment seems to have gotten dropped somewhere between the title/body edit on the day I opened this and the breaking-change update — not sure why; happy to do whatever helps if you have a way to retrigger. In the meantime, ran through manual QA locally (Chromium and Safari) on top of the e2e CI runs that already covered chromium/firefox/webkit on macOS. Six scenarios:
All matched expected. Just pushed a no-op commit to nudge Vercel into rebuilding the preview. |
|
By the way, this PR reminded me of a problem: when the cursor is at the beginning of a heading and there are no sibling elements before it, pressing Backspace causes the heading to recreate. This doesn't happen with other elements Screen.Recording.2026-05-11.at.13.55.21.movI'm not sure if it's appropriate to fix this bug as part of this PR, but it seems to me that you're pretty close to a solution |
580ed62 to
8b6944e
Compare
|
@levensta — thanks for spotting this. Confirmed and folded in ( Root cause: QuoteNode and ListItemNode Tests added in
While addressing this, a closer look at the merge-block exception surfaced a related cascade gap: Updated PR title/body to cover both scopes. |
etrepum
left a comment
There was a problem hiding this comment.
On closer inspection I'm not sure we want this to happen when the preceding block is a ListNode, at least if we're mirroring what google docs does. I think the ListNode should not be considered empty because it has a ListItemNode
…s the current block
8b6944e to
01f7e49
Compare
|
@etrepum — agreed on the ListNode framing. Narrowed the exception to siblings at the same nesting level ( Pinned that behavior with a |
…Backspace at the start of the first block ## Description On iOS, `KEY_BACKSPACE_COMMAND` returns `false` without calling `event.preventDefault()` so the system keyboard keeps refreshing its autocomplete/suggestion bar, and the deletion is delegated to the `beforeinput` `deleteContentBackward` handler (meta#5841, meta#8725). That delegation has one hole: WebKit fires no `beforeinput` when the caret sits at the very start of the editable region. Measured on Safari 26.5, Backspace there produces a `keydown` and nothing else, so nothing performs the deletion, `RangeSelection.deleteCharacter` never runs, and `$collapseAtStart` is never reached. The keystroke is dropped entirely, so a first-block `QuoteNode` or `CodeNode` cannot be turned back into a paragraph, an empty first-block `HeadingNode` cannot either, and a blank first `ParagraphNode` followed by another block is not removed. Chromium does fire `beforeinput` in the same position, so this is iOS-only. Two neighbouring cases are *not* affected, for reasons worth recording: a non-empty first-block heading is deliberately left in place on every platform since meta#8493, and a first-block list is handled by `@lexical/list`'s own `KEY_BACKSPACE_COMMAND` listener at `COMMAND_PRIORITY_BEFORE_EDITOR`, which runs ahead of rich-text's and calls `preventDefault()` itself. `@lexical/plain-text` carries the same pass-through and needs the same carve-out. It registers no nodes of its own, so `ParagraphNode`'s is the only `collapseAtStart` that can run there — a blank first paragraph followed by another block. Enter and paste cannot produce that second block (both insert a line break), but `$initialEditorState`, a restored editor state and direct root mutation all can, and nothing normalizes the root to a single paragraph. Fixing only rich-text would leave one of two identical call sites broken, so the predicate is duplicated there, module-private in each, matching how the neighbouring `$isSelectionCollapsedAtFrontOfIndentedBlock` is kept private to rich-text rather than lifted into `@lexical/utils`. Out of scope, but worth stating so the boundary is not mistaken for an oversight: Backspace at the start of a *later* block still routes through `beforeinput` on iOS, where a separate pre-existing defect mishandles it — the iOS 10-key Korean IME workaround added by meta#8475 applies `removeText()` to any non-collapsed `targetRange`, including the cross-block range WebKit reports at a block boundary, bypassing `deleteCharacter` and with it the block-preserving behavior meta#8493 established. That is a different package and a different fix. This narrows the pass-through with `$isSelectionCollapsedAtStartOfFirstBlock`, so that one caret position takes the same `preventDefault()` + `DELETE_CHARACTER_COMMAND` path every other platform already takes. The predicate is anchored on the root's own first child rather than `getTopLevelElement()`, which stops at shadow roots and slots and would otherwise also match the first block of every table cell, layout item and collapsible section in the document. Whether the caret is at that block's leading edge is delegated to `$isAtStartOfNode` from `@lexical/utils`, which already handles nesting and slot boundaries. Behavior is unchanged everywhere else: the guard is ANDed after `IS_IOS`, the indented-block outdent branch still runs first, and `preventDefault()` on keydown suppresses the `beforeinput` that would otherwise delete a second time. One existing assertion changes as a result — at the start of a first-block paragraph the handler now reports the keystroke as handled, though `ParagraphNode.collapseAtStart` leaves a non-blank paragraph alone, so the content is untouched exactly as before. ## Test plan ### Before ``` $ npx vitest run packages/lexical/src/__tests__/unit/IosKeyboardSuggestions.test.ts FAIL IosKeyboardSuggestions.test.ts > cursor at start of text does not delete (nothing to delete) AssertionError: expected false to be true // Object.is equality 228| expect(handled).toBe(true); FAIL IosKeyboardSuggestions.test.ts > iOS Backspace at the start of the first block > an empty first-block quote collapses to a paragraph AssertionError: expected false to be true // Object.is equality 301| expect(editor.dispatchCommand(KEY_BACKSPACE_COMMAND, event)).toBe(true); FAIL IosKeyboardSuggestions.test.ts > iOS Backspace at the start of the first block > a non-empty first-block quote collapses and keeps its text AssertionError: expected false to be true // Object.is equality 316| expect(editor.dispatchCommand(KEY_BACKSPACE_COMMAND, event)).toBe(true); Test Files 1 failed (1) Tests 3 failed | 10 passed (13) ``` ### After ``` $ npx vitest run packages/lexical/src/__tests__/unit/IosKeyboardSuggestions.test.ts Test Files 1 passed (1) Tests 15 passed (15) $ pnpm run test-unit -- packages/lexical-rich-text packages/lexical-plain-text \ packages/lexical-utils packages/lexical-selection packages/lexical/src/__tests__/unit Test Files 104 passed (104) Tests 1780 passed (1780) $ pnpm run lint && pnpm run flow && pnpm run tsc && pnpm run prettier (eslint: no violations) No errors! (tsc: no errors) All matched files use Prettier code style! $ for b in webkit chromium firefox; do VITEST_BROWSER=$b pnpm run test-browser \ -- packages/lexical-rich-text/src/__tests__/browser/BackspaceBeforeInputAtEditableStart.test.ts; done webkit Tests 4 passed (4) chromium Tests 4 passed (4) firefox Tests 4 passed (4) $ VITEST_BROWSER=webkit pnpm run test-browser -- packages/lexical-rich-text Test Files 21 passed (21) Tests 173 passed (173) ``` The new tests cover both directions of the guard: an empty and a non-empty first-block quote collapse to a paragraph, an indented first-block quote still outdents instead of collapsing, and a later block, a non-collapsed selection, a later child of the first block, and a shadow-root block mid-document all still pass through to `beforeinput`. Each was checked against a reverted source: with the guard removed three fail, with the predicate anchored on `getTopLevelElement()` the shadow-root case fails, and with the outdent branch removed the indent case fails. The plain-text case fails with only its own guard reverted, so the two halves are covered independently. The engine assumption the carve-out rests on is pinned by a browser-mode characterization test that presses a real Backspace against a plain `contenteditable` with no editor attached — going through Lexical would suppress the `beforeinput` being observed, since its keydown handler calls `preventDefault()` on non-iOS platforms. Headless WebKit reproduces the quirk: no `deleteContentBackward` arrives at the start of a first block, empty or not, while Chromium and Firefox both fire one, and all three fire mid-text and at the start of a later block. Inverting the assumption fails the two WebKit cases and leaves the others green, so the test is a real canary: if WebKit ever starts firing there, it fails and the carve-out can be deleted. Verified on a physical iOS device: a first-block quote and code block now collapse to a paragraph; the suggestion bar still refreshes on Backspace elsewhere; no double deletion at a later block or across a range selection; a shadow-root block mid-document is unaffected; an indented first-block quote outdents; press-and-hold Backspace at the first block start behaves; Korean IME composition is unaffected; a first-block list still collapses via `@lexical/list`'s own higher-priority handler. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…Backspace at the start of the first block ## Description On iOS, `KEY_BACKSPACE_COMMAND` returns `false` without calling `event.preventDefault()` so the system keyboard keeps refreshing its autocomplete/suggestion bar, and the deletion is delegated to the `beforeinput` `deleteContentBackward` handler (meta#5841, meta#8725). That delegation has one hole: WebKit fires no `beforeinput` when the caret sits at the very start of the editable region. Measured on Safari 26.5, Backspace there produces a `keydown` and nothing else, so nothing performs the deletion, `RangeSelection.deleteCharacter` never runs, and `$collapseAtStart` is never reached. The keystroke is dropped entirely, so a first-block `QuoteNode` or `CodeNode` cannot be turned back into a paragraph, an empty first-block `HeadingNode` cannot either, and a blank first `ParagraphNode` followed by another block is not removed. Chromium does fire `beforeinput` in the same position, so this is iOS-only. Two neighbouring cases are *not* affected, for reasons worth recording: a non-empty first-block heading is deliberately left in place on every platform since meta#8493, and a first-block list is handled by `@lexical/list`'s own `KEY_BACKSPACE_COMMAND` listener at `COMMAND_PRIORITY_BEFORE_EDITOR`, which runs ahead of rich-text's and calls `preventDefault()` itself. `@lexical/plain-text` carries the same pass-through and needs the same carve-out. It registers no nodes of its own, so `ParagraphNode`'s is the only `collapseAtStart` that can run there — a blank first paragraph followed by another block. Enter and paste cannot produce that second block (both insert a line break), but `$initialEditorState`, a restored editor state and direct root mutation all can, and nothing normalizes the root to a single paragraph. Fixing only rich-text would leave one of two identical call sites broken, so the predicate is duplicated there, module-private in each, matching how the neighbouring `$isSelectionCollapsedAtFrontOfIndentedBlock` is kept private to rich-text rather than lifted into `@lexical/utils`. Out of scope, but worth stating so the boundary is not mistaken for an oversight: Backspace at the start of a *later* block still routes through `beforeinput` on iOS, where a separate pre-existing defect mishandles it — the iOS 10-key Korean IME workaround added by meta#8475 applies `removeText()` to any non-collapsed `targetRange`, including the cross-block range WebKit reports at a block boundary, bypassing `deleteCharacter` and with it the block-preserving behavior meta#8493 established. That is a different package and a different fix. This narrows the pass-through with `$isSelectionCollapsedAtStartOfFirstBlock`, so that one caret position takes the same `preventDefault()` + `DELETE_CHARACTER_COMMAND` path every other platform already takes. The predicate is anchored on the root's own first child rather than `getTopLevelElement()`, which stops at shadow roots and slots and would otherwise also match the first block of every table cell, layout item and collapsible section in the document. Whether the caret is at that block's leading edge is delegated to `$isAtStartOfNode` from `@lexical/utils`, which already handles nesting and slot boundaries. Behavior is unchanged everywhere else: the guard is ANDed after `IS_IOS`, the indented-block outdent branch still runs first, and `preventDefault()` on keydown suppresses the `beforeinput` that would otherwise delete a second time. One existing assertion changes as a result — at the start of a first-block paragraph the handler now reports the keystroke as handled, though `ParagraphNode.collapseAtStart` leaves a non-blank paragraph alone, so the content is untouched exactly as before. ## Test plan ### Before ``` $ npx vitest run packages/lexical/src/__tests__/unit/IosKeyboardSuggestions.test.ts × cursor at start of text does not delete (nothing to delete) × an empty first-block quote collapses to a paragraph × a non-empty first-block quote collapses and keeps its text × a blank first paragraph before another block is removed AssertionError: expected false to be true // Object.is equality (x4) Test Files 1 failed (1) Tests 4 failed | 11 passed (15) ``` ### After ``` $ npx vitest run packages/lexical/src/__tests__/unit/IosKeyboardSuggestions.test.ts Test Files 1 passed (1) Tests 15 passed (15) $ pnpm run test-unit -- packages/lexical-rich-text packages/lexical-plain-text \ packages/lexical-utils packages/lexical-selection packages/lexical/src/__tests__/unit Test Files 104 passed (104) Tests 1780 passed (1780) $ pnpm run lint && pnpm run flow && pnpm run tsc && pnpm run prettier (eslint: no violations) No errors! (tsc: no errors) All matched files use Prettier code style! $ for b in webkit chromium firefox; do VITEST_BROWSER=$b pnpm run test-browser \ -- packages/lexical-rich-text/src/__tests__/browser/BackspaceBeforeInputAtEditableStart.test.ts; done webkit Tests 4 passed (4) chromium Tests 4 passed (4) firefox Tests 4 passed (4) $ VITEST_BROWSER=webkit pnpm run test-browser -- packages/lexical-rich-text Test Files 21 passed (21) Tests 173 passed (173) ``` The new tests cover both directions of the guard: an empty and a non-empty first-block quote collapse to a paragraph, an indented first-block quote still outdents instead of collapsing, and a later block, a non-collapsed selection, a later child of the first block, and a shadow-root block mid-document all still pass through to `beforeinput`. Each was checked against a reverted source: with the guard removed three fail, with the predicate anchored on `getTopLevelElement()` the shadow-root case fails, and with the outdent branch removed the indent case fails. The plain-text case fails with only its own guard reverted, so the two halves are covered independently. The engine assumption the carve-out rests on is pinned by a browser-mode characterization test that presses a real Backspace against a plain `contenteditable` with no editor attached — going through Lexical would suppress the `beforeinput` being observed, since its keydown handler calls `preventDefault()` on non-iOS platforms. Headless WebKit reproduces the quirk: no `deleteContentBackward` arrives at the start of a first block, empty or not, while Chromium and Firefox both fire one, and all three fire mid-text and at the start of a later block. Inverting the assumption fails the two WebKit cases and leaves the others green, so the test is a real canary: if WebKit ever starts firing there, it fails and the carve-out can be deleted. Verified on a physical iOS device: a first-block quote and code block now collapse to a paragraph; the suggestion bar still refreshes on Backspace elsewhere; no double deletion at a later block or across a range selection; a shadow-root block mid-document is unaffected; an indented first-block quote outdents; press-and-hold Backspace at the first block start behaves; Korean IME composition is unaffected; a first-block list still collapses via `@lexical/list`'s own higher-priority handler. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…Backspace at the start of the first block ## Description On iOS, `KEY_BACKSPACE_COMMAND` returns `false` without calling `event.preventDefault()` so the system keyboard keeps refreshing its autocomplete/suggestion bar, and the deletion is delegated to the `beforeinput` `deleteContentBackward` handler (meta#5841, meta#8725). That delegation has one hole: WebKit fires no `beforeinput` when the caret sits at the very start of the editable region. Measured on Safari 26.5, Backspace there produces a `keydown` and nothing else, so nothing performs the deletion, `RangeSelection.deleteCharacter` never runs, and `$collapseAtStart` is never reached. The keystroke is dropped entirely, so a first-block `QuoteNode` or `CodeNode` cannot be turned back into a paragraph, an empty first-block `HeadingNode` cannot either, and a blank first `ParagraphNode` followed by another block is not removed. Chromium does fire `beforeinput` in the same position, so this is iOS-only. Two neighbouring cases are *not* affected, for reasons worth recording: a non-empty first-block heading is deliberately left in place on every platform since meta#8493, and a first-block list is handled by `@lexical/list`'s own `KEY_BACKSPACE_COMMAND` listener at `COMMAND_PRIORITY_BEFORE_EDITOR`, which runs ahead of rich-text's and calls `preventDefault()` itself. `@lexical/plain-text` carries the same pass-through and needs the same carve-out. It registers no nodes of its own, so `ParagraphNode`'s is the only `collapseAtStart` that can run there — a blank first paragraph followed by another block. Enter and paste cannot produce that second block (both insert a line break), but `$initialEditorState`, a restored editor state and direct root mutation all can, and nothing normalizes the root to a single paragraph. Fixing only rich-text would leave one of two identical call sites broken, so the predicate is duplicated there, module-private in each, matching how the neighbouring `$isSelectionCollapsedAtFrontOfIndentedBlock` is kept private to rich-text rather than lifted into `@lexical/utils`. Out of scope, but worth stating so the boundary is not mistaken for an oversight: Backspace at the start of a *later* block still routes through `beforeinput` on iOS, where a separate pre-existing defect mishandles it — the iOS 10-key Korean IME workaround added by meta#8475 applies `removeText()` to any non-collapsed `targetRange`, including the cross-block range WebKit reports at a block boundary, bypassing `deleteCharacter` and with it the block-preserving behavior meta#8493 established. That is a different package and a different fix. This narrows the pass-through with `$isSelectionCollapsedAtStartOfFirstBlock`, so that one caret position takes the same `preventDefault()` + `DELETE_CHARACTER_COMMAND` path every other platform already takes. The predicate is anchored on the root's own first child rather than `getTopLevelElement()`, which stops at shadow roots and slots and would otherwise also match the first block of every table cell, layout item and collapsible section in the document. Whether the caret is at that block's leading edge is delegated to `$isAtStartOfNode` from `@lexical/utils`, which already handles nesting and slot boundaries. Behavior is unchanged everywhere else: the guard is ANDed after `IS_IOS`, the indented-block outdent branch still runs first, and `preventDefault()` on keydown suppresses the `beforeinput` that would otherwise delete a second time. One existing assertion changes as a result — at the start of a first-block paragraph the handler now reports the keystroke as handled, though `ParagraphNode.collapseAtStart` leaves a non-blank paragraph alone, so the content is untouched exactly as before. ## Test plan ### Before ``` $ npx vitest run packages/lexical/src/__tests__/unit/IosKeyboardSuggestions.test.ts × cursor at start of text does not delete (nothing to delete) × an empty first-block quote collapses to a paragraph × a non-empty first-block quote collapses and keeps its text × a blank first paragraph before another block is removed AssertionError: expected false to be true // Object.is equality (x4) Test Files 1 failed (1) Tests 4 failed | 11 passed (15) ``` ### After ``` $ npx vitest run packages/lexical/src/__tests__/unit/IosKeyboardSuggestions.test.ts Test Files 1 passed (1) Tests 15 passed (15) $ pnpm run test-unit -- packages/lexical-rich-text packages/lexical-plain-text \ packages/lexical-utils packages/lexical-selection packages/lexical/src/__tests__/unit Test Files 104 passed (104) Tests 1780 passed (1780) $ pnpm run lint && pnpm run flow && pnpm run tsc && pnpm run prettier (eslint: no violations) No errors! (tsc: no errors) All matched files use Prettier code style! $ for b in webkit chromium firefox; do VITEST_BROWSER=$b pnpm run test-browser \ -- packages/lexical-rich-text/src/__tests__/browser/BackspaceBeforeInputAtEditableStart.test.ts; done webkit Tests 4 passed (4) chromium Tests 4 passed (4) firefox Tests 4 passed (4) $ VITEST_BROWSER=webkit pnpm run test-browser -- packages/lexical-rich-text Test Files 21 passed (21) Tests 173 passed (173) ``` The new tests cover both directions of the guard: an empty and a non-empty first-block quote collapse to a paragraph, an indented first-block quote still outdents instead of collapsing, and a later block, a non-collapsed selection, a later child of the first block, and a shadow-root block mid-document all still pass through to `beforeinput`. Each was checked against a reverted source: with the guard removed three fail, with the predicate anchored on `getTopLevelElement()` the shadow-root case fails, and with the outdent branch removed the indent case fails. The plain-text case fails with only its own guard reverted, so the two halves are covered independently. The engine assumption the carve-out rests on is pinned by a browser-mode characterization test that presses a real Backspace against a plain `contenteditable` with no editor attached — going through Lexical would suppress the `beforeinput` being observed, since its keydown handler calls `preventDefault()` on non-iOS platforms. Headless WebKit reproduces the quirk: no `deleteContentBackward` arrives at the start of a first block, empty or not, while Chromium and Firefox both fire one, and all three fire mid-text and at the start of a later block. Inverting the assumption fails the two WebKit cases and leaves the others green, so the test is a real canary: if WebKit ever starts firing there, it fails and the carve-out can be deleted. Verified on a physical iOS device: a first-block quote and code block now collapse to a paragraph; the suggestion bar still refreshes on Backspace elsewhere; no double deletion at a later block or across a range selection; a shadow-root block mid-document is unaffected; an indented first-block quote outdents; press-and-hold Backspace at the first block start behaves; Korean IME composition is unaffected; a first-block list still collapses via `@lexical/list`'s own higher-priority handler. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…Backspace at the start of the first block ## Description On iOS, `KEY_BACKSPACE_COMMAND` returns `false` without calling `event.preventDefault()` so the system keyboard keeps refreshing its autocomplete/suggestion bar, and the deletion is delegated to the `beforeinput` `deleteContentBackward` handler (meta#5841, meta#8725). That delegation has one hole: WebKit fires no `beforeinput` when the caret sits at the very start of the editable region. Measured on Safari 26.5, Backspace there produces a `keydown` and nothing else, so nothing performs the deletion, `RangeSelection.deleteCharacter` never runs, and `$collapseAtStart` is never reached. The keystroke is dropped entirely, so a first-block `QuoteNode` or `CodeNode` cannot be turned back into a paragraph, an empty first-block `HeadingNode` cannot either, and a blank first `ParagraphNode` followed by another block is not removed. Chromium does fire `beforeinput` in the same position, so this is iOS-only. Two neighbouring cases are *not* affected, for reasons worth recording: a non-empty first-block heading is deliberately left in place on every platform since meta#8493, and a first-block list is handled by `@lexical/list`'s own `KEY_BACKSPACE_COMMAND` listener at `COMMAND_PRIORITY_BEFORE_EDITOR`, which runs ahead of rich-text's and calls `preventDefault()` itself. `@lexical/plain-text` carries the same pass-through and needs the same carve-out. It registers no nodes of its own, so `ParagraphNode`'s is the only `collapseAtStart` that can run there — a blank first paragraph followed by another block. Enter and paste cannot produce that second block (both insert a line break), but `$initialEditorState`, a restored editor state and direct root mutation all can, and nothing normalizes the root to a single paragraph. Fixing only rich-text would leave one of two identical call sites broken, so the predicate is duplicated there, module-private in each, matching how the neighbouring `$isSelectionCollapsedAtFrontOfIndentedBlock` is kept private to rich-text rather than lifted into `@lexical/utils`. Out of scope, but worth stating so the boundary is not mistaken for an oversight: Backspace at the start of a *later* block still routes through `beforeinput` on iOS, where a separate pre-existing defect mishandles it — the iOS 10-key Korean IME workaround added by meta#8475 applies `removeText()` to any non-collapsed `targetRange`, including the cross-block range WebKit reports at a block boundary, bypassing `deleteCharacter` and with it the block-preserving behavior meta#8493 established. That is a different package and a different fix. This narrows the pass-through with `$isSelectionCollapsedAtStartOfFirstBlock`, so that one caret position takes the same `preventDefault()` + `DELETE_CHARACTER_COMMAND` path every other platform already takes. The predicate is anchored on the root's own first child rather than `getTopLevelElement()`, which stops at shadow roots and slots and would otherwise also match the first block of every table cell, layout item and collapsible section in the document. Whether the caret is at that block's leading edge is delegated to `$isAtStartOfNode` from `@lexical/utils`, which already handles nesting and slot boundaries. Behavior is unchanged everywhere else: the guard is ANDed after `IS_IOS`, the indented-block outdent branch still runs first, and `preventDefault()` on keydown suppresses the `beforeinput` that would otherwise delete a second time. One existing assertion changes as a result — at the start of a first-block paragraph the handler now reports the keystroke as handled, though `ParagraphNode.collapseAtStart` leaves a non-blank paragraph alone, so the content is untouched exactly as before. ## Test plan ### Before ``` $ npx vitest run packages/lexical/src/__tests__/unit/IosKeyboardSuggestions.test.ts × cursor at start of text does not delete (nothing to delete) × an empty first-block quote collapses to a paragraph × a non-empty first-block quote collapses and keeps its text × a blank first paragraph before another block is removed AssertionError: expected false to be true // Object.is equality (x4) Test Files 1 failed (1) Tests 4 failed | 11 passed (15) ``` ### After ``` $ pnpm run test-unit packages/lexical/src/__tests__/unit/IosKeyboardSuggestions.test.ts Test Files 1 passed (1) Tests 15 passed (15) $ pnpm run test-unit packages/lexical-rich-text packages/lexical-utils \ packages/lexical-selection packages/lexical/src/__tests__/unit Test Files 101 passed (101) Tests 1766 passed (1766) $ for b in webkit chromium firefox; do VITEST_BROWSER=$b pnpm run test-browser \ packages/lexical-rich-text/src/__tests__/browser/BackspaceBeforeInputAtEditableStart.test.ts; done (identical output from each of the three runs) Test Files 1 passed (1) Tests 4 passed (4) $ VITEST_BROWSER=webkit pnpm run test-browser packages/lexical-rich-text Test Files 2 passed (2) Tests 13 passed (13) $ pnpm run lint && pnpm run flow && pnpm run tsc $ eslint ./ Running Flow... No errors! $ tsc $ npx prettier --check packages/lexical-rich-text/src/index.ts \ packages/lexical-plain-text/src/index.ts \ packages/lexical/src/__tests__/unit/IosKeyboardSuggestions.test.ts \ packages/lexical-rich-text/src/__tests__/browser/BackspaceBeforeInputAtEditableStart.test.ts Checking formatting... All matched files use Prettier code style! ``` `@lexical/plain-text` has no `__tests__` directory of its own, so its two cases live in `IosKeyboardSuggestions.test.ts` alongside the rich-text ones and are covered by the first two commands. The new tests cover both directions of the guard: an empty and a non-empty first-block quote collapse to a paragraph, an indented first-block quote still outdents instead of collapsing, and a later block, a non-collapsed selection, a later child of the first block, and a shadow-root block mid-document all still pass through to `beforeinput`. Each was checked against a reverted source: with the guard removed three fail, with the predicate anchored on `getTopLevelElement()` the shadow-root case fails, and with the outdent branch removed the indent case fails. The plain-text case fails with only its own guard reverted, so the two halves are covered independently. The engine assumption the carve-out rests on is pinned by a browser-mode characterization test that presses a real Backspace against a plain `contenteditable` with no editor attached — going through Lexical would suppress the `beforeinput` being observed, since its keydown handler calls `preventDefault()` on non-iOS platforms. Headless WebKit reproduces the quirk: no `deleteContentBackward` arrives at the start of a first block, empty or not, while Chromium and Firefox both fire one, and all three fire mid-text and at the start of a later block. Inverting the assumption fails the two WebKit cases and leaves the others green, so the test is a real canary: if WebKit ever starts firing there, it fails and the carve-out can be deleted. Verified on a physical iOS device: a first-block quote and code block now collapse to a paragraph; the suggestion bar still refreshes on Backspace elsewhere; no double deletion at a later block or across a range selection; a shadow-root block mid-document is unaffected; an indented first-block quote outdents; press-and-hold Backspace at the first block start behaves; Korean IME composition is unaffected; a first-block list still collapses via `@lexical/list`'s own higher-priority handler. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Upgrades `lexical` and every installed `@lexical/*` package from `0.41.0` to `0.50.0` for Payload 3.x. Updates the dependency pins, lockfile, and `lexicalTargetVersion`. Also exposes the 15 React entry points added on main in [#17554](#17554) through `@payloadcms/richtext-lexical/lexical/react/*`, so custom features can use these APIs through Payload. ## Breaking changes **No application changes or data migration are needed when using Payload's built-in rich text features.** If you maintain custom rich text features, check that they still compile and that custom content loads, copies, and pastes correctly. Lexical removed some older APIs, tightened TypeScript types, and changed how custom nodes are loaded and copied. Tests that inspect the editor's HTML may also need updated selectors or snapshots because Lexical adds some internal markup. **Do not install `lexical` or `@lexical/*` yourself for use with Payload.** Remove any direct dependencies you added for the editor and use Payload's re-exports from `@payloadcms/richtext-lexical/lexical` and `@payloadcms/richtext-lexical/lexical/*`. Payload supplies the matching versions; mixing versions can break the editor. This is the existing [custom feature requirement](https://payloadcms.com/docs/rich-text/custom-features). The 3.x Markdown implementation and converter defaults are preserved. This does not backport the v4 Markdown API changes. ## Breaking changes and Payload migrations Reviewed [0.42.0](https://github.com/facebook/lexical/releases/tag/v0.42.0), [0.43.0](https://github.com/facebook/lexical/releases/tag/v0.43.0), [0.44.0](https://github.com/facebook/lexical/releases/tag/v0.44.0), [0.45.0](https://github.com/facebook/lexical/releases/tag/v0.45.0), [0.46.0](https://github.com/facebook/lexical/releases/tag/v0.46.0), [0.47.0](https://github.com/facebook/lexical/releases/tag/v0.47.0), [0.48.0](https://github.com/facebook/lexical/releases/tag/v0.48.0), [0.49.0](https://github.com/facebook/lexical/releases/tag/v0.49.0), [0.50.0](https://github.com/facebook/lexical/releases/tag/v0.50.0). Listed below are breaking API/type changes and behavior changes explicitly identified as breaking in the release notes. Deprecations and routine bug fixes are omitted. **Yes** means we had to change Payload code; **No** means no Payload migration was required. | Release | Breaking change | Required migration in Payload | Changes made | | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | 0.42 | [Experimental `hasFitNestedTables` moves out of the table package into the playground](meta/lexical#8210). | No | - | | 0.42 | [The deprecated context-menu plugin is removed](meta/lexical#8199). | Yes | Removed the invalid `LexicalContextMenuPlugin` proxy and package export. Custom consumers must use `@payloadcms/richtext-lexical/lexical/react/LexicalNodeContextMenuPlugin` and adapt to its node-context-menu API. | | 0.43 | [Nested editors delegate asynchronously to their parent](meta/lexical#8308), restoring the behavior from before 0.42.0. | No | - | | 0.45 | [`$getReconciledDirection` now inherits direction through shadow-root nodes](meta/lexical#8479). | No | - | | 0.45 | [Backspace at the start of a non-empty block preserves the current block type and formatting](meta/lexical#8493). | No | - | | 0.45 | [Moving nodes between parents adjusts selection offsets](meta/lexical#8501). | No | - | | 0.45 | [Markdown shortcuts can trigger when IME composition commits](meta/lexical#8503). | No | - | | 0.45 | [Rich/plain text automatically removes empty inline elements](meta/lexical#8497). | No | - | | 0.45 | [Triple-click selection normalization moves from core into rich/plain text registration](meta/lexical#8520). Editors bypassing that registration must add `NormalizeTripleClickSelectionExtension`. | No | - | | 0.45 | [`DOMSlot` and the DOM rendering override APIs are generalized](meta/lexical#8519); playground `AutocompleteNode` is replaced by `VisibleLineBreakExtension`. | No | - | | 0.46 | [`static importJSON` widens its parameter to `SerializedLexicalNode & Record<string, unknown>`](#17554). Overrides using serialized interfaces can fail TypeScript checks. | No | - | | 0.46 | [Node lookups and traversal no longer infer unchecked subclass types](meta/lexical#8661). Explicit unchecked traversal generics are deprecated. | Yes | Removed the upload drawer’s assumption that a node lookup returns `UploadNode`. Added `$isUploadNode` before reading or updating upload data. | | 0.46 | [DOM lookups of the editor root return `RootNode` instead of `null`](meta/lexical#8588). Selection-capture handling is also generalized beyond decorators. | No | - | | 0.46 | [`insertNodes` preserves leading line breaks, and managed line breaks receive a DOM marker](meta/lexical#8615). | No | - | | 0.46 | [Experimental DOM-import rules register through node extensions](meta/lexical#8662); the separate per-package import extensions become deprecated aliases. | No | - | | 0.46 | [Experimental named slots change the Yjs sync signature](meta/lexical#8603): `syncLexicalUpdateToYjsV2__EXPERIMENTAL` gains a `dirtyLeaves` parameter. | No | - | | 0.46 | [Copying an element through `NodeSelection` includes its children](meta/lexical#8603), instead of copying an empty shell. | No | - | | 0.46 | [Previously deprecated exports are removed](meta/lexical#8704): `toggleLink`, `insertList`, `removeList`, and ContentEditable’s `Props` alias. Replacements are `$toggleLink`, `$insertList`, `$removeList`, and `ContentEditableProps`. | No | - | | 0.47 | [React 17 support is removed](meta/lexical#8782); React 18 or later is required. | No | - | | 0.47 | [Segmented text temporarily switches to normal mode during IME composition while keeping its DOM](meta/lexical#8784). Subclasses must tolerate that temporary mode. | No | - | | 0.49 | [Built-in nodes adopt `$config()`](meta/lexical#8640). Direct static `importJSON`, `importDOM`, `clone`, and `transform` implementations change; `__type` becomes readonly and automatic node construction needs zero-argument defaults. Custom static implementations remain supported. | No | - | | 0.49 | [`LexicalCommand<T>` checks its payload type more strictly](meta/lexical#8877). `LexicalCommand<unknown>` is no longer a safe catch-all. | Yes | Focus and blur commands were incorrectly registered with `MouseEvent`. Removed the explicit types so Lexical infers `FocusEvent`. | | 0.50 | [Named-slot values cannot be replaced with `LexicalNode.replace()`](meta/lexical#8938). Block converters also [skip slot values](meta/lexical#8905). | No | - | | 0.50 | [Lexical JSON clipboard copying checks `excludeFromCopy('clone')`, not `'html'`](meta/lexical#8995). | No | - | Also updated the copied table action menu for merged-cell bounds, header toggles, and merge/unmerge selection, and fixed the upload alignment test’s image selector. Formatting and diff checks pass for the upload selector fix. Verified all 15 new entry points through source and published export maps with runtime imports and TypeScript checks. ESLint and formatting checks pass. ## Previous Lexical upgrades - [#15760](#15760) — 0.35.0 → 0.41.0 - [#13715](#13715) — 0.34.0 → 0.35.0 - [#13622](#13622) — 0.28.0 → 0.34.0
…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.
…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.
…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.
…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.
…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.
…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.
Description
When a HeadingNode (or any non-empty block ElementNode) sits directly after an empty ParagraphNode, pressing Backspace at the heading's start merges the heading's children into the empty paragraph and removes the heading. The heading's type gets discarded as part of the merge. Separately, when a non-empty HeadingNode is the first block in the document (no previous sibling), pressing Backspace at its start unconditionally replaces it with a brand-new HeadingNode of the same tag — the visible markup is unchanged but the node identity (
__key) churns on every Backspace. The Google Docs convention many users expect is the opposite: in the first case, remove the empty previous block and preserve the heading; in the second, leave the heading untouched.Breaking Change
Two scenarios change at Backspace at a block start:
removeText()calls and selections spanning non-empty blocks are unchanged.HeadingNode.collapseAtStartself-replaced with a fresh HeadingNode of the same tag every time Backspace was pressed, churning the node__keyand firing a phantomdestroyed+createdmutation pair even though the visible markup was unchanged. Now the heading is left in place: no__keychurn, no mutation pair, no resulting history entry. Mutation listeners onHeadingNode(e.g.LexicalTableOfContentsPlugin) and any external code tracking heading keys stop seeing these phantom events; the visible DOM is unchanged in both old and new behavior.Fix
Two narrow exceptions, both at the start of a block on Backspace.
Heading after an empty paragraph.
RangeSelection.deleteCharacterreaches themerge-blockbranch withstate.caret.originpointing at the empty adjacent block andstate.blockpointing at the (non-empty) anchor block. The branch then callsremoveText(), and the cross-block merge in$removeTextFromCaretRangealways wins on the earlier block's type — that's where the heading is lost. A narrow exception is added at the top of themerge-blockbranch: when the adjacent block is empty and the anchor block is not, remove just that empty adjacent block and leave the selection where it is.removeText()isn't called, so the cross-block merge never runs and the anchor block's type is preserved. Type-agnostic: applies to Heading, Quote, list items, custom subclasses without per-type branching. The exception is also limited to adjacent blocks at the same nesting level (caret.origin.getParent() === block.getParent()), so a structural wrapper like aListNodewhose only child is an emptyListItemNodefalls through to the default cross-block merge — theListNodeis not treated as empty just because its only child is. This mirrors Google Docs' Backspace-at-heading behavior next to an empty bullet (the heading text is pulled into the list item).Non-empty heading with no previous block.
RangeSelection.deleteCharacterfalls through the merge logic (no previous block to merge with) and ultimately invokes$collapseAtStart, which walks ancestors callingcollapseAtStartuntil one returnstrue.HeadingNode.collapseAtStartpreviously self-replaced with a freshHeadingNodeof the same tag (and moved children over) even when non-empty — semantically a no-op but observable as__keychurn. The non-empty branch now just signals "handled" by returningtruewithout replacing. The empty-heading branch (heading → paragraph) is unchanged. QuoteNode and ListItemNodecollapseAtStartare intentionally untouched: they implement escape semantics ("Backspace at start escapes to paragraph" / list outdent), so the second exception stays Heading-specific, matching levensta's report.Scope
Two adjacent cases are still out of scope:
removeTextmerge policy. Changing the cross-block merge itself (e.g. "later block wins" or "non-empty wins") would also affect directremoveText()calls and user-initiated selections that span non-empty blocks. That's a broader observable behavior change for any caller relying on the current "earlier block's type wins" rule.LineBreakNodecase. levensta noted on the issue thread that a non-empty paragraph ending in aLineBreakNodetriggers the same merge. Treating that as "empty enough" to qualify for the exception is a judgment call about what counts as empty, and seems worth its own discussion.If a broader scope is preferred, drop a comment and I'll fold either (or both) into this PR.
Refs #4359
Test plan
pnpm vitest run --project unit— 113 files / 2493 tests pass. NewBackspace at start of heading (#4359)describe block inLexicalHeadingNode.test.ts:test.forcases for the merge-block exception: heading preserved after empty paragraph; quote preserved after empty paragraph (type-agnostic); only the empty paragraph is removed between two non-empty siblings; legacy cross-block merge still applies when previous block is non-empty (out-of-scope marker); aListNodewhose only child is an emptyListItemNodefalls through to the default cross-block merge (heading text is pulled into the list item), pinning the same-nesting-level limit on the exception.HeadingNode.collapseAtStartcases for the second exception: non-empty heading preserved with samegetKey()(catches the recreate regression — the previous code self-replaced and the test fails without the fix); preserved when the heading is wrapped inside another ElementNode; preserved when the heading is followed by a non-paragraph sibling (e.g. a list); empty heading still converts to paragraph (existing behavior).pnpm tscclean.Headings/on chromium — 4/4 pass. The existing'... stays as a heading when you backspace at the start of a heading with no previous sibling nodes present'test covers the DOM-level behavior of the second exception; thegetKey()check sits in the unit suite because the regression is invisible at the DOM level. Firefox/webkit weren't re-run this revision — no e2e files changed.Note: the new direct
HeadingNode.collapseAtStarttests invoke the method directly because jsdom doesn't implementSelection.modify, whichdeleteCharacterfalls through to when no previous block exists. The end-to-end keyboard path is covered by the existing e2e test in theHeadings/suite.