Skip to content

[lexical] Bug Fix: undo from a control outside the editor no longer runs the browser's native history over the editor DOM - #8970

Closed
LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:fix-6714-undo-from-link-url-input
Closed

LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:fix-6714-undo-from-link-url-input

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Chromium and WebKit walk up the browser's undo scope, so when the focused control sits outside the editor and has exhausted its own history — the URL field of the playground's floating link toolbar, for example — they dispatch beforeinput/historyUndo at the editor root. The editor has no selection at that point, so $handleBeforeInput returned at its range-selection guard without calling preventDefault, and the browser ran its native undo over the Lexical-managed contenteditable. The state was then rebuilt from the mangled DOM: pasting a link after "Hello world " and pressing undo from the URL field produced Hhttps://lexical.dev, a document that was never in Lexical's history.

Handle historyUndo/historyRedo in that branch: prevent the default and dispatch UNDO_COMMAND/REDO_COMMAND. @lexical/history restores a whole editor state, so it does not need a selection, and undo now removes the pasted link as the user expects. The change is additive — the range-selection path is untouched and the existing switch cases are unchanged.

I traced this rather than inferring it: instrumenting $handleBeforeInput captures historyUndo selection=null activeEl=link-input, and the retarget only happens once the input's own undo stack is exhausted — typing into the URL field first makes the same keystroke undo inside the input instead.

e2e added to Links.spec.mjs, chromium and webkit. Firefox keeps the undo scoped to the focused control and never dispatches the event, so it is skipped with a comment saying why.

Fixes #6714

…e editor no longer runs the browser's native history over the editor DOM

Chromium and WebKit walk up the browser's undo scope, so when the focused
control is outside the editor and has exhausted its own history — the URL
field of the playground's floating link toolbar, for example — they dispatch
`beforeinput`/`historyUndo` at the editor root. The editor has no selection at
that point, so `$handleBeforeInput` returned at its range selection guard
without calling `preventDefault`, and the browser ran its native undo over the
Lexical-managed contenteditable. The editor state was then rebuilt from the
mangled DOM: pasting a link after "Hello world " and pressing undo from the URL
field produced "Hhttps://lexical.dev", a document that was never in Lexical's
history.

Prevent the default and dispatch UNDO_COMMAND/REDO_COMMAND from that branch.
`@lexical/history` restores a whole editor state, so it does not need a
selection, and undo now removes the pasted link as the user expects. Firefox
keeps the undo scoped to the focused control and is unaffected.
@vercel

vercel Bot commented Aug 8, 2026

Copy link
Copy Markdown

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

A member of the Team first needs to authorize it.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 8, 2026
@LeSingh1

Copy link
Copy Markdown
Contributor Author

Consolidated into #9058 with the other PRs that share this defect, per @etrepum's note on #9027 and @mayrang's on #9035. Same fix and same tests, one review.

@LeSingh1 LeSingh1 closed this Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

1 participant