diff options
author | Kenneth Myhra <kennethmyhra@gmail.com> | 2023-02-03 21:50:14 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-02-12 00:18:09 +0000 |
commit | 2363c2a572f4ce66b8e2cf8f0a11cecc5f97ad97 (patch) | |
tree | 1651ac958d129edd15b60e79ac51ad5a257ced99 /Userland/Libraries/LibWeb/HTML/HTMLFormElement.cpp | |
parent | a6cdf6374f391b96919a98255fad57efd321f43c (diff) | |
download | serenity-2363c2a572f4ce66b8e2cf8f0a11cecc5f97ad97.zip |
LibWeb: Implement HTMLFormElement::get_submittable_elements()
This implements a convenience method on HTMLFormElement to retrieve a
list in tree order of submittable elements associated with the form
element.
Diffstat (limited to 'Userland/Libraries/LibWeb/HTML/HTMLFormElement.cpp')
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/HTMLFormElement.cpp | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/HTML/HTMLFormElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLFormElement.cpp index a40da14126..c4310bb0b0 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLFormElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLFormElement.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org> + * Copyright (c) 2023, Kenneth Myhra <kennethmyhra@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -247,4 +248,30 @@ unsigned HTMLFormElement::length() const return elements()->length(); } +// https://html.spec.whatwg.org/multipage/forms.html#category-submit +ErrorOr<Vector<JS::NonnullGCPtr<DOM::Element>>> HTMLFormElement::get_submittable_elements() +{ + Vector<JS::NonnullGCPtr<DOM::Element>> submittable_elements = {}; + for (size_t i = 0; i < elements()->length(); i++) { + auto* element = elements()->item(i); + TRY(populate_vector_with_submittable_elements_in_tree_order(*element, submittable_elements)); + } + return submittable_elements; +} + +ErrorOr<void> HTMLFormElement::populate_vector_with_submittable_elements_in_tree_order(JS::NonnullGCPtr<DOM::Element> element, Vector<JS::NonnullGCPtr<DOM::Element>>& elements) +{ + if (auto* form_associated_element = dynamic_cast<HTML::FormAssociatedElement*>(element.ptr())) { + if (form_associated_element->is_submittable()) + TRY(elements.try_append(element)); + } + + for (size_t i = 0; i < element->children()->length(); i++) { + auto* child = element->children()->item(i); + TRY(populate_vector_with_submittable_elements_in_tree_order(*child, elements)); + } + + return {}; +} + } |