HTMLButtonElement.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. HTMLButtonElement::HTMLButtonElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  11. : HTMLElement(document, move(qualified_name))
  12. {
  13. // https://html.spec.whatwg.org/multipage/form-elements.html#the-button-element:activation-behaviour
  14. activation_behavior = [this](auto&) {
  15. // 1. If element is disabled, then return.
  16. if (!enabled())
  17. return;
  18. // 2. If element does not have a form owner, then return.
  19. if (!form())
  20. return;
  21. // 3. If element's node document is not fully active, then return.
  22. if (!this->document().is_fully_active())
  23. return;
  24. // 4. Switch on element's type attribute's state:
  25. switch (type_state()) {
  26. case TypeAttributeState::Submit:
  27. // Submit Button
  28. // Submit element's form owner from element.
  29. form()->submit_form(*this).release_value_but_fixme_should_propagate_errors();
  30. break;
  31. case TypeAttributeState::Reset:
  32. // Reset Button
  33. // Reset element's form owner.
  34. form()->reset_form();
  35. break;
  36. case TypeAttributeState::Button:
  37. // Button
  38. // Do nothing.
  39. break;
  40. default:
  41. VERIFY_NOT_REACHED();
  42. }
  43. };
  44. }
  45. HTMLButtonElement::~HTMLButtonElement() = default;
  46. void HTMLButtonElement::initialize(JS::Realm& realm)
  47. {
  48. Base::initialize(realm);
  49. set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLButtonElementPrototype>(realm, "HTMLButtonElement"));
  50. }
  51. StringView HTMLButtonElement::type() const
  52. {
  53. auto value = deprecated_attribute(HTML::AttributeNames::type);
  54. #define __ENUMERATE_HTML_BUTTON_TYPE_ATTRIBUTE(keyword, _) \
  55. if (value.equals_ignoring_ascii_case(#keyword##sv)) \
  56. return #keyword##sv;
  57. ENUMERATE_HTML_BUTTON_TYPE_ATTRIBUTES
  58. #undef __ENUMERATE_HTML_BUTTON_TYPE_ATTRIBUTE
  59. // The missing value default and invalid value default are the Submit Button state.
  60. return "submit"sv;
  61. }
  62. HTMLButtonElement::TypeAttributeState HTMLButtonElement::type_state() const
  63. {
  64. auto value = deprecated_attribute(HTML::AttributeNames::type);
  65. #define __ENUMERATE_HTML_BUTTON_TYPE_ATTRIBUTE(keyword, state) \
  66. if (value.equals_ignoring_ascii_case(#keyword##sv)) \
  67. return HTMLButtonElement::TypeAttributeState::state;
  68. ENUMERATE_HTML_BUTTON_TYPE_ATTRIBUTES
  69. #undef __ENUMERATE_HTML_BUTTON_TYPE_ATTRIBUTE
  70. // The missing value default and invalid value default are the Submit Button state.
  71. return HTMLButtonElement::TypeAttributeState::Submit;
  72. }
  73. WebIDL::ExceptionOr<void> HTMLButtonElement::set_type(String const& type)
  74. {
  75. return set_attribute(HTML::AttributeNames::type, type);
  76. }
  77. // https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
  78. i32 HTMLButtonElement::default_tab_index_value() const
  79. {
  80. // See the base function for the spec comments.
  81. return 0;
  82. }
  83. // https://html.spec.whatwg.org/multipage/forms.html#concept-submit-button
  84. // https://html.spec.whatwg.org/multipage/form-elements.html#the-button-element:concept-submit-button
  85. bool HTMLButtonElement::is_submit_button() const
  86. {
  87. // If the type attribute is in the Submit Button state, the element is specifically a submit button.
  88. return type_state() == TypeAttributeState::Submit;
  89. }
  90. // https://html.spec.whatwg.org/multipage/form-elements.html#the-button-element:concept-fe-value
  91. DeprecatedString HTMLButtonElement::value() const
  92. {
  93. if (!has_attribute(AttributeNames::value))
  94. return DeprecatedString::empty();
  95. return deprecated_attribute(AttributeNames::value);
  96. }
  97. }