2020-08-01 03:04:26 +01:00
|
|
|
/*
|
2021-04-28 22:46:44 +02:00
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2020-08-01 03:04:26 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-08-01 03:04:26 +01:00
|
|
|
*/
|
|
|
|
|
2022-03-01 21:12:32 +00:00
|
|
|
#include <LibWeb/DOM/Document.h>
|
2020-08-01 03:04:26 +01:00
|
|
|
#include <LibWeb/HTML/HTMLButtonElement.h>
|
2022-03-01 21:12:32 +00:00
|
|
|
#include <LibWeb/HTML/HTMLFormElement.h>
|
2020-08-01 03:04:26 +01:00
|
|
|
|
|
|
|
namespace Web::HTML {
|
|
|
|
|
2022-02-18 21:00:52 +01:00
|
|
|
HTMLButtonElement::HTMLButtonElement(DOM::Document& document, DOM::QualifiedName qualified_name)
|
2022-03-23 18:55:54 -04:00
|
|
|
: HTMLElement(document, move(qualified_name))
|
2020-08-01 03:04:26 +01:00
|
|
|
{
|
2022-03-01 21:12:32 +00:00
|
|
|
// https://html.spec.whatwg.org/multipage/form-elements.html#the-button-element:activation-behaviour
|
|
|
|
activation_behavior = [this](auto&) {
|
|
|
|
// 1. If element is disabled, then return.
|
|
|
|
if (!enabled())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// 2. If element does not have a form owner, then return.
|
|
|
|
if (!form())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// 3. If element's node document is not fully active, then return.
|
|
|
|
if (!this->document().is_fully_active())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// 4. Switch on element's type attribute's state:
|
|
|
|
switch (type_state()) {
|
|
|
|
case TypeAttributeState::Submit:
|
|
|
|
// Submit Button
|
|
|
|
// Submit element's form owner from element.
|
2023-03-01 20:10:01 +01:00
|
|
|
form()->submit_form(this).release_value_but_fixme_should_propagate_errors();
|
2022-03-01 21:12:32 +00:00
|
|
|
break;
|
|
|
|
case TypeAttributeState::Reset:
|
|
|
|
// Reset Button
|
2022-12-22 19:33:18 -05:00
|
|
|
// Reset element's form owner.
|
|
|
|
form()->reset_form();
|
2022-03-01 21:12:32 +00:00
|
|
|
break;
|
|
|
|
case TypeAttributeState::Button:
|
|
|
|
// Button
|
|
|
|
// Do nothing.
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
};
|
2020-08-01 03:04:26 +01:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:21:51 -06:00
|
|
|
HTMLButtonElement::~HTMLButtonElement() = default;
|
2020-08-01 03:04:26 +01:00
|
|
|
|
2023-01-28 12:33:35 -05:00
|
|
|
JS::ThrowCompletionOr<void> HTMLButtonElement::initialize(JS::Realm& realm)
|
2023-01-10 06:28:20 -05:00
|
|
|
{
|
2023-01-28 12:33:35 -05:00
|
|
|
MUST_OR_THROW_OOM(Base::initialize(realm));
|
2023-01-10 06:28:20 -05:00
|
|
|
set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLButtonElementPrototype>(realm, "HTMLButtonElement"));
|
2023-01-28 12:33:35 -05:00
|
|
|
|
|
|
|
return {};
|
2023-01-10 06:28:20 -05:00
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString HTMLButtonElement::type() const
|
2022-03-01 21:06:29 +00:00
|
|
|
{
|
|
|
|
auto value = attribute(HTML::AttributeNames::type);
|
|
|
|
|
|
|
|
#define __ENUMERATE_HTML_BUTTON_TYPE_ATTRIBUTE(keyword, _) \
|
2022-07-11 17:32:29 +00:00
|
|
|
if (value.equals_ignoring_case(#keyword##sv)) \
|
2022-03-01 21:06:29 +00:00
|
|
|
return #keyword;
|
|
|
|
ENUMERATE_HTML_BUTTON_TYPE_ATTRIBUTES
|
|
|
|
#undef __ENUMERATE_HTML_BUTTON_TYPE_ATTRIBUTE
|
|
|
|
|
|
|
|
// The missing value default and invalid value default are the Submit Button state.
|
|
|
|
return "submit";
|
|
|
|
}
|
|
|
|
|
|
|
|
HTMLButtonElement::TypeAttributeState HTMLButtonElement::type_state() const
|
|
|
|
{
|
|
|
|
auto value = attribute(HTML::AttributeNames::type);
|
|
|
|
|
|
|
|
#define __ENUMERATE_HTML_BUTTON_TYPE_ATTRIBUTE(keyword, state) \
|
2022-07-11 17:32:29 +00:00
|
|
|
if (value.equals_ignoring_case(#keyword##sv)) \
|
2022-03-01 21:06:29 +00:00
|
|
|
return HTMLButtonElement::TypeAttributeState::state;
|
|
|
|
ENUMERATE_HTML_BUTTON_TYPE_ATTRIBUTES
|
|
|
|
#undef __ENUMERATE_HTML_BUTTON_TYPE_ATTRIBUTE
|
|
|
|
|
|
|
|
// The missing value default and invalid value default are the Submit Button state.
|
|
|
|
return HTMLButtonElement::TypeAttributeState::Submit;
|
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
void HTMLButtonElement::set_type(DeprecatedString const& type)
|
2022-03-01 21:06:29 +00:00
|
|
|
{
|
2022-10-30 17:50:04 +00:00
|
|
|
MUST(set_attribute(HTML::AttributeNames::type, type));
|
2022-03-01 21:06:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-05 03:58:14 +00:00
|
|
|
// https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
|
|
|
|
i32 HTMLButtonElement::default_tab_index_value() const
|
|
|
|
{
|
|
|
|
// See the base function for the spec comments.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-01 03:04:26 +01:00
|
|
|
}
|