Browse Source

LibWeb: Implement cloning steps for `HTMLInputElement`

Tim Ledbetter 11 tháng trước cách đây
mục cha
commit
71cfa705d1

+ 2 - 0
Tests/LibWeb/Text/expected/HTML/HTMLInputElement-cloning-steps.txt

@@ -0,0 +1,2 @@
+  Cloned checkbox checked: true
+Cloned text input value: PASS

+ 23 - 0
Tests/LibWeb/Text/input/HTML/HTMLInputElement-cloning-steps.html

@@ -0,0 +1,23 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<form>
+    <input id="checkedCheckbox" type="checkbox">
+    <input type="text" value="FAIL">
+</form>
+<script>
+    test(() => {
+        const form = document.forms[0];
+        const inputs = form.getElementsByTagName("input");
+        inputs[0].checked = true;
+        inputs[1].value = "PASS";
+
+        const clone = form.cloneNode(true);
+        document.body.appendChild(clone);
+
+        println(`Cloned checkbox checked: ${clone.querySelector("input[type=checkbox]").checked}`);
+        println(`Cloned text input value: ${clone.querySelector("input[type=text]").value}`);
+
+        form.remove();
+        clone.remove();
+    });
+</script>

+ 12 - 0
Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp

@@ -1446,6 +1446,18 @@ void HTMLInputElement::form_associated_element_was_removed(DOM::Node*)
     set_shadow_root(nullptr);
 }
 
+// https://html.spec.whatwg.org/multipage/input.html#the-input-element%3Aconcept-node-clone-ext
+WebIDL::ExceptionOr<void> HTMLInputElement::cloned(DOM::Node& copy, bool)
+{
+    // The cloning steps for input elements must propagate the value, dirty value flag, checkedness, and dirty checkedness flag from the node being cloned to the copy.
+    auto& input_clone = verify_cast<HTMLInputElement>(copy);
+    input_clone.m_value = m_value;
+    input_clone.m_dirty_value = m_dirty_value;
+    input_clone.m_checked = m_checked;
+    input_clone.m_dirty_checkedness = m_dirty_checkedness;
+    return {};
+}
+
 // https://html.spec.whatwg.org/multipage/input.html#radio-button-group
 static bool is_in_same_radio_button_group(HTML::HTMLInputElement const& a, HTML::HTMLInputElement const& b)
 {

+ 2 - 0
Userland/Libraries/LibWeb/HTML/HTMLInputElement.h

@@ -176,6 +176,8 @@ public:
     virtual void form_associated_element_was_removed(DOM::Node*) override;
     virtual void form_associated_element_attribute_changed(FlyString const&, Optional<String> const&) override;
 
+    virtual WebIDL::ExceptionOr<void> cloned(Node&, bool) override;
+
     JS::NonnullGCPtr<ValidityState const> validity() const;
 
     // ^HTMLElement