FormAssociatedElement.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/DeprecatedString.h>
  8. #include <AK/WeakPtr.h>
  9. #include <LibWeb/Forward.h>
  10. namespace Web::HTML {
  11. // Form-associated elements should invoke this macro to inject overridden FormAssociatedElement and HTMLElement
  12. // methods as needed. If your class wished to override an HTMLElement method that is overridden here, use the
  13. // following methods instead:
  14. //
  15. // HTMLElement::inserted() -> Use form_associated_element_was_inserted()
  16. // HTMLElement::removed_from() -> Use form_associated_element_was_removed()
  17. //
  18. #define FORM_ASSOCIATED_ELEMENT(ElementBaseClass, ElementClass) \
  19. private: \
  20. virtual HTMLElement& form_associated_element_to_html_element() override \
  21. { \
  22. static_assert(IsBaseOf<HTMLElement, ElementClass>); \
  23. return *this; \
  24. } \
  25. \
  26. virtual void inserted() override \
  27. { \
  28. ElementBaseClass::inserted(); \
  29. form_node_was_inserted(); \
  30. form_associated_element_was_inserted(); \
  31. } \
  32. \
  33. virtual void removed_from(DOM::Node* node) override \
  34. { \
  35. ElementBaseClass::removed_from(node); \
  36. form_node_was_removed(); \
  37. form_associated_element_was_removed(node); \
  38. }
  39. class FormAssociatedElement {
  40. public:
  41. HTMLFormElement* form() { return m_form; }
  42. HTMLFormElement const* form() const { return m_form; }
  43. void set_form(HTMLFormElement*);
  44. bool enabled() const;
  45. void set_parser_inserted(Badge<HTMLParser>);
  46. // https://html.spec.whatwg.org/multipage/forms.html#category-listed
  47. virtual bool is_listed() const { return false; }
  48. // https://html.spec.whatwg.org/multipage/forms.html#category-submit
  49. virtual bool is_submittable() const { return false; }
  50. // https://html.spec.whatwg.org/multipage/forms.html#category-reset
  51. virtual bool is_resettable() const { return false; }
  52. // https://html.spec.whatwg.org/multipage/forms.html#category-autocapitalize
  53. virtual bool is_auto_capitalize_inheriting() const { return false; }
  54. // https://html.spec.whatwg.org/multipage/forms.html#concept-button
  55. virtual bool is_button() const { return false; }
  56. // https://html.spec.whatwg.org/multipage/forms.html#concept-submit-button
  57. virtual bool is_submit_button() const { return false; }
  58. virtual DeprecatedString value() const { return DeprecatedString::empty(); }
  59. virtual HTMLElement& form_associated_element_to_html_element() = 0;
  60. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-form-reset-control
  61. virtual void reset_algorithm() {};
  62. protected:
  63. FormAssociatedElement() = default;
  64. virtual ~FormAssociatedElement() = default;
  65. virtual void form_associated_element_was_inserted() { }
  66. virtual void form_associated_element_was_removed(DOM::Node*) { }
  67. void form_node_was_inserted();
  68. void form_node_was_removed();
  69. private:
  70. WeakPtr<HTMLFormElement> m_form;
  71. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#parser-inserted-flag
  72. bool m_parser_inserted { false };
  73. void reset_form_owner();
  74. };
  75. }