Skip to content

Commit 4c15b12

Browse files
authored
[web] Preserve active autofill form across text input configuration updates (flutter#193269)
Fixes flutter#192102 Fixes flutter#193267 ### Problem Toggling `TextField.obscureText` on Web sends `TextInput.updateConfig`, which decodes a new `EngineAutofillForm` lacking an associated DOM form (`formElement == null`). On blur (`TextInput.clearClient`), the web engine bypasses dormancy and tears down the active DOM input element. Refocusing the field leaves it unresponsive to keyboard events. ### Solution Preserve the active `EngineAutofillForm` across `TextInput.updateConfig` in `DefaultTextEditingStrategy.applyConfiguration`. This retains live DOM ownership so the form goes dormant on blur and wakes up cleanly on refocus. ### Testing Added engine unit tests in `text_editing_test.dart` for multi-field and single-field autofill forms, verifying dormancy and refocus reusability. | Issue | Result | | - | - | | flutter#192102: plain password field | https://github.com/user-attachments/assets/a8c98c15-7dbe-4d91-bf32-bbe90bfffc08 | | flutter#193267: password field with autofill hints | https://github.com/user-attachments/assets/7c57998f-4e80-42bc-bc3c-b7bdc49f27db | ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] All existing and new tests are passing.
1 parent d616c23 commit 4c15b12

3 files changed

Lines changed: 224 additions & 0 deletions

File tree

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,18 @@ abstract class DefaultTextEditingStrategy
16141614
}
16151615

16161616
void applyConfiguration(InputConfiguration config) {
1617+
// A `TextInput.updateConfig` message only describes the focused field, so
1618+
// the [EngineAutofillForm] decoded from it has no DOM form. While the
1619+
// editing element is part of a live form, keep that form. Otherwise
1620+
// [disable] would remove the element from the form instead of letting the
1621+
// form go dormant, and the next connection could not reuse it.
1622+
if (isEnabled && config.autofillGroup != null) {
1623+
final EngineAutofillForm? activeGroup = inputConfiguration.autofillGroup;
1624+
if (activeGroup?.formElement != null) {
1625+
config = config.copyWith(autofillGroup: activeGroup);
1626+
}
1627+
}
1628+
16171629
inputConfiguration = config;
16181630

16191631
if (config.readOnly) {

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,43 @@ void testMain() {
753753
expect(dormantForms[form.formIdentifier], form);
754754
});
755755

756+
// See https://github.com/flutter/flutter/issues/192102
757+
test('TextInput.updateConfig preserves active autofill form', () {
758+
final (form: EngineAutofillForm form, textField: SemanticTextField textField) =
759+
activateGroup();
760+
final DomHTMLFormElement formElement = form.formElement!;
761+
762+
// A `TextInput.updateConfig` message (e.g. toggling obscureText) only
763+
// describes the focused field, so the decoded group has no 'fields' and
764+
// no DOM form.
765+
final focusedMap =
766+
_autofillFields(<String>['username'], <String>['field1']).single['autofill']!
767+
as Map<String, Object?>;
768+
testTextEditing.configuration = InputConfiguration(
769+
viewId: kImplicitViewId,
770+
obscureText: true,
771+
autofill: AutofillInfo.fromFrameworkMessage(focusedMap),
772+
autofillGroup: EngineAutofillForm.fromFrameworkMessage(kImplicitViewId, focusedMap, null),
773+
);
774+
const TextInputUpdateConfig().run(testTextEditing);
775+
776+
// The live form is kept and the focused field stays linked to it.
777+
expect(strategy.inputConfiguration.obscureText, isTrue);
778+
expect(strategy.inputConfiguration.autofillGroup, same(form));
779+
expect(form.formElement, same(formElement));
780+
expect(textField.editableElement.getAttribute('form'), form.formDomId);
781+
782+
strategy.disable();
783+
784+
// On blur the form goes dormant instead of being dropped, so the next
785+
// connection can reuse it.
786+
expect(dormantForms[form.formIdentifier], same(form));
787+
expect(form.formElement, same(formElement));
788+
expect(formElement.isConnected, isTrue);
789+
expect(textField.editableElement.getAttribute('form'), isNull);
790+
expect(formElement.contains(form.elements['field1']), isTrue);
791+
});
792+
756793
// Focus A, autofill A, focus B, autofill a different credential, focus A
757794
// again, then assert each field is represented in the form exactly once
758795
// with its latest value and the focused field is linked by attribute (not

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

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2119,6 +2119,181 @@ Future<void> testMain() async {
21192119
expect(dormantForms, hasLength(1));
21202120
});
21212121

2122+
test('TextInput.updateConfig preserves active autofill form', () async {
2123+
final Map<String, dynamic> initialConfig = createFlutterConfig(
2124+
'text',
2125+
autofillHint: 'password',
2126+
autofillHintsForFields: <String>['username', 'password'],
2127+
obscureText: true,
2128+
);
2129+
final setClient = MethodCall('TextInput.setClient', <dynamic>[123, initialConfig]);
2130+
sendFrameworkMessage(codec.encodeMethodCall(setClient));
2131+
2132+
const setEditingState = MethodCall('TextInput.setEditingState', <String, dynamic>{
2133+
'text': 'secret',
2134+
'selectionBase': 6,
2135+
'selectionExtent': 6,
2136+
'composingBase': -1,
2137+
'composingExtent': -1,
2138+
});
2139+
sendFrameworkMessage(codec.encodeMethodCall(setEditingState));
2140+
2141+
const show = MethodCall('TextInput.show');
2142+
sendFrameworkMessage(codec.encodeMethodCall(show));
2143+
2144+
final MethodCall setSizeAndTransform = configureSetSizeAndTransformMethodCall(
2145+
150,
2146+
50,
2147+
Matrix4.identity().storage.toList(),
2148+
);
2149+
sendFrameworkMessage(codec.encodeMethodCall(setSizeAndTransform));
2150+
2151+
final DefaultTextEditingStrategy strategy = textEditing!.strategy;
2152+
expect(strategy.appendedToForm, isTrue);
2153+
2154+
final EngineAutofillForm originalAutofillGroup = strategy.inputConfiguration.autofillGroup!;
2155+
final DomHTMLFormElement originalForm = originalAutofillGroup.formElement!;
2156+
final DomHTMLElement activeElement = strategy.activeDomElement;
2157+
2158+
expect(originalForm.contains(activeElement), isTrue);
2159+
2160+
// Simulates toggling obscureText (TextInput.updateConfig only describes
2161+
// the focused field, omitting 'fields').
2162+
final Map<String, dynamic> updatedConfig = createFlutterConfig(
2163+
'text',
2164+
autofillHint: 'password',
2165+
);
2166+
final updateConfig = MethodCall('TextInput.updateConfig', updatedConfig);
2167+
sendFrameworkMessage(codec.encodeMethodCall(updateConfig));
2168+
2169+
final EngineAutofillForm updatedAutofillGroup = strategy.inputConfiguration.autofillGroup!;
2170+
expect(updatedAutofillGroup, same(originalAutofillGroup));
2171+
expect(updatedAutofillGroup.formElement, same(originalForm));
2172+
expect(originalForm.contains(activeElement), isTrue);
2173+
2174+
const clearClient = MethodCall('TextInput.clearClient');
2175+
sendFrameworkMessage(codec.encodeMethodCall(clearClient));
2176+
2177+
// The form must go dormant rather than having the DOM element removed.
2178+
expect(dormantForms, hasLength(1));
2179+
expect(dormantForms.values.single.formElement, same(originalForm));
2180+
expect(originalForm.isConnected, isTrue);
2181+
});
2182+
2183+
test(
2184+
'singleTextField Autofill: TextInput.updateConfig preserves form and allows dormancy',
2185+
() async {
2186+
final Map<String, dynamic> initialConfig = createFlutterConfig(
2187+
'text',
2188+
autofillHint: 'password',
2189+
obscureText: true,
2190+
);
2191+
final setClient = MethodCall('TextInput.setClient', <dynamic>[123, initialConfig]);
2192+
sendFrameworkMessage(codec.encodeMethodCall(setClient));
2193+
2194+
const setEditingState = MethodCall('TextInput.setEditingState', <String, dynamic>{
2195+
'text': 'secret',
2196+
'selectionBase': 6,
2197+
'selectionExtent': 6,
2198+
'composingBase': -1,
2199+
'composingExtent': -1,
2200+
});
2201+
sendFrameworkMessage(codec.encodeMethodCall(setEditingState));
2202+
2203+
const show = MethodCall('TextInput.show');
2204+
sendFrameworkMessage(codec.encodeMethodCall(show));
2205+
2206+
final MethodCall setSizeAndTransform = configureSetSizeAndTransformMethodCall(
2207+
150,
2208+
50,
2209+
Matrix4.identity().storage.toList(),
2210+
);
2211+
sendFrameworkMessage(codec.encodeMethodCall(setSizeAndTransform));
2212+
2213+
final DefaultTextEditingStrategy strategy = textEditing!.strategy;
2214+
expect(strategy.appendedToForm, isTrue);
2215+
2216+
final DomHTMLFormElement originalForm = strategy.focusedFormElement!;
2217+
expect(originalForm.contains(strategy.activeDomElement), isTrue);
2218+
2219+
// Toggle obscureText
2220+
final Map<String, dynamic> updatedConfig = createFlutterConfig(
2221+
'text',
2222+
autofillHint: 'password',
2223+
);
2224+
final updateConfig = MethodCall('TextInput.updateConfig', updatedConfig);
2225+
sendFrameworkMessage(codec.encodeMethodCall(updateConfig));
2226+
2227+
expect(strategy.focusedFormElement, same(originalForm));
2228+
2229+
const clearClient = MethodCall('TextInput.clearClient');
2230+
sendFrameworkMessage(codec.encodeMethodCall(clearClient));
2231+
2232+
expect(dormantForms, hasLength(1));
2233+
expect(dormantForms.values.single.formElement, same(originalForm));
2234+
expect(originalForm.isConnected, isTrue);
2235+
},
2236+
);
2237+
2238+
test('autofill form remains reusable after TextInput.updateConfig', () async {
2239+
final Map<String, dynamic> initialConfig = createFlutterConfig(
2240+
'text',
2241+
autofillHint: 'password',
2242+
autofillHintsForFields: <String>['username', 'password'],
2243+
obscureText: true,
2244+
);
2245+
final setClient = MethodCall('TextInput.setClient', <dynamic>[123, initialConfig]);
2246+
sendFrameworkMessage(codec.encodeMethodCall(setClient));
2247+
2248+
const show = MethodCall('TextInput.show');
2249+
sendFrameworkMessage(codec.encodeMethodCall(show));
2250+
2251+
final MethodCall setSizeAndTransform = configureSetSizeAndTransformMethodCall(
2252+
150,
2253+
50,
2254+
Matrix4.identity().storage.toList(),
2255+
);
2256+
sendFrameworkMessage(codec.encodeMethodCall(setSizeAndTransform));
2257+
2258+
final DefaultTextEditingStrategy strategy = textEditing!.strategy;
2259+
final DomHTMLFormElement form = strategy.focusedFormElement!;
2260+
2261+
// Toggle obscureText
2262+
final Map<String, dynamic> updatedConfig = createFlutterConfig(
2263+
'text',
2264+
autofillHint: 'password',
2265+
);
2266+
final updateConfig = MethodCall('TextInput.updateConfig', updatedConfig);
2267+
sendFrameworkMessage(codec.encodeMethodCall(updateConfig));
2268+
2269+
// Blur
2270+
const clearClient = MethodCall('TextInput.clearClient');
2271+
sendFrameworkMessage(codec.encodeMethodCall(clearClient));
2272+
expect(dormantForms, hasLength(1));
2273+
2274+
// Refocus the same field with its full autofill configuration.
2275+
final Map<String, dynamic> reconnectConfig = createFlutterConfig(
2276+
'text',
2277+
autofillHint: 'password',
2278+
autofillHintsForFields: <String>['username', 'password'],
2279+
);
2280+
final setClientAgain = MethodCall('TextInput.setClient', <dynamic>[124, reconnectConfig]);
2281+
sendFrameworkMessage(codec.encodeMethodCall(setClientAgain));
2282+
sendFrameworkMessage(codec.encodeMethodCall(show));
2283+
sendFrameworkMessage(codec.encodeMethodCall(setSizeAndTransform));
2284+
2285+
// The dormant form is revived and reused.
2286+
expect(dormantForms, hasLength(1));
2287+
expect(strategy.focusedFormElement, same(form));
2288+
expect(form.contains(strategy.activeDomElement), isTrue);
2289+
expect(form.isConnected, isTrue);
2290+
2291+
const finishAutofillContext = MethodCall('TextInput.finishAutofillContext', false);
2292+
sendFrameworkMessage(codec.encodeMethodCall(finishAutofillContext));
2293+
expect(dormantForms, isEmpty);
2294+
expect(form.isConnected, isFalse);
2295+
});
2296+
21222297
test('No capitalization: setClient, setEditingState, show', () {
21232298
final Map<String, dynamic> noCapitalizationConfig = createFlutterConfig('text');
21242299
final setClient = MethodCall('TextInput.setClient', <dynamic>[123, noCapitalizationConfig]);

0 commit comments

Comments
 (0)