Skip to content

Truly independent web browser

LibWeb: Activate controls from trusted keyboard input

Andreas Kling7ed50403c1ad
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index 578994f0736..287705abae5 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -4156,6 +4156,8 @@ void Document::set_focused_area(GC::Ptr<Node> node, InvalidateFocusPseudoClasses
return;
page().invalidate_compositor_keyboard_scroll_state_for_document(*this);
+ if (auto navigable = this->navigable())
+ navigable->event_handler().cancel_keyboard_activation();
if (node)
m_sequential_focus_navigation_starting_point = nullptr;
diff --git a/Libraries/LibWeb/HTML/HTMLElement.cpp b/Libraries/LibWeb/HTML/HTMLElement.cpp
index 6f9fd46b56a..bcc3a4845e0 100644
--- a/Libraries/LibWeb/HTML/HTMLElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLElement.cpp
@@ -1209,7 +1209,7 @@ void HTMLElement::inserted()
}
// https://html.spec.whatwg.org/multipage/webappapis.html#fire-a-synthetic-pointer-event
-bool HTMLElement::fire_a_synthetic_pointer_event(Utf16FlyString const& type, DOM::Element& target, bool not_trusted)
+bool HTMLElement::fire_a_synthetic_pointer_event(Utf16FlyString const& type, DOM::Element& target, NotTrusted not_trusted, u32 modifiers)
{
// 1. Let event be the result of creating an event using PointerEvent.
// 2. Initialize event's type attribute to e.
@@ -1219,7 +1219,14 @@ bool HTMLElement::fire_a_synthetic_pointer_event(Utf16FlyString const& type, DOM
// https://w3c.github.io/pointerevents/#click-auxclick-and-contextmenu
UIEvents::PointerEventOptions options;
options.pointer_id = -1;
- auto event = UIEvents::PointerEvent::create(event_name, move(options), 0, 0, 0, 0, HighResolutionTime::current_high_resolution_time(HTML::relevant_global_object(*this)));
+ options.ctrl_key = modifiers & UIEvents::Mod_Ctrl;
+ options.shift_key = modifiers & UIEvents::Mod_Shift;
+ options.alt_key = modifiers & UIEvents::Mod_Alt;
+ options.meta_key = modifiers & UIEvents::Mod_Super;
+ if (auto browsing_context = target.document().browsing_context())
+ options.view = browsing_context->window_proxy();
+ auto event = UIEvents::PointerEvent::create(event_name, move(options), 0, 0, 0, 0, HighResolutionTime::current_high_resolution_time(HTML::relevant_global_object(target)));
+ event->set_is_trusted(true);
// 3. Initialize event's bubbles and cancelable attributes to true.
event->set_bubbles(true);
@@ -1229,14 +1236,15 @@ bool HTMLElement::fire_a_synthetic_pointer_event(Utf16FlyString const& type, DOM
event->set_composed(true);
// 5. If the not trusted flag is set, initialize event's isTrusted attribute to false.
- if (not_trusted) {
+ if (not_trusted == NotTrusted::Yes) {
event->set_is_trusted(false);
}
- // FIXME: 6. Initialize event's ctrlKey, shiftKey, altKey, and metaKey attributes according to the current state
- // of the key input device, if any (false for any keys that are not available).
+ // 6. Initialize event's ctrlKey, shiftKey, altKey, and metaKey attributes according to the current state
+ // of the key input device, if any (false for any keys that are not available).
- // FIXME: 7. Initialize event's view attribute to target's node document's Window object, if any, and null otherwise.
+ // 7. Initialize event's view attribute to target's node document's Window object, if any, and null otherwise.
+ // NB: These attributes are initialized through options when creating the event above.
// FIXME: 8. event's getModifierState() method is to return values appropriately describing the current state of the key input device.
@@ -1337,7 +1345,7 @@ void HTMLElement::click()
m_click_in_progress = true;
// 4. Fire a synthetic pointer event named click at this element, with the not trusted flag set.
- fire_a_synthetic_pointer_event(HTML::EventNames::click, *this, true);
+ fire_a_synthetic_pointer_event(HTML::EventNames::click, *this, NotTrusted::Yes);
// 5. Unset this element's click in progress flag.
m_click_in_progress = false;
diff --git a/Libraries/LibWeb/HTML/HTMLElement.h b/Libraries/LibWeb/HTML/HTMLElement.h
index c84c4ffee8f..75ab42e0f5c 100644
--- a/Libraries/LibWeb/HTML/HTMLElement.h
+++ b/Libraries/LibWeb/HTML/HTMLElement.h
@@ -137,7 +137,11 @@ public:
bool autocorrect() const;
void set_autocorrect(bool);
- bool fire_a_synthetic_pointer_event(Utf16FlyString const& type, DOM::Element& target, bool not_trusted);
+ enum class NotTrusted {
+ No,
+ Yes,
+ };
+ static bool fire_a_synthetic_pointer_event(Utf16FlyString const& type, DOM::Element& target, NotTrusted not_trusted, u32 modifiers = 0);
// https://html.spec.whatwg.org/multipage/forms.html#category-label
virtual bool is_labelable() const { return is_form_associated_custom_element(); }
diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cpp
index c7a8669edf0..b8cbde1930c 100644
--- a/Libraries/LibWeb/Page/EventHandler.cpp
+++ b/Libraries/LibWeb/Page/EventHandler.cpp
@@ -1659,7 +1659,7 @@ static constexpr bool is_enter_key_or_interoperable_enter_key_combo(UIEvents::Ke
{
if (key != UIEvents::KeyCode::Key_Return)
return false;
- if (!modifiers)
+ if (!(modifiers & ~UIEvents::Mod_Keypad))
return true;
if (modifiers & (UIEvents::KeyModifier::Mod_Shift | UIEvents::KeyModifier::Mod_Ctrl))
return true;
@@ -2352,7 +2352,7 @@ EventResult EventHandler::handle_keydown(UIEvents::KeyCode key, u32 modifiers, u
case UIEvents::KeyCode::Key_Space: {
if ((modifiers_without_keypad & ~UIEvents::KeyModifier::Mod_Shift) != UIEvents::KeyModifier::Mod_None)
break;
- // FIXME: These elements must run their activation behavior instead of merely swallowing the key.
+ // NB: Keyboard activation is handled after dispatching the key events.
if (focused_area_activates_on_space(document->focused_area().ptr()))
break;
bool scroll_backward = (modifiers_without_keypad & UIEvents::KeyModifier::Mod_Shift) != UIEvents::KeyModifier::Mod_None;
@@ -3003,6 +3003,115 @@ static GC::RootVector<GC::Ref<DOM::StaticRange>> target_ranges_for_input_event(D
return target_ranges;
}
+static GC::Ptr<DOM::Element> keyboard_activation_target(DOM::Node& target, UIEvents::KeyCode key)
+{
+ auto* element = as_if<DOM::Element>(target);
+ // INTEROP: Like WebKit and Gecko, only activate controls that remain focused after their key event handlers run.
+ if (!element || element->document().focused_area().ptr() != element || !element->is_connected() || !element->is_focusable())
+ return nullptr;
+ if (element->find_in_shadow_including_ancestry([](auto const& ancestor) { return ancestor.is_inert(); }))
+ return nullptr;
+
+ if (auto* button = as_if<HTML::HTMLButtonElement>(*element))
+ return button->enabled() ? element : nullptr;
+
+ if (auto* input = as_if<HTML::HTMLInputElement>(*element)) {
+ if (!input->enabled())
+ return nullptr;
+ using enum HTML::HTMLInputElement::TypeAttributeState;
+ switch (input->type_state()) {
+ case Button:
+ case SubmitButton:
+ case ResetButton:
+ case ImageButton:
+ case FileUpload:
+ case Color:
+ return element;
+ case Checkbox:
+ case RadioButton:
+ return key == UIEvents::Key_Space ? element : nullptr;
+ default:
+ return nullptr;
+ }
+ }
+
+ if (auto* summary = as_if<HTML::HTMLSummaryElement>(*element))
+ return summary->is_summary_for_its_parent_details() ? element : nullptr;
+
+ if (key == UIEvents::Key_Return && element->created_hyperlink())
+ return element;
+
+ return nullptr;
+}
+
+void EventHandler::cancel_keyboard_activation()
+{
+ if (m_keyboard_activation_target)
+ m_keyboard_activation_target->set_being_activated(false);
+ m_keyboard_activation_target = nullptr;
+}
+
+// https://html.spec.whatwg.org/multipage/interaction.html#activation-behavior
+// When the user triggers an element with a defined activation behavior in a manner other than clicking it,
+// the default action of the interaction event must be to fire a click event at the element.
+EventResult EventHandler::handle_keyboard_activation(Utf16FlyString const& event_name, UIEvents::KeyCode key, u32 modifiers, DOM::Node& target, bool cancelled, bool repeat)
+{
+ if (key != UIEvents::Key_Return && key != UIEvents::Key_Space)
+ return cancelled ? EventResult::Cancelled : EventResult::Accepted;
+
+ if (key == UIEvents::Key_Return && event_name == UIEvents::EventNames::keyup)
+ return cancelled ? EventResult::Cancelled : EventResult::Accepted;
+
+ if (key == UIEvents::Key_Space && event_name == UIEvents::EventNames::keypress) {
+ if (cancelled)
+ return EventResult::Cancelled;
+ // NB: An activation keypress must not also insert text into an input button or checkbox.
+ return m_keyboard_activation_target ? EventResult::Handled : EventResult::Accepted;
+ }
+
+ if (key == UIEvents::Key_Space && event_name == UIEvents::EventNames::keyup) {
+ GC::Ptr<DOM::Element> activation_target = m_keyboard_activation_target.ptr();
+ cancel_keyboard_activation();
+ if (!cancelled && target.document().is_fully_active() && m_navigable->active_document().ptr() == &target.document() && activation_target && activation_target.ptr() == keyboard_activation_target(target, key).ptr()) {
+ HTML::HTMLElement::fire_a_synthetic_pointer_event(HTML::EventNames::click, *activation_target, HTML::HTMLElement::NotTrusted::No, modifiers);
+ return EventResult::Handled;
+ }
+ return cancelled ? EventResult::Cancelled : EventResult::Accepted;
+ }
+
+ if (key == UIEvents::Key_Space && event_name == UIEvents::EventNames::keydown && !repeat)
+ cancel_keyboard_activation();
+
+ if (cancelled)
+ return EventResult::Cancelled;
+
+ if (!target.document().is_fully_active() || m_navigable->active_document().ptr() != &target.document())
+ return EventResult::Dropped;
+
+ auto activation_target = keyboard_activation_target(target, key);
+ if (!activation_target)
+ return EventResult::Accepted;
+
+ // INTEROP: Buttons, checkboxes, radios, and summaries activate on Space release. Remember the press so that a
+ // canceled keydown or a focus change cannot cause a click on release, and key repeat only clicks once.
+ if (key == UIEvents::Key_Space && event_name == UIEvents::EventNames::keydown) {
+ m_keyboard_activation_target = activation_target;
+ activation_target->set_being_activated(true);
+ }
+
+ // INTEROP: Like Blink and WebKit, hyperlinks activate on Enter keydown. Other controls activate on keypress,
+ // allowing either keydown or keypress to cancel their click.
+ if (key == UIEvents::Key_Return) {
+ auto const& activation_event_name = activation_target->created_hyperlink() ? UIEvents::EventNames::keydown : UIEvents::EventNames::keypress;
+ if (event_name != activation_event_name)
+ return EventResult::Accepted;
+ HTML::HTMLElement::fire_a_synthetic_pointer_event(HTML::EventNames::click, *activation_target, HTML::HTMLElement::NotTrusted::No, modifiers);
+ return EventResult::Handled;
+ }
+
+ return EventResult::Accepted;
+}
+
EventResult EventHandler::fire_keyboard_event(Utf16FlyString const& event_name, HTML::LocalNavigable& navigable, UIEvents::KeyCode key, u32 modifiers, u32 code_point, bool repeat, Optional<u32> key_code)
{
GC::Ptr<DOM::Document> document = navigable.active_document();
@@ -3026,7 +3135,8 @@ EventResult EventHandler::fire_keyboard_event(Utf16FlyString const& event_name,
auto event = UIEvents::KeyboardEvent::create_from_platform_event(HTML::relevant_global_object(*document), event_name, key, modifiers, code_point, repeat);
if (key_code.has_value())
event->set_key_code(*key_code);
- return focused_area->dispatch_event(event) ? EventResult::Accepted : EventResult::Cancelled;
+ auto cancelled = !focused_area->dispatch_event(event);
+ return navigable.event_handler().handle_keyboard_activation(event_name, key, modifiers, *focused_area, cancelled, repeat);
}
// FIXME: De-duplicate this. This is just to prevent wasting a KeyboardEvent allocation when recursing into an (i)frame.
@@ -3035,7 +3145,8 @@ EventResult EventHandler::fire_keyboard_event(Utf16FlyString const& event_name,
event->set_key_code(*key_code);
GC::Ptr target = document->body() ?: &document->root();
- return target->dispatch_event(event) ? EventResult::Accepted : EventResult::Cancelled;
+ auto cancelled = !target->dispatch_event(event);
+ return navigable.event_handler().handle_keyboard_activation(event_name, key, modifiers, *target, cancelled, repeat);
}
// https://w3c.github.io/uievents/#event-type-textInput
diff --git a/Libraries/LibWeb/Page/EventHandler.h b/Libraries/LibWeb/Page/EventHandler.h
index a23bb95fc97..9aa9e45db82 100644
--- a/Libraries/LibWeb/Page/EventHandler.h
+++ b/Libraries/LibWeb/Page/EventHandler.h
@@ -109,6 +109,7 @@ public:
};
KeyboardScrollSnapshot keyboard_scroll_snapshot() const;
EventResult handle_keyup(UIEvents::KeyCode, unsigned modifiers, u32 code_point, bool repeat);
+ void cancel_keyboard_activation();
EventResult handle_drag_and_drop_event(DragEvent::Type, CSSPixelPoint, CSSPixelPoint screen_position, unsigned button, unsigned buttons, unsigned modifiers, Vector<HTML::SelectedFile> files);
EventResult handle_pinch_event(CSSPixelPoint, PinchEvent const&, Optional<RemoteInputEventTarget>* remote_target);
@@ -157,6 +158,7 @@ private:
int page_scroll_distance_for_key_input() const;
EventResult fire_keyboard_event(Utf16FlyString const& event_name, HTML::LocalNavigable&, UIEvents::KeyCode, unsigned modifiers, u32 code_point, bool repeat, Optional<u32> key_code = {});
+ EventResult handle_keyboard_activation(Utf16FlyString const& event_name, UIEvents::KeyCode, u32 modifiers, DOM::Node& target, bool cancelled, bool repeat);
[[nodiscard]] EventResult fire_text_input_event(HTML::LocalNavigable&, Utf16String const& data);
[[nodiscard]] EventResult input_event(Utf16FlyString const& event_name, Utf16FlyString const& input_type, HTML::LocalNavigable&, Variant<u32, Utf16String> code_point_or_string, GC::Ptr<DOM::StaticRange> deletion_range = nullptr);
@@ -354,6 +356,7 @@ private:
OwnPtr<MiddleButtonScrollHandler> m_middle_button_scroll_handler;
NonnullOwnPtr<DragAndDropEventHandler> m_drag_and_drop_event_handler;
+ GC::Weak<DOM::Element> m_keyboard_activation_target;
Optional<UIEvents::KeyCode> m_held_scroll_key;
OwnPtr<HTML::UserScrollGestureHold> m_scroll_key_gesture_hold;
diff --git a/Tests/LibWeb/Text/expected/UIEvents/KeyEvent-keypress.txt b/Tests/LibWeb/Text/expected/UIEvents/KeyEvent-keypress.txt
index 39e67f6a74a..21095a1ed00 100644
--- a/Tests/LibWeb/Text/expected/UIEvents/KeyEvent-keypress.txt
+++ b/Tests/LibWeb/Text/expected/UIEvents/KeyEvent-keypress.txt
@@ -12,5 +12,6 @@ keydown key=Enter charCode=0 modifiers=Shift
keypress key=Enter charCode=0 modifiers=Shift
keydown key=Enter charCode=0 modifiers=Meta
keydown key=Enter charCode=0
+keypress key=Enter charCode=0
keydown key=c charCode=0 modifiers=Control
keydown key=v charCode=0 modifiers=Meta
diff --git a/Tests/LibWeb/Text/expected/UIEvents/keyboard-activation-defaults.txt b/Tests/LibWeb/Text/expected/UIEvents/keyboard-activation-defaults.txt
new file mode 100644
index 00000000000..94834fa1026
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/UIEvents/keyboard-activation-defaults.txt
@@ -0,0 +1,8 @@
+button Space: trusted clicks=[true]
+button Enter: trusted clicks=[true]
+inputbutton Space: trusted clicks=[true]
+inputbutton Enter: trusted clicks=[true]
+Enter focus change: active=inputbutton, pressed=false, clicks=0
+Space focus change: active=inputbutton, pressed=false, clicks=0
+Focused hyperlink descendant: clicks=0
+Hyperlink Enter: keydown, click trusted=true, keyup
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/the-button-element/button-activate-keyup-prevented.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/the-button-element/button-activate-keyup-prevented.txt
index f12aaa50599..4199c38f593 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/the-button-element/button-activate-keyup-prevented.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/the-button-element/button-activate-keyup-prevented.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail Button activation submits on keyup, but not if keydown is defaultPrevented
\ No newline at end of file
+1 Pass
+Pass Button activation submits on keyup, but not if keydown is defaultPrevented
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/interactive-elements/the-details-element/details-keyboard-activation.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/interactive-elements/the-details-element/details-keyboard-activation.txt
index fb8b4d4f9ac..7c1856a58ee 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/interactive-elements/the-details-element/details-keyboard-activation.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/interactive-elements/the-details-element/details-keyboard-activation.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail Details activation with space bar
\ No newline at end of file
+1 Pass
+Pass Details activation with space bar
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/popover-focus-3.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/popover-focus-3.txt
index 87cb5b5cad5..ff94aa6f495 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/popover-focus-3.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/popover-focus-3.txt
@@ -2,14 +2,13 @@ Harness status: OK
Found 9 tests
-3 Pass
-6 Fail
-Fail Circular reference tab navigation with popovertarget invocation
-Fail Circular reference tab navigation with command/commandfor invocation
-Fail Circular reference tab navigation with imperative invocation
-Fail Popover focus returns when popover is hidden by invoker with popovertarget invocation
-Fail Popover focus returns when popover is hidden by invoker with commandfor invocation
-Fail Popover focus returns when popover is hidden by invoker with imperative invocation
+9 Pass
+Pass Circular reference tab navigation with popovertarget invocation
+Pass Circular reference tab navigation with command/commandfor invocation
+Pass Circular reference tab navigation with imperative invocation
+Pass Popover focus returns when popover is hidden by invoker with popovertarget invocation
+Pass Popover focus returns when popover is hidden by invoker with commandfor invocation
+Pass Popover focus returns when popover is hidden by invoker with imperative invocation
Pass Popover focus only returns to invoker when focus is within the popover with popovertarget invocation
Pass Popover focus only returns to invoker when focus is within the popover with command/commandfor invocation
Pass Popover focus only returns to invoker when focus is within the popover with imperative invocation
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/uievents/interface/keyboard-click-event.txt b/Tests/LibWeb/Text/expected/wpt-import/uievents/interface/keyboard-click-event.txt
new file mode 100644
index 00000000000..e2bb432320d
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/uievents/interface/keyboard-click-event.txt
@@ -0,0 +1,9 @@
+Harness status: OK
+
+Found 4 tests
+
+4 Pass
+Pass Test that the Keyboard generated click does not fire pointer or mouse events for button when pressing space key
+Pass Test that the Keyboard generated click does not fire pointer or mouse events for inputbutton when pressing space key
+Pass Test that the Keyboard generated click does not fire pointer or mouse events for button when pressing enter key
+Pass Test that the Keyboard generated click does not fire pointer or mouse events for inputbutton when pressing enter key
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/UIEvents/keyboard-activation-defaults.html b/Tests/LibWeb/Text/input/UIEvents/keyboard-activation-defaults.html
new file mode 100644
index 00000000000..62d38d51daa
--- /dev/null
+++ b/Tests/LibWeb/Text/input/UIEvents/keyboard-activation-defaults.html
@@ -0,0 +1,67 @@
+<!doctype html>
+<script src="../include.js"></script>
+<button id="button">Button</button>
+<input id="inputbutton" type="button" value="Input button" />
+<script>
+ test(() => {
+ const button = document.getElementById("button");
+ const input = document.getElementById("inputbutton");
+ const keys = [
+ ["Space", " "],
+ ["Enter", "\uE006"],
+ ];
+
+ for (const element of [button, input]) {
+ for (const [name, key] of keys) {
+ const clicks = [];
+ const clicked = event => clicks.push(event.isTrusted);
+ element.addEventListener("click", clicked);
+ internals.sendText(element, key);
+ element.removeEventListener("click", clicked);
+ println(`${element.id} ${name}: trusted clicks=${JSON.stringify(clicks)}`);
+ }
+ }
+
+ for (const [name, key, eventType] of [
+ ["Enter", "\uE006", "keypress"],
+ ["Space", " ", "keydown"],
+ ]) {
+ let clicks = 0;
+ const moveFocus = () => input.focus();
+ const clicked = () => ++clicks;
+ button.addEventListener(eventType, moveFocus);
+ button.addEventListener("click", clicked);
+ internals.sendText(button, key);
+ button.removeEventListener(eventType, moveFocus);
+ button.removeEventListener("click", clicked);
+ println(
+ `${name} focus change: active=${document.activeElement.id}, pressed=${button.matches(":active")}, clicks=${clicks}`
+ );
+ }
+
+ const link = document.createElement("a");
+ link.href = "#keyboard-activation";
+ const child = document.createElement("span");
+ child.tabIndex = 0;
+ link.appendChild(child);
+ document.body.appendChild(link);
+ let descendantClicks = 0;
+ const countClick = event => {
+ ++descendantClicks;
+ event.preventDefault();
+ };
+ link.addEventListener("click", countClick);
+ internals.sendText(child, "\uE006");
+ link.removeEventListener("click", countClick);
+ println(`Focused hyperlink descendant: clicks=${descendantClicks}`);
+
+ const events = [];
+ for (const type of ["keydown", "keypress", "click", "keyup"])
+ link.addEventListener(type, event => {
+ events.push(type === "click" ? `click trusted=${event.isTrusted}` : type);
+ if (type === "click") event.preventDefault();
+ });
+ internals.sendText(link, "\uE006");
+ println(`Hyperlink Enter: ${events.join(", ")}`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/uievents/interface/keyboard-click-event.html b/Tests/LibWeb/Text/input/wpt-import/uievents/interface/keyboard-click-event.html
new file mode 100644
index 00000000000..a09a52f3e1f
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/uievents/interface/keyboard-click-event.html
@@ -0,0 +1,50 @@
+<script src="../../resources/testharness.js"></script>
+<script src="../../resources/testharnessreport.js"></script>
+<script src="../../resources/testdriver.js"></script>
+<script src="../../resources/testdriver-actions.js"></script>
+<script src="../../resources/testdriver-vendor.js"></script>
+
+<p>Tests that a button pressed by using Space or Enter will fire only the click event</p>
+<button id="button">Click Me by pressing Enter or Space</button>
+<input id="inputbutton" type="button" value="Click me by pressing Enter or Space">
+
+<script>
+let button = document.getElementById("button");
+let inputbutton = document.getElementById("inputbutton");
+let radiobutton = document.getElementById("radiobutton");
+let elementList = [button, inputbutton];
+let eventLog = [];
+const enterKey = '\uE006';
+const spaceKey = ' ';
+let keys = [spaceKey, enterKey];
+const eventList = ["pointerdown", "pointerup", "mousedown", "mouseup", "click"];
+elementList.forEach((el)=>{eventList.forEach((ev)=>el.addEventListener(ev, (e)=>{
+ eventLog.push(`${ev}_${el.id}`);
+ if(ev === "click" && currentTest){
+ currentTest.step(()=>{
+ if(e instanceof PointerEvent){
+ // We want the test to run on all browsers even if click is not a PointerEvent.
+ assert_equals(e.pointerId, -1, "Click's pointerId has reserved value");
+ assert_equals(e.pointerType, "", "Click's pointerType has default value");
+ }
+ });
+ }
+}));});
+let currentTest;
+let testElements = [button, inputbutton];
+let keyNameMap = new Map([[spaceKey, "space"], [enterKey, "enter"]]);
+keys.forEach((key)=>testElements.forEach((el)=>promise_test((test)=> new Promise(async (resolve,reject)=>{
+ currentTest = test;
+ eventLog = [];
+ var eventWatcher = new EventWatcher(test, el, ['click']);
+ let waitForClick = eventWatcher.wait_for('click');
+ el.focus();
+ let actions = new test_driver.Actions();
+ await test_driver
+ .send_keys(el, key);
+ await waitForClick;
+
+ assert_array_equals(eventLog, [`click_${el.id}`], "The Keyboard generated click only sends the click event.");
+ resolve();
+}), `Test that the Keyboard generated click does not fire pointer or mouse events for ${el.id} when pressing ${keyNameMap.get(key)} key`)));
+</script>