HTMLButtonElement.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLButtonElementPrototype>(realm, "HTMLButtonElement"));
  20. }
  21. HTMLButtonElement::TypeAttributeState HTMLButtonElement::type_state() const
  22. {
  23. auto value = deprecated_attribute(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. DeprecatedString HTMLButtonElement::value() const
  51. {
  52. if (!has_attribute(AttributeNames::value))
  53. return DeprecatedString::empty();
  54. return deprecated_attribute(AttributeNames::value);
  55. }
  56. bool HTMLButtonElement::has_activation_behavior() const
  57. {
  58. return true;
  59. }
  60. void HTMLButtonElement::activation_behavior(DOM::Event const&)
  61. {
  62. // https://html.spec.whatwg.org/multipage/form-elements.html#the-button-element:activation-behaviour
  63. // 1. If element is disabled, then return.
  64. if (!enabled())
  65. return;
  66. // 2. If element does not have a form owner, then return.
  67. if (!form())
  68. return;
  69. // 3. If element's node document is not fully active, then return.
  70. if (!this->document().is_fully_active())
  71. return;
  72. // 4. Switch on element's type attribute's state:
  73. switch (type_state()) {
  74. case TypeAttributeState::Submit:
  75. // Submit Button
  76. // Submit element's form owner from element.
  77. form()->submit_form(*this).release_value_but_fixme_should_propagate_errors();
  78. break;
  79. case TypeAttributeState::Reset:
  80. // Reset Button
  81. // Reset element's form owner.
  82. form()->reset_form();
  83. break;
  84. case TypeAttributeState::Button:
  85. // Button
  86. // Do nothing.
  87. break;
  88. default:
  89. VERIFY_NOT_REACHED();
  90. }
  91. }
  92. }