1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 03:08:13 +00:00

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.
This commit is contained in:
Kenneth Myhra 2023-02-03 21:50:14 +01:00 committed by Linus Groh
parent a6cdf6374f
commit 2363c2a572
2 changed files with 31 additions and 0 deletions

View file

@ -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 {};
}
}

View file

@ -34,6 +34,8 @@ public:
void add_associated_element(Badge<FormAssociatedElement>, HTMLElement&);
void remove_associated_element(Badge<FormAssociatedElement>, HTMLElement&);
ErrorOr<Vector<JS::NonnullGCPtr<DOM::Element>>> get_submittable_elements();
JS::NonnullGCPtr<DOM::HTMLCollection> elements() const;
unsigned length() const;
@ -46,6 +48,8 @@ private:
virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override;
ErrorOr<void> populate_vector_with_submittable_elements_in_tree_order(JS::NonnullGCPtr<DOM::Element> element, Vector<JS::NonnullGCPtr<DOM::Element>>& elements);
bool m_firing_submission_events { false };
// https://html.spec.whatwg.org/multipage/forms.html#locked-for-reset