From 4b5c993fe4d7cedca6f4a21b2ce18a9bcac722db Mon Sep 17 00:00:00 2001 From: Bruno Leroux Date: Fri, 16 Feb 2024 08:04:48 +0100 Subject: [PATCH] [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. --- .../lib/src/engine/keyboard_binding.dart | 5 +++++ .../test/engine/keyboard_converter_test.dart | 19 +++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/keyboard_binding.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/keyboard_binding.dart index 3c10d09a481..4567f414fb6 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/keyboard_binding.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/keyboard_binding.dart @@ -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) { diff --git a/engine/src/flutter/lib/web_ui/test/engine/keyboard_converter_test.dart b/engine/src/flutter/lib/web_ui/test/engine/keyboard_converter_test.dart index d381393fa89..dab4914f64f 100644 --- a/engine/src/flutter/lib/web_ui/test/engine/keyboard_converter_test.dart +++ b/engine/src/flutter/lib/web_ui/test/engine/keyboard_converter_test.dart @@ -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 keyDataList = []; + 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,