HTMLInputElement.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <andreas@ladybird.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@ladybird.org>
  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 FormAssociatedTextControlElement
  48. , public Layout::ImageProvider {
  49. WEB_PLATFORM_OBJECT(HTMLInputElement, HTMLElement);
  50. GC_DECLARE_ALLOCATOR(HTMLInputElement);
  51. FORM_ASSOCIATED_ELEMENT(HTMLElement, HTMLInputElement)
  52. public:
  53. virtual ~HTMLInputElement() override;
  54. virtual GC::Ptr<Layout::Node> create_layout_node(CSS::StyleProperties) override;
  55. virtual void adjust_computed_style(CSS::StyleProperties&) override;
  56. enum class TypeAttributeState {
  57. #define __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(_, state) state,
  58. ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTES
  59. #undef __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE
  60. };
  61. StringView type() const;
  62. TypeAttributeState type_state() const { return m_type; }
  63. WebIDL::ExceptionOr<void> set_type(String const&);
  64. String default_value() const { return get_attribute_value(HTML::AttributeNames::value); }
  65. virtual String value() const override;
  66. WebIDL::ExceptionOr<void> set_value(String const&);
  67. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-relevant-value
  68. virtual String relevant_value() override { return value(); }
  69. WebIDL::ExceptionOr<void> set_relevant_value(String const& value) override { return set_value(value); }
  70. virtual void set_dirty_value_flag(bool flag) override { m_dirty_value = flag; }
  71. void commit_pending_changes();
  72. String placeholder() const;
  73. Optional<String> placeholder_value() const;
  74. bool checked() const { return m_checked; }
  75. enum class ChangeSource {
  76. Programmatic,
  77. User,
  78. };
  79. void set_checked(bool, ChangeSource = ChangeSource::Programmatic);
  80. bool checked_binding() const { return checked(); }
  81. void set_checked_binding(bool);
  82. bool indeterminate() const { return m_indeterminate; }
  83. void set_indeterminate(bool);
  84. bool is_mutable() const { return m_is_mutable; }
  85. void did_pick_color(Optional<Color> picked_color, ColorPickerUpdateState state);
  86. enum class MultipleHandling {
  87. Replace,
  88. Append,
  89. };
  90. void did_select_files(Span<SelectedFile> selected_files, MultipleHandling = MultipleHandling::Replace);
  91. GC::Ptr<FileAPI::FileList> files();
  92. void set_files(GC::Ptr<FileAPI::FileList>);
  93. FileFilter parse_accept_attribute() const;
  94. // NOTE: User interaction
  95. // https://html.spec.whatwg.org/multipage/input.html#update-the-file-selection
  96. void update_the_file_selection(GC::Ref<FileAPI::FileList>);
  97. WebIDL::Long max_length() const;
  98. WebIDL::ExceptionOr<void> set_max_length(WebIDL::Long);
  99. WebIDL::Long min_length() const;
  100. WebIDL::ExceptionOr<void> set_min_length(WebIDL::Long);
  101. unsigned size() const;
  102. WebIDL::ExceptionOr<void> set_size(unsigned value);
  103. struct SelectedCoordinate {
  104. int x { 0 };
  105. int y { 0 };
  106. };
  107. SelectedCoordinate selected_coordinate() const { return m_selected_coordinate; }
  108. JS::Object* value_as_date() const;
  109. WebIDL::ExceptionOr<void> set_value_as_date(Optional<GC::Root<JS::Object>> const&);
  110. double value_as_number() const;
  111. WebIDL::ExceptionOr<void> set_value_as_number(double value);
  112. WebIDL::ExceptionOr<void> step_up(WebIDL::Long n = 1);
  113. WebIDL::ExceptionOr<void> step_down(WebIDL::Long n = 1);
  114. WebIDL::ExceptionOr<bool> check_validity();
  115. WebIDL::ExceptionOr<bool> report_validity();
  116. void set_custom_validity(String const&);
  117. WebIDL::ExceptionOr<void> show_picker();
  118. // ^EventTarget
  119. // https://html.spec.whatwg.org/multipage/interaction.html#the-tabindex-attribute:the-input-element
  120. // https://html.spec.whatwg.org/multipage/interaction.html#focusable-area
  121. // https://html.spec.whatwg.org/multipage/semantics-other.html#concept-element-disabled
  122. virtual bool is_focusable() const override;
  123. // ^FormAssociatedElement
  124. // https://html.spec.whatwg.org/multipage/forms.html#category-listed
  125. virtual bool is_listed() const override { return true; }
  126. // https://html.spec.whatwg.org/multipage/forms.html#category-submit
  127. virtual bool is_submittable() const override { return true; }
  128. // https://html.spec.whatwg.org/multipage/forms.html#category-reset
  129. virtual bool is_resettable() const override { return true; }
  130. // https://html.spec.whatwg.org/multipage/forms.html#category-autocapitalize
  131. virtual bool is_auto_capitalize_inheriting() const override { return true; }
  132. // https://html.spec.whatwg.org/multipage/forms.html#concept-button
  133. virtual bool is_button() const override;
  134. // https://html.spec.whatwg.org/multipage/forms.html#concept-submit-button
  135. virtual bool is_submit_button() const override;
  136. bool is_single_line() const;
  137. virtual void reset_algorithm() override;
  138. virtual void clear_algorithm() override;
  139. virtual void form_associated_element_was_inserted() override;
  140. virtual void form_associated_element_was_removed(DOM::Node*) override;
  141. virtual void form_associated_element_attribute_changed(FlyString const&, Optional<String> const&) override;
  142. virtual WebIDL::ExceptionOr<void> cloned(Node&, bool) override;
  143. GC::Ref<ValidityState const> validity() const;
  144. // ^HTMLElement
  145. // https://html.spec.whatwg.org/multipage/forms.html#category-label
  146. virtual bool is_labelable() const override { return type_state() != TypeAttributeState::Hidden; }
  147. virtual Optional<ARIA::Role> default_role() const override;
  148. GC::Ptr<Element> placeholder_element() { return m_placeholder_element; }
  149. GC::Ptr<Element const> placeholder_element() const { return m_placeholder_element; }
  150. virtual bool has_activation_behavior() const override;
  151. virtual void activation_behavior(DOM::Event const&) override;
  152. bool has_input_activation_behavior() const;
  153. bool change_event_applies() const;
  154. bool value_as_date_applies() const;
  155. bool value_as_number_applies() const;
  156. bool step_applies() const;
  157. bool step_up_or_down_applies() const;
  158. bool select_applies() const;
  159. bool selection_or_range_applies() const;
  160. bool selection_direction_applies() const;
  161. bool has_selectable_text() const;
  162. static bool selection_or_range_applies_for_type_state(TypeAttributeState);
  163. Optional<String> selection_direction_binding() { return selection_direction(); }
  164. // ^FormAssociatedTextControlElement
  165. virtual void did_edit_text_node() override;
  166. virtual GC::Ptr<DOM::Text> form_associated_element_to_text_node() override { return m_text_node; }
  167. private:
  168. HTMLInputElement(DOM::Document&, DOM::QualifiedName);
  169. void type_attribute_changed(TypeAttributeState old_state, TypeAttributeState new_state);
  170. virtual void apply_presentational_hints(CSS::StyleProperties&) const override;
  171. // ^DOM::Node
  172. virtual bool is_html_input_element() const final { return true; }
  173. // ^DOM::EventTarget
  174. virtual void did_lose_focus() override;
  175. virtual void did_receive_focus() override;
  176. virtual void legacy_pre_activation_behavior() override;
  177. virtual void legacy_cancelled_activation_behavior() override;
  178. virtual void legacy_cancelled_activation_behavior_was_not_called() override;
  179. // ^DOM::Element
  180. virtual i32 default_tab_index_value() const override;
  181. virtual void computed_css_values_changed() override;
  182. // https://html.spec.whatwg.org/multipage/input.html#image-button-state-(type=image):dimension-attributes
  183. virtual bool supports_dimension_attributes() const override { return type_state() == TypeAttributeState::ImageButton; }
  184. // ^Layout::ImageProvider
  185. virtual bool is_image_available() const override;
  186. virtual Optional<CSSPixels> intrinsic_width() const override;
  187. virtual Optional<CSSPixels> intrinsic_height() const override;
  188. virtual Optional<CSSPixelFraction> intrinsic_aspect_ratio() const override;
  189. virtual RefPtr<Gfx::ImmutableBitmap> current_image_bitmap(Gfx::IntSize = {}) const override;
  190. virtual void set_visible_in_viewport(bool) override;
  191. virtual GC::Ref<DOM::Element const> to_html_element() const override { return *this; }
  192. virtual void initialize(JS::Realm&) override;
  193. virtual void visit_edges(Cell::Visitor&) override;
  194. Optional<double> convert_string_to_number(StringView input) const;
  195. String convert_number_to_string(double input) const;
  196. WebIDL::ExceptionOr<GC::Ptr<JS::Date>> convert_string_to_date(StringView input) const;
  197. String covert_date_to_string(GC::Ref<JS::Date> input) const;
  198. Optional<double> min() const;
  199. Optional<double> max() const;
  200. double default_step() const;
  201. double step_scale_factor() const;
  202. Optional<double> allowed_value_step() const;
  203. double step_base() const;
  204. WebIDL::ExceptionOr<void> step_up_or_down(bool is_down, WebIDL::Long n);
  205. static TypeAttributeState parse_type_attribute(StringView);
  206. void create_shadow_tree_if_needed();
  207. void update_shadow_tree();
  208. void create_button_input_shadow_tree();
  209. void create_text_input_shadow_tree();
  210. void create_color_input_shadow_tree();
  211. void create_file_input_shadow_tree();
  212. void create_range_input_shadow_tree();
  213. WebIDL::ExceptionOr<void> run_input_activation_behavior(DOM::Event const&);
  214. void set_checked_within_group();
  215. void handle_maxlength_attribute();
  216. void handle_readonly_attribute(Optional<String> const& value);
  217. WebIDL::ExceptionOr<void> handle_src_attribute(String const& value);
  218. void user_interaction_did_change_input_value();
  219. // https://html.spec.whatwg.org/multipage/input.html#value-sanitization-algorithm
  220. String value_sanitization_algorithm(String const&) const;
  221. enum class ValueAttributeMode {
  222. Value,
  223. Default,
  224. DefaultOn,
  225. Filename,
  226. };
  227. static ValueAttributeMode value_attribute_mode_for_type_state(TypeAttributeState);
  228. ValueAttributeMode value_attribute_mode() const;
  229. void update_placeholder_visibility();
  230. GC::Ptr<DOM::Element> m_placeholder_element;
  231. GC::Ptr<DOM::Text> m_placeholder_text_node;
  232. void update_text_input_shadow_tree();
  233. GC::Ptr<DOM::Element> m_inner_text_element;
  234. GC::Ptr<DOM::Text> m_text_node;
  235. bool m_checked { false };
  236. void update_color_well_element();
  237. GC::Ptr<DOM::Element> m_color_well_element;
  238. void update_file_input_shadow_tree();
  239. GC::Ptr<DOM::Element> m_file_button;
  240. GC::Ptr<DOM::Element> m_file_label;
  241. void update_slider_shadow_tree_elements();
  242. GC::Ptr<DOM::Element> m_slider_runnable_track;
  243. GC::Ptr<DOM::Element> m_slider_progress_element;
  244. GC::Ptr<DOM::Element> m_slider_thumb;
  245. GC::Ptr<DecodedImageData> image_data() const;
  246. GC::Ptr<SharedResourceRequest> m_resource_request;
  247. SelectedCoordinate m_selected_coordinate;
  248. Optional<DOM::DocumentLoadEventDelayer> m_load_event_delayer;
  249. // https://html.spec.whatwg.org/multipage/input.html#dom-input-indeterminate
  250. bool m_indeterminate { false };
  251. // https://html.spec.whatwg.org/multipage/input.html#concept-input-checked-dirty-flag
  252. bool m_dirty_checkedness { false };
  253. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-fe-dirty
  254. bool m_dirty_value { false };
  255. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:concept-fe-mutable
  256. bool m_is_mutable { true };
  257. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:legacy-pre-activation-behavior
  258. bool m_before_legacy_pre_activation_behavior_checked { false };
  259. bool m_before_legacy_pre_activation_behavior_indeterminate { false };
  260. GC::Ptr<HTMLInputElement> m_legacy_pre_activation_behavior_checked_element_in_group;
  261. // https://html.spec.whatwg.org/multipage/input.html#concept-input-type-file-selected
  262. GC::Ptr<FileAPI::FileList> m_selected_files;
  263. TypeAttributeState m_type { TypeAttributeState::Text };
  264. String m_value;
  265. String m_last_src_value;
  266. bool m_has_uncommitted_changes { false };
  267. };
  268. }
  269. namespace Web::DOM {
  270. template<>
  271. inline bool Node::fast_is<HTML::HTMLInputElement>() const { return is_html_input_element(); }
  272. }