Fix: Retain focus and caret position after Backspace undo of a prefix block transform - #82116
Merged
Merged
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Mamaduka
reviewed
Aug 27, 2026
Mamaduka
left a comment
Member
There was a problem hiding this comment.
Thanks for working on this, @SainathPoojary!
Left couple of questions regarding the new e2e tests.
What?
Closes #82079
Fixes lost focus and caret position when you press Backspace right after a prefix transform (like
##,-, or>) turns a Paragraph into a Heading, List, or Quote block.Why?
When a prefix transform runs, the block is replaced and the cursor is moved to the new block, but these two things were dispatched as separate, unbatched updates. Because of that, the undo history recorded the new block's cursor position as both the "before" and "after" state, instead of the original block's. So when Backspace triggered undo, Gutenberg tried to restore focus using a block that no longer existed after the undo, and focus was lost. The user then had to click back into the block to keep typing, which is also an accessibility problem.
How?
selectionChange()andonReplace()ininput-rules.jsnow run inside a singleregistry.batch()call, so they are recorded as one atomic update. This way the undo history correctly keeps the original block's cursor position as the "before" state, andrestoreSelection()can put the caret back where it was after Backspace-undo.Testing Instructions
##(hash, hash, space). The block turns into a Heading.-(List) and>(Quote) to confirm the same fix applies to other prefix transforms.Screenshots or screencast
Screen.Recording.2026-08-27.at.2.40.30.PM.mov
Use of AI Tools
Used AI assistance (GitHub Copilot) to help write PR description and validate the fix and tests. I reviewed the analysis, tested the change manually and with the added e2e tests, and I take responsibility for the code in this PR.