HTMLButtonElement.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. HTMLButtonElement::HTMLButtonElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  12. : HTMLElement(document, move(qualified_name))
  13. {
  14. set_prototype(&window().ensure_web_prototype<Bindings::HTMLButtonElementPrototype>("HTMLButtonElement"));
  15. // https://html.spec.whatwg.org/multipage/form-elements.html#the-button-element:activation-behaviour
  16. activation_behavior = [this](auto&) {
  17. // 1. If element is disabled, then return.
  18. if (!enabled())
  19. return;
  20. // 2. If element does not have a form owner, then return.
  21. if (!form())
  22. return;
  23. // 3. If element's node document is not fully active, then return.
  24. if (!this->document().is_fully_active())
  25. return;
  26. // 4. Switch on element's type attribute's state:
  27. switch (type_state()) {
  28. case TypeAttributeState::Submit:
  29. // Submit Button
  30. // Submit element's form owner from element.
  31. form()->submit_form(this);
  32. break;
  33. case TypeAttributeState::Reset:
  34. // Reset Button
  35. // FIXME: Reset element's form owner.
  36. TODO();
  37. break;
  38. case TypeAttributeState::Button:
  39. // Button
  40. // Do nothing.
  41. break;
  42. default:
  43. VERIFY_NOT_REACHED();
  44. }
  45. };
  46. }
  47. HTMLButtonElement::~HTMLButtonElement() = default;
  48. String HTMLButtonElement::type() const
  49. {
  50. auto value = attribute(HTML::AttributeNames::type);
  51. #define __ENUMERATE_HTML_BUTTON_TYPE_ATTRIBUTE(keyword, _) \
  52. if (value.equals_ignoring_case(#keyword##sv)) \
  53. return #keyword;
  54. ENUMERATE_HTML_BUTTON_TYPE_ATTRIBUTES
  55. #undef __ENUMERATE_HTML_BUTTON_TYPE_ATTRIBUTE
  56. // The missing value default and invalid value default are the Submit Button state.
  57. return "submit";
  58. }
  59. HTMLButtonElement::TypeAttributeState HTMLButtonElement::type_state() const
  60. {
  61. auto value = attribute(HTML::AttributeNames::type);
  62. #define __ENUMERATE_HTML_BUTTON_TYPE_ATTRIBUTE(keyword, state) \
  63. if (value.equals_ignoring_case(#keyword##sv)) \
  64. return HTMLButtonElement::TypeAttributeState::state;
  65. ENUMERATE_HTML_BUTTON_TYPE_ATTRIBUTES
  66. #undef __ENUMERATE_HTML_BUTTON_TYPE_ATTRIBUTE
  67. // The missing value default and invalid value default are the Submit Button state.
  68. return HTMLButtonElement::TypeAttributeState::Submit;
  69. }
  70. void HTMLButtonElement::set_type(String const& type)
  71. {
  72. set_attribute(HTML::AttributeNames::type, type);
  73. }
  74. }