[Web] Ignore invalid keyboard events related to autofill (flutter/engine#50590)

## Description

This PR fixes an 'Unexpected null value' error related to autoFill.
When using Autofill feature, Chrome emits keyboard events whose `event.code` and `event.key` are null.

## Related Issue

Fixes https://github.com/flutter/flutter/issues/114620.

## Tests

Adds 1 test.
This commit is contained in:
Bruno Leroux 2024-02-16 08:04:48 +01:00 committed by GitHub
parent ea6b80144d
commit 4b5c993fe4
2 changed files with 22 additions and 2 deletions

View File

@ -586,6 +586,11 @@ class KeyboardConverter {
// * Some key data might be synthesized to update states after the main key
// data. They are always scheduled asynchronously with results discarded.
void handleEvent(FlutterHtmlKeyboardEvent event) {
// Autofill on Chrome sends keyboard events whose key and code are null.
if (event.key == null || event.code == null) {
return;
}
assert(_dispatchKeyData == null);
bool sentAnyEvents = false;
_dispatchKeyData = (ui.KeyData data) {

View File

@ -1142,6 +1142,21 @@ void testMain() {
);
keyDataList.clear();
});
test('Ignore DOM event when event.key is null', () {
// Regression test for https://github.com/flutter/flutter/issues/114620.
final List<ui.KeyData> keyDataList = <ui.KeyData>[];
final KeyboardConverter converter = KeyboardConverter((ui.KeyData key) {
keyDataList.add(key);
return true;
}, OperatingSystem.linux);
converter.handleEvent(keyDownEvent(null, null));
converter.handleEvent(keyUpEvent(null, null));
// Invalid key events are ignored.
expect(keyDataList, isEmpty);
});
}
// Flags used for the `modifiers` argument of `key***Event` functions.
@ -1153,7 +1168,7 @@ const int kMeta = 0x8;
// Utility functions to make code more concise.
//
// To add timeStamp , use syntax `..timeStamp = `.
MockKeyboardEvent keyDownEvent(String code, String key, [int modifiers = 0, int location = 0]) {
MockKeyboardEvent keyDownEvent(String? code, String? key, [int modifiers = 0, int location = 0]) {
return MockKeyboardEvent(
type: 'keydown',
code: code,
@ -1166,7 +1181,7 @@ MockKeyboardEvent keyDownEvent(String code, String key, [int modifiers = 0, int
);
}
MockKeyboardEvent keyUpEvent(String code, String key, [int modifiers = 0, int location = 0]) {
MockKeyboardEvent keyUpEvent(String? code, String? key, [int modifiers = 0, int location = 0]) {
return MockKeyboardEvent(
type: 'keyup',
code: code,