Bug 1947470 - Do MoveInsideAndClamp for the given position:fixed rectangle to avoid unexpected scrolling. r=dlrobertson

For safety, both of helper_scrollIntoView_bug1950744.html and
helper_scrollIntoView_bug1950744-2.html run with
layout.scroll_fixed_content_into_view_visually=true and
layout.scroll_fixed_content_into_view_visually=false because these tests
should NOT cause any scrolling regardless of the pref value.

Differential Revision: https://phabricator.services.mozilla.com/D243735
This commit is contained in:
Hiroyuki Ikezoe
2025-04-08 00:17:48 +00:00
parent 39b3f6141e
commit 66f31cb50d
4 changed files with 41 additions and 25 deletions

View File

@@ -3856,18 +3856,31 @@ void PresShell::ScrollFrameIntoVisualViewport(Maybe<nsPoint>& aDestination,
const nsSize visualViewportSize =
rootScrollContainer->GetVisualViewportSize();
const nsSize layoutViewportSize = root->GetLayoutViewportSize();
const nsRect layoutViewport = nsRect(nsPoint(), layoutViewportSize);
// `positon:fixed` element are attached/fixed to the ViewportFrame, which is
// the parent of the root scroll container frame, thus what we need here is
// the visible area of the position:fixed element inside the root scroll
// container frame.
// For example, if the top left position of the fixed element is (-100,
// -100), it's outside of the scrollable range either in the layout viewport
// or the visual viewport. Likewise, if the right bottom position of the
// fixed element is (110vw, 110vh), it's also outside of the scrollable
// range.
const nsRect clampedPositionFixedRect =
aPositionFixedRect.MoveInsideAndClamp(layoutViewport);
// If the position:fixed element is already inside the visual viewport, we
// don't need to scroll visually.
if (aPositionFixedRect.y >= 0 &&
aPositionFixedRect.YMost() <= visualViewportSize.height &&
aPositionFixedRect.x >= 0 &&
aPositionFixedRect.XMost() <= visualViewportSize.width) {
if (clampedPositionFixedRect.y >= 0 &&
clampedPositionFixedRect.YMost() <= visualViewportSize.height &&
clampedPositionFixedRect.x >= 0 &&
clampedPositionFixedRect.XMost() <= visualViewportSize.width) {
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;
}