script: Implement scrollIntoView (#38230)

This is an implementation for `scrollIntoView`. For now, it is called
when a certain element gains focus.

Testing: Existing WPT tests
Fixes: #24059

Signed-off-by: abdelrahman1234567 <abdelrahman.hossameldin.awadalla@huawei.com>
This commit is contained in:
Abdelrahman Hossam 2025-08-06 16:08:25 +08:00 committed by GitHub
parent dcb90bb85e
commit 17a269a8ad
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
34 changed files with 492 additions and 318 deletions

View file

@ -58,6 +58,7 @@ use percent_encoding::percent_decode;
use profile_traits::ipc as profile_ipc;
use profile_traits::time::TimerMetadataFrameType;
use regex::bytes::Regex;
use script_bindings::codegen::GenericBindings::ElementBinding::ElementMethods;
use script_bindings::interfaces::DocumentHelpers;
use script_bindings::script_runtime::JSContext;
use script_traits::{ConstellationInputEvent, DocumentActivity, ProgressiveWebMetricType};
@ -93,6 +94,9 @@ use crate::dom::bindings::codegen::Bindings::BeforeUnloadEventBinding::BeforeUnl
use crate::dom::bindings::codegen::Bindings::DocumentBinding::{
DocumentMethods, DocumentReadyState, DocumentVisibilityState, NamedPropertyValue,
};
use crate::dom::bindings::codegen::Bindings::ElementBinding::{
ScrollIntoViewContainer, ScrollIntoViewOptions, ScrollLogicalPosition,
};
use crate::dom::bindings::codegen::Bindings::EventBinding::Event_Binding::EventMethods;
use crate::dom::bindings::codegen::Bindings::HTMLIFrameElementBinding::HTMLIFrameElement_Binding::HTMLIFrameElementMethods;
use crate::dom::bindings::codegen::Bindings::HTMLInputElementBinding::HTMLInputElementMethods;
@ -105,12 +109,13 @@ use crate::dom::bindings::codegen::Bindings::PermissionStatusBinding::Permission
use crate::dom::bindings::codegen::Bindings::ShadowRootBinding::ShadowRootMethods;
use crate::dom::bindings::codegen::Bindings::TouchBinding::TouchMethods;
use crate::dom::bindings::codegen::Bindings::WindowBinding::{
FrameRequestCallback, ScrollBehavior, WindowMethods,
FrameRequestCallback, ScrollBehavior, ScrollOptions, WindowMethods,
};
use crate::dom::bindings::codegen::Bindings::XPathEvaluatorBinding::XPathEvaluatorMethods;
use crate::dom::bindings::codegen::Bindings::XPathNSResolverBinding::XPathNSResolver;
use crate::dom::bindings::codegen::UnionTypes::{
NodeOrString, StringOrElementCreationOptions, TrustedHTMLOrString,
BooleanOrScrollIntoViewOptions, NodeOrString, StringOrElementCreationOptions,
TrustedHTMLOrString,
};
use crate::dom::bindings::domname::{
self, is_valid_attribute_local_name, is_valid_element_local_name, namespace_from_domstring,
@ -1384,6 +1389,18 @@ impl Document {
DeviceIntRect::from_untyped(&rect.to_box2d()),
));
}
// Scroll operation to happen after element gets focus.
// This is needed to ensure that the focused element is visible.
elem.ScrollIntoView(BooleanOrScrollIntoViewOptions::ScrollIntoViewOptions(
ScrollIntoViewOptions {
parent: ScrollOptions {
behavior: ScrollBehavior::Smooth,
},
block: ScrollLogicalPosition::Center,
inline: ScrollLogicalPosition::Center,
container: ScrollIntoViewContainer::All,
},
));
}
}