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());
}