Skip to content

feat: add webContents.caretBrowsingEnabled - #52696

Merged
jkleinsc merged 11 commits into
electron:mainfrom
snackzone:zm-caret-browsing-api
Aug 20, 2026
Merged

jkleinsc merged 11 commits into
electron:mainfrom
snackzone:zm-caret-browsing-api

Conversation

@snackzone

@snackzone snackzone commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

Description of Change

Adds the ability to enable Chromium's Caret Browsing mode via webContents.caretBrowsingEnabled, setCaretBrowsingEnabled(), and isCaretBrowsingEnabled(). Closes #31339. Note this doesn't add the F7 keyboard shortcut requested in the issue; this is left to be configured by the consuming app.

Screen.Recording.2026-08-10.at.9.55.24.AM.mov

This change primarily exposes the Blink caret_browsing_enabled renderer preference as a settable property on webContents.

Since setting the Blink preference does not update ui::AXPlatform automatically, screen reader integration had to be handled manually here. This was a little tricky since ui::AXPlatform is a process-wide singleton whereas webContents.caretBrowsingEnabled is per-WebContents. Two approaches were considered:

Mirroring

Any WebContents that sets the pref updates ui::AXPlatform. Simple but can lead to states where the pref is out of sync with ui::AXPlatform: disabling caret browsing on Window A while it's enabled on Window B would break the accessibility in Window B - the cursor would still be rendered but screen readers would no longer announce it.

Refcount (what this PR does)

Track the number of open WebContents with caret browsing enabled via a refcount and toggle ui::AXPlatform::GetInstance().SetCaretBrowsingState() accordingly. More book-keeping but guarantees that ui::AXPlatform always has caret browsing enabled for WebContents with the pref enabled. Note that these can still be out of sync - if caret browsing is enabled in any WebContents, then ui::AXPlatform will behave as if it's enabled for all of them. I decided that - where accessibility is concerned - over-reporting is preferable to the under-reporting issue in the mirroring approach.

Notes for reviewers:
  • Although the C++ changes here are small and straightforward, I am not a C++ developer and relied on AI assistance (Opus 5) for those changes.
  • These changes were only tested on MacOS.

Checklist

Release Notes

Notes: Added webContents.caretBrowsingEnabled for toggling caret browsing in a WebContents.

@welcome

welcome Bot commented Aug 7, 2026

Copy link
Copy Markdown

💖 Thanks for opening this pull request! 💖

Semantic PR titles

We use semantic commit messages to streamline the release process. Before your pull request can be merged, you should update your pull request title to start with a semantic prefix.

Examples of commit messages with semantic prefixes:

  • fix: don't overwrite prevent_default if default wasn't prevented
  • feat: add app.isPackaged() method
  • docs: app.isDefaultProtocolClient is now available on Linux

Commit signing

This repo enforces commit signatures for all incoming PRs.
To sign your commits, see GitHub's documentation on Telling Git about your signing key.

PR tips

Things that will help get your PR across the finish line:

  • Follow the JavaScript, C++, and Python coding style.
  • Run npm run lint locally to catch formatting errors earlier.
  • Document any user-facing changes you've made following the documentation styleguide.
  • Include tests when adding/changing behavior.
  • Include screenshots and animated GIFs whenever possible.

We get a lot of pull requests on this repo, so please be patient and we will get back to you as soon as we can.

@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Aug 7, 2026
Exposes the existing Blink renderer preference `caret_browsing_enabled`
as a settable property on webContents. Blink already applies this
preference on every renderer preferences sync, so toggling it takes
effect immediately without a reload.

Enables assistive-technology use cases that need a keyboard-navigable
caret in page content, which Chrome offers but Electron had no API for.

The specs arrow through non-editable prose rather than only reading the
preference back, because Blink honors the caret movement editor commands
in non-editable content only when caret browsing is on — so that is what
actually observes the renderer having received the preference.

Refs electron#31339

Co-Authored-By: Claude Opus 5 <[email protected]>
@snackzone
snackzone force-pushed the zm-caret-browsing-api branch 3 times, most recently from 581c824 to 415bc00 Compare August 7, 2026 23:39
Blink draws and moves a caret once caret_browsing_enabled is set, but
assistive technology is never told, so a screen reader stays silent.
Notify ui::AXPlatform too, as Chrome does in
renderer_preferences_util.cc. It is process-wide while the preference is
per-WebContents, so refcount the instances that enable it.

* Caret attributes gate on AXPlatform::IsCaretBrowsingEnabled() before
  falling back to AXNode::HasVisibleCaretOrSelection(), which is false
  for a collapsed caret in non-editable content; untold, macOS reports
  NSNotFound and Windows never fires IA2_EVENT_TEXT_CARET_MOVED.
* A mirror would let one window disabling caret browsing degrade
  accessibility in another; Chrome's input is a uniform profile pref.
* Reconcile from a cached flag, before the preference-equality early
  return and at construction: a <webview> guest inherits the embedder's
  preferences via BrowserPluginGuest::InitInternal(), so a delta taken
  from the preference transition would release an uncounted reference.
* The destructor releases from the cached flag before its early returns,
  since web_contents() may be gone and the state must not stay pinned.

Co-Authored-By: Claude Opus 5 <[email protected]>
@snackzone
snackzone force-pushed the zm-caret-browsing-api branch from 415bc00 to 261fdb3 Compare August 8, 2026 00:10
@snackzone
snackzone marked this pull request as ready for review August 10, 2026 17:56

@erickzhao erickzhao 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.

API LGTM

Does not close since it doesn't add the F7 keyboard shortcut; this is left to be configured by the consuming app.

IMO that should be good enough to close the issue if we want to leave it to the app developer to implement.

Comment thread docs/api/web-contents.md Resolved
Link the "caret browsing" string in both method descriptions to the
property's section, per review feedback.

Co-Authored-By: Claude Opus 5 <[email protected]>
@snackzone

Copy link
Copy Markdown
Contributor Author

Does not close since it doesn't add the F7 keyboard shortcut; this is left to be configured by the consuming app.

IMO that should be good enough to close the issue if we want to leave it to the app developer to implement.

SGTM, updated the PR description

@ckerr ckerr 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.

API LGTM

@ckerr
ckerr self-requested a review August 12, 2026 17:06
@dsanders11 dsanders11 added api-review/requested 🗳 semver/minor backwards-compatible functionality labels Aug 12, 2026
@georgexu99 georgexu99 added api-review/approved ✅ and removed semver/minor backwards-compatible functionality api-review/requested 🗳 labels Aug 12, 2026
@dsanders11 dsanders11 added semver/minor backwards-compatible functionality api-review/requested 🗳 target/44-x-y PR should also be added to the "44-x-y" branch. labels Aug 12, 2026
// refcount. Runs before any of the early returns below so a WebContents
// destroyed with caret browsing on cannot leak a count and pin the platform
// state on forever.
ReconcileCaretBrowsingCount(false);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

How does the refcount get released for guests and background pages?

@snackzone snackzone Aug 17, 2026 •

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.

Do guest/background pages not go through this codepath when they are destroyed? Working on getting both of these cases manually verified

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.

Just manually verified with a test app - creating and destroying both background pages and guest windows with caret browsing enabled updates the refcount. Does that match your understanding?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

yep! that works for me

@georgexu99

Copy link
Copy Markdown
Contributor

Looks good on my end, I'm sending claude for a review as well

snackzone and others added 2 commits August 18, 2026 11:25
The negative caret test could pass vacuously: collapseSelectionToStart()
sets anchorOffset to 0 unconditionally, so an offset of 0 proved only that
executeJavaScript ran, not that the synthesized Right presses reached the
page. With the positive control in a separate BrowserWindow, nothing ruled
out a dead input pipeline in the negative one.

Merge the two into one window that first proves the caret moves while
enabled, then disables and polls for the caret staying put. Polling both
phases also drops the guaranteed 3s dead wait: the old predicate
(offset === 3) could never hold once the feature worked, so every green run
burned the full timeout and threw the error away in .catch(() => {}).

Co-Authored-By: Claude Opus 5 <[email protected]>
One conflict, in WebContents::WebContentsDestroyed(): electron#52847 added Unpin()
at the top of the function, where this branch had added the release of the
process-wide caret browsing count. Both sides want to be there, so both are
kept.

The caret release goes first, so the count is dropped before Unpin() makes
the wrapper collectible, and its comment is trimmed to avoid repeating the
"underlying content::WebContents is gone" preamble now sitting under it.

electron#52847 pins Type::kRemote wrappers for the life of their content::WebContents
and unpins them here. That makes the release in WebContentsDestroyed() more
load-bearing than when it was written, not less: a remote wrapper is now
pinned for its whole life, so its destructor cannot run until a GC after
this Unpin(), and the count would otherwise be held for that whole window.

Co-Authored-By: Claude Opus 5 <[email protected]>
@snackzone

Copy link
Copy Markdown
Contributor Author

@georgexu99 thanks for the feedback! Addressed each suggestion in its own commit and resolved merge conflicts

@jkleinsc jkleinsc 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.

LGTM. Just one minor nit.

@samuelmaddock samuelmaddock 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.

API LGTM

@jkleinsc
jkleinsc merged commit 8345258 into electron:main Aug 20, 2026
120 of 123 checks passed
@welcome

welcome Bot commented Aug 20, 2026

Copy link
Copy Markdown

Congrats on merging your first pull request! 🎉🎉🎉

@release-clerk

release-clerk Bot commented Aug 20, 2026

Copy link
Copy Markdown

Release Notes Persisted

Added webContents.caretBrowsingEnabled for toggling caret browsing in a WebContents.

@trop

trop Bot commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

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

@trop trop Bot removed the target/42-x-y PR should also be added to the "42-x-y" branch. label Aug 20, 2026
@trop

trop Bot commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

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

@trop

trop Bot commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

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

@trop trop Bot added in-flight/43-x-y in-flight/44-x-y merged/44-x-y PR was merged to the "44-x-y" branch. and removed target/43-x-y PR should also be added to the "43-x-y" branch. target/44-x-y PR should also be added to the "44-x-y" branch. in-flight/44-x-y in-flight/42-x-y labels Aug 20, 2026
@trop trop Bot added merged/43-x-y PR was merged to the "43-x-y" branch. and removed in-flight/43-x-y labels Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

45-x-y api-review/approved ✅ merged/43-x-y PR was merged to the "43-x-y" branch. merged/44-x-y PR was merged to the "44-x-y" branch. semver/minor backwards-compatible functionality

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

[Feature Request]: Caret browsing from upstream Chromium

8 participants