From fd297a324859964aa4d6babff1877cf851317c68 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sun, 3 Dec 2023 09:54:26 -0500 Subject: [PATCH] LibWeb: Run the unfocusing steps when a click does not focus anything For example, when clicking the document body outside of a focused input element, we should unfocus that element. --- .../Text/expected/input-click-to-unfocus.txt | 2 ++ .../Text/input/input-click-to-unfocus.html | 23 +++++++++++++++++++ .../Libraries/LibWeb/Internals/Internals.cpp | 10 ++++++++ .../Libraries/LibWeb/Internals/Internals.h | 2 ++ .../Libraries/LibWeb/Internals/Internals.idl | 2 ++ .../Libraries/LibWeb/Page/EventHandler.cpp | 3 +++ 6 files changed, 42 insertions(+) create mode 100644 Tests/LibWeb/Text/expected/input-click-to-unfocus.txt create mode 100644 Tests/LibWeb/Text/input/input-click-to-unfocus.html diff --git a/Tests/LibWeb/Text/expected/input-click-to-unfocus.txt b/Tests/LibWeb/Text/expected/input-click-to-unfocus.txt new file mode 100644 index 0000000000..6b6d977d72 --- /dev/null +++ b/Tests/LibWeb/Text/expected/input-click-to-unfocus.txt @@ -0,0 +1,2 @@ + focus +blur diff --git a/Tests/LibWeb/Text/input/input-click-to-unfocus.html b/Tests/LibWeb/Text/input/input-click-to-unfocus.html new file mode 100644 index 0000000000..35efd99f6c --- /dev/null +++ b/Tests/LibWeb/Text/input/input-click-to-unfocus.html @@ -0,0 +1,23 @@ + + + diff --git a/Userland/Libraries/LibWeb/Internals/Internals.cpp b/Userland/Libraries/LibWeb/Internals/Internals.cpp index 75fcd3ffad..15a10d4044 100644 --- a/Userland/Libraries/LibWeb/Internals/Internals.cpp +++ b/Userland/Libraries/LibWeb/Internals/Internals.cpp @@ -84,6 +84,16 @@ void Internals::commit_text() page->handle_keydown(Key_Return, 0, 0); } +void Internals::click(double x, double y) +{ + auto* page = global_object().browsing_context()->page(); + if (!page) + return; + + page->handle_mousedown({ x, y }, { x, y }, 1, 0, 0); + page->handle_mouseup({ x, y }, { x, y }, 1, 0, 0); +} + WebIDL::ExceptionOr Internals::dispatch_user_activated_event(DOM::EventTarget& target, DOM::Event& event) { event.set_is_trusted(true); diff --git a/Userland/Libraries/LibWeb/Internals/Internals.h b/Userland/Libraries/LibWeb/Internals/Internals.h index c3f21ac44e..7372d84e7c 100644 --- a/Userland/Libraries/LibWeb/Internals/Internals.h +++ b/Userland/Libraries/LibWeb/Internals/Internals.h @@ -25,6 +25,8 @@ public: void send_text(HTML::HTMLElement&, String const&); void commit_text(); + void click(double x, double y); + WebIDL::ExceptionOr dispatch_user_activated_event(DOM::EventTarget&, DOM::Event& event); private: diff --git a/Userland/Libraries/LibWeb/Internals/Internals.idl b/Userland/Libraries/LibWeb/Internals/Internals.idl index 69f3733173..1842ae1476 100644 --- a/Userland/Libraries/LibWeb/Internals/Internals.idl +++ b/Userland/Libraries/LibWeb/Internals/Internals.idl @@ -10,6 +10,8 @@ undefined sendText(HTMLElement target, DOMString text); undefined commitText(); + undefined click(double x, double y); + boolean dispatchUserActivatedEvent(EventTarget target, Event event); }; diff --git a/Userland/Libraries/LibWeb/Page/EventHandler.cpp b/Userland/Libraries/LibWeb/Page/EventHandler.cpp index dc372dc183..591e67af86 100644 --- a/Userland/Libraries/LibWeb/Page/EventHandler.cpp +++ b/Userland/Libraries/LibWeb/Page/EventHandler.cpp @@ -409,6 +409,9 @@ bool EventHandler::handle_mousedown(CSSPixelPoint position, CSSPixelPoint screen // If we didn't focus anything, place the document text cursor at the mouse position. // FIXME: This is all rather strange. Find a better solution. if (!did_focus_something) { + if (auto* focused_element = document->focused_element()) + HTML::run_unfocusing_steps(focused_element); + auto& realm = document->realm(); m_browsing_context->set_cursor_position(DOM::Position::create(realm, *paintable->dom_node(), result->index_in_node)); if (auto selection = document->get_selection()) {