diff --git a/Userland/Libraries/LibWeb/HTML/FormAssociatedElement.cpp b/Userland/Libraries/LibWeb/HTML/FormAssociatedElement.cpp index aa43970d0d0..4fd893314f2 100644 --- a/Userland/Libraries/LibWeb/HTML/FormAssociatedElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/FormAssociatedElement.cpp @@ -166,6 +166,14 @@ void FormAssociatedElement::reset_form_owner() } } +// https://w3c.github.io/webdriver/#dfn-clear-algorithm +void FormAssociatedElement::clear_algorithm() +{ + // When the clear algorithm is invoked for an element that does not define its own clear algorithm, its reset + // algorithm must be invoked instead. + reset_algorithm(); +} + // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-fs-formaction String FormAssociatedElement::form_action() const { diff --git a/Userland/Libraries/LibWeb/HTML/FormAssociatedElement.h b/Userland/Libraries/LibWeb/HTML/FormAssociatedElement.h index 5ca9da57e5f..de88c022559 100644 --- a/Userland/Libraries/LibWeb/HTML/FormAssociatedElement.h +++ b/Userland/Libraries/LibWeb/HTML/FormAssociatedElement.h @@ -98,6 +98,8 @@ public: // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-form-reset-control virtual void reset_algorithm() {}; + virtual void clear_algorithm(); + String form_action() const; WebIDL::ExceptionOr set_form_action(String const&); diff --git a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp index 50afef6ddd9..0f4725ee98a 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp @@ -1528,7 +1528,8 @@ void HTMLInputElement::reset_algorithm() m_checked = has_attribute(AttributeNames::checked); // empty the list of selected files, - m_selected_files = FileAPI::FileList::create(realm()); + if (m_selected_files) + m_selected_files = FileAPI::FileList::create(realm()); // and then invoke the value sanitization algorithm, if the type attribute's current state defines one. m_value = value_sanitization_algorithm(m_value); @@ -1544,6 +1545,42 @@ void HTMLInputElement::reset_algorithm() update_shadow_tree(); } +// https://w3c.github.io/webdriver/#dfn-clear-algorithm +void HTMLInputElement::clear_algorithm() +{ + // The clear algorithm for input elements is to set the dirty value flag and dirty checkedness flag back to false, + m_dirty_value = false; + m_dirty_checkedness = false; + + // set the value of the element to an empty string, + auto old_value = move(m_value); + m_value = String {}; + + // set the checkedness of the element to true if the element has a checked content attribute and false if it does not, + m_checked = has_attribute(AttributeNames::checked); + + // empty the list of selected files, + if (m_selected_files) + m_selected_files = FileAPI::FileList::create(realm()); + + // and then invoke the value sanitization algorithm iff the type attribute's current state defines one. + m_value = value_sanitization_algorithm(m_value); + + // Unlike their associated reset algorithms, changes made to form controls as part of these algorithms do count as + // changes caused by the user (and thus, e.g. do cause input events to fire). + user_interaction_did_change_input_value(); + + if (m_value != old_value) + relevant_value_was_changed(m_text_node); + + if (m_text_node) { + m_text_node->set_data(m_value); + update_placeholder_visibility(); + } + + update_shadow_tree(); +} + void HTMLInputElement::form_associated_element_was_inserted() { create_shadow_tree_if_needed(); diff --git a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h index 5edf09fce04..4d53b1ebed9 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h @@ -179,6 +179,7 @@ public: bool is_single_line() const; virtual void reset_algorithm() override; + virtual void clear_algorithm() override; virtual void form_associated_element_was_inserted() override; virtual void form_associated_element_was_removed(DOM::Node*) override; diff --git a/Userland/Libraries/LibWeb/HTML/HTMLOutputElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLOutputElement.cpp index 9bd056a0718..f90b8cb4ae6 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLOutputElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLOutputElement.cpp @@ -100,4 +100,14 @@ void HTMLOutputElement::reset_algorithm() m_default_value_override = {}; } +// https://w3c.github.io/webdriver/#dfn-clear-algorithm +void HTMLOutputElement::clear_algorithm() +{ + // The clear algorithm for output elements is set the element's value mode flag to default + m_default_value_override = default_value(); + + // and then to set the element's textContent IDL attribute to an empty string (thus clearing the element's child nodes). + string_replace_all({}); +} + } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLOutputElement.h b/Userland/Libraries/LibWeb/HTML/HTMLOutputElement.h index 4e1cf36f1ac..dad7a6ef6b8 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLOutputElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLOutputElement.h @@ -52,6 +52,7 @@ public: virtual bool is_labelable() const override { return true; } virtual void reset_algorithm() override; + virtual void clear_algorithm() override; // https://www.w3.org/TR/html-aria/#el-output virtual Optional default_role() const override { return ARIA::Role::status; } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp index 9c555bbefd6..eb8062c6972 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp @@ -119,6 +119,20 @@ void HTMLTextAreaElement::reset_algorithm() } } +// https://w3c.github.io/webdriver/#dfn-clear-algorithm +void HTMLTextAreaElement::clear_algorithm() +{ + // The clear algorithm for textarea elements is to set the dirty value flag back to false, + m_dirty_value = false; + + // and set the raw value of element to an empty string. + set_raw_value(child_text_content()); + + // Unlike their associated reset algorithms, changes made to form controls as part of these algorithms do count as + // changes caused by the user (and thus, e.g. do cause input events to fire). + queue_firing_input_event(); +} + // https://html.spec.whatwg.org/multipage/forms.html#the-textarea-element:concept-node-clone-ext WebIDL::ExceptionOr HTMLTextAreaElement::cloned(DOM::Node& copy, bool) { diff --git a/Userland/Libraries/LibWeb/HTML/HTMLTextAreaElement.h b/Userland/Libraries/LibWeb/HTML/HTMLTextAreaElement.h index 726c8119786..eea9c3811da 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLTextAreaElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLTextAreaElement.h @@ -63,6 +63,7 @@ public: virtual bool is_labelable() const override { return true; } virtual void reset_algorithm() override; + virtual void clear_algorithm() override; virtual WebIDL::ExceptionOr cloned(Node&, bool) override;