Skip to content

Commit 2fd8904

Browse files
authored
[web] Hide synthetic autofill inputs from assistive technology (flutter#195618)
Fixes flutter#195174 ## Problem With semantics enabled, the web engine keeps a hidden autofill form in the page so the browser can autofill and save credentials. The synthetic inputs in that form have no label and are not hidden from assistive technology, so screen readers and checkers like axe find extra unnamed text boxes holding the user's input. The form stays in the page for the rest of the session. ## Fix In semantics mode, synthetic autofill inputs now get `aria-hidden="true"` and `tabIndex = -1` at both creation sites. Each field is already exposed through its own semantics element, browser autofill does not read ARIA, and the element being edited is never a synthetic one. Without semantics the inputs are left as they are, because password manager extensions fill them and some skip `aria-hidden` inputs. ## Demo Before: https://flutter-demo-88-before.web.app After: https://flutter-demo-88-after.web.app Type in Username and Password, then press "Inspect hidden form". Before, both hidden inputs are exposed and the accessibility tree has two unnamed text boxes. After, they are `aria-hidden` and only the two real fields are exposed. ## Verification Tested in Chrome, before and after this change, with semantics on and off: | | Semantics on | Semantics off | |---|---|---| | Before | https://flutter-demo-88-before.web.app | https://flutter-demo-88-before.web.app/?semantics=off | | After | https://flutter-demo-88-after.web.app | https://flutter-demo-88-after.web.app/?semantics=off | Type a username and password, press Sign in, which calls `TextInput.finishAutofillContext()`, and save the credential. Then reload and pick the saved credential. - Credential save: Chrome saves the typed username and password in all four cases. - Autofill: selecting the saved credential fills both Username and Password in all four cases.
1 parent e467dec commit 2fd8904

3 files changed

Lines changed: 52 additions & 0 deletions

File tree

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,10 +463,34 @@ class EngineAutofillForm {
463463
shouldHideElement: !_isWebKit,
464464
shouldDisablePointerEvents: _isWebKit,
465465
);
466+
_hideSyntheticElementFromAssistiveTechnology(synthetic);
466467
elements[id] = synthetic;
467468
formElement!.append(synthetic);
468469
}
469470

471+
/// Hides a synthetic autofill element from screen readers and keyboard
472+
/// navigation in semantics mode.
473+
///
474+
/// Synthetic elements exist for the browser's autofill and form submission.
475+
/// The semantics tree already exposes each field through its own element, so
476+
/// a synthetic copy would show up as an extra, unlabeled control, and the
477+
/// dormant form keeps it in the page for the rest of the session. Browser
478+
/// autofill does not read ARIA, so credential save and autofill keep working
479+
/// through these elements.
480+
///
481+
/// Without semantics, password manager extensions fill these elements, and
482+
/// some of them skip elements hidden from assistive technology, so they are
483+
/// left untouched.
484+
///
485+
/// See: https://github.com/flutter/flutter/issues/195174
486+
void _hideSyntheticElementFromAssistiveTechnology(DomHTMLElement element) {
487+
if (!associateFocusedElementByAttribute) {
488+
return;
489+
}
490+
element.setAttribute('aria-hidden', 'true');
491+
element.tabIndex = -1;
492+
}
493+
470494
/// Makes the form dormant.
471495
///
472496
/// A dormant form stays in the DOM and does not interact with the framework until it's woken up.
@@ -529,6 +553,7 @@ class EngineAutofillForm {
529553
shouldHideElement: !_isWebKit,
530554
shouldDisablePointerEvents: _isWebKit,
531555
);
556+
_hideSyntheticElementFromAssistiveTechnology(htmlElement);
532557
}
533558

534559
elements[field.autofillInfo.uniqueIdentifier] = htmlElement;

‎engine/src/flutter/lib/web_ui/test/engine/semantics/text_field_test.dart‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,30 @@ void testMain() {
931931
expect(dormantForms[form.formIdentifier], form);
932932
});
933933

934+
// See https://github.com/flutter/flutter/issues/195174
935+
test('hides synthetic autofill elements from assistive technology', () {
936+
final (form: EngineAutofillForm form, textField: SemanticTextField textField) =
937+
activateGroup();
938+
939+
// The synthetic element created for the non-focused member.
940+
final DomHTMLElement password = form.elements['field2']!;
941+
expect(password.getAttribute('aria-hidden'), 'true');
942+
expect(password.tabIndex, -1);
943+
944+
// The focused member is the real semantics element and stays exposed.
945+
expect(textField.editableElement.getAttribute('aria-hidden'), isNull);
946+
947+
strategy.disable();
948+
949+
// The synthetic placeholder created on blur stays in the dormant form for
950+
// the rest of the session, so it must be hidden as well.
951+
final DomHTMLElement placeholder = form.elements['field1']!;
952+
expect(placeholder, isNot(same(textField.editableElement)));
953+
expect(placeholder.getAttribute('aria-hidden'), 'true');
954+
expect(placeholder.tabIndex, -1);
955+
expect(textField.editableElement.getAttribute('aria-hidden'), isNull);
956+
});
957+
934958
// See https://github.com/flutter/flutter/issues/192102
935959
test('TextInput.updateConfig preserves active autofill form', () {
936960
final (form: EngineAutofillForm form, textField: SemanticTextField textField) =

‎engine/src/flutter/lib/web_ui/test/engine/text_editing_test.dart‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3825,6 +3825,9 @@ Future<void> testMain() async {
38253825

38263826
// Editing state is applied to the element.
38273827
expect(passwordElement.value, 'Test');
3828+
// Without semantics, password manager extensions fill these elements,
3829+
// and some skip elements hidden from assistive technology.
3830+
expect(passwordElement.getAttribute('aria-hidden'), isNull);
38283831
// When we only set the `value` without changing selection, the browser
38293832
// automatically sets the selection to the end of the value.
38303833
expect(passwordElement.selectionStart, 4);

0 commit comments

Comments
 (0)