FormAssociatedElement.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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/String.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. \
  40. virtual void attribute_changed(FlyString const& name, Optional<String> const& value) override \
  41. { \
  42. ElementBaseClass::attribute_changed(name, value); \
  43. form_node_attribute_changed(name, value); \
  44. form_associated_element_attribute_changed(name, value); \
  45. }
  46. class FormAssociatedElement {
  47. public:
  48. HTMLFormElement* form() { return m_form; }
  49. HTMLFormElement const* form() const { return m_form; }
  50. void set_form(HTMLFormElement*);
  51. void element_id_changed(Badge<DOM::Document>);
  52. void element_with_id_was_added_or_removed(Badge<DOM::Document>);
  53. bool enabled() const;
  54. void set_parser_inserted(Badge<HTMLParser>);
  55. // https://html.spec.whatwg.org/multipage/forms.html#category-listed
  56. virtual bool is_listed() const { return false; }
  57. // https://html.spec.whatwg.org/multipage/forms.html#category-submit
  58. virtual bool is_submittable() const { return false; }
  59. // https://html.spec.whatwg.org/multipage/forms.html#category-reset
  60. virtual bool is_resettable() const { return false; }
  61. // https://html.spec.whatwg.org/multipage/forms.html#category-autocapitalize
  62. virtual bool is_auto_capitalize_inheriting() const { return false; }
  63. // https://html.spec.whatwg.org/multipage/forms.html#concept-button
  64. virtual bool is_button() const { return false; }
  65. // https://html.spec.whatwg.org/multipage/forms.html#concept-submit-button
  66. virtual bool is_submit_button() const { return false; }
  67. virtual String value() const { return String {}; }
  68. virtual HTMLElement& form_associated_element_to_html_element() = 0;
  69. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-form-reset-control
  70. virtual void reset_algorithm() {};
  71. protected:
  72. FormAssociatedElement() = default;
  73. virtual ~FormAssociatedElement() = default;
  74. virtual void form_associated_element_was_inserted() { }
  75. virtual void form_associated_element_was_removed(DOM::Node*) { }
  76. virtual void form_associated_element_attribute_changed(FlyString const&, Optional<String> const&) { }
  77. void form_node_was_inserted();
  78. void form_node_was_removed();
  79. void form_node_attribute_changed(FlyString const&, Optional<String> const&);
  80. private:
  81. WeakPtr<HTMLFormElement> m_form;
  82. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#parser-inserted-flag
  83. bool m_parser_inserted { false };
  84. void reset_form_owner();
  85. };
  86. }