webdriver: Implement element clear (#38208)

Initial Implementation of [Element
Clear](https://w3c.github.io/webdriver/#element-clear).

Testing: `tests/wpt/tests/webdriver/tests/classic/element_clear/`

---------

Signed-off-by: PotatoCP <Kenzie.Raditya.Tirtarahardja@huawei.com>
Signed-off-by: Kenzie Raditya Tirtarahardja <kenzieradityatirtarahardja18@gmail.com>
Co-authored-by: Euclid Ye <yezhizhenjiakang@gmail.com>
Co-authored-by: Josh Matthews <josh@joshmatthews.net>
This commit is contained in:
Kenzie Raditya Tirtarahardja 2025-07-25 01:49:31 +08:00 committed by GitHub
parent 1fb782bc38
commit 4b12ae73fe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 175 additions and 367 deletions

View file

@ -1259,7 +1259,6 @@ impl FormControl for HTMLElement {
} }
fn to_element(&self) -> &Element { fn to_element(&self) -> &Element {
debug_assert!(self.is_form_associated_custom_element());
self.as_element() self.as_element()
} }
@ -1268,5 +1267,5 @@ impl FormControl for HTMLElement {
true true
} }
// TODO candidate_for_validation, satisfies_constraints traits // TODO satisfies_constraints traits
} }

View file

@ -1698,8 +1698,18 @@ pub(crate) trait FormControl: DomObject {
} }
} }
/// <https://html.spec.whatwg.org/multipage/#candidate-for-constraint-validation>
fn is_candidate_for_constraint_validation(&self) -> bool {
let element = self.to_element();
let html_element = element.downcast::<HTMLElement>();
if let Some(html_element) = html_element {
html_element.is_submittable_element() || element.is_instance_validatable()
} else {
false
}
}
// XXXKiChjang: Implement these on inheritors // XXXKiChjang: Implement these on inheritors
// fn candidate_for_validation(&self) -> bool;
// fn satisfies_constraints(&self) -> bool; // fn satisfies_constraints(&self) -> bool;
} }

View file

@ -2193,6 +2193,26 @@ impl HTMLInputElement {
self.upcast::<Node>().dirty(NodeDamage::Other); self.upcast::<Node>().dirty(NodeDamage::Other);
} }
/// <https://w3c.github.io/webdriver/#ref-for-dfn-clear-algorithm-3>
/// Used by WebDriver to clear the input element.
pub(crate) fn clear(&self, can_gc: CanGc) {
// Step 1. Reset dirty value and dirty checkedness flags.
self.value_dirty.set(false);
self.checked_changed.set(false);
// Step 2. Set value to empty string.
self.textinput.borrow_mut().set_content(DOMString::from(""));
// Step 3. Set checkedness based on presence of content attribute.
self.update_checked_state(self.DefaultChecked(), false);
self.value_changed(can_gc);
// Step 4. Empty selected files
self.filelist.set(None);
// Step 5. invoke the value sanitization algorithm iff
// the type attribute's current state defines one.
// This is covered in `fn sanitize_value` called below.
self.enable_sanitization();
self.upcast::<Node>().dirty(NodeDamage::Other);
}
fn update_placeholder_shown_state(&self) { fn update_placeholder_shown_state(&self) {
if !self.input_type().is_textual_or_password() { if !self.input_type().is_textual_or_password() {
return; return;

View file

@ -204,7 +204,7 @@ impl HTMLTextAreaElement {
} }
// https://html.spec.whatwg.org/multipage/#concept-fe-mutable // https://html.spec.whatwg.org/multipage/#concept-fe-mutable
fn is_mutable(&self) -> bool { pub(crate) fn is_mutable(&self) -> bool {
// https://html.spec.whatwg.org/multipage/#the-textarea-element%3Aconcept-fe-mutable // https://html.spec.whatwg.org/multipage/#the-textarea-element%3Aconcept-fe-mutable
// https://html.spec.whatwg.org/multipage/#the-readonly-attribute:concept-fe-mutable // https://html.spec.whatwg.org/multipage/#the-readonly-attribute:concept-fe-mutable
!(self.upcast::<Element>().disabled_state() || self.ReadOnly()) !(self.upcast::<Element>().disabled_state() || self.ReadOnly())
@ -450,6 +450,13 @@ impl HTMLTextAreaElementMethods<crate::DomTypeHolder> for HTMLTextAreaElement {
} }
impl HTMLTextAreaElement { impl HTMLTextAreaElement {
/// <https://w3c.github.io/webdriver/#ref-for-dfn-clear-algorithm-4>
/// Used by WebDriver to clear the textarea element.
pub(crate) fn clear(&self) {
self.value_dirty.set(false);
self.textinput.borrow_mut().set_content(DOMString::from(""));
}
pub(crate) fn reset(&self) { pub(crate) fn reset(&self) {
// https://html.spec.whatwg.org/multipage/#the-textarea-element:concept-form-reset-control // https://html.spec.whatwg.org/multipage/#the-textarea-element:concept-form-reset-control
let mut textinput = self.textinput.borrow_mut(); let mut textinput = self.textinput.borrow_mut();

View file

@ -2250,6 +2250,15 @@ impl ScriptThread {
WebDriverScriptCommand::DeleteCookie(name, reply) => { WebDriverScriptCommand::DeleteCookie(name, reply) => {
webdriver_handlers::handle_delete_cookie(&documents, pipeline_id, name, reply) webdriver_handlers::handle_delete_cookie(&documents, pipeline_id, name, reply)
}, },
WebDriverScriptCommand::ElementClear(element_id, reply) => {
webdriver_handlers::handle_element_clear(
&documents,
pipeline_id,
element_id,
reply,
can_gc,
)
},
WebDriverScriptCommand::FindElementsCSSSelector(selector, reply) => { WebDriverScriptCommand::FindElementsCSSSelector(selector, reply) => {
webdriver_handlers::handle_find_elements_css_selector( webdriver_handlers::handle_find_elements_css_selector(
&documents, &documents,

View file

@ -41,6 +41,7 @@ use crate::dom::bindings::codegen::Bindings::HTMLElementBinding::HTMLElementMeth
use crate::dom::bindings::codegen::Bindings::HTMLInputElementBinding::HTMLInputElementMethods; use crate::dom::bindings::codegen::Bindings::HTMLInputElementBinding::HTMLInputElementMethods;
use crate::dom::bindings::codegen::Bindings::HTMLOptionElementBinding::HTMLOptionElementMethods; use crate::dom::bindings::codegen::Bindings::HTMLOptionElementBinding::HTMLOptionElementMethods;
use crate::dom::bindings::codegen::Bindings::HTMLSelectElementBinding::HTMLSelectElementMethods; use crate::dom::bindings::codegen::Bindings::HTMLSelectElementBinding::HTMLSelectElementMethods;
use crate::dom::bindings::codegen::Bindings::HTMLTextAreaElementBinding::HTMLTextAreaElementMethods;
use crate::dom::bindings::codegen::Bindings::NodeBinding::{GetRootNodeOptions, NodeMethods}; use crate::dom::bindings::codegen::Bindings::NodeBinding::{GetRootNodeOptions, NodeMethods};
use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods; use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
use crate::dom::bindings::codegen::Bindings::XMLSerializerBinding::XMLSerializerMethods; use crate::dom::bindings::codegen::Bindings::XMLSerializerBinding::XMLSerializerMethods;
@ -64,11 +65,13 @@ use crate::dom::globalscope::GlobalScope;
use crate::dom::htmlbodyelement::HTMLBodyElement; use crate::dom::htmlbodyelement::HTMLBodyElement;
use crate::dom::htmldatalistelement::HTMLDataListElement; use crate::dom::htmldatalistelement::HTMLDataListElement;
use crate::dom::htmlelement::HTMLElement; use crate::dom::htmlelement::HTMLElement;
use crate::dom::htmlformelement::FormControl;
use crate::dom::htmliframeelement::HTMLIFrameElement; use crate::dom::htmliframeelement::HTMLIFrameElement;
use crate::dom::htmlinputelement::{HTMLInputElement, InputType}; use crate::dom::htmlinputelement::{HTMLInputElement, InputType};
use crate::dom::htmloptgroupelement::HTMLOptGroupElement; use crate::dom::htmloptgroupelement::HTMLOptGroupElement;
use crate::dom::htmloptionelement::HTMLOptionElement; use crate::dom::htmloptionelement::HTMLOptionElement;
use crate::dom::htmlselectelement::HTMLSelectElement; use crate::dom::htmlselectelement::HTMLSelectElement;
use crate::dom::htmltextareaelement::HTMLTextAreaElement;
use crate::dom::node::{Node, NodeTraits, ShadowIncluding}; use crate::dom::node::{Node, NodeTraits, ShadowIncluding};
use crate::dom::nodelist::NodeList; use crate::dom::nodelist::NodeList;
use crate::dom::types::ShadowRoot; use crate::dom::types::ShadowRoot;
@ -1651,6 +1654,107 @@ pub(crate) fn handle_get_url(
.unwrap(); .unwrap();
} }
/// <https://w3c.github.io/webdriver/#dfn-mutable-form-control-element>
fn element_is_mutable_form_control(element: &Element) -> bool {
if let Some(input_element) = element.downcast::<HTMLInputElement>() {
input_element.is_mutable() &&
matches!(
input_element.input_type(),
InputType::Text |
InputType::Search |
InputType::Url |
InputType::Tel |
InputType::Email |
InputType::Password |
InputType::Date |
InputType::Month |
InputType::Week |
InputType::Time |
InputType::DatetimeLocal |
InputType::Number |
InputType::Range |
InputType::Color |
InputType::File
)
} else if let Some(textarea_element) = element.downcast::<HTMLTextAreaElement>() {
textarea_element.is_mutable()
} else {
false
}
}
/// <https://w3c.github.io/webdriver/#dfn-clear-a-resettable-element>
fn clear_a_resettable_element(element: &Element, can_gc: CanGc) -> Result<(), ErrorStatus> {
let html_element = element
.downcast::<HTMLElement>()
.ok_or(ErrorStatus::UnknownError)?;
// Step 1 - 2. if element is a candidate for constraint
// validation and value is empty, abort steps.
if html_element.is_candidate_for_constraint_validation() {
if let Some(input_element) = element.downcast::<HTMLInputElement>() {
if input_element.Value().is_empty() {
return Ok(());
}
} else if let Some(textarea_element) = element.downcast::<HTMLTextAreaElement>() {
if textarea_element.Value().is_empty() {
return Ok(());
}
}
}
// Step 3. Invoke the focusing steps for the element.
html_element.Focus(can_gc);
// Step 4. Run clear algorithm for element.
if let Some(input_element) = element.downcast::<HTMLInputElement>() {
input_element.clear(can_gc);
} else if let Some(textarea_element) = element.downcast::<HTMLTextAreaElement>() {
textarea_element.clear();
} else {
unreachable!("We have confirm previously that element is mutable form control");
}
let event_target = element.upcast::<EventTarget>();
event_target.fire_bubbling_event(atom!("input"), can_gc);
event_target.fire_bubbling_event(atom!("change"), can_gc);
// Step 5. Run the unfocusing steps for the element.
html_element.Blur(can_gc);
Ok(())
}
/// <https://w3c.github.io/webdriver/#element-clear>
pub(crate) fn handle_element_clear(
documents: &DocumentCollection,
pipeline: PipelineId,
element_id: String,
reply: IpcSender<Result<(), ErrorStatus>>,
can_gc: CanGc,
) {
reply
.send(
get_known_element(documents, pipeline, element_id).and_then(|element| {
// Step 4. If element is not editable, return ErrorStatus::InvalidElementState.
// TODO: editing hosts and content editable elements are not implemented yet,
// hence we currently skip the check
if !element_is_mutable_form_control(&element) {
return Err(ErrorStatus::InvalidElementState);
}
// TODO: Step 5. Scroll Into View
// TODO: Step 6 - 10
// Wait until element become interactable and check.
// Step 11
// TODO: Clear content editable elements
clear_a_resettable_element(&element, can_gc)
}),
)
.unwrap();
}
fn get_option_parent(node: &Node) -> Option<DomRoot<Node>> { fn get_option_parent(node: &Node) -> Option<DomRoot<Node>> {
// Get parent for `<option>` or `<optiongrp>` based on container spec: // Get parent for `<option>` or `<optiongrp>` based on container spec:
// > 1. Let datalist parent be the first datalist element reached by traversing the tree // > 1. Let datalist parent be the first datalist element reached by traversing the tree

View file

@ -184,6 +184,7 @@ pub enum WebDriverScriptCommand {
), ),
DeleteCookies(IpcSender<Result<(), ErrorStatus>>), DeleteCookies(IpcSender<Result<(), ErrorStatus>>),
DeleteCookie(String, IpcSender<Result<(), ErrorStatus>>), DeleteCookie(String, IpcSender<Result<(), ErrorStatus>>),
ElementClear(String, IpcSender<Result<(), ErrorStatus>>),
ExecuteScript(String, IpcSender<WebDriverJSResult>), ExecuteScript(String, IpcSender<WebDriverJSResult>),
ExecuteAsyncScript(String, IpcSender<WebDriverJSResult>), ExecuteAsyncScript(String, IpcSender<WebDriverJSResult>),
FindElementsCSSSelector(String, IpcSender<Result<Vec<String>, ErrorStatus>>), FindElementsCSSSelector(String, IpcSender<Result<Vec<String>, ErrorStatus>>),

View file

@ -2111,6 +2111,26 @@ impl Handler {
Ok(WebDriverResponse::Void) Ok(WebDriverResponse::Void)
} }
/// <https://w3c.github.io/webdriver/#element-clear>
fn handle_element_clear(&self, element: &WebElement) -> WebDriverResult<WebDriverResponse> {
// Step 1. If session's current browsing context is no longer open,
// return ErrorStatus::NoSuchWindow.
self.verify_browsing_context_is_open(self.session()?.browsing_context_id)?;
// Step 2. Try to handle any user prompt.
self.handle_any_user_prompts(self.session()?.webview_id)?;
// Step 3-11 handled in script thread.
let (sender, receiver) = ipc::channel().unwrap();
let cmd = WebDriverScriptCommand::ElementClear(element.to_string(), sender);
self.browsing_context_script_command(cmd, VerifyBrowsingContextIsOpen::No)?;
match wait_for_script_response(receiver)? {
Ok(_) => Ok(WebDriverResponse::Void),
Err(error) => Err(WebDriverError::new(error, "")),
}
}
/// <https://w3c.github.io/webdriver/#element-click> /// <https://w3c.github.io/webdriver/#element-click>
fn handle_element_click(&mut self, element: &WebElement) -> WebDriverResult<WebDriverResponse> { fn handle_element_click(&mut self, element: &WebElement) -> WebDriverResult<WebDriverResponse> {
// Step 1. If session's current browsing context is no longer open, // Step 1. If session's current browsing context is no longer open,
@ -2513,6 +2533,7 @@ impl WebDriverHandler<ServoExtensionRoute> for Handler {
WebDriverCommand::ElementSendKeys(ref element, ref keys) => { WebDriverCommand::ElementSendKeys(ref element, ref keys) => {
self.handle_element_send_keys(element, keys) self.handle_element_send_keys(element, keys)
}, },
WebDriverCommand::ElementClear(ref element) => self.handle_element_clear(element),
WebDriverCommand::ElementClick(ref element) => self.handle_element_click(element), WebDriverCommand::ElementClick(ref element) => self.handle_element_click(element),
WebDriverCommand::DismissAlert => self.handle_dismiss_alert(), WebDriverCommand::DismissAlert => self.handle_dismiss_alert(),
WebDriverCommand::AcceptAlert => self.handle_accept_alert(), WebDriverCommand::AcceptAlert => self.handle_accept_alert(),

View file

@ -1,210 +1,15 @@
[clear.py] [clear.py]
[test_null_response_value]
expected: FAIL
[test_no_top_browsing_context]
expected: FAIL
[test_no_browsing_context] [test_no_browsing_context]
expected: FAIL expected: FAIL
[test_no_such_element_with_invalid_value]
expected: FAIL
[test_no_such_element_with_shadow_root]
expected: FAIL
[test_no_such_element_from_other_window_handle[open\]]
expected: FAIL
[test_no_such_element_from_other_window_handle[closed\]]
expected: FAIL
[test_no_such_element_from_other_frame[open\]]
expected: FAIL
[test_no_such_element_from_other_frame[closed\]]
expected: FAIL
[test_stale_element_reference[top_context\]]
expected: FAIL
[test_stale_element_reference[child_context\]]
expected: FAIL
[test_pointer_interactable] [test_pointer_interactable]
expected: FAIL expected: FAIL
[test_keyboard_interactable]
expected: FAIL
[test_input[number-42-\]]
expected: FAIL
[test_input[range-42-50\]]
expected: FAIL
[test_input[email-foo@example.com-\]]
expected: FAIL
[test_input[password-password-\]]
expected: FAIL
[test_input[search-search-\]]
expected: FAIL
[test_input[tel-999-\]]
expected: FAIL
[test_input[text-text-\]]
expected: FAIL
[test_input[url-https://example.com/-\]]
expected: FAIL
[test_input[color-#ff0000-#000000\]]
expected: FAIL
[test_input[date-2017-12-26-\]]
expected: FAIL
[test_input[datetime-2017-12-26T19:48-\]]
expected: FAIL
[test_input[datetime-local-2017-12-26T19:48-\]]
expected: FAIL
[test_input[time-19:48-\]]
expected: FAIL
[test_input[month-2017-11-\]]
expected: FAIL
[test_input[week-2017-W52-\]]
expected: FAIL
[test_input_readonly[number\]]
expected: FAIL
[test_input_readonly[range\]]
expected: FAIL
[test_input_readonly[email\]]
expected: FAIL
[test_input_readonly[password\]]
expected: FAIL
[test_input_readonly[search\]]
expected: FAIL
[test_input_readonly[tel\]]
expected: FAIL
[test_input_readonly[text\]]
expected: FAIL
[test_input_readonly[url\]]
expected: FAIL
[test_input_readonly[color\]]
expected: FAIL
[test_input_readonly[date\]]
expected: FAIL
[test_input_readonly[datetime\]]
expected: FAIL
[test_input_readonly[datetime-local\]]
expected: FAIL
[test_input_readonly[time\]]
expected: FAIL
[test_input_readonly[month\]]
expected: FAIL
[test_input_readonly[week\]]
expected: FAIL
[test_input_readonly[file\]]
expected: FAIL
[test_textarea]
expected: FAIL
[test_textarea_readonly]
expected: FAIL
[test_input_file]
expected: FAIL
[test_input_file_multiple]
expected: FAIL
[test_button[button\]]
expected: FAIL
[test_button[reset\]]
expected: FAIL
[test_button[submit\]]
expected: FAIL
[test_button_with_subtree]
expected: FAIL
[test_contenteditable] [test_contenteditable]
expected: FAIL expected: FAIL
[test_designmode] [test_designmode]
expected: FAIL expected: FAIL
[test_resettable_element_focus_when_empty]
expected: FAIL
[test_resettable_element_does_not_satisfy_validation_constraints[number-foo\]]
expected: FAIL
[test_resettable_element_does_not_satisfy_validation_constraints[email-foo\]]
expected: FAIL
[test_resettable_element_does_not_satisfy_validation_constraints[url-foo\]]
expected: FAIL
[test_resettable_element_does_not_satisfy_validation_constraints[date-foo\]]
expected: FAIL
[test_resettable_element_does_not_satisfy_validation_constraints[datetime-local-foo\]]
expected: FAIL
[test_resettable_element_does_not_satisfy_validation_constraints[time-foo\]]
expected: FAIL
[test_resettable_element_does_not_satisfy_validation_constraints[month-foo\]]
expected: FAIL
[test_resettable_element_does_not_satisfy_validation_constraints[week-foo\]]
expected: FAIL
[test_non_editable_inputs[checkbox\]]
expected: FAIL
[test_non_editable_inputs[radio\]]
expected: FAIL
[test_non_editable_inputs[hidden\]]
expected: FAIL
[test_non_editable_inputs[submit\]]
expected: FAIL
[test_non_editable_inputs[button\]]
expected: FAIL
[test_non_editable_inputs[image\]]
expected: FAIL
[test_scroll_into_view] [test_scroll_into_view]
expected: FAIL expected: FAIL

View file

@ -1,114 +0,0 @@
[disabled.py]
[test_button[button\]]
expected: FAIL
[test_button[reset\]]
expected: FAIL
[test_button[submit\]]
expected: FAIL
[test_input[button\]]
expected: FAIL
[test_input[checkbox\]]
expected: FAIL
[test_input[color\]]
expected: FAIL
[test_input[date\]]
expected: FAIL
[test_input[datetime-local\]]
expected: FAIL
[test_input[email\]]
expected: FAIL
[test_input[file\]]
expected: FAIL
[test_input[image\]]
expected: FAIL
[test_input[month\]]
expected: FAIL
[test_input[number\]]
expected: FAIL
[test_input[password\]]
expected: FAIL
[test_input[radio\]]
expected: FAIL
[test_input[range\]]
expected: FAIL
[test_input[reset\]]
expected: FAIL
[test_input[search\]]
expected: FAIL
[test_input[submit\]]
expected: FAIL
[test_input[tel\]]
expected: FAIL
[test_input[text\]]
expected: FAIL
[test_input[time\]]
expected: FAIL
[test_input[url\]]
expected: FAIL
[test_input[week\]]
expected: FAIL
[test_textarea]
expected: FAIL
[test_fieldset_descendant]
expected: FAIL
[test_fieldset_descendant_first_legend]
expected: FAIL
[test_fieldset_descendant_not_first_legend]
expected: FAIL
[test_option]
expected: FAIL
[test_option_optgroup]
expected: FAIL
[test_option_select]
expected: FAIL
[test_optgroup_select]
expected: FAIL
[test_select]
expected: FAIL
[test_xhtml[button\]]
expected: FAIL
[test_xhtml[input\]]
expected: FAIL
[test_xhtml[select\]]
expected: FAIL
[test_xhtml[textarea\]]
expected: FAIL
[test_xml]
expected: FAIL

View file

@ -1,54 +0,0 @@
[user_prompts.py]
[test_accept[alert-None\]]
expected: FAIL
[test_accept[confirm-True\]]
expected: FAIL
[test_accept[prompt-\]]
expected: FAIL
[test_accept_and_notify[alert-None\]]
expected: FAIL
[test_accept_and_notify[confirm-True\]]
expected: FAIL
[test_accept_and_notify[prompt-\]]
expected: FAIL
[test_dismiss[alert-None\]]
expected: FAIL
[test_dismiss[confirm-False\]]
expected: FAIL
[test_dismiss[prompt-None\]]
expected: FAIL
[test_dismiss_and_notify[alert-None\]]
expected: FAIL
[test_dismiss_and_notify[confirm-False\]]
expected: FAIL
[test_dismiss_and_notify[prompt-None\]]
expected: FAIL
[test_ignore[alert\]]
expected: FAIL
[test_ignore[confirm\]]
expected: FAIL
[test_ignore[prompt\]]
expected: FAIL
[test_default[alert-None\]]
expected: FAIL
[test_default[confirm-False\]]
expected: FAIL
[test_default[prompt-None\]]
expected: FAIL