Bug 1947223 - Bail out from ScrollFrameIntoVisualViewport if the given position:fixed element is outside of the viewport. r=dlrobertson

Differential Revision: https://phabricator.services.mozilla.com/D237853
This commit is contained in:
Hiroyuki Ikezoe
2025-02-19 01:20:15 +00:00
parent b9a0b2d282
commit b74be41f38
2 changed files with 93 additions and 0 deletions

View File

@@ -3764,6 +3764,33 @@ void PresShell::DoScrollContentIntoView() {
data->mContentScrollHAxis, data->mContentToScrollToFlags);
}
static bool NeedToVisuallyScroll(const nsSize& aLayoutViewportSize,
const nsRect& aPositionFixedRect) {
// position:fixed elements are fixed to the layout viewport, thus the
// coordinate system is (0, 0) origin.
// (and the maximum visible position is the layout viewport size, elements
// outside of the size will never be laid out)
const nsRect layoutViewport = nsRect(nsPoint(), aLayoutViewportSize);
// `BaseRect::Intersects(const Sub& aRect)` does return false if `aRect` is
// empty, but we do want to visually scroll to empty position:fixed elements
// if the elements are inside the layout viewport.
if (aPositionFixedRect.IsEmpty()) {
if (aPositionFixedRect.x > layoutViewport.XMost() ||
aPositionFixedRect.XMost() < layoutViewport.x ||
aPositionFixedRect.y > layoutViewport.YMost() ||
aPositionFixedRect.YMost() < layoutViewport.y) {
return false;
}
return true;
}
if (!layoutViewport.Intersects(aPositionFixedRect)) {
return false;
}
return true;
}
void PresShell::ScrollFrameIntoVisualViewport(Maybe<nsPoint>& aDestination,
const nsRect& aPositionFixedRect,
ScrollFlags aScrollFlags) {
@@ -3799,6 +3826,13 @@ void PresShell::ScrollFrameIntoVisualViewport(Maybe<nsPoint>& aDestination,
return;
}
// If the position:fixed element is totally outside of the the layout
// viewport, it will never be in the viewport.
const nsSize layoutViewportSize = root->GetLayoutViewportSize();
if (!NeedToVisuallyScroll(layoutViewportSize, aPositionFixedRect)) {
return;
}
aDestination = Some(aPositionFixedRect.TopLeft());
}

View File

@@ -0,0 +1,59 @@
<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<link rel="help" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1947223">
<style>
html {
width: 200vw;
height: 200vh;
}
.fixed {
position: fixed;
top: 0px;
left: -200px;
width: 100px;
height: 100px;
overflow: scroll;
}
#target {
width: 100px;
height: 100px;
}
</style>
<div class="fixed">
<div id="target"></div>
<div style="height: 200vh"></div>
</div>
<script>
promise_test(async t => {
assert_equals(window.scrollY, 0);
assert_equals(visualViewport.pageTop, 0);
// Scroll the root scroll container a bit.
const scrollPromise =
new Promise(resolve => window.addEventListener("scroll", resolve));
window.scrollTo(0, 10);
await scrollPromise;
assert_equals(window.scrollY, 10);
assert_equals(visualViewport.pageTop, 10);
window.addEventListener("scroll", () => {
assert_unreached("Any scroll event should not be observed");
});
// Now trigger a scrollIntoView call to an element inside a position:fixed element.
// NOTE: smooth scroll is a trigger of the firefox bug.
document.querySelector('#target').scrollIntoView({ behavior: "smooth" });
// Wait four rAFs to give a chance the scrollIntoView scrolls this document.
await new Promise(resolve => requestAnimationFrame(resolve));
await new Promise(resolve => requestAnimationFrame(resolve));
await new Promise(resolve => requestAnimationFrame(resolve));
await new Promise(resolve => requestAnimationFrame(resolve));
assert_equals(window.scrollY, 10);
assert_equals(visualViewport.pageTop, 10);
}, "Element.scrollIntoView doesn't scroll a position:fixed element outside of the layout viewport");
</script>