diff --git a/Applications/Browser/Tab.cpp b/Applications/Browser/Tab.cpp index 7c56f4ad7d..66888a8625 100644 --- a/Applications/Browser/Tab.cpp +++ b/Applications/Browser/Tab.cpp @@ -159,13 +159,8 @@ Tab::Tab() auto url = m_page_view->document()->complete_url(href); on_tab_open_request(url); } else { - if (href.starts_with("#")) { - auto anchor = href.substring_view(1, href.length() - 1); - m_page_view->scroll_to_anchor(anchor); - } else { - auto url = m_page_view->document()->complete_url(href); - m_page_view->load(url); - } + auto url = m_page_view->document()->complete_url(href); + m_page_view->load(url); } }; diff --git a/Libraries/LibWeb/Frame/EventHandler.cpp b/Libraries/LibWeb/Frame/EventHandler.cpp index d9fb51d48c..bdf9dc966d 100644 --- a/Libraries/LibWeb/Frame/EventHandler.cpp +++ b/Libraries/LibWeb/Frame/EventHandler.cpp @@ -131,6 +131,9 @@ bool EventHandler::handle_mousedown(const Gfx::IntPoint& position, unsigned butt auto href = link->href(); if (href.starts_with("javascript:")) { 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); diff --git a/Libraries/LibWeb/Frame/Frame.cpp b/Libraries/LibWeb/Frame/Frame.cpp index 11f4ade734..b36c2ba2dd 100644 --- a/Libraries/LibWeb/Frame/Frame.cpp +++ b/Libraries/LibWeb/Frame/Frame.cpp @@ -25,6 +25,7 @@ */ #include +#include #include #include #include @@ -116,9 +117,33 @@ void Frame::did_scroll(Badge) void Frame::scroll_to_anchor(const String& fragment) { - // FIXME: This logic is backwards, the work should be done in here, - // and then we just request that the "view" scrolls to a certain content offset. - page().client().page_did_request_scroll_to_anchor(fragment); + if (!document()) + return; + + const auto* element = document()->get_element_by_id(fragment); + if (!element) { + auto candidates = document()->get_elements_by_name(fragment); + for (auto* candidate : candidates) { + if (is(*candidate)) { + element = to(candidate); + break; + } + } + } + + if (!element || !element->layout_node()) + return; + + auto& layout_node = *element->layout_node(); + + Gfx::FloatRect float_rect { layout_node.box_type_agnostic_position(), { (float)viewport_rect().width(), (float)viewport_rect().height() } }; + if (is(layout_node)) { + auto& layout_box = to(layout_node); + auto padding_box = layout_box.box_model().padding_box(layout_box); + float_rect.move_by(-padding_box.left, -padding_box.top); + } + + page().client().page_did_request_scroll_into_view(enclosing_int_rect(float_rect)); } Gfx::IntRect Frame::to_main_frame_rect(const Gfx::IntRect& a_rect) diff --git a/Libraries/LibWeb/Frame/Frame.h b/Libraries/LibWeb/Frame/Frame.h index b72e25c912..0b17362975 100644 --- a/Libraries/LibWeb/Frame/Frame.h +++ b/Libraries/LibWeb/Frame/Frame.h @@ -71,6 +71,7 @@ public: EventHandler& event_handler() { return m_event_handler; } const EventHandler& event_handler() const { return m_event_handler; } + void scroll_to(const Gfx::IntPoint&); void scroll_to_anchor(const String&); Frame& main_frame() { return m_main_frame; } @@ -82,6 +83,7 @@ public: Gfx::IntPoint to_main_frame_position(const Gfx::IntPoint&); Gfx::IntRect to_main_frame_rect(const Gfx::IntRect&); + private: explicit Frame(Element& host_element, Frame& main_frame); explicit Frame(Page&); diff --git a/Libraries/LibWeb/Page.h b/Libraries/LibWeb/Page.h index 850de68594..84b7e9d050 100644 --- a/Libraries/LibWeb/Page.h +++ b/Libraries/LibWeb/Page.h @@ -82,10 +82,10 @@ public: virtual void page_did_leave_tooltip_area() { } virtual void page_did_hover_link(const URL&) { } virtual void page_did_unhover_link() { } - virtual void page_did_request_scroll_to_anchor([[maybe_unused]] const String& fragment) { } virtual void page_did_invalidate(const Gfx::IntRect&) { } virtual void page_did_change_favicon(const Gfx::Bitmap&) { } virtual void page_did_layout() { } + virtual void page_did_request_scroll_into_view(const Gfx::IntRect&) { } }; } diff --git a/Libraries/LibWeb/PageView.cpp b/Libraries/LibWeb/PageView.cpp index 4ae7dc2abf..56b7acf430 100644 --- a/Libraries/LibWeb/PageView.cpp +++ b/Libraries/LibWeb/PageView.cpp @@ -239,11 +239,6 @@ void PageView::page_did_unhover_link() { } -void PageView::page_did_request_scroll_to_anchor(const String& fragment) -{ - scroll_to_anchor(fragment); -} - void PageView::page_did_invalidate(const Gfx::IntRect&) { update(); @@ -403,38 +398,9 @@ LayoutDocument* PageView::layout_root() return const_cast(document()->layout_node()); } -void PageView::scroll_to_anchor(const StringView& name) +void PageView::page_did_request_scroll_into_view(const Gfx::IntRect& rect) { - if (!document()) - return; - - const auto* element = document()->get_element_by_id(name); - if (!element) { - auto candidates = document()->get_elements_by_name(name); - for (auto* candidate : candidates) { - if (is(*candidate)) { - element = to(candidate); - break; - } - } - } - - if (!element) { - dbg() << "PageView::scroll_to_anchor(): Anchor not found: '" << name << "'"; - return; - } - if (!element->layout_node()) { - dbg() << "PageView::scroll_to_anchor(): Anchor found but without layout node: '" << name << "'"; - return; - } - auto& layout_node = *element->layout_node(); - Gfx::FloatRect float_rect { layout_node.box_type_agnostic_position(), { (float)visible_content_rect().width(), (float)visible_content_rect().height() } }; - if (is(layout_node)) { - auto& layout_box = to(layout_node); - auto padding_box = layout_box.box_model().padding_box(layout_box); - float_rect.move_by(-padding_box.left, -padding_box.top); - } - scroll_into_view(enclosing_int_rect(float_rect), true, true); + scroll_into_view(rect, true, true); window()->set_override_cursor(GUI::StandardCursor::None); } diff --git a/Libraries/LibWeb/PageView.h b/Libraries/LibWeb/PageView.h index 4bcb7db072..fc6a6e3f14 100644 --- a/Libraries/LibWeb/PageView.h +++ b/Libraries/LibWeb/PageView.h @@ -54,7 +54,6 @@ public: void reload(); bool load(const URL&); - void scroll_to_anchor(const StringView&); URL url() const; @@ -110,10 +109,10 @@ private: virtual void page_did_leave_tooltip_area() override; virtual void page_did_hover_link(const URL&) override; virtual void page_did_unhover_link() override; - virtual void page_did_request_scroll_to_anchor(const String& fragment) override; virtual void page_did_invalidate(const Gfx::IntRect&) override; virtual void page_did_change_favicon(const Gfx::Bitmap&) override; virtual void page_did_layout() override; + virtual void page_did_request_scroll_into_view(const Gfx::IntRect&) override; void layout_and_sync_size();