FormAssociatedElement.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #include <LibWeb/WebIDL/Types.h>
  11. namespace Web::HTML {
  12. // Form-associated elements should invoke this macro to inject overridden FormAssociatedElement and HTMLElement
  13. // methods as needed. If your class wished to override an HTMLElement method that is overridden here, use the
  14. // following methods instead:
  15. //
  16. // HTMLElement::inserted() -> Use form_associated_element_was_inserted()
  17. // HTMLElement::removed_from() -> Use form_associated_element_was_removed()
  18. //
  19. #define FORM_ASSOCIATED_ELEMENT(ElementBaseClass, ElementClass) \
  20. private: \
  21. virtual HTMLElement& form_associated_element_to_html_element() override \
  22. { \
  23. static_assert(IsBaseOf<HTMLElement, ElementClass>); \
  24. return *this; \
  25. } \
  26. \
  27. virtual void inserted() override \
  28. { \
  29. ElementBaseClass::inserted(); \
  30. form_node_was_inserted(); \
  31. form_associated_element_was_inserted(); \
  32. } \
  33. \
  34. virtual void removed_from(DOM::Node* node) override \
  35. { \
  36. ElementBaseClass::removed_from(node); \
  37. form_node_was_removed(); \
  38. form_associated_element_was_removed(node); \
  39. } \
  40. \
  41. virtual void attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value) override \
  42. { \
  43. ElementBaseClass::attribute_changed(name, old_value, value); \
  44. form_node_attribute_changed(name, value); \
  45. form_associated_element_attribute_changed(name, value); \
  46. }
  47. class FormAssociatedElement {
  48. public:
  49. HTMLFormElement* form() { return m_form; }
  50. HTMLFormElement const* form() const { return m_form; }
  51. void set_form(HTMLFormElement*);
  52. void element_id_changed(Badge<DOM::Document>);
  53. void element_with_id_was_added_or_removed(Badge<DOM::Document>);
  54. bool enabled() const;
  55. void set_parser_inserted(Badge<HTMLParser>);
  56. // https://html.spec.whatwg.org/multipage/forms.html#category-listed
  57. virtual bool is_listed() const { return false; }
  58. // https://html.spec.whatwg.org/multipage/forms.html#category-submit
  59. virtual bool is_submittable() const { return false; }
  60. // https://html.spec.whatwg.org/multipage/forms.html#category-reset
  61. virtual bool is_resettable() const { return false; }
  62. // https://html.spec.whatwg.org/multipage/forms.html#category-autocapitalize
  63. virtual bool is_auto_capitalize_inheriting() const { return false; }
  64. // https://html.spec.whatwg.org/multipage/forms.html#concept-button
  65. virtual bool is_button() const { return false; }
  66. // https://html.spec.whatwg.org/multipage/forms.html#concept-submit-button
  67. virtual bool is_submit_button() const { return false; }
  68. virtual String value() const { return String {}; }
  69. virtual HTMLElement& form_associated_element_to_html_element() = 0;
  70. HTMLElement const& form_associated_element_to_html_element() const { return const_cast<FormAssociatedElement&>(*this).form_associated_element_to_html_element(); }
  71. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-form-reset-control
  72. virtual void reset_algorithm() {};
  73. WebIDL::UnsignedLong selection_start() const;
  74. WebIDL::ExceptionOr<void> set_selection_start(WebIDL::UnsignedLong);
  75. WebIDL::UnsignedLong selection_end() const;
  76. WebIDL::ExceptionOr<void> set_selection_end(WebIDL::UnsignedLong);
  77. protected:
  78. FormAssociatedElement() = default;
  79. virtual ~FormAssociatedElement() = default;
  80. virtual void form_associated_element_was_inserted() { }
  81. virtual void form_associated_element_was_removed(DOM::Node*) { }
  82. virtual void form_associated_element_attribute_changed(FlyString const&, Optional<String> const&) { }
  83. void form_node_was_inserted();
  84. void form_node_was_removed();
  85. void form_node_attribute_changed(FlyString const&, Optional<String> const&);
  86. private:
  87. WeakPtr<HTMLFormElement> m_form;
  88. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#parser-inserted-flag
  89. bool m_parser_inserted { false };
  90. void reset_form_owner();
  91. };
  92. }