ソースを参照

LibWeb: Support HTMLFormElement.elements and HTMLFormElement.length

Note that we implement .elements as a HTMLCollection for now, instead of
the correct HTMLFormControlsCollection subclass. This covers most
use-cases already.

1% progression on ACID3. :^)
Andreas Kling 3 年 前
コミット
fa17776a51

+ 42 - 0
Userland/Libraries/LibWeb/HTML/HTMLFormElement.cpp

@@ -8,8 +8,14 @@
 #include <LibWeb/DOM/Document.h>
 #include <LibWeb/HTML/BrowsingContext.h>
 #include <LibWeb/HTML/EventNames.h>
+#include <LibWeb/HTML/HTMLButtonElement.h>
+#include <LibWeb/HTML/HTMLFieldSetElement.h>
 #include <LibWeb/HTML/HTMLFormElement.h>
 #include <LibWeb/HTML/HTMLInputElement.h>
+#include <LibWeb/HTML/HTMLObjectElement.h>
+#include <LibWeb/HTML/HTMLOutputElement.h>
+#include <LibWeb/HTML/HTMLSelectElement.h>
+#include <LibWeb/HTML/HTMLTextAreaElement.h>
 #include <LibWeb/HTML/SubmitEvent.h>
 #include <LibWeb/Page/Page.h>
 #include <LibWeb/URL/URL.h>
@@ -152,4 +158,40 @@ String HTMLFormElement::action() const
     return value;
 }
 
+static bool is_form_control(DOM::Element const& element)
+{
+    if (is<HTMLButtonElement>(element)
+        || is<HTMLFieldSetElement>(element)
+        || is<HTMLObjectElement>(element)
+        || is<HTMLOutputElement>(element)
+        || is<HTMLSelectElement>(element)
+        || is<HTMLTextAreaElement>(element)) {
+        return true;
+    }
+
+    if (is<HTMLInputElement>(element)
+        && !element.get_attribute(HTML::AttributeNames::type).equals_ignoring_case("image")) {
+        return true;
+    }
+
+    return false;
+}
+
+// https://html.spec.whatwg.org/multipage/forms.html#dom-form-elements
+NonnullRefPtr<DOM::HTMLCollection> HTMLFormElement::elements() const
+{
+    // FIXME: This should return the same HTMLFormControlsCollection object every time,
+    //        but that would cause a reference cycle since HTMLCollection refs the root.
+    return DOM::HTMLCollection::create(const_cast<HTMLFormElement&>(*this), [this](Element const& element) {
+        return is_form_control(element);
+    });
+}
+
+// https://html.spec.whatwg.org/multipage/forms.html#dom-form-length
+unsigned HTMLFormElement::length() const
+{
+    // The length IDL attribute must return the number of nodes represented by the elements collection.
+    return elements()->length();
+}
+
 }

+ 3 - 0
Userland/Libraries/LibWeb/HTML/HTMLFormElement.h

@@ -29,6 +29,9 @@ public:
     void add_associated_element(Badge<FormAssociatedElement>, HTMLElement&);
     void remove_associated_element(Badge<FormAssociatedElement>, HTMLElement&);
 
+    NonnullRefPtr<DOM::HTMLCollection> elements() const;
+    unsigned length() const;
+
 private:
     bool m_firing_submission_events { false };
 

+ 6 - 0
Userland/Libraries/LibWeb/HTML/HTMLFormElement.idl

@@ -1,3 +1,4 @@
+#import <DOM/HTMLCollection.idl>
 #import <HTML/HTMLElement.idl>
 
 interface HTMLFormElement : HTMLElement {
@@ -9,4 +10,9 @@ interface HTMLFormElement : HTMLElement {
 
     undefined submit();
 
+    // FIXME: Should be [SameObject] and a HTMLFormControlsCollection
+    readonly attribute HTMLCollection elements;
+
+    readonly attribute unsigned long length;
+
 };