From 40b2d24d5585a94f09f521bb0ceb055dcae7b6e9 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Thu, 1 Aug 2024 13:41:20 -0400 Subject: [PATCH] LibWeb: Support ctrl/cmd-clicking a link to open it in a new tab The spec does not define activation behavior of ctrl/cmd clicks, so we have to go a bit ad-hoc here. When an anchor element is clicked with the cmd (on macOS) or ctrl (on other platforms) modifier pressed, we will skip activation of that element and pass the event on to the chrome. We still dispatch the event to allow scripts to cancel the event. --- Userland/Libraries/LibWeb/HTML/HTMLAnchorElement.cpp | 8 ++++++++ Userland/Libraries/LibWeb/Page/EventHandler.cpp | 4 +++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibWeb/HTML/HTMLAnchorElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLAnchorElement.cpp index fcf17b51dce..25d1be2df92 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLAnchorElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLAnchorElement.cpp @@ -80,6 +80,14 @@ void HTMLAnchorElement::activation_behavior(Web::DOM::Event const& event) if (href().is_empty()) return; + // AD-HOC: Do not activate the element for clicks with the ctrl/cmd modifier present. This lets + // the chrome open the link in a new tab. + if (is(event)) { + auto const& mouse_event = static_cast(event); + if (mouse_event.platform_ctrl_key()) + return; + } + // 2. Let hyperlinkSuffix be null. Optional hyperlink_suffix {}; diff --git a/Userland/Libraries/LibWeb/Page/EventHandler.cpp b/Userland/Libraries/LibWeb/Page/EventHandler.cpp index d7baffd8efa..0700318c576 100644 --- a/Userland/Libraries/LibWeb/Page/EventHandler.cpp +++ b/Userland/Libraries/LibWeb/Page/EventHandler.cpp @@ -313,7 +313,9 @@ bool EventHandler::handle_mouseup(CSSPixelPoint viewport_position, CSSPixelPoint auto href = link->href(); auto url = document->parse_url(href); - if (button == UIEvents::MouseButton::Middle) { + if (button == UIEvents::MouseButton::Primary && (modifiers & UIEvents::Mod_PlatformCtrl) != 0) { + m_navigable->page().client().page_did_click_link(url, link->target().to_byte_string(), modifiers); + } else if (button == UIEvents::MouseButton::Middle) { m_navigable->page().client().page_did_middle_click_link(url, link->target().to_byte_string(), modifiers); } else if (button == UIEvents::MouseButton::Secondary) { m_navigable->page().client().page_did_request_link_context_menu(viewport_position, url, link->target().to_byte_string(), modifiers);