FormAssociatedElement.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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/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. virtual void clear_algorithm();
  82. String form_action() const;
  83. WebIDL::ExceptionOr<void> set_form_action(String const&);
  84. protected:
  85. FormAssociatedElement() = default;
  86. virtual ~FormAssociatedElement() = default;
  87. virtual void form_associated_element_was_inserted() { }
  88. virtual void form_associated_element_was_removed(DOM::Node*) { }
  89. virtual void form_associated_element_attribute_changed(FlyString const&, Optional<String> const&) { }
  90. void form_node_was_inserted();
  91. void form_node_was_removed();
  92. void form_node_attribute_changed(FlyString const&, Optional<String> const&);
  93. private:
  94. void reset_form_owner();
  95. WeakPtr<HTMLFormElement> m_form;
  96. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#parser-inserted-flag
  97. bool m_parser_inserted { false };
  98. };
  99. enum class SelectionSource {
  100. UI,
  101. DOM,
  102. };
  103. class FormAssociatedTextControlElement : public FormAssociatedElement {
  104. public:
  105. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-relevant-value
  106. virtual String relevant_value() = 0;
  107. virtual WebIDL::ExceptionOr<void> set_relevant_value(String const&) = 0;
  108. virtual void set_dirty_value_flag(bool flag) = 0;
  109. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-select
  110. WebIDL::ExceptionOr<void> select();
  111. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-selectionstart
  112. Optional<WebIDL::UnsignedLong> selection_start() const;
  113. WebIDL::ExceptionOr<void> set_selection_start(Optional<WebIDL::UnsignedLong> const&);
  114. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-selectionend
  115. Optional<WebIDL::UnsignedLong> selection_end() const;
  116. WebIDL::ExceptionOr<void> set_selection_end(Optional<WebIDL::UnsignedLong> const&);
  117. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-selectiondirection
  118. Optional<String> selection_direction() const;
  119. void set_selection_direction(Optional<String> direction);
  120. WebIDL::ExceptionOr<void> set_selection_direction_binding(Optional<String> direction);
  121. SelectionDirection selection_direction_state() const { return m_selection_direction; }
  122. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-setrangetext
  123. WebIDL::ExceptionOr<void> set_range_text(String const& replacement);
  124. WebIDL::ExceptionOr<void> set_range_text(String const& replacement, WebIDL::UnsignedLong start, WebIDL::UnsignedLong end, Bindings::SelectionMode = Bindings::SelectionMode::Preserve);
  125. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-setselectionrange
  126. void set_the_selection_range(Optional<WebIDL::UnsignedLong> start, Optional<WebIDL::UnsignedLong> end, SelectionDirection direction = SelectionDirection::None, SelectionSource source = SelectionSource::DOM);
  127. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-setselectionrange
  128. WebIDL::ExceptionOr<void> set_selection_range(Optional<WebIDL::UnsignedLong> start, Optional<WebIDL::UnsignedLong> end, Optional<String> direction);
  129. // https://w3c.github.io/selection-api/#dfn-has-scheduled-selectionchange-event
  130. bool has_scheduled_selectionchange_event() const { return m_has_scheduled_selectionchange_event; }
  131. void set_scheduled_selectionchange_event(bool value) { m_has_scheduled_selectionchange_event = value; }
  132. protected:
  133. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-relevant-value
  134. void relevant_value_was_changed(JS::GCPtr<DOM::Text>);
  135. virtual void selection_was_changed([[maybe_unused]] size_t selection_start, [[maybe_unused]] size_t selection_end) { }
  136. private:
  137. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-selection
  138. WebIDL::UnsignedLong m_selection_start { 0 };
  139. WebIDL::UnsignedLong m_selection_end { 0 };
  140. SelectionDirection m_selection_direction { SelectionDirection::None };
  141. // https://w3c.github.io/selection-api/#dfn-has-scheduled-selectionchange-event
  142. bool m_has_scheduled_selectionchange_event { false };
  143. };
  144. }