Skip to content

Commit c9c0114

Browse files
committed
[web] Preserve active autofill form across text input configuration updates
Fixes #193267 Fixes #192102 TextInput.updateConfig decodes a new EngineAutofillForm with a null formElement. Assigning this new form replaced the active autofill group, disconnecting the strategy from the live DOM <form> and its input listeners. Consequently, disable() failed its formElement null-check and stripped the active DOM input instead of putting the form to sleep, breaking subsequent refocus on iOS Safari and Chrome. Preserve the active EngineAutofillForm across TextInput.updateConfig when the form identity matches, and update its field metadata in place.
1 parent 53c2903 commit c9c0114

2 files changed

Lines changed: 216 additions & 0 deletions

File tree

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,33 @@ class EngineAutofillForm {
344344
);
345345
}
346346

347+
/// Whether [other] represents the same autofill form identity.
348+
bool hasSameIdentity(EngineAutofillForm other) {
349+
return formIdentifier == other.formIdentifier &&
350+
viewId == other.viewId &&
351+
focusedElementId == other.focusedElementId &&
352+
associateFocusedElementByAttribute == other.associateFocusedElementByAttribute;
353+
}
354+
355+
/// Updates this active autofill form with metadata from a newer framework
356+
/// configuration without replacing the DOM form owned by this instance.
357+
///
358+
/// A `TextInput.updateConfig` message creates a new [EngineAutofillForm]
359+
/// while the current text input connection may still be using this form's
360+
/// DOM elements and event listeners. Replacing the form object in that case
361+
/// would disconnect the configuration from the live DOM form.
362+
void updateFrameworkConfiguration(EngineAutofillForm configuration) {
363+
assert(hasSameIdentity(configuration));
364+
365+
items
366+
..clear()
367+
..addAll(configuration.items);
368+
369+
if (formElement != null) {
370+
_updateFieldValues();
371+
}
372+
}
373+
347374
static String _getFormIdentifier(Map<String, FieldItem> items) {
348375
final ids = <String>[];
349376
for (final FieldItem item in items.values) {
@@ -1614,7 +1641,20 @@ abstract class DefaultTextEditingStrategy
16141641
}
16151642

16161643
void applyConfiguration(InputConfiguration config) {
1644+
if (isEnabled && _appendedToForm) {
1645+
final EngineAutofillForm? currentGroup = inputConfiguration.autofillGroup;
1646+
final EngineAutofillForm? updatedGroup = config.autofillGroup;
1647+
1648+
if (currentGroup != null &&
1649+
updatedGroup != null &&
1650+
currentGroup.hasSameIdentity(updatedGroup)) {
1651+
currentGroup.updateFrameworkConfiguration(updatedGroup);
1652+
config = config.copyWith(autofillGroup: currentGroup);
1653+
}
1654+
}
1655+
16171656
inputConfiguration = config;
1657+
owner.configuration = config;
16181658

16191659
if (config.readOnly) {
16201660
activeDomElement.setAttribute('readonly', 'readonly');

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

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2119,6 +2119,182 @@ 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+
2130+
final setClient = MethodCall('TextInput.setClient', <dynamic>[123, initialConfig]);
2131+
sendFrameworkMessage(codec.encodeMethodCall(setClient));
2132+
2133+
const setEditingState = MethodCall('TextInput.setEditingState', <String, dynamic>{
2134+
'text': 'secret',
2135+
'selectionBase': 6,
2136+
'selectionExtent': 6,
2137+
'composingBase': -1,
2138+
'composingExtent': -1,
2139+
});
2140+
sendFrameworkMessage(codec.encodeMethodCall(setEditingState));
2141+
2142+
const show = MethodCall('TextInput.show');
2143+
sendFrameworkMessage(codec.encodeMethodCall(show));
2144+
2145+
final MethodCall setSizeAndTransform = configureSetSizeAndTransformMethodCall(
2146+
150,
2147+
50,
2148+
Matrix4.identity().storage.toList(),
2149+
);
2150+
sendFrameworkMessage(codec.encodeMethodCall(setSizeAndTransform));
2151+
2152+
final DefaultTextEditingStrategy strategy = textEditing!.strategy;
2153+
2154+
expect(strategy.appendedToForm, isTrue);
2155+
2156+
final EngineAutofillForm originalAutofillGroup = strategy.inputConfiguration.autofillGroup!;
2157+
2158+
final DomHTMLFormElement originalForm = originalAutofillGroup.formElement!;
2159+
2160+
final DomHTMLElement activeElement = strategy.activeDomElement;
2161+
2162+
expect(originalForm.contains(activeElement), isTrue);
2163+
2164+
// Simulates toggling TextField.obscureText while the autofill-enabled
2165+
// password field remains connected.
2166+
final Map<String, dynamic> updatedConfig = createFlutterConfig(
2167+
'text',
2168+
autofillHint: 'password',
2169+
autofillHintsForFields: <String>['username', 'password'],
2170+
);
2171+
2172+
final updateConfig = MethodCall('TextInput.updateConfig', updatedConfig);
2173+
2174+
sendFrameworkMessage(codec.encodeMethodCall(updateConfig));
2175+
2176+
final EngineAutofillForm updatedAutofillGroup = strategy.inputConfiguration.autofillGroup!;
2177+
2178+
// updateConfig must continue using the object that owns the live DOM form.
2179+
expect(identical(updatedAutofillGroup, originalAutofillGroup), isTrue);
2180+
2181+
expect(updatedAutofillGroup.formElement, same(originalForm));
2182+
expect(originalForm.contains(activeElement), isTrue);
2183+
2184+
const clearClient = MethodCall('TextInput.clearClient');
2185+
sendFrameworkMessage(codec.encodeMethodCall(clearClient));
2186+
2187+
// The form must go dormant rather than having the focused DOM element
2188+
// removed through the non-autofill path.
2189+
expect(dormantForms, hasLength(1));
2190+
expect(dormantForms.values.single.formElement, same(originalForm));
2191+
expect(originalForm.isConnected, isTrue);
2192+
});
2193+
2194+
test('TextInput.updateConfig preserves active single-field autofill form', () async {
2195+
final Map<String, dynamic> initialConfig = createFlutterConfig(
2196+
'text',
2197+
autofillHint: 'password',
2198+
obscureText: true,
2199+
);
2200+
2201+
final setClient = MethodCall('TextInput.setClient', <dynamic>[123, initialConfig]);
2202+
sendFrameworkMessage(codec.encodeMethodCall(setClient));
2203+
2204+
const show = MethodCall('TextInput.show');
2205+
sendFrameworkMessage(codec.encodeMethodCall(show));
2206+
2207+
final MethodCall setSizeAndTransform = configureSetSizeAndTransformMethodCall(
2208+
150,
2209+
50,
2210+
Matrix4.identity().storage.toList(),
2211+
);
2212+
sendFrameworkMessage(codec.encodeMethodCall(setSizeAndTransform));
2213+
2214+
final DefaultTextEditingStrategy strategy = textEditing!.strategy;
2215+
2216+
expect(strategy.appendedToForm, isTrue);
2217+
2218+
final EngineAutofillForm originalAutofillGroup = strategy.inputConfiguration.autofillGroup!;
2219+
final DomHTMLFormElement originalForm = originalAutofillGroup.formElement!;
2220+
2221+
final Map<String, dynamic> updatedConfig = createFlutterConfig(
2222+
'text',
2223+
autofillHint: 'password',
2224+
);
2225+
2226+
final updateConfig = MethodCall('TextInput.updateConfig', updatedConfig);
2227+
sendFrameworkMessage(codec.encodeMethodCall(updateConfig));
2228+
2229+
final EngineAutofillForm updatedAutofillGroup = strategy.inputConfiguration.autofillGroup!;
2230+
expect(identical(updatedAutofillGroup, originalAutofillGroup), isTrue);
2231+
expect(updatedAutofillGroup.formElement, same(originalForm));
2232+
2233+
const clearClient = MethodCall('TextInput.clearClient');
2234+
sendFrameworkMessage(codec.encodeMethodCall(clearClient));
2235+
2236+
expect(dormantForms, hasLength(1));
2237+
expect(dormantForms.values.single.formElement, same(originalForm));
2238+
expect(originalForm.isConnected, isTrue);
2239+
});
2240+
2241+
test('autofill form remains reusable after TextInput.updateConfig', () async {
2242+
final Map<String, dynamic> config = createFlutterConfig(
2243+
'text',
2244+
autofillHint: 'password',
2245+
autofillHintsForFields: <String>['username', 'password'],
2246+
obscureText: true,
2247+
);
2248+
2249+
sendFrameworkMessage(
2250+
codec.encodeMethodCall(MethodCall('TextInput.setClient', <dynamic>[123, config])),
2251+
);
2252+
2253+
sendFrameworkMessage(codec.encodeMethodCall(const MethodCall('TextInput.show')));
2254+
2255+
sendFrameworkMessage(
2256+
codec.encodeMethodCall(
2257+
configureSetSizeAndTransformMethodCall(100, 40, Matrix4.identity().storage.toList()),
2258+
),
2259+
);
2260+
2261+
final DomHTMLFormElement originalForm = textEditing!.strategy.focusedFormElement!;
2262+
2263+
final Map<String, dynamic> visiblePasswordConfig = createFlutterConfig(
2264+
'text',
2265+
autofillHint: 'password',
2266+
autofillHintsForFields: <String>['username', 'password'],
2267+
);
2268+
2269+
sendFrameworkMessage(
2270+
codec.encodeMethodCall(MethodCall('TextInput.updateConfig', visiblePasswordConfig)),
2271+
);
2272+
2273+
sendFrameworkMessage(codec.encodeMethodCall(const MethodCall('TextInput.clearClient')));
2274+
2275+
expect(dormantForms, hasLength(1));
2276+
expect(dormantForms.values.single.formElement, same(originalForm));
2277+
2278+
// Reconnect to the same autofill field.
2279+
sendFrameworkMessage(
2280+
codec.encodeMethodCall(
2281+
MethodCall('TextInput.setClient', <dynamic>[124, visiblePasswordConfig]),
2282+
),
2283+
);
2284+
2285+
sendFrameworkMessage(codec.encodeMethodCall(const MethodCall('TextInput.show')));
2286+
2287+
sendFrameworkMessage(
2288+
codec.encodeMethodCall(
2289+
configureSetSizeAndTransformMethodCall(100, 40, Matrix4.identity().storage.toList()),
2290+
),
2291+
);
2292+
2293+
expect(textEditing!.strategy.focusedFormElement, same(originalForm));
2294+
2295+
expect(originalForm.contains(textEditing!.strategy.activeDomElement), isTrue);
2296+
});
2297+
21222298
test('No capitalization: setClient, setEditingState, show', () {
21232299
final Map<String, dynamic> noCapitalizationConfig = createFlutterConfig('text');
21242300
final setClient = MethodCall('TextInput.setClient', <dynamic>[123, noCapitalizationConfig]);

0 commit comments

Comments
 (0)