Selaa lähdekoodia

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.
Timothy Flynn 11 kuukautta sitten
vanhempi
commit
40b2d24d55

+ 8 - 0
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<UIEvents::MouseEvent>(event)) {
+        auto const& mouse_event = static_cast<UIEvents::MouseEvent const&>(event);
+        if (mouse_event.platform_ctrl_key())
+            return;
+    }
+
     // 2. Let hyperlinkSuffix be null.
     Optional<String> hyperlink_suffix {};
 

+ 3 - 1
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);