HTMLButtonElement.cpp 3.5 KB

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