mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 03:55:24 +00:00
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.
This commit is contained in:
parent
1037a24076
commit
02c5e22f06
Notes:
sideshowbarker
2024-07-19 05:06:24 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/02c5e22f06d
6 changed files with 23 additions and 24 deletions
|
@ -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);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -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()) {
|
||||
|
|
|
@ -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> 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<HTMLAnchorElement> 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) {
|
||||
|
|
|
@ -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&) { }
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -60,9 +60,9 @@ public:
|
|||
void set_should_show_line_box_borders(bool value) { m_should_show_line_box_borders = value; }
|
||||
|
||||
Function<void(const Gfx::IntPoint& screen_position)> on_context_menu_request;
|
||||
Function<void(const String& href, const String& target, unsigned modifiers)> on_link_click;
|
||||
Function<void(const URL&, const String& target, unsigned modifiers)> on_link_click;
|
||||
Function<void(const String& href, const Gfx::IntPoint& screen_position)> on_link_context_menu_request;
|
||||
Function<void(const String& href)> on_link_middle_click;
|
||||
Function<void(const URL&)> on_link_middle_click;
|
||||
Function<void(const URL&)> on_link_hover;
|
||||
Function<void(const String&)> on_title_change;
|
||||
Function<void(const URL&)> 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;
|
||||
|
|
Loading…
Add table
Reference in a new issue