HTMLInputElement.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Adam Hodgen <ant1441@gmail.com>
  4. * Copyright (c) 2023, Bastiaan van der Plaat <bastiaan.v.d.plaat@gmail.com>
  5. * Copyright (c) 2024, Jelle Raaijmakers <jelle@gmta.nl>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #pragma once
  10. #include <LibWeb/DOM/DocumentLoadEventDelayer.h>
  11. #include <LibWeb/DOM/Text.h>
  12. #include <LibWeb/FileAPI/FileList.h>
  13. #include <LibWeb/HTML/ColorPickerUpdateState.h>
  14. #include <LibWeb/HTML/FileFilter.h>
  15. #include <LibWeb/HTML/FormAssociatedElement.h>
  16. #include <LibWeb/HTML/HTMLElement.h>
  17. #include <LibWeb/Layout/ImageProvider.h>
  18. #include <LibWeb/WebIDL/DOMException.h>
  19. #include <LibWeb/WebIDL/Types.h>
  20. namespace Web::HTML {
  21. // https://html.spec.whatwg.org/multipage/input.html#attr-input-type
  22. #define ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTES \
  23. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(hidden, Hidden) \
  24. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(text, Text) \
  25. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(search, Search) \
  26. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(tel, Telephone) \
  27. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(url, URL) \
  28. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(email, Email) \
  29. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(password, Password) \
  30. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(date, Date) \
  31. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(month, Month) \
  32. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(week, Week) \
  33. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(time, Time) \
  34. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE("datetime-local", LocalDateAndTime) \
  35. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(number, Number) \
  36. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(range, Range) \
  37. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(color, Color) \
  38. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(checkbox, Checkbox) \
  39. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(radio, RadioButton) \
  40. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(file, FileUpload) \
  41. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(submit, SubmitButton) \
  42. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(image, ImageButton) \
  43. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(reset, ResetButton) \
  44. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(button, Button)
  45. class HTMLInputElement final
  46. : public HTMLElement
  47. , public FormAssociatedElement
  48. , public DOM::EditableTextNodeOwner
  49. , public Layout::ImageProvider {
  50. WEB_PLATFORM_OBJECT(HTMLInputElement, HTMLElement);
  51. JS_DECLARE_ALLOCATOR(HTMLInputElement);
  52. FORM_ASSOCIATED_ELEMENT(HTMLElement, HTMLInputElement)
  53. public:
  54. virtual ~HTMLInputElement() override;
  55. virtual JS::GCPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
  56. virtual void adjust_computed_style(CSS::StyleProperties&) override;
  57. enum class TypeAttributeState {
  58. #define __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(_, state) state,
  59. ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTES
  60. #undef __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE
  61. };
  62. StringView type() const;
  63. TypeAttributeState type_state() const { return m_type; }
  64. WebIDL::ExceptionOr<void> set_type(String const&);
  65. String default_value() const { return get_attribute_value(HTML::AttributeNames::value); }
  66. virtual String value() const override;
  67. WebIDL::ExceptionOr<void> set_value(String const&);
  68. void commit_pending_changes();
  69. String placeholder() const;
  70. Optional<String> placeholder_value() const;
  71. bool checked() const { return m_checked; }
  72. enum class ChangeSource {
  73. Programmatic,
  74. User,
  75. };
  76. void set_checked(bool, ChangeSource = ChangeSource::Programmatic);
  77. bool checked_binding() const { return checked(); }
  78. void set_checked_binding(bool);
  79. bool indeterminate() const { return m_indeterminate; }
  80. void set_indeterminate(bool);
  81. bool is_mutable() const { return m_is_mutable; }
  82. void did_pick_color(Optional<Color> picked_color, ColorPickerUpdateState state);
  83. void did_select_files(Span<SelectedFile> selected_files);
  84. JS::GCPtr<FileAPI::FileList> files();
  85. void set_files(JS::GCPtr<FileAPI::FileList>);
  86. FileFilter parse_accept_attribute() const;
  87. // NOTE: User interaction
  88. // https://html.spec.whatwg.org/multipage/input.html#update-the-file-selection
  89. void update_the_file_selection(JS::NonnullGCPtr<FileAPI::FileList>);
  90. WebIDL::Long max_length() const;
  91. WebIDL::ExceptionOr<void> set_max_length(WebIDL::Long);
  92. WebIDL::Long min_length() const;
  93. WebIDL::ExceptionOr<void> set_min_length(WebIDL::Long);
  94. unsigned size() const;
  95. WebIDL::ExceptionOr<void> set_size(unsigned value);
  96. struct SelectedCoordinate {
  97. int x { 0 };
  98. int y { 0 };
  99. };
  100. SelectedCoordinate selected_coordinate() const { return m_selected_coordinate; }
  101. JS::Object* value_as_date() const;
  102. WebIDL::ExceptionOr<void> set_value_as_date(Optional<JS::Handle<JS::Object>> const&);
  103. double value_as_number() const;
  104. WebIDL::ExceptionOr<void> set_value_as_number(double value);
  105. WebIDL::ExceptionOr<void> step_up(WebIDL::Long n = 1);
  106. WebIDL::ExceptionOr<void> step_down(WebIDL::Long n = 1);
  107. WebIDL::ExceptionOr<bool> check_validity();
  108. WebIDL::ExceptionOr<bool> report_validity();
  109. void set_custom_validity(String const&);
  110. WebIDL::ExceptionOr<void> show_picker();
  111. // ^DOM::EditableTextNodeOwner
  112. virtual void did_edit_text_node(Badge<DOM::Document>) override;
  113. // ^EventTarget
  114. // https://html.spec.whatwg.org/multipage/interaction.html#the-tabindex-attribute:the-input-element
  115. virtual bool is_focusable() const override { return m_type != TypeAttributeState::Hidden; }
  116. // ^FormAssociatedElement
  117. // https://html.spec.whatwg.org/multipage/forms.html#category-listed
  118. virtual bool is_listed() const override { return true; }
  119. // https://html.spec.whatwg.org/multipage/forms.html#category-submit
  120. virtual bool is_submittable() const override { return true; }
  121. // https://html.spec.whatwg.org/multipage/forms.html#category-reset
  122. virtual bool is_resettable() const override { return true; }
  123. // https://html.spec.whatwg.org/multipage/forms.html#category-autocapitalize
  124. virtual bool is_auto_capitalize_inheriting() const override { return true; }
  125. // https://html.spec.whatwg.org/multipage/forms.html#concept-button
  126. virtual bool is_button() const override;
  127. // https://html.spec.whatwg.org/multipage/forms.html#concept-submit-button
  128. virtual bool is_submit_button() const override;
  129. bool is_single_line() const;
  130. virtual void reset_algorithm() override;
  131. virtual void form_associated_element_was_inserted() override;
  132. virtual void form_associated_element_was_removed(DOM::Node*) override;
  133. virtual void form_associated_element_attribute_changed(FlyString const&, Optional<String> const&) override;
  134. virtual WebIDL::ExceptionOr<void> cloned(Node&, bool) override;
  135. JS::NonnullGCPtr<ValidityState const> validity() const;
  136. // ^HTMLElement
  137. // https://html.spec.whatwg.org/multipage/forms.html#category-label
  138. virtual bool is_labelable() const override { return type_state() != TypeAttributeState::Hidden; }
  139. virtual Optional<ARIA::Role> default_role() const override;
  140. JS::GCPtr<Element> placeholder_element() { return m_placeholder_element; }
  141. JS::GCPtr<Element const> placeholder_element() const { return m_placeholder_element; }
  142. virtual bool has_activation_behavior() const override;
  143. virtual void activation_behavior(DOM::Event const&) override;
  144. bool has_input_activation_behavior() const;
  145. bool change_event_applies() const;
  146. bool value_as_date_applies() const;
  147. bool value_as_number_applies() const;
  148. bool step_applies() const;
  149. bool step_up_or_down_applies() const;
  150. bool select_applies() const;
  151. bool selection_or_range_applies() const;
  152. private:
  153. HTMLInputElement(DOM::Document&, DOM::QualifiedName);
  154. // ^DOM::Node
  155. virtual bool is_html_input_element() const final { return true; }
  156. // ^DOM::EventTarget
  157. virtual void did_lose_focus() override;
  158. virtual void did_receive_focus() override;
  159. virtual void legacy_pre_activation_behavior() override;
  160. virtual void legacy_cancelled_activation_behavior() override;
  161. virtual void legacy_cancelled_activation_behavior_was_not_called() override;
  162. // ^DOM::Element
  163. virtual i32 default_tab_index_value() const override;
  164. virtual void computed_css_values_changed() override;
  165. // https://html.spec.whatwg.org/multipage/input.html#image-button-state-(type=image):dimension-attributes
  166. virtual bool supports_dimension_attributes() const override { return type_state() == TypeAttributeState::ImageButton; }
  167. // ^Layout::ImageProvider
  168. virtual bool is_image_available() const override;
  169. virtual Optional<CSSPixels> intrinsic_width() const override;
  170. virtual Optional<CSSPixels> intrinsic_height() const override;
  171. virtual Optional<CSSPixelFraction> intrinsic_aspect_ratio() const override;
  172. virtual RefPtr<Gfx::ImmutableBitmap> current_image_bitmap(Gfx::IntSize = {}) const override;
  173. virtual void set_visible_in_viewport(bool) override;
  174. virtual JS::NonnullGCPtr<DOM::Element const> to_html_element() const override { return *this; }
  175. virtual void initialize(JS::Realm&) override;
  176. virtual void visit_edges(Cell::Visitor&) override;
  177. Optional<double> convert_string_to_number(StringView input) const;
  178. String convert_number_to_string(double input) const;
  179. WebIDL::ExceptionOr<JS::GCPtr<JS::Date>> convert_string_to_date(StringView input) const;
  180. String covert_date_to_string(JS::NonnullGCPtr<JS::Date> input) const;
  181. Optional<double> min() const;
  182. Optional<double> max() const;
  183. double default_step() const;
  184. double step_scale_factor() const;
  185. Optional<double> allowed_value_step() const;
  186. double step_base() const;
  187. WebIDL::ExceptionOr<void> step_up_or_down(bool is_down, WebIDL::Long n);
  188. static TypeAttributeState parse_type_attribute(StringView);
  189. void create_shadow_tree_if_needed();
  190. void update_shadow_tree();
  191. void create_button_input_shadow_tree();
  192. void create_text_input_shadow_tree();
  193. void create_color_input_shadow_tree();
  194. void create_file_input_shadow_tree();
  195. void create_range_input_shadow_tree();
  196. WebIDL::ExceptionOr<void> run_input_activation_behavior(DOM::Event const&);
  197. void set_checked_within_group();
  198. void handle_maxlength_attribute();
  199. void handle_readonly_attribute(Optional<String> const& value);
  200. WebIDL::ExceptionOr<void> handle_src_attribute(String const& value);
  201. void user_interaction_did_change_input_value();
  202. // https://html.spec.whatwg.org/multipage/input.html#value-sanitization-algorithm
  203. String value_sanitization_algorithm(String const&) const;
  204. enum class ValueAttributeMode {
  205. Value,
  206. Default,
  207. DefaultOn,
  208. Filename,
  209. };
  210. ValueAttributeMode value_attribute_mode() const;
  211. void update_placeholder_visibility();
  212. JS::GCPtr<DOM::Element> m_placeholder_element;
  213. JS::GCPtr<DOM::Text> m_placeholder_text_node;
  214. void update_text_input_shadow_tree();
  215. JS::GCPtr<DOM::Element> m_inner_text_element;
  216. JS::GCPtr<DOM::Text> m_text_node;
  217. bool m_checked { false };
  218. void update_color_well_element();
  219. JS::GCPtr<DOM::Element> m_color_well_element;
  220. void update_file_input_shadow_tree();
  221. JS::GCPtr<DOM::Element> m_file_button;
  222. JS::GCPtr<DOM::Element> m_file_label;
  223. void update_slider_shadow_tree_elements();
  224. JS::GCPtr<DOM::Element> m_slider_thumb;
  225. JS::GCPtr<DOM::Element> m_slider_progress_element;
  226. JS::GCPtr<DecodedImageData> image_data() const;
  227. JS::GCPtr<SharedResourceRequest> m_resource_request;
  228. SelectedCoordinate m_selected_coordinate;
  229. Optional<DOM::DocumentLoadEventDelayer> m_load_event_delayer;
  230. // https://html.spec.whatwg.org/multipage/input.html#dom-input-indeterminate
  231. bool m_indeterminate { false };
  232. // https://html.spec.whatwg.org/multipage/input.html#concept-input-checked-dirty-flag
  233. bool m_dirty_checkedness { false };
  234. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-fe-dirty
  235. bool m_dirty_value { false };
  236. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:concept-fe-mutable
  237. bool m_is_mutable { true };
  238. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:legacy-pre-activation-behavior
  239. bool m_before_legacy_pre_activation_behavior_checked { false };
  240. bool m_before_legacy_pre_activation_behavior_indeterminate { false };
  241. JS::GCPtr<HTMLInputElement> m_legacy_pre_activation_behavior_checked_element_in_group;
  242. // https://html.spec.whatwg.org/multipage/input.html#concept-input-type-file-selected
  243. JS::GCPtr<FileAPI::FileList> m_selected_files;
  244. TypeAttributeState m_type { TypeAttributeState::Text };
  245. String m_value;
  246. String m_last_src_value;
  247. bool m_has_uncommitted_changes { false };
  248. };
  249. }
  250. namespace Web::DOM {
  251. template<>
  252. inline bool Node::fast_is<HTML::HTMLInputElement>() const { return is_html_input_element(); }
  253. }