LibWeb: Support non-required numpad code names

These aren't required to comply with the UIEvents spec, but they are
required by WebDriver.
This commit is contained in:
Timothy Flynn 2024-10-09 18:47:44 -04:00 committed by Andreas Kling
parent a11e5055c7
commit 5b2633d90f
Notes: github-actions[bot] 2024-10-10 08:42:15 +00:00
6 changed files with 85 additions and 9 deletions

View file

@ -0,0 +1,20 @@
key="0" code=Numpad0
key="1" code=Numpad1
key="2" code=Numpad2
key="3" code=Numpad3
key="4" code=Numpad4
key="5" code=Numpad5
key="6" code=Numpad6
key="7" code=Numpad7
key="8" code=Numpad8
key="9" code=Numpad9
key="+" code=NumpadAdd
key="," code=NumpadComma
key="." code=NumpadDecimal
key="/" code=NumpadDivide
key="*" code=NumpadMultiply
key="-" code=NumpadSubtract
key="=" code=NumpadEqual
key="#" code=NumpadHash
key="(" code=NumpadParenLeft
key=")" code=NumpadParenRight

View file

@ -0,0 +1,38 @@
<input id="input" />
<script src="../include.js"></script>
<script>
const NUMPAD_KEYS = [
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"+",
",",
".",
"/",
"*",
"-",
"=",
"#",
"(",
")",
];
test(() => {
let input = document.getElementById("input");
input.addEventListener("keydown", e => {
println(`key="${e.key}" code=${e.code}`);
});
NUMPAD_KEYS.forEach(key => {
internals.sendText(input, key, internals.MOD_KEYPAD);
});
});
</script>

View file

@ -74,21 +74,21 @@ JS::Object* Internals::hit_test(double x, double y)
return nullptr;
}
void Internals::send_text(HTML::HTMLElement& target, String const& text)
void Internals::send_text(HTML::HTMLElement& target, String const& text, WebIDL::UnsignedShort modifiers)
{
auto& page = internals_page();
target.focus();
for (auto code_point : text.code_points())
page.handle_keydown(UIEvents::code_point_to_key_code(code_point), 0, code_point);
page.handle_keydown(UIEvents::code_point_to_key_code(code_point), modifiers, code_point);
}
void Internals::send_key(HTML::HTMLElement& target, String const& key_name)
void Internals::send_key(HTML::HTMLElement& target, String const& key_name, WebIDL::UnsignedShort modifiers)
{
auto key_code = UIEvents::key_code_from_string(key_name);
target.focus();
internals_page().handle_keydown(key_code, 0, 0);
internals_page().handle_keydown(key_code, modifiers, 0);
}
void Internals::commit_text()

View file

@ -9,6 +9,7 @@
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/Internals/InternalAnimationTimeline.h>
#include <LibWeb/UIEvents/MouseButton.h>
#include <LibWeb/WebIDL/Types.h>
namespace Web::Internals {
@ -24,8 +25,8 @@ public:
void gc();
JS::Object* hit_test(double x, double y);
void send_text(HTML::HTMLElement&, String const&);
void send_key(HTML::HTMLElement&, String const&);
void send_text(HTML::HTMLElement&, String const&, WebIDL::UnsignedShort modifiers);
void send_key(HTML::HTMLElement&, String const&, WebIDL::UnsignedShort modifiers);
void commit_text();
void click(double x, double y);

View file

@ -9,8 +9,15 @@ interface Internals {
undefined gc();
object hitTest(double x, double y);
undefined sendText(HTMLElement target, DOMString text);
undefined sendKey(HTMLElement target, DOMString keyName);
const unsigned short MOD_NONE = 0;
const unsigned short MOD_ALT = 1;
const unsigned short MOD_CTRL = 2;
const unsigned short MOD_SHIFT = 4;
const unsigned short MOD_SUPER = 8;
const unsigned short MOD_KEYPAD = 16;
undefined sendText(HTMLElement target, DOMString text, optional unsigned short modifiers = 0);
undefined sendKey(HTMLElement target, DOMString keyName, optional unsigned short modifiers = 0);
undefined commitText();
undefined click(double x, double y);

View file

@ -326,6 +326,8 @@ static ErrorOr<String> get_event_code(KeyCode platform_key, unsigned modifiers)
return "Numpad9"_string;
case KeyCode::Key_Plus:
return "NumpadAdd"_string;
case KeyCode::Key_Comma:
return "NumpadComma"_string;
case KeyCode::Key_Period:
case KeyCode::Key_Delete:
return "NumpadDecimal"_string;
@ -334,9 +336,17 @@ static ErrorOr<String> get_event_code(KeyCode platform_key, unsigned modifiers)
case KeyCode::Key_Return:
return "NumpadEnter"_string;
case KeyCode::Key_Asterisk:
return "NumpadAsterisk"_string;
return "NumpadMultiply"_string;
case KeyCode::Key_Minus:
return "NumpadSubtract"_string;
case KeyCode::Key_Equal:
return "NumpadEqual"_string;
case KeyCode::Key_Hashtag:
return "NumpadHash"_string;
case KeyCode::Key_LeftParen:
return "NumpadParenLeft"_string;
case KeyCode::Key_RightParen:
return "NumpadParenRight"_string;
default:
break;
}