From 4ea363277eecc8f24326d8a906cf44c2c06baa8c Mon Sep 17 00:00:00 2001 From: atbrakhi Date: Wed, 1 Oct 2025 16:45:33 +0200 Subject: [PATCH] script: Support `keyCode`, `charCode` in KeyboardEvent constructor (#39590) Support `keyCode` and `charCode` fields in KeyboardEventInit for Speedometer 3.0 Speedometer 3.0 fails because Servo's KeyboardEvent constructor ignores keyCode and charCode parameters, hardcoding them to 0. This breaks frameworks that check `event.keyCode === 13` for Enter key detection. This is how [Speedometer 3.0 dispatches key events](https://github.com/WebKit/Speedometer/blob/8d67f28d0281ac4330f283495b7f48286654ad7d/resources/benchmark-runner.mjs#L166) vs [Speedometer 2.0 triggerEnter](https://github.com/WebKit/Speedometer/blob/491acc2d64307e655fbeded8c871ef0d07e21997/resources/tests.mjs#L60). Speedometer 3.0 uses the modern KeyboardEvent constructor but passes [legacy fields like keyCode and charCode](https://w3c.github.io/uievents/#legacy-dictionary-KeyboardEventInit) in the init dictionary for backwards compatibility with older frameworks(for example: backbone.js) This change uncomments the legacy KeyboardEventInit fields and updates the constructor to read them from the init dictionary instead of hardcoding to 0. Testing: No new tests added. Fixes: part of https://github.com/servo/servo/issues/16719 Servo running Speedometer3.0 successfully speedometer 3 0 --------- Signed-off-by: atbrakhi --- components/script/dom/keyboardevent.rs | 4 +-- .../webidls/KeyboardEvent.webidl | 4 +-- tests/wpt/meta/MANIFEST.json | 7 +++++ .../keyboard/keyboardevent-legacy.html | 29 +++++++++++++++++++ 4 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 tests/wpt/tests/uievents/keyboard/keyboardevent-legacy.html diff --git a/components/script/dom/keyboardevent.rs b/components/script/dom/keyboardevent.rs index 526122ef039..9fac1656b35 100644 --- a/components/script/dom/keyboardevent.rs +++ b/components/script/dom/keyboardevent.rs @@ -186,8 +186,8 @@ impl KeyboardEventMethods for KeyboardEvent { init.repeat, init.isComposing, modifiers, - 0, - 0, + init.charCode, + init.keyCode, can_gc, ); *event.key.borrow_mut() = init.key.clone(); diff --git a/components/script_bindings/webidls/KeyboardEvent.webidl b/components/script_bindings/webidls/KeyboardEvent.webidl index 9bddcb03a56..f3842e5a916 100644 --- a/components/script_bindings/webidls/KeyboardEvent.webidl +++ b/components/script_bindings/webidls/KeyboardEvent.webidl @@ -53,8 +53,8 @@ dictionary KeyboardEventInit : EventModifierInit { }; // https://w3c.github.io/uievents/#legacy-dictionary-KeyboardEventInit -/*partial dictionary KeyboardEventInit { +partial dictionary KeyboardEventInit { unsigned long charCode = 0; unsigned long keyCode = 0; unsigned long which = 0; -};*/ +}; diff --git a/tests/wpt/meta/MANIFEST.json b/tests/wpt/meta/MANIFEST.json index 188ee94a68e..fe5e2a170a8 100644 --- a/tests/wpt/meta/MANIFEST.json +++ b/tests/wpt/meta/MANIFEST.json @@ -841982,6 +841982,13 @@ ] }, "keyboard": { + "keyboardevent-legacy.html": [ + "df1a80893c5c24cd42321c717add74ea87b64a33", + [ + null, + {} + ] + ], "modifier-keys-combinations.html": [ "1b364ff72ce539b31ef86f4a1bcf75aed6868845", [ diff --git a/tests/wpt/tests/uievents/keyboard/keyboardevent-legacy.html b/tests/wpt/tests/uievents/keyboard/keyboardevent-legacy.html new file mode 100644 index 00000000000..df1a80893c5 --- /dev/null +++ b/tests/wpt/tests/uievents/keyboard/keyboardevent-legacy.html @@ -0,0 +1,29 @@ + + +KeyboardEvent legacy fields initialization Test: KeyCode and charCode + + + + + + + +
+ +