From 32653f34f94779893837cbfed01ab2dba562cf3e Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 26 Mar 2023 15:42:28 +0200 Subject: [PATCH] LibWeb: Don't force relayout whole page on programmatic scroll When scrolling the page, we may need to flush any pending layout changes. This is required because otherwise, we don't know if the target scroll position is valid. However, we don't need to *force* a layout. If the layout tree is already up-to-date, we can use it as-is. Also, if we're scrolling to (0, 0), we don't need to update the layout at all, since (0, 0) is always a guaranteed valid scroll position. --- Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp b/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp index c6c7cc8ef2..51799ac714 100644 --- a/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp +++ b/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp @@ -389,8 +389,12 @@ void BrowsingContext::set_needs_display(CSSPixelRect const& rect) void BrowsingContext::scroll_to(CSSPixelPoint position) { - if (active_document()) - active_document()->force_layout(); + // NOTE: Scrolling to a position requires up-to-date layout *unless* we're scrolling to (0, 0) + // as (0, 0) is always guaranteed to be a valid scroll position. + if (!position.is_zero()) { + if (active_document()) + active_document()->update_layout(); + } if (m_page) m_page->client().page_did_request_scroll_to(position); @@ -416,7 +420,7 @@ void BrowsingContext::scroll_to_anchor(DeprecatedString const& fragment) if (!element) return; - document->force_layout(); + document->update_layout(); if (!element->layout_node()) return;