Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit cc0e99b

Browse files
author
nturgut
committed
add capitalization support to autofill fields
1 parent 6ae1e8d commit cc0e99b

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

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

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,10 @@ class EngineAutofillForm {
114114
if (fields != null) {
115115
for (Map<String, dynamic> field in fields.cast<Map<String, dynamic>>()) {
116116
final Map<String, dynamic> autofillInfo = field['autofill'];
117-
final AutofillInfo autofill =
118-
AutofillInfo.fromFrameworkMessage(autofillInfo);
117+
final AutofillInfo autofill = AutofillInfo.fromFrameworkMessage(
118+
autofillInfo,
119+
textCapitalization: TextCapitalizationUtil.fromInputConfiguration(
120+
field['textCapitalization']));
119121

120122
// The focused text editing element will not be created here.
121123
final AutofillInfo focusedElement =
@@ -169,17 +171,24 @@ class EngineAutofillForm {
169171
keys.forEach((String key) {
170172
final html.Element element = elements![key]!;
171173
subscriptions.add(element.onInput.listen((html.Event e) {
172-
_handleChange(element, key);
174+
if (items![key] == null) {
175+
throw StateError(
176+
'Autofill would not work withuot Autofill value set');
177+
} else {
178+
final AutofillInfo autofillInfo = items![key] as AutofillInfo;
179+
_handleChange(element, autofillInfo);
180+
}
173181
}));
174182
});
175183
return subscriptions;
176184
}
177185

178-
void _handleChange(html.Element domElement, String? tag) {
179-
EditingState newEditingState =
180-
EditingState.fromDomElement(domElement as html.HtmlElement?);
186+
void _handleChange(html.Element domElement, AutofillInfo autofillInfo) {
187+
EditingState newEditingState = EditingState.fromDomElement(
188+
domElement as html.HtmlElement?,
189+
textCapitalization: autofillInfo.textCapitalization);
181190

182-
_sendAutofillEditingState(tag, newEditingState);
191+
_sendAutofillEditingState(autofillInfo.uniqueIdentifier, newEditingState);
183192
}
184193

185194
/// Sends the 'TextInputClient.updateEditingStateWithTag' message to the framework.
@@ -210,7 +219,8 @@ class AutofillInfo {
210219
AutofillInfo(
211220
{required this.editingState,
212221
required this.uniqueIdentifier,
213-
required this.hint});
222+
required this.hint,
223+
required this.textCapitalization});
214224

215225
/// The current text and selection state of a text field.
216226
final EditingState editingState;
@@ -220,14 +230,29 @@ class AutofillInfo {
220230
/// Used as id of the text field.
221231
final String uniqueIdentifier;
222232

233+
/// Information on how should autofilled text capitalized.
234+
///
235+
/// For example for [TextCapitalization.characters] each letter is converted
236+
/// to upper case.
237+
///
238+
/// This value is not necessary for autofilling the focused element since
239+
/// [DefaultTextEditingStrategy._inputConfiguration] already has this
240+
/// information.
241+
///
242+
/// On the other hand for the multi element forms, for the input elements
243+
/// other the focused field, we need to use this information.
244+
final TextCapitalizationUtil textCapitalization;
245+
223246
/// Attribute used for autofill.
224247
///
225248
/// Used as a guidance to the browser as to the type of information expected
226249
/// in the field.
227250
/// See: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/autocomplete
228251
final String hint;
229252

230-
factory AutofillInfo.fromFrameworkMessage(Map<String, dynamic> autofill) {
253+
factory AutofillInfo.fromFrameworkMessage(Map<String, dynamic> autofill,
254+
{TextCapitalizationUtil textCapitalization =
255+
const TextCapitalizationUtil.defaultCapitalization()}) {
231256
assert(autofill != null); // ignore: unnecessary_null_comparison
232257
final String uniqueIdentifier = autofill['uniqueIdentifier']!;
233258
final List<dynamic> hintsList = autofill['hints'];
@@ -236,7 +261,8 @@ class AutofillInfo {
236261
return AutofillInfo(
237262
uniqueIdentifier: uniqueIdentifier,
238263
hint: BrowserAutofillHints.instance.flutterToEngine(hintsList[0]),
239-
editingState: editingState);
264+
editingState: editingState,
265+
textCapitalization: textCapitalization);
240266
}
241267

242268
void applyToDomElement(html.HtmlElement domElement,

0 commit comments

Comments
 (0)