summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/HTML/HTMLFormElement.cpp
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2022-02-25 21:19:06 +0100
committerAndreas Kling <kling@serenityos.org>2022-02-25 21:19:06 +0100
commitfa17776a5108f72fb018bd4472f1c206ab3c7d6b (patch)
tree26c4ebbb0d734e2b3922d6a83e9602b432d646db /Userland/Libraries/LibWeb/HTML/HTMLFormElement.cpp
parentfbee0490a3db6ed9a87c2f8cc2e5565f1de1fc1b (diff)
downloadserenity-fa17776a5108f72fb018bd4472f1c206ab3c7d6b.zip
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. :^)
Diffstat (limited to 'Userland/Libraries/LibWeb/HTML/HTMLFormElement.cpp')
-rw-r--r--Userland/Libraries/LibWeb/HTML/HTMLFormElement.cpp42
1 files changed, 42 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/HTML/HTMLFormElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLFormElement.cpp
index 00e3a55113..3ab834a5b3 100644
--- a/Userland/Libraries/LibWeb/HTML/HTMLFormElement.cpp
+++ b/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();
+}
+
}