Skip to content

fix: make webContents.centerSelection() work on Windows and Linux - #55909

Merged
jkleinsc merged 2 commits into
mainfrom
claude/fix-center-selection-all-platforms
Nov 18, 2026
Merged

jkleinsc merged 2 commits into
mainfrom
claude/fix-center-selection-all-platforms

Conversation

@claude

@claude claude Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Requested by David Babenko-Sanders · Slack thread

Description of Change

webContents.centerSelection() and <webview>.centerSelection() do nothing on Windows and Linux, because Chromium's WebContentsImpl::CenterSelection() wraps its whole body in #if BUILDFLAG(IS_MAC). That guard was copied from the neighbouring CopyToFindPboard() when CenterSelection was added in https://crrev.com/c/4258445; CopyToFindPboard() needs it because only macOS has a find pasteboard, but the rest of the CenterSelection path (the FrameWidgetInputHandler mojom message, FrameWidgetInputHandlerImpl, WebFrameWidgetImpl and WebLocalFrameImpl::CenterSelection()) is platform-neutral. A new Chromium patch, fix_allow_webcontentsimpl_centerselection_on_all_platforms.patch, removes the guard, and should be upstreamed. A new serial centerSelection() spec in api-web-contents.spec.ts selects text far below the fold in a shown, focused window and checks that centerSelection() scrolls it to the middle of the viewport on every platform. Electron wasn't built locally: the new test was confirmed to fail on an unpatched 47.0.0-nightly.20261117 on Linux, so CI is the first run with the patch applied.

Checklist

Release Notes

🤖 Generated with Claude Code

https://claude.ai/code/session_01KX1hkNiiBq2fQEsXWusxpm

Notes: Fixed webContents.centerSelection() so that it works on Windows and Linux.

@claude claude Bot added semver/patch backwards-compatible bug fixes target/44-x-y PR should also be added to the "44-x-y" branch. target/45-x-y PR should also be added to the "45-x-y" branch. target/46-x-y PR should also be added to the "46-x-y" branch. labels Nov 18, 2026
@dsanders11
dsanders11 marked this pull request as ready for review November 18, 2026 07:25
@dsanders11
dsanders11 requested a review from a team as a code owner November 18, 2026 07:25

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I reviewed this PR and found no bugs. It adds a Chromium patch under patches/, which has code owners, so someone who can see the upstream tree should still look at it.

What was reviewed (standalone clone, so no Chromium sources were available):

  • The patch only removes the #if BUILDFLAG(IS_MAC)/#endif pair in WebContentsImpl::CenterSelection(). It is listed in patches/chromium/.patches, and its message explains how to tell when it can be dropped. docs/api/web-contents.md and webview-tag.md have no macOS-only tag on centerSelection(), so no doc change is needed.
  • The new spec is in a serial suite, uses vitest matchers and puts no expect() inside listeners. One question: the test's comment says document scrolls are animated, but waitUntil stops as soon as scrollY !== 0 (spec/api-web-contents.spec.ts:3016) and then asserts the final centered position. If the scroll really is animated, this could flake. Waiting on the centered condition itself would avoid that.
  • I couldn't check from here whether the patch applies or whether the scroll is animated. I didn't run the spec either.
Extended reasoning...

The change adds one Chromium patch that removes a Mac-only build guard around WebContentsImpl::CenterSelection(), registers it in patches/chromium/.patches and adds one serial spec. It touches no security-sensitive surface: no IPC, permissions or webPreferences changes. I deferred because /patches/ is owned by patch owners in .github/CODEOWNERS, and because this standalone clone has no upstream Chromium code to check the hunk or the scroll behavior against.

Wait until the middle of the selection is within tolerance of the middle
of the viewport instead of until the page has scrolled at all, and say
why we wait: centerSelection() reaches the renderer asynchronously, but
the scroll itself (RevealSelection -> ScrollRectToVisible) is instant
since the page does not use CSS smooth scrolling.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KX1hkNiiBq2fQEsXWusxpm

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Code review found no issues

No high-confidence issues detected in this change.

@jkleinsc
jkleinsc merged commit ef8d809 into main Nov 18, 2026
76 checks passed
@jkleinsc
jkleinsc deleted the claude/fix-center-selection-all-platforms branch November 18, 2026 16:00
@release-clerk

release-clerk Bot commented Nov 18, 2026

Copy link
Copy Markdown

Release Notes Persisted

Fixed webContents.centerSelection() so that it works on Windows and Linux.

@trop

trop Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

I was unable to backport this PR to "44-x-y" cleanly;
you will need to perform this backport manually.

@trop trop Bot added needs-manual-bp/44-x-y and removed target/44-x-y PR should also be added to the "44-x-y" branch. labels Nov 18, 2026
@trop

trop Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

I have automatically backported this PR to "45-x-y", please check out #55915

@trop trop Bot added in-flight/45-x-y and removed target/45-x-y PR should also be added to the "45-x-y" branch. labels Nov 18, 2026
@trop

trop Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

I have automatically backported this PR to "46-x-y", please check out #55916

@trop trop Bot added in-flight/46-x-y merged/46-x-y PR was merged to the "46-x-y" branch. and removed target/46-x-y PR should also be added to the "46-x-y" branch. in-flight/46-x-y labels Nov 18, 2026
@trop trop Bot added merged/45-x-y PR was merged to the "45-x-y" branch. and removed in-flight/45-x-y labels Nov 18, 2026
@trop

trop Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

claude[bot] has manually backported this PR to "44-x-y", please check out #55950

dsanders11 pushed a commit that referenced this pull request Nov 20, 2026
…5950)

fix: make webContents.centerSelection() work on Windows and Linux (#55909)

* fix: make webContents.centerSelection() work on Windows and Linux


Claude-Session: https://claude.ai/code/session_01KX1hkNiiBq2fQEsXWusxpm

* test: wait for the centred position in the centerSelection spec

Wait until the middle of the selection is within tolerance of the middle
of the viewport instead of until the page has scrolled at all, and say
why we wait: centerSelection() reaches the renderer asynchronously, but
the scroll itself (RevealSelection -> ScrollRectToVisible) is instant
since the page does not use CSS smooth scrolling.


Claude-Session: https://claude.ai/code/session_01KX1hkNiiBq2fQEsXWusxpm

---------


(cherry picked from commit ef8d809)


Claude-Session: https://claude.ai/code/session_01KX1hkPAirbYV7a7v3Ck4FB

Co-authored-by: Claude <[email protected]>
@trop trop Bot added merged/44-x-y PR was merged to the "44-x-y" branch. and removed in-flight/44-x-y labels Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

47-x-y merged/44-x-y PR was merged to the "44-x-y" branch. merged/45-x-y PR was merged to the "45-x-y" branch. merged/46-x-y PR was merged to the "46-x-y" branch. semver/patch backwards-compatible bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants