Explorar el Código

LibWeb: Implement the form associated element clear algorithm

This is a method defined in the WebDriver spec, but requires access to a
bunch of private fields in these classes, so this is implemented in the
same manner as the reset algorithm.
Timothy Flynn hace 9 meses
padre
commit
516f5f7008

+ 8 - 0
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
 {

+ 2 - 0
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<void> set_form_action(String const&);
 

+ 38 - 1
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();

+ 1 - 0
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;

+ 10 - 0
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({});
+}
+
 }

+ 1 - 0
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<ARIA::Role> default_role() const override { return ARIA::Role::status; }

+ 14 - 0
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<void> HTMLTextAreaElement::cloned(DOM::Node& copy, bool)
 {

+ 1 - 0
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<void> cloned(Node&, bool) override;