Skip to content

Commit 031c6b8

Browse files
[web] Autofill all AutofillGroup fields in semantics mode (flutter#187108)
**Fixes flutter#180652** **Problem** With a screen reader on, picking a saved login only filled the field you tapped. The other fields stayed empty. Browsers autofill a login by putting all its fields in one hidden `<form>`. But in screen-reader mode each field has to live in its own spot for accessibility, so it never went into that form. The browser saw a half-built login and filled only one field. **Fix** Still build the hidden form, but instead of moving the field into it, just link them with the HTML `form="<id>"` attribute. The field stays where the screen reader needs it, and the browser now sees a complete login and fills every field. **Demo** Semantics ON: - Before: https://flutter-demo-30-before.web.app — only email fills - After: https://flutter-demo-30-after.web.app — both fields fill Semantics OFF (regression check, both behave the same): - Before: https://flutter-demo-31-before.web.app - After: https://flutter-demo-31-after.web.app **Known limitation** Chrome on iOS still fills only the username, not the password. That is a pre-existing WebKit limitation unrelated to this change (it also happens without a screen reader) and is tracked separately in flutter#185327. --------- Co-authored-by: Harry Terkelsen <[email protected]>
1 parent 29e612e commit 031c6b8

3 files changed

Lines changed: 561 additions & 15 deletions

File tree

‎engine/src/flutter/lib/web_ui/lib/src/engine/semantics/text_field.dart‎

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
import 'package:ui/ui.dart' as ui;
6+
import 'package:ui/ui_web/src/ui_web.dart' as ui_web;
67

78
import '../dom.dart';
89
import '../platform_dispatcher.dart';
@@ -47,6 +48,13 @@ class SemanticsTextEditingStrategy extends DefaultTextEditingStrategy {
4748
/// Current input configuration supplied by the "flutter/textinput" channel.
4849
InputConfiguration? inputConfig;
4950

51+
/// Whether an autofill form has been woken up for the active field.
52+
///
53+
/// Tracked locally because the base strategy's `_appendedToForm` is private
54+
/// to its library, and [SemanticsTextEditingStrategy] fully overrides
55+
/// [disable] (it never calls `super.disable()`).
56+
bool _formIsActive = false;
57+
5058
/// The semantics implementation does not operate on DOM nodes, but only
5159
/// remembers the config and callbacks. This is because the DOM nodes are
5260
/// supplied in the semantics update and enabled by [activate].
@@ -114,6 +122,24 @@ class SemanticsTextEditingStrategy extends DefaultTextEditingStrategy {
114122
}
115123
subscriptions.clear();
116124
lastEditingState = null;
125+
126+
// The focused field is linked to the autofill form by the `form`
127+
// attribute. On blur, detach it and leave a synthetic placeholder holding
128+
// its value, then keep the form dormant in the DOM so the autofill context
129+
// can still be submitted (credential save via
130+
// `TextInput.finishAutofillContext`) and the group stays complete when
131+
// another field is focused.
132+
if (_formIsActive && inputConfiguration.autofillGroup != null) {
133+
final EngineAutofillForm group = inputConfiguration.autofillGroup!;
134+
if (inputConfiguration.autofill != null) {
135+
group.demoteFocusedToSynthetic(activeDomElement, inputConfiguration.autofill!);
136+
}
137+
if (group.formElement != null) {
138+
group.goDormant();
139+
}
140+
_formIsActive = false;
141+
}
142+
117143
EnginePlatformDispatcher.instance.viewManager.safeBlur(activeDomElement);
118144
domElement = null;
119145
activeTextField = null;
@@ -146,8 +172,25 @@ class SemanticsTextEditingStrategy extends DefaultTextEditingStrategy {
146172
OnActionCallback? onAction,
147173
}) {
148174
isEnabled = true;
149-
inputConfiguration = inputConfig;
150-
applyConfiguration(inputConfig);
175+
final EngineAutofillForm? autofillGroup = inputConfig.autofillGroup;
176+
inputConfiguration = autofillGroup == null
177+
? inputConfig
178+
: inputConfig.copyWith(
179+
autofillGroup: autofillGroup.copyWith(associateFocusedElementByAttribute: true),
180+
);
181+
applyConfiguration(inputConfiguration);
182+
183+
// Build the autofill form here, before [addEventHandlers] runs (it runs
184+
// later in the same [enable] call). [addEventHandlers] subscribes to the
185+
// `input` events of the synthetic group fields, so those fields must exist
186+
// by then or non-focused fields would never propagate autofilled values.
187+
//
188+
// Note [placeElement]/[placeForm] are never reached via the normal
189+
// placement path in semantics mode ([initializeElementPlacement] is a
190+
// no-op), so the form must be set up explicitly here.
191+
if (hasAutofillGroup) {
192+
placeForm();
193+
}
151194
}
152195

153196
@override
@@ -165,7 +208,27 @@ class SemanticsTextEditingStrategy extends DefaultTextEditingStrategy {
165208
}
166209

167210
@override
168-
void placeForm() {}
211+
void placeForm() {
212+
// Safari autofills grouped credential fields by heuristic without needing a
213+
// form. The attribute-linked form regresses that: a non-focused field's real
214+
// input is left outside the form and stops being filled
215+
// (flutter/flutter#180652). Skip the form on Safari and let its native
216+
// heuristic fill the whole group. `_formIsActive` stays false, so [disable]
217+
// skips the demote/dormant cleanup too.
218+
//
219+
// Other WebKit browsers (Chrome, Firefox on iOS) do not fill by heuristic
220+
// and need the form path, so they are not skipped here.
221+
if (ui_web.browser.isSafari) {
222+
return;
223+
}
224+
225+
// The focused element is the real semantics-owned `<input>`. It must not be
226+
// moved into the form (that regressed a11y tab traversal, see
227+
// flutter/flutter#180652). Link it to the form via the `form` attribute
228+
// instead. See [EngineAutofillForm.wakeUp].
229+
inputConfiguration.autofillGroup!.wakeUp(activeDomElement, inputConfiguration.autofill!);
230+
_formIsActive = true;
231+
}
169232

170233
@override
171234
void updateElementPlacement(EditableTextGeometry textGeometry) {
@@ -364,6 +427,17 @@ class SemanticTextField extends SemanticRole {
364427
(editableElement as DomElementWithDisabledProperty).disabled = !semanticsObject.isEnabled;
365428
}
366429

430+
/// Whether an autofill group owns the autofill-related attributes of this
431+
/// field.
432+
///
433+
/// When the field participates in an autofill group, [AutofillInfo.applyToDomElement]
434+
/// sets the element's `name` (and `id`/`autocomplete`) to the autofill hint.
435+
/// A plain semantic input never has a `name`, so a non-empty `name` is a
436+
/// reliable, order-independent signal that [_updateInputType] must not
437+
/// overwrite `autocomplete`, otherwise grouped autofill silently breaks on
438+
/// the next semantics update (flutter/flutter#180652).
439+
bool get _isAutofillOwned => editableElement.getAttribute('name')?.isNotEmpty ?? false;
440+
367441
void _updateInputType() {
368442
if (semanticsObject.flags.isMultiline) {
369443
// text area can't be annotated with input type
@@ -379,7 +453,9 @@ class SemanticTextField extends SemanticRole {
379453
// proper selection/cursor operations.
380454
input.removeAttribute('inputmode');
381455
input.removeAttribute('autocapitalize');
382-
input.autocomplete = 'off';
456+
if (!_isAutofillOwned) {
457+
input.autocomplete = 'off';
458+
}
383459
input.type = 'text';
384460

385461
switch (semanticsObject.inputType) {
@@ -392,7 +468,9 @@ class SemanticTextField extends SemanticRole {
392468
case ui.SemanticsInputType.email:
393469
input.setAttribute('inputmode', 'email');
394470
input.setAttribute('autocapitalize', 'none');
395-
input.autocomplete = 'email';
471+
if (!_isAutofillOwned) {
472+
input.autocomplete = 'email';
473+
}
396474
default:
397475
}
398476
}

‎engine/src/flutter/lib/web_ui/lib/src/engine/text_editing/text_editing.dart‎

Lines changed: 139 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ class EngineAutofillForm {
192192
required this.items,
193193
required this.formIdentifier,
194194
required this.focusedElementId,
195+
this.associateFocusedElementByAttribute = false,
195196
});
196197

197198
DomHTMLFormElement? formElement;
@@ -218,10 +219,40 @@ class EngineAutofillForm {
218219

219220
final String focusedElementId;
220221

222+
/// Whether the real focused text field lives outside [formElement] and is
223+
/// associated with it via the HTML `form` attribute.
224+
///
225+
/// This is used by semantics mode, where moving the real text field into the
226+
/// form would break accessibility traversal.
227+
final bool associateFocusedElementByAttribute;
228+
221229
bool get _isSafariStrategy =>
222230
textEditing.strategy is SafariDesktopTextEditingStrategy ||
223231
textEditing.strategy is IOSTextEditingStrategy;
224232

233+
/// Whether the current browser engine is WebKit (Safari and all iOS
234+
/// browsers).
235+
///
236+
/// WebKit refuses to autofill an input that is zero-sized, so synthetic
237+
/// autofill fields must keep a real size on WebKit, while other engines
238+
/// tolerate zero-sized fields. An engine check is used rather than the active
239+
/// text editing strategy because in semantics mode the active strategy is
240+
/// [SemanticsTextEditingStrategy], not the Safari/iOS strategy, so the
241+
/// strategy type alone would miss WebKit there.
242+
bool get _isWebKit => ui_web.browser.browserEngine == ui_web.BrowserEngine.webkit;
243+
244+
/// Stable DOM id for the form, used so a focused element that lives outside
245+
/// the form, in semantics mode, can be associated with it via the HTML `form`
246+
/// attribute.
247+
///
248+
/// Derived from [formIdentifier] so it stays the same when the form goes
249+
/// dormant and is woken again by a new instance, and so that distinct
250+
/// identifiers map to distinct ids. The latter matters when multiple autofill
251+
/// forms coexist in one document, for example multi-view, multiple autofill
252+
/// groups, or a dormant form left in the DOM, where a focused element must
253+
/// never be associated with the wrong form.
254+
String get formDomId => 'flt-af-$formIdentifier';
255+
225256
/// Creates an [EngineAutofillForm] from the JSON representation of a Flutter
226257
/// framework `TextInputConfiguration` object.
227258
///
@@ -289,6 +320,17 @@ class EngineAutofillForm {
289320
);
290321
}
291322

323+
EngineAutofillForm copyWith({bool? associateFocusedElementByAttribute}) {
324+
return EngineAutofillForm(
325+
viewId: viewId,
326+
items: items,
327+
formIdentifier: formIdentifier,
328+
focusedElementId: focusedElementId,
329+
associateFocusedElementByAttribute:
330+
associateFocusedElementByAttribute ?? this.associateFocusedElementByAttribute,
331+
);
332+
}
333+
292334
static String _getFormIdentifier(Map<String, FieldItem> items) {
293335
final ids = <String>[];
294336
for (final FieldItem item in items.values) {
@@ -301,7 +343,16 @@ class EngineAutofillForm {
301343

302344
/// Wakes up the form with the given focused element.
303345
///
304-
/// The [focusedElement] is inserted into the form, replacing the old focused element.
346+
/// The [focusedElement] is inserted into the form, replacing the old focused
347+
/// element.
348+
///
349+
/// When [associateFocusedElementByAttribute] is true (semantics mode), the focused
350+
/// element is a real `<input>` owned by the semantics tree and must stay in
351+
/// its `<flt-semantics>` node. Moving it into the form regressed a11y tab
352+
/// traversal (see flutter/flutter#180652).
353+
/// Instead it is linked to the form via the HTML `form` attribute, and any
354+
/// synthetic placeholder previously created for that field is removed so the
355+
/// field is represented in the form exactly once.
305356
void wakeUp(DomHTMLElement focusedElement, AutofillInfo focusedAutofill) {
306357
// Since we're disabling pointer events on the form to fix Safari autofill,
307358
// we need to explicitly set pointer events on the active input element in
@@ -311,7 +362,13 @@ class EngineAutofillForm {
311362
focusedElement.style.pointerEvents = 'all';
312363
}
313364

314-
final EngineAutofillForm? existingForm = dormantForms[formIdentifier];
365+
EngineAutofillForm? existingForm = dormantForms[formIdentifier];
366+
if (existingForm != null &&
367+
existingForm.associateFocusedElementByAttribute != associateFocusedElementByAttribute) {
368+
existingForm.formElement?.remove();
369+
dormantForms.remove(formIdentifier);
370+
existingForm = null;
371+
}
315372

316373
final firstWakeUp = formElement == null;
317374

@@ -336,11 +393,24 @@ class EngineAutofillForm {
336393
}
337394
}
338395

339-
// There's potentially a new focused element that needs to be inserted into the existing form.
340-
//
341-
// Do not cause DOM disturbance unless necessary. Doing superfluous DOM operations may seem
342-
// harmless, but it actually causes focus changes that could break things.
343-
if (!formElement!.contains(focusedElement)) {
396+
if (associateFocusedElementByAttribute) {
397+
// Promote the focused field to its real (semantics-owned) element: drop
398+
// any synthetic placeholder so the field is not submitted twice, then
399+
// link the real element to the form by attribute.
400+
final DomElement? synthetic = elements[focusedAutofill.uniqueIdentifier];
401+
if (synthetic != null && synthetic != focusedElement) {
402+
synthetic.remove();
403+
}
404+
elements.remove(focusedAutofill.uniqueIdentifier);
405+
focusedElement.setAttribute('form', formDomId);
406+
} else if (!formElement!.contains(focusedElement)) {
407+
// There's potentially a new focused element that needs to be inserted
408+
// into the existing form.
409+
//
410+
// Do not cause DOM disturbance unless necessary. Doing superfluous DOM
411+
// operations may seem harmless, but it actually causes focus changes that
412+
// could break things.
413+
//
344414
// Find the matching element and replace it with the new focused element.
345415
final DomElement oldFocusedElement = elements[focusedAutofill.uniqueIdentifier]!;
346416
elements[focusedAutofill.uniqueIdentifier] = focusedElement;
@@ -350,6 +420,40 @@ class EngineAutofillForm {
350420
_updateFieldValues();
351421
}
352422

423+
/// Demotes a field that is losing focus in semantics mode back to a synthetic
424+
/// in-form placeholder.
425+
///
426+
/// In semantics mode the focused field is represented by its real element via
427+
/// the `form` attribute (see [wakeUp]). When it blurs, the real element must
428+
/// be detached from the form and replaced by a synthetic element carrying its
429+
/// last value, so the field still participates in form submission (credential
430+
/// save via `TextInput.finishAutofillContext`) and stays grouped when another
431+
/// field in the group is focused.
432+
void demoteFocusedToSynthetic(DomHTMLElement realElement, AutofillInfo autofill) {
433+
realElement.removeAttribute('form');
434+
final String id = autofill.uniqueIdentifier;
435+
final FieldItem? field = items[id];
436+
if (field == null || formElement == null) {
437+
return;
438+
}
439+
if (elements[id] != null && elements[id] != realElement) {
440+
// A synthetic placeholder already represents this field.
441+
return;
442+
}
443+
final DomHTMLElement synthetic = field.inputType.createDomElement();
444+
field.autofillInfo.applyToDomElement(synthetic);
445+
// Preserve the value the user (or autofill) just put in the real element so
446+
// the field still submits correctly for credential save.
447+
EditingState.fromDomElement(realElement).applyTextToDomElement(synthetic);
448+
_styleAutofillElements(
449+
synthetic,
450+
shouldHideElement: !_isWebKit,
451+
shouldDisablePointerEvents: _isWebKit,
452+
);
453+
elements[id] = synthetic;
454+
formElement!.append(synthetic);
455+
}
456+
353457
/// Makes the form dormant.
354458
///
355459
/// A dormant form stays in the DOM and does not interact with the framework until it's woken up.
@@ -376,16 +480,25 @@ class EngineAutofillForm {
376480
formElement.noValidate = true;
377481
formElement.method = 'post';
378482
formElement.action = '#';
483+
formElement.id = formDomId;
379484
formElement.addEventListener('submit', preventDefaultListener);
380485

381486
// We need to explicitly disable pointer events on the form in Safari Desktop and iOS,
382487
// so that we don't have pointer event collisions if users hover over or click
383488
// into the invisible autofill elements within the form.
384-
_styleAutofillElements(formElement, shouldDisablePointerEvents: _isSafariStrategy);
489+
_styleAutofillElements(formElement, shouldDisablePointerEvents: _isWebKit);
385490

386491
for (final FieldItem field in items.values) {
387492
final DomHTMLElement htmlElement;
388493
if (field.autofillInfo.uniqueIdentifier == focusedAutofill.uniqueIdentifier) {
494+
if (associateFocusedElementByAttribute) {
495+
// The focused element is a real semantics-owned element that stays
496+
// in its `<flt-semantics>` node and is linked via the `form`
497+
// attribute by [wakeUp]. Do not create or append a synthetic
498+
// placeholder for it here, otherwise the field would be submitted
499+
// twice.
500+
continue;
501+
}
389502
// Do not create the focused element here since it is created already. Use the provided one.
390503
htmlElement = focusedElement;
391504
} else {
@@ -400,8 +513,8 @@ class EngineAutofillForm {
400513
// sized and placed on the DOM, we also have to disable pointer events.
401514
_styleAutofillElements(
402515
htmlElement,
403-
shouldHideElement: !_isSafariStrategy,
404-
shouldDisablePointerEvents: _isSafariStrategy,
516+
shouldHideElement: !_isWebKit,
517+
shouldDisablePointerEvents: _isWebKit,
405518
);
406519
}
407520

@@ -1146,6 +1259,22 @@ class InputConfiguration {
11461259
enableInteractiveSelection =
11471260
flutterInputConfiguration.tryBool('enableInteractiveSelection') ?? true;
11481261

1262+
InputConfiguration copyWith({EngineAutofillForm? autofillGroup}) {
1263+
return InputConfiguration(
1264+
viewId: viewId,
1265+
inputType: inputType,
1266+
inputAction: inputAction,
1267+
obscureText: obscureText,
1268+
readOnly: readOnly,
1269+
autocorrect: autocorrect,
1270+
textCapitalization: textCapitalization,
1271+
autofill: autofill,
1272+
autofillGroup: autofillGroup ?? this.autofillGroup,
1273+
enableDeltaModel: enableDeltaModel,
1274+
enableInteractiveSelection: enableInteractiveSelection,
1275+
);
1276+
}
1277+
11491278
/// The ID of the view that contains the text field.
11501279
final int viewId;
11511280

0 commit comments

Comments
 (0)