readonly input keeps its selection after the focus leaves the document

#40768970 Area: Blink>Editing / selection painting Flag HideUnfocusedReadonlyTextControlSelection CL 8618286 In Review

TL;DR

Select the text of a <input readonly>, then move the focus out of the document (TAB into an iframe, click the address bar). Chrome keeps painting the selection; Firefox and IE11 drop it. An editable input in the same situation always dropped it.

FrameSelection::SelectionHasFocus() only tied a selection to its text control when the selection was editable. In a readonly input the inner editor is not editable, so the ancestor walk climbed out of the UA shadow tree and inherited focus from the document fallback.

The fix treats focusable text controls uniformly: a non-editable selection inside a text control reports no focus once that control is not the focused element. Disabled controls are excluded -- they can never hold focus, so their selection is an ordinary document selection.

The change

// third_party/blink/renderer/core/editing/frame_selection.cc
// FrameSelection::SelectionHasFocus()

if (RuntimeEnabledFeatures::
        HideUnfocusedReadonlyTextControlSelectionEnabled() &&
    !IsEditable(*current)) {
  const TextControlElement* text_control = EnclosingTextControl(current);
  if (text_control && !text_control->IsDisabledFormControl()) {
    return false;
  }
}

3 files, +53/-0: the condition above, FrameSelectionTest.SelectionInReadonlyTextControl plus a control test, and the status: "stable" kill switch in runtime_enabled_features.json5.

What changes, what does not

casebeforeafter
readonly input, focus leftselection paintedselection hidden
editable input, focus lefthiddenhidden
disabled control, focus leftpaintedpainted
plain text outside a controlpaintedpainted

Samples

Rendered runs live in your browser (stock Chrome shows the bug in case 1). Expected and Diff are content_shell captures with the flag on, and the pixel mask between flag-off and flag-on.

1. readonly input -- the bug

Selects the readonly input, then moves the focus into the iframe. Unpatched Chrome keeps the grey highlight.
Expected: no highlight after the focus moved
Flag on: no highlight once the control lost the focus.
Diff mask over the input text
5503 px differ -- exactly the highlight rectangle inside the input.
content_shell: before / after
Before: highlight stays
beforeflag off: highlight stays
After: highlight gone
afterflag on: highlight gone

2. editable input -- reference behavior

An editable input already hid its selection on blur; that is the behavior the readonly case now matches.
Expected: no highlight
Flag on: unchanged.
Empty diff mask
0 px differ.
content_shell: before / after
Before
beforeflag off
After
afterflag on

3. disabled control -- the carve-out

Double-click the text, then click into the iframe. The selection must stay -- this is what FocusingButtonHidesRangeInDisabledTextControl pins, and what the first, broader version of the fix broke.
Expected: selection still painted
Flag on: selection still painted.
Empty diff mask
0 px differ.
content_shell: before / after (double-click synthesized)
Before
beforeflag off: stays
After
afterflag on: stays

4. plain text outside any control -- unchanged

Document selections keep being painted when the focus leaves, same as before.
Expected: selection still painted
Flag on: unchanged.
Empty diff mask
0 px differ.
content_shell: before / after
Before
beforeflag off: stays
After
afterflag on: stays

Verification

Unit tests (debug component build, local)

Screenshots

Captured with capture_screenshots.py against a local content_shell, same build, flag off vs flag on, focus emulation enabled so the offscreen window counts as focused.

xvfb-run -a python3 capture_screenshots.py

Links

Issue 40768970 CL 8618286 standalone sampler.html capture_screenshots.py