[web] Flutter for web autocorrect support (flutter/engine#13922)

* support for autocorrect

* fixing the unit tests. Note there is no unit tests for the autocorrect since the unit tests do not run on webkit

* addressing PR comments

* fix typo
This commit is contained in:
Nurhan Turgut
2019-11-20 06:47:09 -08:00
committed by GitHub
parent c9f8d20878
commit ae5cce0b17
2 changed files with 63 additions and 1 deletions

View File

@@ -179,13 +179,15 @@ class InputConfiguration {
@required this.inputType,
@required this.inputAction,
@required this.obscureText,
@required this.autocorrect,
});
InputConfiguration.fromFlutter(Map<String, dynamic> flutterInputConfiguration)
: inputType = EngineInputType.fromName(
flutterInputConfiguration['inputType']['name']),
inputAction = flutterInputConfiguration['inputAction'],
obscureText = flutterInputConfiguration['obscureText'];
obscureText = flutterInputConfiguration['obscureText'],
autocorrect = flutterInputConfiguration['autocorrect'];
/// The type of information being edited in the input control.
final EngineInputType inputType;
@@ -195,6 +197,15 @@ class InputConfiguration {
/// Whether to hide the text being edited.
final bool obscureText;
/// Whether to enable autocorrection.
///
/// Definition of autocorrect can be found in:
/// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input
///
/// For future manual tests, note that autocorrect is an attribute only
/// supported by Safari.
final bool autocorrect;
}
typedef _OnChangeCallback = void Function(EditingState editingState);
@@ -372,6 +383,10 @@ class TextEditingElement {
if (inputConfig.obscureText) {
domElement.setAttribute('type', 'password');
}
final String autocorrectValue = inputConfig.autocorrect ? 'on' : 'off';
domElement.setAttribute('autocorrect', autocorrectValue);
_setStaticStyleAttributes(domElement);
owner._setDynamicStyleAttributes(domElement);
domRenderer.glassPaneElement.append(domElement);

View File

@@ -26,6 +26,7 @@ final InputConfiguration singlelineConfig = InputConfiguration(
inputType: EngineInputType.text,
obscureText: false,
inputAction: 'TextInputAction.done',
autocorrect: true,
);
final Map<String, dynamic> flutterSinglelineConfig =
createFlutterConfig('text');
@@ -34,6 +35,7 @@ final InputConfiguration multilineConfig = InputConfiguration(
inputType: EngineInputType.multiline,
obscureText: false,
inputAction: 'TextInputAction.newline',
autocorrect: true,
);
final Map<String, dynamic> flutterMultilineConfig =
createFlutterConfig('multiline');
@@ -105,6 +107,7 @@ void main() {
inputType: EngineInputType.text,
inputAction: 'TextInputAction.done',
obscureText: true,
autocorrect: true,
);
editingElement.enable(
config,
@@ -119,6 +122,46 @@ void main() {
editingElement.disable();
});
test('Knows to turn autocorrect off', () {
final InputConfiguration config = InputConfiguration(
inputType: EngineInputType.text,
inputAction: 'TextInputAction.done',
obscureText: false,
autocorrect: false,
);
editingElement.enable(
config,
onChange: trackEditingState,
onAction: trackInputAction,
);
expect(document.getElementsByTagName('input'), hasLength(1));
final InputElement input = document.getElementsByTagName('input')[0];
expect(editingElement.domElement, input);
expect(input.getAttribute('autocorrect'), 'off');
editingElement.disable();
});
test('Knows to turn autocorrect on', () {
final InputConfiguration config = InputConfiguration(
inputType: EngineInputType.text,
inputAction: 'TextInputAction.done',
obscureText: false,
autocorrect: true,
);
editingElement.enable(
config,
onChange: trackEditingState,
onAction: trackInputAction,
);
expect(document.getElementsByTagName('input'), hasLength(1));
final InputElement input = document.getElementsByTagName('input')[0];
expect(editingElement.domElement, input);
expect(input.getAttribute('autocorrect'), 'on');
editingElement.disable();
});
test('Can read editing state correctly', () {
editingElement.enable(
singlelineConfig,
@@ -263,6 +306,7 @@ void main() {
inputType: EngineInputType.text,
obscureText: false,
inputAction: 'TextInputAction.done',
autocorrect: true,
);
editingElement.enable(
config,
@@ -286,6 +330,7 @@ void main() {
inputType: EngineInputType.multiline,
obscureText: false,
inputAction: 'TextInputAction.done',
autocorrect: true,
);
editingElement.enable(
config,
@@ -1129,6 +1174,7 @@ class PlatformMessagesSpy {
Map<String, dynamic> createFlutterConfig(
String inputType, {
bool obscureText = false,
bool autocorrect = true,
String inputAction,
}) {
return <String, dynamic>{
@@ -1136,6 +1182,7 @@ Map<String, dynamic> createFlutterConfig(
'name': 'TextInputType.$inputType',
},
'obscureText': obscureText,
'autocorrect': autocorrect,
'inputAction': inputAction ?? 'TextInputAction.done',
};
}