HTMLInputElement.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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@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 FormAssociatedTextControlElement
  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(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. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-relevant-value
  69. virtual String relevant_value() override { return value(); }
  70. WebIDL::ExceptionOr<void> set_relevant_value(String const& value) override { return set_value(value); }
  71. virtual void set_dirty_value_flag(bool flag) override { m_dirty_value = flag; }
  72. void commit_pending_changes();
  73. String placeholder() const;
  74. Optional<String> placeholder_value() const;
  75. bool checked() const { return m_checked; }
  76. enum class ChangeSource {
  77. Programmatic,
  78. User,
  79. };
  80. void set_checked(bool, ChangeSource = ChangeSource::Programmatic);
  81. bool checked_binding() const { return checked(); }
  82. void set_checked_binding(bool);
  83. bool indeterminate() const { return m_indeterminate; }
  84. void set_indeterminate(bool);
  85. bool is_mutable() const { return m_is_mutable; }
  86. void did_pick_color(Optional<Color> picked_color, ColorPickerUpdateState state);
  87. enum class MultipleHandling {
  88. Replace,
  89. Append,
  90. };
  91. void did_select_files(Span<SelectedFile> selected_files, MultipleHandling = MultipleHandling::Replace);
  92. JS::GCPtr<FileAPI::FileList> files();
  93. void set_files(JS::GCPtr<FileAPI::FileList>);
  94. FileFilter parse_accept_attribute() const;
  95. // NOTE: User interaction
  96. // https://html.spec.whatwg.org/multipage/input.html#update-the-file-selection
  97. void update_the_file_selection(JS::NonnullGCPtr<FileAPI::FileList>);
  98. WebIDL::Long max_length() const;
  99. WebIDL::ExceptionOr<void> set_max_length(WebIDL::Long);
  100. WebIDL::Long min_length() const;
  101. WebIDL::ExceptionOr<void> set_min_length(WebIDL::Long);
  102. unsigned size() const;
  103. WebIDL::ExceptionOr<void> set_size(unsigned value);
  104. struct SelectedCoordinate {
  105. int x { 0 };
  106. int y { 0 };
  107. };
  108. SelectedCoordinate selected_coordinate() const { return m_selected_coordinate; }
  109. JS::Object* value_as_date() const;
  110. WebIDL::ExceptionOr<void> set_value_as_date(Optional<JS::Handle<JS::Object>> const&);
  111. double value_as_number() const;
  112. WebIDL::ExceptionOr<void> set_value_as_number(double value);
  113. WebIDL::ExceptionOr<void> step_up(WebIDL::Long n = 1);
  114. WebIDL::ExceptionOr<void> step_down(WebIDL::Long n = 1);
  115. WebIDL::ExceptionOr<bool> check_validity();
  116. WebIDL::ExceptionOr<bool> report_validity();
  117. void set_custom_validity(String const&);
  118. WebIDL::ExceptionOr<void> show_picker();
  119. // ^DOM::EditableTextNodeOwner
  120. virtual void did_edit_text_node(Badge<DOM::Document>) override;
  121. // ^EventTarget
  122. // https://html.spec.whatwg.org/multipage/interaction.html#the-tabindex-attribute:the-input-element
  123. virtual bool is_focusable() const override { return m_type != TypeAttributeState::Hidden; }
  124. // ^FormAssociatedElement
  125. // https://html.spec.whatwg.org/multipage/forms.html#category-listed
  126. virtual bool is_listed() const override { return true; }
  127. // https://html.spec.whatwg.org/multipage/forms.html#category-submit
  128. virtual bool is_submittable() const override { return true; }
  129. // https://html.spec.whatwg.org/multipage/forms.html#category-reset
  130. virtual bool is_resettable() const override { return true; }
  131. // https://html.spec.whatwg.org/multipage/forms.html#category-autocapitalize
  132. virtual bool is_auto_capitalize_inheriting() const override { return true; }
  133. // https://html.spec.whatwg.org/multipage/forms.html#concept-button
  134. virtual bool is_button() const override;
  135. // https://html.spec.whatwg.org/multipage/forms.html#concept-submit-button
  136. virtual bool is_submit_button() const override;
  137. bool is_single_line() const;
  138. virtual void reset_algorithm() override;
  139. virtual void clear_algorithm() override;
  140. virtual void form_associated_element_was_inserted() override;
  141. virtual void form_associated_element_was_removed(DOM::Node*) override;
  142. virtual void form_associated_element_attribute_changed(FlyString const&, Optional<String> const&) override;
  143. virtual WebIDL::ExceptionOr<void> cloned(Node&, bool) override;
  144. JS::NonnullGCPtr<ValidityState const> validity() const;
  145. // ^HTMLElement
  146. // https://html.spec.whatwg.org/multipage/forms.html#category-label
  147. virtual bool is_labelable() const override { return type_state() != TypeAttributeState::Hidden; }
  148. virtual Optional<ARIA::Role> default_role() const override;
  149. JS::GCPtr<Element> placeholder_element() { return m_placeholder_element; }
  150. JS::GCPtr<Element const> placeholder_element() const { return m_placeholder_element; }
  151. virtual bool has_activation_behavior() const override;
  152. virtual void activation_behavior(DOM::Event const&) override;
  153. bool has_input_activation_behavior() const;
  154. bool change_event_applies() const;
  155. bool value_as_date_applies() const;
  156. bool value_as_number_applies() const;
  157. bool step_applies() const;
  158. bool step_up_or_down_applies() const;
  159. bool select_applies() const;
  160. bool selection_or_range_applies() const;
  161. bool selection_direction_applies() const;
  162. bool has_selectable_text() const;
  163. static bool selection_or_range_applies_for_type_state(TypeAttributeState);
  164. Optional<String> selection_direction_binding() { return selection_direction(); }
  165. protected:
  166. void selection_was_changed(size_t selection_start, size_t selection_end) override;
  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 JS::NonnullGCPtr<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<JS::GCPtr<JS::Date>> convert_string_to_date(StringView input) const;
  197. String covert_date_to_string(JS::NonnullGCPtr<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. JS::GCPtr<DOM::Element> m_placeholder_element;
  231. JS::GCPtr<DOM::Text> m_placeholder_text_node;
  232. void update_text_input_shadow_tree();
  233. JS::GCPtr<DOM::Element> m_inner_text_element;
  234. JS::GCPtr<DOM::Text> m_text_node;
  235. bool m_checked { false };
  236. void update_color_well_element();
  237. JS::GCPtr<DOM::Element> m_color_well_element;
  238. void update_file_input_shadow_tree();
  239. JS::GCPtr<DOM::Element> m_file_button;
  240. JS::GCPtr<DOM::Element> m_file_label;
  241. void update_slider_shadow_tree_elements();
  242. JS::GCPtr<DOM::Element> m_slider_runnable_track;
  243. JS::GCPtr<DOM::Element> m_slider_progress_element;
  244. JS::GCPtr<DOM::Element> m_slider_thumb;
  245. JS::GCPtr<DecodedImageData> image_data() const;
  246. JS::GCPtr<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. JS::GCPtr<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. JS::GCPtr<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. }