HTMLInputElement.h 13 KB

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