Coverage Report

Created: 2026-02-14 08:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/Userland/Libraries/LibWeb/HTML/HTMLButtonElement.cpp
Line
Count
Source
1
/*
2
 * Copyright (c) 2020, the SerenityOS developers.
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#include <LibWeb/Bindings/HTMLButtonElementPrototype.h>
8
#include <LibWeb/DOM/Document.h>
9
#include <LibWeb/HTML/HTMLButtonElement.h>
10
#include <LibWeb/HTML/HTMLFormElement.h>
11
12
namespace Web::HTML {
13
14
JS_DEFINE_ALLOCATOR(HTMLButtonElement);
15
16
HTMLButtonElement::HTMLButtonElement(DOM::Document& document, DOM::QualifiedName qualified_name)
17
0
    : HTMLElement(document, move(qualified_name))
18
0
{
19
0
}
20
21
0
HTMLButtonElement::~HTMLButtonElement() = default;
22
23
void HTMLButtonElement::initialize(JS::Realm& realm)
24
0
{
25
0
    Base::initialize(realm);
26
0
    WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLButtonElement);
27
0
}
28
29
HTMLButtonElement::TypeAttributeState HTMLButtonElement::type_state() const
30
0
{
31
0
    auto value = get_attribute_value(HTML::AttributeNames::type);
32
33
0
#define __ENUMERATE_HTML_BUTTON_TYPE_ATTRIBUTE(keyword, state) \
34
0
    if (value.equals_ignoring_ascii_case(#keyword##sv))        \
35
0
        return HTMLButtonElement::TypeAttributeState::state;
36
0
    ENUMERATE_HTML_BUTTON_TYPE_ATTRIBUTES
37
0
#undef __ENUMERATE_HTML_BUTTON_TYPE_ATTRIBUTE
38
39
    // The missing value default and invalid value default are the Submit Button state.
40
0
    return HTMLButtonElement::TypeAttributeState::Submit;
41
0
}
42
43
WebIDL::ExceptionOr<void> HTMLButtonElement::set_type(String const& type)
44
0
{
45
0
    return set_attribute(HTML::AttributeNames::type, type);
46
0
}
47
48
// https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
49
i32 HTMLButtonElement::default_tab_index_value() const
50
0
{
51
    // See the base function for the spec comments.
52
0
    return 0;
53
0
}
54
55
// https://html.spec.whatwg.org/multipage/forms.html#concept-submit-button
56
// https://html.spec.whatwg.org/multipage/form-elements.html#the-button-element:concept-submit-button
57
bool HTMLButtonElement::is_submit_button() const
58
0
{
59
    // If the type attribute is in the Submit Button state, the element is specifically a submit button.
60
0
    return type_state() == TypeAttributeState::Submit;
61
0
}
62
63
// https://html.spec.whatwg.org/multipage/form-elements.html#the-button-element:concept-fe-value
64
String HTMLButtonElement::value() const
65
0
{
66
0
    return attribute(AttributeNames::value).value_or(String {});
67
0
}
68
69
bool HTMLButtonElement::has_activation_behavior() const
70
0
{
71
0
    return true;
72
0
}
73
74
void HTMLButtonElement::activation_behavior(DOM::Event const& event)
75
0
{
76
    // https://html.spec.whatwg.org/multipage/form-elements.html#the-button-element:activation-behaviour
77
    // 1. If element is disabled, then return.
78
0
    if (!enabled())
79
0
        return;
80
81
    // 2. If element's node document is not fully active, then return.
82
0
    if (!this->document().is_fully_active())
83
0
        return;
84
85
    // 3. If element has a form owner then switch on element's type attribute's state, then:
86
0
    if (form() != nullptr) {
87
0
        switch (type_state()) {
88
0
        case TypeAttributeState::Submit:
89
            // Submit Button
90
            // Submit element's form owner from element with userInvolvement set to event's user navigation involvement.
91
0
            form()->submit_form(*this, { .user_involvement = user_navigation_involvement(event) }).release_value_but_fixme_should_propagate_errors();
92
0
            break;
93
0
        case TypeAttributeState::Reset:
94
            // Reset Button
95
            // Reset element's form owner.
96
0
            form()->reset_form();
97
0
            break;
98
0
        case TypeAttributeState::Button:
99
            // Button
100
            // Do nothing.
101
0
            break;
102
0
        default:
103
0
            VERIFY_NOT_REACHED();
104
0
        }
105
0
    }
106
107
    // 4. FIXME: Run the popover target attribute activation behavior given element.
108
0
}
109
110
}