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.
This commit is contained in:
Timothy Flynn 2024-08-01 13:41:20 -04:00 committed by Andreas Kling
parent c2d5e30a35
commit 40b2d24d55
Notes: github-actions[bot] 2024-08-02 06:07:52 +00:00
2 changed files with 11 additions and 1 deletions

View file

@ -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 {};

View file

@ -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);