HTMLButtonElement.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. void HTMLButtonElement::visit_edges(Visitor& visitor)
  38. {
  39. Base::visit_edges(visitor);
  40. PopoverInvokerElement::visit_edges(visitor);
  41. }
  42. // https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
  43. i32 HTMLButtonElement::default_tab_index_value() const
  44. {
  45. // See the base function for the spec comments.
  46. return 0;
  47. }
  48. // https://html.spec.whatwg.org/multipage/forms.html#concept-submit-button
  49. // https://html.spec.whatwg.org/multipage/form-elements.html#the-button-element:concept-submit-button
  50. bool HTMLButtonElement::is_submit_button() const
  51. {
  52. // If the type attribute is in the Submit Button state, the element is specifically a submit button.
  53. return type_state() == TypeAttributeState::Submit;
  54. }
  55. // https://html.spec.whatwg.org/multipage/form-elements.html#the-button-element:concept-fe-value
  56. String HTMLButtonElement::value() const
  57. {
  58. return attribute(AttributeNames::value).value_or(String {});
  59. }
  60. bool HTMLButtonElement::has_activation_behavior() const
  61. {
  62. return true;
  63. }
  64. void HTMLButtonElement::activation_behavior(DOM::Event const& event)
  65. {
  66. // https://html.spec.whatwg.org/multipage/form-elements.html#the-button-element:activation-behaviour
  67. // 1. If element is disabled, then return.
  68. if (!enabled())
  69. return;
  70. // 2. If element's node document is not fully active, then return.
  71. if (!this->document().is_fully_active())
  72. return;
  73. // 3. If element has a form owner then switch on element's type attribute's state, then:
  74. if (form() != nullptr) {
  75. switch (type_state()) {
  76. case TypeAttributeState::Submit:
  77. // Submit Button
  78. // Submit element's form owner from element with userInvolvement set to event's user navigation involvement.
  79. form()->submit_form(*this, { .user_involvement = user_navigation_involvement(event) }).release_value_but_fixme_should_propagate_errors();
  80. break;
  81. case TypeAttributeState::Reset:
  82. // Reset Button
  83. // Reset element's form owner.
  84. form()->reset_form();
  85. break;
  86. case TypeAttributeState::Button:
  87. // Button
  88. // Do nothing.
  89. break;
  90. default:
  91. VERIFY_NOT_REACHED();
  92. }
  93. }
  94. // 4. FIXME: Run the popover target attribute activation behavior given element.
  95. }
  96. bool HTMLButtonElement::is_focusable() const
  97. {
  98. return enabled();
  99. }
  100. }