Serenity Operating System
1/*
2 * Copyright (c) 2020, the SerenityOS developers.
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <LibWeb/DOM/Document.h>
8#include <LibWeb/HTML/HTMLButtonElement.h>
9#include <LibWeb/HTML/HTMLFormElement.h>
10
11namespace Web::HTML {
12
13HTMLButtonElement::HTMLButtonElement(DOM::Document& document, DOM::QualifiedName qualified_name)
14 : HTMLElement(document, move(qualified_name))
15{
16 // https://html.spec.whatwg.org/multipage/form-elements.html#the-button-element:activation-behaviour
17 activation_behavior = [this](auto&) {
18 // 1. If element is disabled, then return.
19 if (!enabled())
20 return;
21
22 // 2. If element does not have a form owner, then return.
23 if (!form())
24 return;
25
26 // 3. If element's node document is not fully active, then return.
27 if (!this->document().is_fully_active())
28 return;
29
30 // 4. Switch on element's type attribute's state:
31 switch (type_state()) {
32 case TypeAttributeState::Submit:
33 // Submit Button
34 // Submit element's form owner from element.
35 form()->submit_form(this).release_value_but_fixme_should_propagate_errors();
36 break;
37 case TypeAttributeState::Reset:
38 // Reset Button
39 // Reset element's form owner.
40 form()->reset_form();
41 break;
42 case TypeAttributeState::Button:
43 // Button
44 // Do nothing.
45 break;
46 default:
47 VERIFY_NOT_REACHED();
48 }
49 };
50}
51
52HTMLButtonElement::~HTMLButtonElement() = default;
53
54JS::ThrowCompletionOr<void> HTMLButtonElement::initialize(JS::Realm& realm)
55{
56 MUST_OR_THROW_OOM(Base::initialize(realm));
57 set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLButtonElementPrototype>(realm, "HTMLButtonElement"));
58
59 return {};
60}
61
62DeprecatedString HTMLButtonElement::type() const
63{
64 auto value = attribute(HTML::AttributeNames::type);
65
66#define __ENUMERATE_HTML_BUTTON_TYPE_ATTRIBUTE(keyword, _) \
67 if (value.equals_ignoring_ascii_case(#keyword##sv)) \
68 return #keyword;
69 ENUMERATE_HTML_BUTTON_TYPE_ATTRIBUTES
70#undef __ENUMERATE_HTML_BUTTON_TYPE_ATTRIBUTE
71
72 // The missing value default and invalid value default are the Submit Button state.
73 return "submit";
74}
75
76HTMLButtonElement::TypeAttributeState HTMLButtonElement::type_state() const
77{
78 auto value = attribute(HTML::AttributeNames::type);
79
80#define __ENUMERATE_HTML_BUTTON_TYPE_ATTRIBUTE(keyword, state) \
81 if (value.equals_ignoring_ascii_case(#keyword##sv)) \
82 return HTMLButtonElement::TypeAttributeState::state;
83 ENUMERATE_HTML_BUTTON_TYPE_ATTRIBUTES
84#undef __ENUMERATE_HTML_BUTTON_TYPE_ATTRIBUTE
85
86 // The missing value default and invalid value default are the Submit Button state.
87 return HTMLButtonElement::TypeAttributeState::Submit;
88}
89
90void HTMLButtonElement::set_type(DeprecatedString const& type)
91{
92 MUST(set_attribute(HTML::AttributeNames::type, type));
93}
94
95// https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
96i32 HTMLButtonElement::default_tab_index_value() const
97{
98 // See the base function for the spec comments.
99 return 0;
100}
101
102}