FormAssociatedElement.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2024, Jelle Raaijmakers <jelle@gmta.nl>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/String.h>
  9. #include <AK/WeakPtr.h>
  10. #include <LibWeb/Bindings/HTMLFormElementPrototype.h>
  11. #include <LibWeb/Forward.h>
  12. #include <LibWeb/WebIDL/Types.h>
  13. namespace Web::HTML {
  14. // Form-associated elements should invoke this macro to inject overridden FormAssociatedElement and HTMLElement
  15. // methods as needed. If your class wished to override an HTMLElement method that is overridden here, use the
  16. // following methods instead:
  17. //
  18. // HTMLElement::inserted() -> Use form_associated_element_was_inserted()
  19. // HTMLElement::removed_from() -> Use form_associated_element_was_removed()
  20. //
  21. #define FORM_ASSOCIATED_ELEMENT(ElementBaseClass, ElementClass) \
  22. private: \
  23. virtual HTMLElement& form_associated_element_to_html_element() override \
  24. { \
  25. static_assert(IsBaseOf<HTMLElement, ElementClass>); \
  26. return *this; \
  27. } \
  28. \
  29. virtual void inserted() override \
  30. { \
  31. ElementBaseClass::inserted(); \
  32. form_node_was_inserted(); \
  33. form_associated_element_was_inserted(); \
  34. } \
  35. \
  36. virtual void removed_from(DOM::Node* node) override \
  37. { \
  38. ElementBaseClass::removed_from(node); \
  39. form_node_was_removed(); \
  40. form_associated_element_was_removed(node); \
  41. } \
  42. \
  43. virtual void attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value) override \
  44. { \
  45. ElementBaseClass::attribute_changed(name, old_value, value); \
  46. form_node_attribute_changed(name, value); \
  47. form_associated_element_attribute_changed(name, value); \
  48. }
  49. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#selection-direction
  50. enum class SelectionDirection {
  51. Forward,
  52. Backward,
  53. None,
  54. };
  55. class FormAssociatedElement {
  56. public:
  57. HTMLFormElement* form() { return m_form; }
  58. HTMLFormElement const* form() const { return m_form; }
  59. void set_form(HTMLFormElement*);
  60. void element_id_changed(Badge<DOM::Document>);
  61. void element_with_id_was_added_or_removed(Badge<DOM::Document>);
  62. bool enabled() const;
  63. void set_parser_inserted(Badge<HTMLParser>);
  64. // https://html.spec.whatwg.org/multipage/forms.html#category-listed
  65. virtual bool is_listed() const { return false; }
  66. // https://html.spec.whatwg.org/multipage/forms.html#category-submit
  67. virtual bool is_submittable() const { return false; }
  68. // https://html.spec.whatwg.org/multipage/forms.html#category-reset
  69. virtual bool is_resettable() const { return false; }
  70. // https://html.spec.whatwg.org/multipage/forms.html#category-autocapitalize
  71. virtual bool is_auto_capitalize_inheriting() const { return false; }
  72. // https://html.spec.whatwg.org/multipage/forms.html#concept-button
  73. virtual bool is_button() const { return false; }
  74. // https://html.spec.whatwg.org/multipage/forms.html#concept-submit-button
  75. virtual bool is_submit_button() const { return false; }
  76. virtual String value() const { return String {}; }
  77. virtual HTMLElement& form_associated_element_to_html_element() = 0;
  78. HTMLElement const& form_associated_element_to_html_element() const { return const_cast<FormAssociatedElement&>(*this).form_associated_element_to_html_element(); }
  79. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-form-reset-control
  80. virtual void reset_algorithm() {};
  81. String form_action() const;
  82. WebIDL::ExceptionOr<void> set_form_action(String const&);
  83. protected:
  84. FormAssociatedElement() = default;
  85. virtual ~FormAssociatedElement() = default;
  86. virtual void form_associated_element_was_inserted() { }
  87. virtual void form_associated_element_was_removed(DOM::Node*) { }
  88. virtual void form_associated_element_attribute_changed(FlyString const&, Optional<String> const&) { }
  89. void form_node_was_inserted();
  90. void form_node_was_removed();
  91. void form_node_attribute_changed(FlyString const&, Optional<String> const&);
  92. private:
  93. void reset_form_owner();
  94. WeakPtr<HTMLFormElement> m_form;
  95. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#parser-inserted-flag
  96. bool m_parser_inserted { false };
  97. };
  98. class FormAssociatedTextControlElement : public FormAssociatedElement {
  99. public:
  100. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-relevant-value
  101. virtual String relevant_value() = 0;
  102. virtual WebIDL::ExceptionOr<void> set_relevant_value(String const&) = 0;
  103. virtual void set_dirty_value_flag(bool flag) = 0;
  104. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-select
  105. WebIDL::ExceptionOr<void> select();
  106. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-selectionstart
  107. Optional<WebIDL::UnsignedLong> selection_start() const;
  108. WebIDL::ExceptionOr<void> set_selection_start(Optional<WebIDL::UnsignedLong> const&);
  109. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-selectionend
  110. Optional<WebIDL::UnsignedLong> selection_end() const;
  111. WebIDL::ExceptionOr<void> set_selection_end(Optional<WebIDL::UnsignedLong> const&);
  112. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-selectiondirection
  113. Optional<String> selection_direction() const;
  114. void set_selection_direction(Optional<String> direction);
  115. SelectionDirection selection_direction_state() const { return m_selection_direction; }
  116. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-setrangetext
  117. WebIDL::ExceptionOr<void> set_range_text(String const& replacement);
  118. WebIDL::ExceptionOr<void> set_range_text(String const& replacement, WebIDL::UnsignedLong start, WebIDL::UnsignedLong end, Bindings::SelectionMode = Bindings::SelectionMode::Preserve);
  119. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-setselectionrange
  120. void set_the_selection_range(Optional<WebIDL::UnsignedLong> start, Optional<WebIDL::UnsignedLong> end, SelectionDirection direction = SelectionDirection::None);
  121. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-setselectionrange
  122. WebIDL::ExceptionOr<void> set_selection_range(Optional<WebIDL::UnsignedLong> start, Optional<WebIDL::UnsignedLong> end, Optional<String> direction);
  123. protected:
  124. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-relevant-value
  125. void relevant_value_was_changed(JS::GCPtr<DOM::Text>);
  126. virtual void selection_was_changed([[maybe_unused]] size_t selection_start, [[maybe_unused]] size_t selection_end) { }
  127. private:
  128. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-selection
  129. WebIDL::UnsignedLong m_selection_start { 0 };
  130. WebIDL::UnsignedLong m_selection_end { 0 };
  131. SelectionDirection m_selection_direction { SelectionDirection::None };
  132. };
  133. }