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 {
debug_assert!(self.is_form_associated_custom_element());
self.as_element()
}
@ -1268,5 +1267,5 @@ impl FormControl for HTMLElement {
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
// fn candidate_for_validation(&self) -> bool;
// fn satisfies_constraints(&self) -> bool;
}

View file

@ -2193,6 +2193,26 @@ impl HTMLInputElement {
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) {
if !self.input_type().is_textual_or_password() {
return;

View file

@ -204,7 +204,7 @@ impl HTMLTextAreaElement {
}
// 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-readonly-attribute:concept-fe-mutable
!(self.upcast::<Element>().disabled_state() || self.ReadOnly())
@ -450,6 +450,13 @@ impl HTMLTextAreaElementMethods<crate::DomTypeHolder> for 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) {
// https://html.spec.whatwg.org/multipage/#the-textarea-element:concept-form-reset-control
let mut textinput = self.textinput.borrow_mut();