script: Avoid panic when scrolling area of window is larger than viewport (#39367)

Sometimes, the computed scrolling area of window is larger than
viewport. This causes panics in `Window.scroll` with `f32::clamp(0.0,
some negative number)`.

Eventually, we should find out why "computed scrolling area of window is
larger than viewport". But let's avoid the panics first.

Testing: This avoids panic, so definitely not covered by existing tests.
But it would be hard to write a automated test for this in headless
mode.
Fixes: #39346

Signed-off-by: Euclid Ye <yezhizhenjiakang@gmail.com>
This commit is contained in:
Euclid Ye 2025-09-18 15:51:53 +08:00 committed by GitHub
parent dbeee677b3
commit af7de5ccf1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2160,9 +2160,9 @@ impl Window {
// 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.
let scrolling_area = self.scrolling_area_query(None);
let x = xfinite.clamp(0.0, scrolling_area.width() as f32 - viewport.width);
let y = yfinite.clamp(0.0, scrolling_area.height() as f32 - viewport.height);
let scrolling_area = self.scrolling_area_query(None).to_f32();
let x = xfinite.clamp(0.0, 0.0f32.max(scrolling_area.width() - viewport.width));
let y = yfinite.clamp(0.0, 0.0f32.max(scrolling_area.height() - viewport.height));
// Step 10: If position is the same as the viewports current scroll position, and
// the viewport does not have an ongoing smooth scroll, abort these steps.