HTMLInputElement.h 12 KB

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