script: Make scrollIntoView more similar to the specification (#39094)

Rework the flow of code in `Element:scroll_into_view_with_options` to
more closely follow the specification. This also simplifies the code a
bit and adds some TODOs about future improvements.

Testing: This should not change behavior and is thus covered by existing
tests.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
Co-authored-by: Oriol Brufau <obrufau@igalia.com>
This commit is contained in:
Martin Robinson 2025-09-02 15:11:02 -07:00 committed by GitHub
parent b0b70ec6b7
commit 2fc816d561
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 105 additions and 125 deletions

View file

@ -3236,6 +3236,28 @@ impl Node {
let _ = require_well_formed;
self.xml_serialize(xml_serialize::TraversalScope::ChildrenOnly(None))
}
/// Return true if this node establishes a "scrolling box" for the purposes of `scrollIntoView`.
pub(crate) fn establishes_scrolling_box(&self) -> bool {
// For now, `Document` represents the viewport.
//
// TODO: Is this the right thing to do? Maybe `Document` should be ignored and viewport
// should be represented by the root of the DOM flat tree.
if self.is::<Document>() {
return true;
}
let Some(element) = self.downcast::<Element>() else {
// Shadow roots and other nodes are not scrolling boxes.
return false;
};
// TODO: This should ask layout whether or not the element establishes a scrolling
// box. This heuristic is wrong.
element.style().is_some_and(|style| {
let overflow_x = style.get_box().clone_overflow_x();
let overflow_y = style.get_box().clone_overflow_y();
overflow_x.is_scrollable() || overflow_y.is_scrollable()
})
}
}
impl NodeMethods<crate::DomTypeHolder> for Node {