From 4969ad9d6b540bc7d90bcdd40859a5ba743c2a7f Mon Sep 17 00:00:00 2001 From: Aliaksandr Kalenik Date: Sun, 24 Dec 2023 20:10:14 +0100 Subject: [PATCH] LibWeb: Limit scroll position by overflow area in Window::scroll() This change fixes "vertical shift" in inspector. --- .../LibWeb/Text/expected/window-scrollTo.txt | 1 + Tests/LibWeb/Text/input/window-scrollTo.html | 28 +++++++++++++++++++ Userland/Libraries/LibWeb/HTML/Window.cpp | 20 ++++++++----- 3 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 Tests/LibWeb/Text/expected/window-scrollTo.txt create mode 100644 Tests/LibWeb/Text/input/window-scrollTo.html diff --git a/Tests/LibWeb/Text/expected/window-scrollTo.txt b/Tests/LibWeb/Text/expected/window-scrollTo.txt new file mode 100644 index 00000000000..5cdae986c3e --- /dev/null +++ b/Tests/LibWeb/Text/expected/window-scrollTo.txt @@ -0,0 +1 @@ + The page has been scrolled to y: 1616 diff --git a/Tests/LibWeb/Text/input/window-scrollTo.html b/Tests/LibWeb/Text/input/window-scrollTo.html new file mode 100644 index 00000000000..728e9382753 --- /dev/null +++ b/Tests/LibWeb/Text/input/window-scrollTo.html @@ -0,0 +1,28 @@ + + + +
+
diff --git a/Userland/Libraries/LibWeb/HTML/Window.cpp b/Userland/Libraries/LibWeb/HTML/Window.cpp index 663f831be09..7d58758f071 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.cpp +++ b/Userland/Libraries/LibWeb/HTML/Window.cpp @@ -64,6 +64,7 @@ #include #include #include +#include #include #include #include @@ -1260,30 +1261,35 @@ void Window::scroll(ScrollToOptions const& options) // 6. Let viewport height be the height of the viewport excluding the height of the scroll bar, if any. auto viewport_height = viewport_rect.height(); - (void)viewport_width; - (void)viewport_height; + auto const document = top_level_traversable->active_document(); + auto scrolling_area = document->paintable_box()->scrollable_overflow_rect()->to_type(); - // FIXME: 7. + // 7. FIXME: For now we always assume overflow direction is rightward // -> If the viewport has rightward overflow direction // Let x be max(0, min(x, viewport scrolling area width - viewport width)). + x = max(0.0f, min(x, scrolling_area.width() - viewport_width)); // -> If the viewport has leftward overflow direction // Let x be min(0, max(x, viewport width - viewport scrolling area width)). - // FIXME: 8. + // 8. FIXME: For now we always assume overflow direction is downward // -> If the viewport has downward overflow direction // Let y be max(0, min(y, viewport scrolling area height - viewport height)). + y = max(0.0f, min(y, scrolling_area.height() - viewport_height)); // -> If the viewport has upward overflow direction // Let y be min(0, max(y, viewport height - viewport scrolling area height)). // FIXME: 9. Let position be the scroll position the viewport would have by aligning the x-coordinate x of the viewport // scrolling area with the left of the viewport and aligning the y-coordinate y of the viewport scrolling area // with the top of the viewport. + auto position = Gfx::FloatPoint { x, y }; - // FIXME: 10. If position is the same as the viewport’s current scroll position, and the viewport does not have an ongoing - // smooth scroll, abort these steps. + // 10. If position is the same as the viewport’s current scroll position, and the viewport does not have an ongoing + // smooth scroll, abort these steps. + if (position == viewport_rect.location()) + return; // 11. Let document be the viewport’s associated Document. - auto const document = top_level_traversable->active_document(); + // NOTE: document is already defined above. // 12. Perform a scroll of the viewport to position, document’s root element as the associated element, if there is // one, or null otherwise, and the scroll behavior being the value of the behavior dictionary member of options.