From c0d884ddb2ebbe6fcfb41f72dbf655b634f6561b Mon Sep 17 00:00:00 2001 From: Kenzie Raditya Tirtarahardja Date: Tue, 12 Aug 2025 14:33:15 +0800 Subject: [PATCH] script: Implement `getModifierState` for mouse event (#38535) Implement missing idl function `getModifierState` for mouse event. The implementation is exactly the same as in keyboard event https://w3c.github.io/uievents/#dom-keyboardevent-getmodifierstate. https://github.com/servo/servo/blob/6651f37c05138112cecf33d59de2709b715841f1/components/script/dom/keyboardevent.rs#L267-L283 Testing: Fix `./tests/wpt/tests/infrastructure/testdriver/actions/actionsWithKeyPressed.html` --------- Signed-off-by: PotatoCP --- components/script/dom/mouseevent.rs | 19 +++++++++++++++++++ .../script_bindings/webidls/MouseEvent.webidl | 2 +- .../meta/uievents/idlharness.window.js.ini | 15 --------------- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/components/script/dom/mouseevent.rs b/components/script/dom/mouseevent.rs index aff9c4118f7..fe88f0b3356 100644 --- a/components/script/dom/mouseevent.rs +++ b/components/script/dom/mouseevent.rs @@ -527,4 +527,23 @@ impl MouseEventMethods for MouseEvent { fn IsTrusted(&self) -> bool { self.uievent.IsTrusted() } + + /// + fn GetModifierState(&self, key_arg: DOMString) -> bool { + self.modifiers.get().contains(match &*key_arg { + "Alt" => Modifiers::ALT, + "AltGraph" => Modifiers::ALT_GRAPH, + "CapsLock" => Modifiers::CAPS_LOCK, + "Control" => Modifiers::CONTROL, + "Fn" => Modifiers::FN, + "FnLock" => Modifiers::FN_LOCK, + "Meta" => Modifiers::META, + "NumLock" => Modifiers::NUM_LOCK, + "ScrollLock" => Modifiers::SCROLL_LOCK, + "Shift" => Modifiers::SHIFT, + "Symbol" => Modifiers::SYMBOL, + "SymbolLock" => Modifiers::SYMBOL_LOCK, + _ => return false, + }) + } } diff --git a/components/script_bindings/webidls/MouseEvent.webidl b/components/script_bindings/webidls/MouseEvent.webidl index 0a3c5c015fd..9726ea14506 100644 --- a/components/script_bindings/webidls/MouseEvent.webidl +++ b/components/script_bindings/webidls/MouseEvent.webidl @@ -24,7 +24,7 @@ interface MouseEvent : UIEvent { readonly attribute EventTarget? relatedTarget; // Introduced in DOM Level 3 readonly attribute unsigned short buttons; - //boolean getModifierState (DOMString keyArg); + boolean getModifierState (DOMString keyArg); [Pref="dom_mouse_event_which_enabled"] readonly attribute long which; diff --git a/tests/wpt/meta/uievents/idlharness.window.js.ini b/tests/wpt/meta/uievents/idlharness.window.js.ini index 55e71ccaf64..4a777dd66a7 100644 --- a/tests/wpt/meta/uievents/idlharness.window.js.ini +++ b/tests/wpt/meta/uievents/idlharness.window.js.ini @@ -11,24 +11,9 @@ [UIEvent interface: new FocusEvent("event") must inherit property "which" with the proper type] expected: FAIL - [MouseEvent interface: operation getModifierState(DOMString)] - expected: FAIL - [MouseEvent interface: operation initMouseEvent(DOMString, optional boolean, optional boolean, optional Window?, optional long, optional long, optional long, optional long, optional long, optional boolean, optional boolean, optional boolean, optional boolean, optional short, optional EventTarget?)] expected: FAIL - [MouseEvent interface: new MouseEvent("event") must inherit property "getModifierState(DOMString)" with the proper type] - expected: FAIL - - [MouseEvent interface: calling getModifierState(DOMString) on new MouseEvent("event") with too few arguments must throw TypeError] - expected: FAIL - - [MouseEvent interface: new WheelEvent("event") must inherit property "getModifierState(DOMString)" with the proper type] - expected: FAIL - - [MouseEvent interface: calling getModifierState(DOMString) on new WheelEvent("event") with too few arguments must throw TypeError] - expected: FAIL - [InputEvent interface: attribute inputType] expected: FAIL