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