diff options
author | Srikavin Ramkumar <srikavinramkumar@gmail.com> | 2022-12-22 19:32:20 -0500 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-01-03 18:09:40 +0100 |
commit | 7cc6ffe5b77d51a488cfc916a5d0a572a914aed9 (patch) | |
tree | cabc908ce8e3d71611243b1cb5c207a03ba47035 /Userland/Libraries/LibWeb/HTML/HTMLFormElement.cpp | |
parent | 2376385f0c528000468961e338b7c7cb86c6fa2c (diff) | |
download | serenity-7cc6ffe5b77d51a488cfc916a5d0a572a914aed9.zip |
LibWeb: Implement HTMLFormElement::reset
This patch sets up the necessary infrastructure for implementing reset
algorithms for form-associated controls.
Diffstat (limited to 'Userland/Libraries/LibWeb/HTML/HTMLFormElement.cpp')
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/HTMLFormElement.cpp | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/HTML/HTMLFormElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLFormElement.cpp index 93a39dea6a..9149a10c77 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLFormElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLFormElement.cpp @@ -6,6 +6,7 @@ #include <AK/StringBuilder.h> #include <LibWeb/DOM/Document.h> +#include <LibWeb/DOM/Event.h> #include <LibWeb/HTML/BrowsingContext.h> #include <LibWeb/HTML/EventNames.h> #include <LibWeb/HTML/HTMLButtonElement.h> @@ -136,11 +137,49 @@ void HTMLFormElement::submit_form(JS::GCPtr<HTMLElement> submitter, bool from_su page->load(request); } +// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#resetting-a-form +void HTMLFormElement::reset_form() +{ + // 1. Let reset be the result of firing an event named reset at form, with the bubbles and cancelable attributes initialized to true. + auto reset_event = DOM::Event::create(realm(), HTML::EventNames::reset); + reset_event->set_bubbles(true); + reset_event->set_cancelable(true); + + bool reset = dispatch_event(*reset_event); + + // 2. If reset is true, then invoke the reset algorithm of each resettable element whose form owner is form. + if (reset) { + for (auto element : m_associated_elements) { + VERIFY(is<FormAssociatedElement>(*element)); + auto& form_associated_element = dynamic_cast<FormAssociatedElement&>(*element); + if (form_associated_element.is_resettable()) + form_associated_element.reset_algorithm(); + } + } +} + void HTMLFormElement::submit() { submit_form(this, true); } +// https://html.spec.whatwg.org/multipage/forms.html#dom-form-reset +void HTMLFormElement::reset() +{ + // 1. If the form element is marked as locked for reset, then return. + if (m_locked_for_reset) + return; + + // 2. Mark the form element as locked for reset. + m_locked_for_reset = true; + + // 3. Reset the form element. + reset_form(); + + // 4. Unmark the form element as locked for reset. + m_locked_for_reset = false; +} + void HTMLFormElement::add_associated_element(Badge<FormAssociatedElement>, HTMLElement& element) { m_associated_elements.append(element); |