HTMLButtonElement.cpp 2.7 KB

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