Skip to content

Fix: Retain focus and caret position after Backspace undo of a prefix block transform - #82116

Merged
ellatrix merged 7 commits into
WordPress:trunkfrom
SainathPoojary:fix/82079
Aug 28, 2026
Merged

ellatrix merged 7 commits into
WordPress:trunkfrom
SainathPoojary:fix/82079

Conversation

@SainathPoojary

@SainathPoojary SainathPoojary commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

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() and onReplace() in input-rules.js now run inside a single registry.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, and restoreSelection() can put the caret back where it was after Backspace-undo.

Testing Instructions

  1. Open a post or page.
  2. Click into the empty paragraph block.
  3. Type ## (hash, hash, space). The block turns into a Heading.
  4. Press Backspace. The block turns back into a Paragraph.
  5. Without clicking anywhere, start typing. The text should be added right where the cursor was, no need to click back in.
  6. Repeat with - (List) and > (Quote) to confirm the same fix applies to other prefix transforms.
  7. Try the same steps with the paragraph nested inside a Group block, to confirm focus is retained there too.

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.

@github-actions

github-actions Bot commented Aug 27, 2026

Copy link
Copy Markdown

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: SainathPoojary <sainathpoojary@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: aduth <aduth@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions github-actions Bot added the [Package] Block editor /packages/block-editor label Aug 27, 2026
@Mamaduka Mamaduka added [Type] Bug An existing feature does not function as intended [Feature] Block Transforms Block transforms from one block to another labels Aug 27, 2026

@Mamaduka Mamaduka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this, @SainathPoojary!

Left couple of questions regarding the new e2e tests.

Comment thread test/e2e/specs/editor/various/rich-text.spec.js Outdated
Comment thread test/e2e/specs/editor/various/rich-text.spec.js Outdated