Fix Mac text selection behavior to collapse selection when reversing back to anchor point

Commit: 75d7369d | 2026-02-20 07:16:16

← Back to List

Fix Mac text selection behavior to collapse selection when reversing back to anchor point

Minor
Commit Hash: 75d7369d2e0caede23c93833da01c7e7df75f2b2
Commit Time: 2026-02-20 07:16:16
Impact Level: Minor
Generated By: webview2-upstream-sentry
Upstream Review: View Upstream Review 🔗

📋 Summary

This commit modifies the behavior of Textfield line selection commands (Command+Shift+Left/Right) on macOS to match native platform conventions (e.g., latest NSTextView, TextEdit). Previously, using selection extension commands to reverse back to the anchor point would "flip" the selection and extend in the opposite direction (due to gfx::SELECTION_EXTEND blindly flipping the anchor when crossed). Native macOS behavior is to collapse (cancel) the selection when the cursor returns to the anchor point. Changes include: adding IS_MAC platform-specific logic in ui/gfx/render_text.cc's MoveCursor method using strict inequality to determine selection reversal, ensuring returning exactly to the selection start collapses rather than reverses; updating the SELECTION_CARET branch to handle caret at start position; correspondingly updating tests in render_text_unittest.cc, bridged_native_widget_unittest.mm, textfield_unittest.cc, and textarea_unittest.cc.

🎯 Impact Analysis

This change has minor impact on WebView2Mac. The modifications are in ui/gfx/render_text.cc (text rendering and selection logic) and related test files. RenderText is the Views layer text rendering engine used for Chromium's custom-drawn UI controls (such as Views Textfield in the address bar, form inputs, etc.). Web page text selection in WebView2Mac is handled by the Blink engine and does not go through RenderText. This change only affects the selection behavior of Views layer Textfield controls on macOS. Edge downstream has no references to RenderText::MoveCursor or related selection logic in edge_embedded_browser/ or components/edge_webview2/. If a WebView2Mac host application uses Views Textfield controls, their selection behavior will be affected, but this is a general Views framework improvement. No downstream patch is needed.

Impacted Classes:

gfx::RenderText