HTMLButtonElement.cpp 3.6 KB

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