From 02c5e22f06dbe198fdc51eef488929518856d5c8 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 6 Jul 2020 19:41:10 +0200 Subject: [PATCH] LibWeb: Make the link click hooks include the destination URL We have all the context needed for relative URL resolution inside the engine, so let's not make embedders worry about this. --- Applications/Browser/Tab.cpp | 4 +--- Applications/Help/main.cpp | 5 ++--- Libraries/LibWeb/Frame/EventHandler.cpp | 18 ++++++++++-------- Libraries/LibWeb/Page.h | 4 ++-- Libraries/LibWeb/PageView.cpp | 8 ++++---- Libraries/LibWeb/PageView.h | 8 ++++---- 6 files changed, 23 insertions(+), 24 deletions(-) diff --git a/Applications/Browser/Tab.cpp b/Applications/Browser/Tab.cpp index c475cd993e..8093e6942e 100644 --- a/Applications/Browser/Tab.cpp +++ b/Applications/Browser/Tab.cpp @@ -154,12 +154,10 @@ Tab::Tab() update_bookmark_button(url.to_string()); }; - m_page_view->on_link_click = [this](auto& href, auto& target, unsigned modifiers) { + m_page_view->on_link_click = [this](auto& url, auto& target, unsigned modifiers) { if (target == "_blank" || modifiers == Mod_Ctrl) { - auto url = m_page_view->document()->complete_url(href); on_tab_open_request(url); } else { - auto url = m_page_view->document()->complete_url(href); m_page_view->load(url); } }; diff --git a/Applications/Help/main.cpp b/Applications/Help/main.cpp index e1449a9579..9a2a7a1cb4 100644 --- a/Applications/Help/main.cpp +++ b/Applications/Help/main.cpp @@ -151,10 +151,9 @@ int main(int argc, char* argv[]) open_page(path); }; - page_view.on_link_click = [&](const String& href, auto&, unsigned) { + page_view.on_link_click = [&](auto& url, auto&, unsigned) { char* current_path = strdup(history.current().characters()); - char* dir_path = dirname(current_path); - char* path = realpath(String::format("%s/%s", dir_path, href.characters()).characters(), nullptr); + char* path = realpath(url.path().characters(), nullptr); free(current_path); auto tree_view_index = model->index_from_path(path); if (tree_view_index.has_value()) { diff --git a/Libraries/LibWeb/Frame/EventHandler.cpp b/Libraries/LibWeb/Frame/EventHandler.cpp index f5def91650..97bdfb327c 100644 --- a/Libraries/LibWeb/Frame/EventHandler.cpp +++ b/Libraries/LibWeb/Frame/EventHandler.cpp @@ -104,7 +104,7 @@ bool EventHandler::handle_mousedown(const Gfx::IntPoint& position, unsigned butt if (!layout_root_ptr) return false; auto& layout_root = *layout_root_ptr; - auto& document = *m_frame.document(); + NonnullRefPtr document = *m_frame.document(); auto& page_client = m_frame.page().client(); auto result = layout_root.hit_test(position); @@ -112,7 +112,7 @@ bool EventHandler::handle_mousedown(const Gfx::IntPoint& position, unsigned butt return false; RefPtr node = result.layout_node->node(); - document.set_hovered_node(node); + document->set_hovered_node(node); if (!node) return false; @@ -125,27 +125,29 @@ bool EventHandler::handle_mousedown(const Gfx::IntPoint& position, unsigned butt auto offset = compute_mouse_event_offset(position, *result.layout_node); node->dispatch_event(MouseEvent::create("mousedown", offset.x(), offset.y())); if (RefPtr link = node->enclosing_link_element()) { - dbg() << "Web::EventHandler: Clicking on a link to " << link->href(); - + auto href = link->href(); + auto url = document->complete_url(href); + dbg() << "Web::EventHandler: Clicking on a link to " << url; if (button == GUI::MouseButton::Left) { auto href = link->href(); + auto url = document->complete_url(href); if (href.starts_with("javascript:")) { - document.run_javascript(href.substring_view(11, href.length() - 11)); + document->run_javascript(href.substring_view(11, href.length() - 11)); } else if (href.starts_with('#')) { auto anchor = href.substring_view(1, href.length() - 1); m_frame.scroll_to_anchor(anchor); } else { if (m_frame.is_main_frame()) { - page_client.page_did_click_link(link->href(), link->target(), modifiers); + page_client.page_did_click_link(url, link->target(), modifiers); } else { // FIXME: Handle different targets! - m_frame.loader().load(document.complete_url(link->href())); + m_frame.loader().load(url); } } } else if (button == GUI::MouseButton::Right) { page_client.page_did_request_link_context_menu(m_frame.to_main_frame_position(position), link->href(), link->target(), modifiers); } else if (button == GUI::MouseButton::Middle) { - page_client.page_did_middle_click_link(link->href(), link->target(), modifiers); + page_client.page_did_middle_click_link(url, link->target(), modifiers); } } else { if (button == GUI::MouseButton::Left) { diff --git a/Libraries/LibWeb/Page.h b/Libraries/LibWeb/Page.h index 84b7e9d050..d33d2b5cdc 100644 --- a/Libraries/LibWeb/Page.h +++ b/Libraries/LibWeb/Page.h @@ -76,8 +76,8 @@ public: virtual void page_did_request_cursor_change(GUI::StandardCursor) { } virtual void page_did_request_context_menu(const Gfx::IntPoint&) { } virtual void page_did_request_link_context_menu(const Gfx::IntPoint&, [[maybe_unused]] const String& href, [[maybe_unused]] const String& target, [[maybe_unused]] unsigned modifiers) { } - virtual void page_did_click_link([[maybe_unused]] const String& href, [[maybe_unused]] const String& target, [[maybe_unused]] unsigned modifiers) { } - virtual void page_did_middle_click_link([[maybe_unused]] const String& href, [[maybe_unused]] const String& target, [[maybe_unused]] unsigned modifiers) { } + virtual void page_did_click_link(const URL&, [[maybe_unused]] const String& target, [[maybe_unused]] unsigned modifiers) { } + virtual void page_did_middle_click_link(const URL&, [[maybe_unused]] const String& target, [[maybe_unused]] unsigned modifiers) { } virtual void page_did_enter_tooltip_area(const Gfx::IntPoint&, const String&) { } virtual void page_did_leave_tooltip_area() { } virtual void page_did_hover_link(const URL&) { } diff --git a/Libraries/LibWeb/PageView.cpp b/Libraries/LibWeb/PageView.cpp index 204c72c177..b55b185a18 100644 --- a/Libraries/LibWeb/PageView.cpp +++ b/Libraries/LibWeb/PageView.cpp @@ -207,16 +207,16 @@ void PageView::page_did_request_link_context_menu(const Gfx::IntPoint& content_p on_link_context_menu_request(href, screen_relative_rect().location().translated(to_widget_position(content_position))); } -void PageView::page_did_click_link(const String& href, const String& target, unsigned modifiers) +void PageView::page_did_click_link(const URL& url, const String& target, unsigned modifiers) { if (on_link_click) - on_link_click(href, target, modifiers); + on_link_click(url, target, modifiers); } -void PageView::page_did_middle_click_link(const String& href, [[maybe_unused]] const String& target, [[maybe_unused]] unsigned modifiers) +void PageView::page_did_middle_click_link(const URL& url, [[maybe_unused]] const String& target, [[maybe_unused]] unsigned modifiers) { if (on_link_middle_click) - on_link_middle_click(href); + on_link_middle_click(url); } void PageView::page_did_enter_tooltip_area(const Gfx::IntPoint& content_position, const String& title) diff --git a/Libraries/LibWeb/PageView.h b/Libraries/LibWeb/PageView.h index f837764269..c44814059f 100644 --- a/Libraries/LibWeb/PageView.h +++ b/Libraries/LibWeb/PageView.h @@ -60,9 +60,9 @@ public: void set_should_show_line_box_borders(bool value) { m_should_show_line_box_borders = value; } Function on_context_menu_request; - Function on_link_click; + Function on_link_click; Function on_link_context_menu_request; - Function on_link_middle_click; + Function on_link_middle_click; Function on_link_hover; Function on_title_change; Function on_load_start; @@ -103,8 +103,8 @@ private: virtual void page_did_request_cursor_change(GUI::StandardCursor) override; virtual void page_did_request_context_menu(const Gfx::IntPoint&) override; virtual void page_did_request_link_context_menu(const Gfx::IntPoint&, const String& href, const String& target, unsigned modifiers) override; - virtual void page_did_click_link(const String& href, const String& target, unsigned modifiers) override; - virtual void page_did_middle_click_link(const String& href, const String& target, unsigned modifiers) override; + virtual void page_did_click_link(const URL&, const String& target, unsigned modifiers) override; + virtual void page_did_middle_click_link(const URL&, const String& target, unsigned modifiers) override; virtual void page_did_enter_tooltip_area(const Gfx::IntPoint&, const String&) override; virtual void page_did_leave_tooltip_area() override; virtual void page_did_hover_link(const URL&) override;