Skip to content

Truly independent web browser

LibWeb: Scroll the layout viewport before the visual viewport

Zaggy10240ae5da90a4f8
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cpp
index 54dca5d182b..5f8bf9283f3 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp
@@ -7864,17 +7864,6 @@ GC::Ref<WebIDL::Promise> LocalNavigable::perform_a_scroll_of_the_viewport(CSSPix
// 13. Let element be doc’s root element if there is one, null otherwise.
- // 14. Perform a scroll of the viewport’s scrolling box to its current scroll position + (layout dx, layout dy)
- // with element as the associated element, and behavior as the scroll behavior. Let scrollPromise1 be the
- // Promise returned from this step.
- // NB: Performed after step 15, below.
- TemporaryExecutionContext temporary_execution_context { HTML::relevant_realm(*doc) };
-
- // 15. Perform a scroll of vv’s scrolling box to its current scroll position + (visual dx, visual dy) with element
- // as the associated element, and behavior as the scroll behavior. Let scrollPromise2 be the Promise returned
- // from this step.
- // AD-HOC: Step 15 is performed before step 14 so that the visual viewport's scroll and scrollend events are
- // dispatched before the window's.
CSSPixelPoint visual_delta { visual_dx, visual_dy };
if (visual_delta.is_zero())
doc->set_needs_repaint(Badge<HTML::LocalNavigable> {}, InvalidateDisplayList::No);
@@ -7887,6 +7876,15 @@ GC::Ref<WebIDL::Promise> LocalNavigable::perform_a_scroll_of_the_viewport(CSSPix
bool compositor_splits_the_scroll = has_compositor_context() && relative_displacement.has_value() && !scrolls_smoothly(behavior, doc->document_element());
auto instant_scroll_requester = compositor_splits_the_scroll ? InstantScrollRequester::Caller : InstantScrollRequester::ThisScroll;
+ // 14. Perform a scroll of the viewport’s scrolling box to its current scroll position + (layout dx, layout dy)
+ // with element as the associated element, and behavior as the scroll behavior. Let scrollPromise1 be the
+ // Promise returned from this step.
+ TemporaryExecutionContext temporary_execution_context { HTML::relevant_realm(*doc) };
+ auto layout_viewport_scroll = perform_a_scroll_of_the_layout_viewport({ layout_dx, layout_dy }, behavior, trigger, relative_displacement, scroll_kind, animation_kind, instant_scroll_requester);
+
+ // 15. Perform a scroll of vv’s scrolling box to its current scroll position + (visual dx, visual dy) with element
+ // as the associated element, and behavior as the scroll behavior. Let scrollPromise2 be the Promise returned
+ // from this step.
auto visual_viewport_scroll = perform_a_scroll_of_a_scrolling_box(visual_viewport_stable_node_id, {
.position = vv->offset() + visual_delta,
.behavior = behavior,
@@ -7897,8 +7895,6 @@ GC::Ref<WebIDL::Promise> LocalNavigable::perform_a_scroll_of_the_viewport(CSSPix
.instant_scroll_requester = instant_scroll_requester,
});
- auto layout_viewport_scroll = perform_a_scroll_of_the_layout_viewport({ layout_dx, layout_dy }, behavior, trigger, relative_displacement, scroll_kind, animation_kind, instant_scroll_requester);
-
if (compositor_splits_the_scroll) {
// A layout viewport that came to rest at a snap position goes there, and the visual viewport with it.
if (layout_viewport_scroll.destination_is_a_snap_position) {
diff --git a/Tests/LibWeb/Text/expected/visual-viewport-scroll-event-order.txt b/Tests/LibWeb/Text/expected/visual-viewport-scroll-event-order.txt
new file mode 100644
index 00000000000..3f9463f7881
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/visual-viewport-scroll-event-order.txt
@@ -0,0 +1,4 @@
+scale: 2
+window scrolled: true
+visual viewport scrolled: true
+scroll order: window,visualViewport
diff --git a/Tests/LibWeb/Text/input/visual-viewport-scroll-event-order.html b/Tests/LibWeb/Text/input/visual-viewport-scroll-event-order.html
new file mode 100644
index 00000000000..c8e4967c31f
--- /dev/null
+++ b/Tests/LibWeb/Text/input/visual-viewport-scroll-event-order.html
@@ -0,0 +1,37 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<div style="width: 200vw; height: 200vh"></div>
+<script>
+ promiseTest(async () => {
+ await animationFrame();
+ await animationFrame();
+
+ const pinchScrollend = new Promise(resolve => visualViewport.addEventListener("scrollend", resolve, { once: true }));
+ internals.pinch(100, 100, 1.0, 0);
+ await animationFrame();
+ await animationFrame();
+ println(`scale: ${visualViewport.scale}`);
+ await pinchScrollend;
+
+ // https://drafts.csswg.org/cssom-view/#viewport-perform-a-scroll
+ // A scroll of the viewport scrolls the viewport's scrolling box in step 14 and the visual viewport's in step
+ // 15, in that order, which is the order of their scroll events.
+ const scrollOrder = [];
+ const bothScrolls = Promise.all([
+ new Promise(resolve => addEventListener("scroll", () => {
+ scrollOrder.push("window");
+ resolve();
+ }, { once: true })),
+ new Promise(resolve => visualViewport.addEventListener("scroll", () => {
+ scrollOrder.push("visualViewport");
+ resolve();
+ }, { once: true })),
+ ]);
+ const offsetTopBeforeScroll = visualViewport.offsetTop;
+ scrollBy(0, 800);
+ await bothScrolls;
+ println(`window scrolled: ${scrollY > 0}`);
+ println(`visual viewport scrolled: ${visualViewport.offsetTop > offsetTopBeforeScroll}`);
+ println(`scroll order: ${scrollOrder.join(",")}`);
+ });
+</script>