script: Add FocusOptions argument to Element.focus and implement FocusOptions.preventScroll (#38495)

This is an implementation of the `prevent_scroll` feature in the focus
transaction system. It allows to control whether focusing an element
should prevent scrolling or not.

Spec:
https://html.spec.whatwg.org/multipage/interaction.html#dom-focusoptions-preventscroll
Testing: Existing WPT tests

Signed-off-by: abdelrahman1234567 <abdelrahman.hossameldin.awadalla@huawei.com>
This commit is contained in:
Abdelrahman Hossam 2025-08-22 22:05:32 +08:00 committed by GitHub
parent 2ac8665e03
commit 176e42d36d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 95 additions and 60 deletions

View file

@ -42,6 +42,7 @@ use crate::dom::bindings::codegen::Bindings::ElementBinding::ElementMethods;
use crate::dom::bindings::codegen::Bindings::HTMLElementBinding::HTMLElementMethods;
use crate::dom::bindings::codegen::Bindings::HTMLInputElementBinding::HTMLInputElementMethods;
use crate::dom::bindings::codegen::Bindings::HTMLOptionElementBinding::HTMLOptionElementMethods;
use crate::dom::bindings::codegen::Bindings::HTMLOrSVGElementBinding::FocusOptions;
use crate::dom::bindings::codegen::Bindings::HTMLSelectElementBinding::HTMLSelectElementMethods;
use crate::dom::bindings::codegen::Bindings::HTMLTextAreaElementBinding::HTMLTextAreaElementMethods;
use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
@ -1234,7 +1235,9 @@ pub(crate) fn handle_will_send_keys(
// run the focusing steps for the element.
if let Some(html_element) = element.downcast::<HTMLElement>() {
if !element.is_active_element() {
html_element.Focus(can_gc);
// TODO: "Focusing steps" has a different meaning from the focus() method.
// The actual focusing steps should be implemented
html_element.Focus(&FocusOptions::default(), can_gc);
} else {
element_has_focus = element.focus_state();
}
@ -1772,7 +1775,9 @@ fn clear_a_resettable_element(element: &Element, can_gc: CanGc) -> Result<(), Er
}
// Step 3. Invoke the focusing steps for the element.
html_element.Focus(can_gc);
// TODO: "Focusing steps" has a different meaning from the focus() method.
// The actual focusing steps should be implemented
html_element.Focus(&FocusOptions::default(), can_gc);
// Step 4. Run clear algorithm for element.
if let Some(input_element) = element.downcast::<HTMLInputElement>() {
@ -1920,7 +1925,11 @@ pub(crate) fn handle_element_click(
// Step 8.5
match container.downcast::<HTMLElement>() {
Some(html_element) => html_element.Focus(can_gc),
Some(html_element) => {
// TODO: "Focusing steps" has a different meaning from the focus() method.
// The actual focusing steps should be implemented
html_element.Focus(&FocusOptions::default(), can_gc);
},
None => return Err(ErrorStatus::UnknownError),
}