Skip to content

Rich text: check the selection first in ownsSelection - #80548

Open
ellatrix wants to merge 1 commit into
trunkfrom
fix/owns-selection-forced-layout
Open

Rich text: check the selection first in ownsSelection#80548
ellatrix wants to merge 1 commit into
trunkfrom
fix/owns-selection-forced-layout

Conversation

@ellatrix

@ellatrix ellatrix commented Jul 22, 2026

Copy link
Copy Markdown
Member

What?

Reorders ownsSelection so the selection-containment check runs before the editing-host checks.

Follow-up to #80549 (merged). That one read the contentEditable attribute to drop the forced layout, but didn't move the typing metric. This tries the other axis: ordering.

Why?

ownsSelection runs once per editable per keystroke. Under editableRoot the host is the canvas wrapper, which contains every editable element, so the host checks (contentEditable reads + contains) pass for every instance and don't tell them apart. Only the selection does.

With the host checks first, each of the ~N non-owning instances does two contentEditable reads and an activeElement.contains( element ) walk before reaching the check that eliminates it. Doing the discriminating check first lets them bail sooner.

How?

Move the anchor/focus containment check ahead of the host checks. A non-owning instance now returns after one element.contains( anchorNode ), instead of also doing the two contentEditable reads and the activeElement.contains( element ) walk.

Same boolean, behavior unchanged. This is purely about the order of cheap checks; the forced-layout read is already gone in trunk via #80549.

Testing Instructions

  1. Behavior is unchanged: ownsSelection returns the same value for the same state.
  2. npm run test:unit packages/rich-text passes.
  3. Watch the type metric on codevitals for whether the reordering alone moves it.

Use of AI Tools

Written with the help of Claude Code.

@github-actions

github-actions Bot commented Jul 22, 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: ellatrix <ellatrix@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@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] Rich text /packages/rich-text label Jul 22, 2026
@github-actions

github-actions Bot commented Jul 22, 2026

Copy link
Copy Markdown

Size Change: 0 B

Total Size: 7.75 MB

compressed-size-action

@ellatrix

Copy link
Copy Markdown
Member Author

I think we should merge #80549 first and see, then maybe reorder if necessary. #80549 is the better fix, we should avoid forcing layout

@Mamaduka

Copy link
Copy Markdown
Member

Don't have a strong opinion on merge order.

@Mamaduka

Copy link
Copy Markdown
Member

Unfortunately, there are no changes to typing metrics after #80549 was merged - https://codevitals.run/public/WordPress/gutenberg/metrics?metric=type.

Let's rebase and try this one as well.

@ellatrix

Copy link
Copy Markdown
Member Author

So has nothing to do with layout? 🤔

Reorder ownsSelection so the selection-containment check runs before the
editing-host checks. Under editableRoot the host is the canvas wrapper, which
contains every editable element, so the host checks pass for all of them and
only the selection tells them apart. Running the discriminating check first
lets a non-owning instance bail before the host checks.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XrG35JQKJBX1iNof5wtfjN
@ellatrix
ellatrix force-pushed the fix/owns-selection-forced-layout branch from d1e7e05 to 2fa1316 Compare July 23, 2026 00:27
@ellatrix ellatrix changed the title Rich text: keep the forced-layout read out of the ownsSelection hot path Rich text: check the selection first in ownsSelection Jul 23, 2026
@github-actions

Copy link
Copy Markdown

Flaky tests detected in 2fa1316.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/29969133947
📝 Reported issues:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Rich text /packages/rich-text [Type] Performance Related to performance efforts

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants