FormAssociatedElement.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <andreas@ladybird.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/DOM/InputEventsTarget.h>
  12. #include <LibWeb/Forward.h>
  13. #include <LibWeb/WebIDL/Types.h>
  14. namespace Web::HTML {
  15. // Form-associated elements should invoke this macro to inject overridden FormAssociatedElement and HTMLElement
  16. // methods as needed. If your class wished to override an HTMLElement method that is overridden here, use the
  17. // following methods instead:
  18. //
  19. // HTMLElement::inserted() -> Use form_associated_element_was_inserted()
  20. // HTMLElement::removed_from() -> Use form_associated_element_was_removed()
  21. //
  22. #define FORM_ASSOCIATED_ELEMENT(ElementBaseClass, ElementClass) \
  23. private: \
  24. virtual HTMLElement& form_associated_element_to_html_element() override \
  25. { \
  26. static_assert(IsBaseOf<HTMLElement, ElementClass>); \
  27. return *this; \
  28. } \
  29. \
  30. virtual void inserted() override \
  31. { \
  32. ElementBaseClass::inserted(); \
  33. form_node_was_inserted(); \
  34. form_associated_element_was_inserted(); \
  35. } \
  36. \
  37. virtual void removed_from(DOM::Node* node) override \
  38. { \
  39. ElementBaseClass::removed_from(node); \
  40. form_node_was_removed(); \
  41. form_associated_element_was_removed(node); \
  42. } \
  43. \
  44. virtual void attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value) override \
  45. { \
  46. ElementBaseClass::attribute_changed(name, old_value, value); \
  47. form_node_attribute_changed(name, value); \
  48. form_associated_element_attribute_changed(name, value); \
  49. }
  50. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#selection-direction
  51. enum class SelectionDirection {
  52. Forward,
  53. Backward,
  54. None,
  55. };
  56. class FormAssociatedElement {
  57. public:
  58. HTMLFormElement* form() { return m_form; }
  59. HTMLFormElement const* form() const { return m_form; }
  60. void set_form(HTMLFormElement*);
  61. void element_id_changed(Badge<DOM::Document>);
  62. void element_with_id_was_added_or_removed(Badge<DOM::Document>);
  63. bool enabled() const;
  64. void set_parser_inserted(Badge<HTMLParser>);
  65. // https://html.spec.whatwg.org/multipage/forms.html#category-listed
  66. virtual bool is_listed() const { return false; }
  67. // https://html.spec.whatwg.org/multipage/forms.html#category-submit
  68. virtual bool is_submittable() const { return false; }
  69. // https://html.spec.whatwg.org/multipage/forms.html#category-reset
  70. virtual bool is_resettable() const { return false; }
  71. // https://html.spec.whatwg.org/multipage/forms.html#category-autocapitalize
  72. virtual bool is_auto_capitalize_inheriting() const { return false; }
  73. // https://html.spec.whatwg.org/multipage/forms.html#concept-button
  74. virtual bool is_button() const { return false; }
  75. // https://html.spec.whatwg.org/multipage/forms.html#concept-submit-button
  76. virtual bool is_submit_button() const { return false; }
  77. virtual String value() const { return String {}; }
  78. virtual HTMLElement& form_associated_element_to_html_element() = 0;
  79. HTMLElement const& form_associated_element_to_html_element() const { return const_cast<FormAssociatedElement&>(*this).form_associated_element_to_html_element(); }
  80. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-form-reset-control
  81. virtual void reset_algorithm() { }
  82. virtual void clear_algorithm();
  83. String form_action() const;
  84. WebIDL::ExceptionOr<void> set_form_action(String const&);
  85. protected:
  86. FormAssociatedElement() = default;
  87. virtual ~FormAssociatedElement() = default;
  88. virtual void form_associated_element_was_inserted() { }
  89. virtual void form_associated_element_was_removed(DOM::Node*) { }
  90. virtual void form_associated_element_attribute_changed(FlyString const&, Optional<String> const&) { }
  91. void form_node_was_inserted();
  92. void form_node_was_removed();
  93. void form_node_attribute_changed(FlyString const&, Optional<String> const&);
  94. private:
  95. void reset_form_owner();
  96. WeakPtr<HTMLFormElement> m_form;
  97. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#parser-inserted-flag
  98. bool m_parser_inserted { false };
  99. };
  100. enum class SelectionSource {
  101. UI,
  102. DOM,
  103. };
  104. class FormAssociatedTextControlElement : public FormAssociatedElement
  105. , public InputEventsTarget {
  106. public:
  107. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-relevant-value
  108. virtual String relevant_value() = 0;
  109. virtual WebIDL::ExceptionOr<void> set_relevant_value(String const&) = 0;
  110. virtual void set_dirty_value_flag(bool flag) = 0;
  111. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-select
  112. WebIDL::ExceptionOr<void> select();
  113. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-selectionstart
  114. Optional<WebIDL::UnsignedLong> selection_start() const;
  115. WebIDL::ExceptionOr<void> set_selection_start(Optional<WebIDL::UnsignedLong> const&);
  116. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-selectionend
  117. Optional<WebIDL::UnsignedLong> selection_end() const;
  118. WebIDL::ExceptionOr<void> set_selection_end(Optional<WebIDL::UnsignedLong> const&);
  119. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-selectiondirection
  120. Optional<String> selection_direction() const;
  121. void set_selection_direction(Optional<String> direction);
  122. WebIDL::ExceptionOr<void> set_selection_direction_binding(Optional<String> direction);
  123. SelectionDirection selection_direction_state() const { return m_selection_direction; }
  124. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-setrangetext
  125. WebIDL::ExceptionOr<void> set_range_text(String const& replacement);
  126. WebIDL::ExceptionOr<void> set_range_text(String const& replacement, WebIDL::UnsignedLong start, WebIDL::UnsignedLong end, Bindings::SelectionMode = Bindings::SelectionMode::Preserve);
  127. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-setselectionrange
  128. void set_the_selection_range(Optional<WebIDL::UnsignedLong> start, Optional<WebIDL::UnsignedLong> end, SelectionDirection direction = SelectionDirection::None, SelectionSource source = SelectionSource::DOM);
  129. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-setselectionrange
  130. WebIDL::ExceptionOr<void> set_selection_range(Optional<WebIDL::UnsignedLong> start, Optional<WebIDL::UnsignedLong> end, Optional<String> direction);
  131. // https://w3c.github.io/selection-api/#dfn-has-scheduled-selectionchange-event
  132. bool has_scheduled_selectionchange_event() const { return m_has_scheduled_selectionchange_event; }
  133. void set_scheduled_selectionchange_event(bool value) { m_has_scheduled_selectionchange_event = value; }
  134. virtual JS::GCPtr<DOM::Text> form_associated_element_to_text_node() = 0;
  135. virtual JS::GCPtr<DOM::Text const> form_associated_element_to_text_node() const { return const_cast<FormAssociatedTextControlElement&>(*this).form_associated_element_to_text_node(); }
  136. virtual void handle_insert(String const&) override;
  137. virtual void handle_delete(DeleteDirection) override;
  138. virtual void handle_return_key() override;
  139. virtual void select_all() override;
  140. virtual void set_selection_anchor(JS::NonnullGCPtr<DOM::Node>, size_t offset) override;
  141. virtual void set_selection_focus(JS::NonnullGCPtr<DOM::Node>, size_t offset) override;
  142. virtual void move_cursor_to_start(CollapseSelection) override;
  143. virtual void move_cursor_to_end(CollapseSelection) override;
  144. virtual void increment_cursor_position_offset(CollapseSelection) override;
  145. virtual void decrement_cursor_position_offset(CollapseSelection) override;
  146. virtual void increment_cursor_position_to_next_word(CollapseSelection) override;
  147. virtual void decrement_cursor_position_to_previous_word(CollapseSelection) override;
  148. JS::GCPtr<DOM::Position> cursor_position() const;
  149. protected:
  150. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-relevant-value
  151. void relevant_value_was_changed();
  152. virtual void selection_was_changed([[maybe_unused]] size_t selection_start, [[maybe_unused]] size_t selection_end) { }
  153. private:
  154. void collapse_selection_to_offset(size_t);
  155. void selection_was_changed();
  156. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-selection
  157. WebIDL::UnsignedLong m_selection_start { 0 };
  158. WebIDL::UnsignedLong m_selection_end { 0 };
  159. SelectionDirection m_selection_direction { SelectionDirection::None };
  160. // https://w3c.github.io/selection-api/#dfn-has-scheduled-selectionchange-event
  161. bool m_has_scheduled_selectionchange_event { false };
  162. };
  163. }