HTMLInputElement.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Adam Hodgen <ant1441@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <LibWeb/DOM/Text.h>
  9. #include <LibWeb/FileAPI/FileList.h>
  10. #include <LibWeb/HTML/FormAssociatedElement.h>
  11. #include <LibWeb/HTML/HTMLElement.h>
  12. #include <LibWeb/WebIDL/DOMException.h>
  13. namespace Web::HTML {
  14. // https://html.spec.whatwg.org/multipage/input.html#attr-input-type
  15. #define ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTES \
  16. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(hidden, Hidden) \
  17. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(text, Text) \
  18. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(search, Search) \
  19. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(tel, Telephone) \
  20. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(url, URL) \
  21. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(email, Email) \
  22. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(password, Password) \
  23. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(date, Date) \
  24. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(month, Month) \
  25. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(week, Week) \
  26. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(time, Time) \
  27. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE("datetime-local", LocalDateAndTime) \
  28. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(number, Number) \
  29. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(range, Range) \
  30. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(color, Color) \
  31. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(checkbox, Checkbox) \
  32. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(radio, RadioButton) \
  33. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(file, FileUpload) \
  34. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(submit, SubmitButton) \
  35. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(image, ImageButton) \
  36. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(reset, ResetButton) \
  37. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(button, Button)
  38. class HTMLInputElement final
  39. : public HTMLElement
  40. , public FormAssociatedElement
  41. , public DOM::EditableTextNodeOwner {
  42. WEB_PLATFORM_OBJECT(HTMLInputElement, HTMLElement);
  43. JS_DECLARE_ALLOCATOR(HTMLInputElement);
  44. FORM_ASSOCIATED_ELEMENT(HTMLElement, HTMLInputElement)
  45. public:
  46. virtual ~HTMLInputElement() override;
  47. virtual JS::GCPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
  48. enum class TypeAttributeState {
  49. #define __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(_, state) state,
  50. ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTES
  51. #undef __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE
  52. };
  53. StringView type() const;
  54. TypeAttributeState type_state() const { return m_type; }
  55. WebIDL::ExceptionOr<void> set_type(String const&);
  56. DeprecatedString default_value() const { return deprecated_attribute(HTML::AttributeNames::value); }
  57. DeprecatedString name() const { return deprecated_attribute(HTML::AttributeNames::name); }
  58. virtual DeprecatedString value() const override;
  59. WebIDL::ExceptionOr<void> set_value(String const&);
  60. Optional<DeprecatedString> placeholder_value() const;
  61. bool checked() const { return m_checked; }
  62. enum class ChangeSource {
  63. Programmatic,
  64. User,
  65. };
  66. void set_checked(bool, ChangeSource = ChangeSource::Programmatic);
  67. bool checked_binding() const { return checked(); }
  68. void set_checked_binding(bool);
  69. bool indeterminate() const { return m_indeterminate; }
  70. void set_indeterminate(bool);
  71. bool is_mutable() const { return m_is_mutable; }
  72. void did_pick_color(Optional<Color> picked_color);
  73. JS::GCPtr<FileAPI::FileList> files();
  74. void set_files(JS::GCPtr<FileAPI::FileList>);
  75. // NOTE: User interaction
  76. // https://html.spec.whatwg.org/multipage/input.html#update-the-file-selection
  77. void update_the_file_selection(JS::NonnullGCPtr<FileAPI::FileList>);
  78. WebIDL::ExceptionOr<double> value_as_number() const;
  79. WebIDL::ExceptionOr<void> set_value_as_number(double value);
  80. WebIDL::ExceptionOr<bool> check_validity();
  81. WebIDL::ExceptionOr<bool> report_validity();
  82. void set_custom_validity(String const&);
  83. WebIDL::ExceptionOr<void> select();
  84. WebIDL::ExceptionOr<void> set_selection_range(u32 start, u32 end, Optional<String> const& direction = {});
  85. WebIDL::ExceptionOr<void> show_picker();
  86. // ^DOM::EditableTextNodeOwner
  87. virtual void did_edit_text_node(Badge<BrowsingContext>) override;
  88. // ^EventTarget
  89. // https://html.spec.whatwg.org/multipage/interaction.html#the-tabindex-attribute:the-input-element
  90. virtual bool is_focusable() const override { return m_type != TypeAttributeState::Hidden; }
  91. // ^HTMLElement
  92. virtual void attribute_changed(FlyString const&, Optional<String> const&) override;
  93. // ^FormAssociatedElement
  94. // https://html.spec.whatwg.org/multipage/forms.html#category-listed
  95. virtual bool is_listed() const override { return true; }
  96. // https://html.spec.whatwg.org/multipage/forms.html#category-submit
  97. virtual bool is_submittable() const override { return true; }
  98. // https://html.spec.whatwg.org/multipage/forms.html#category-reset
  99. virtual bool is_resettable() const override { return true; }
  100. // https://html.spec.whatwg.org/multipage/forms.html#category-autocapitalize
  101. virtual bool is_auto_capitalize_inheriting() const override { return true; }
  102. // https://html.spec.whatwg.org/multipage/forms.html#concept-button
  103. virtual bool is_button() const override;
  104. // https://html.spec.whatwg.org/multipage/forms.html#concept-submit-button
  105. virtual bool is_submit_button() const override;
  106. virtual void reset_algorithm() override;
  107. virtual void form_associated_element_was_inserted() override;
  108. // ^HTMLElement
  109. // https://html.spec.whatwg.org/multipage/forms.html#category-label
  110. virtual bool is_labelable() const override { return type_state() != TypeAttributeState::Hidden; }
  111. virtual Optional<ARIA::Role> default_role() const override;
  112. JS::GCPtr<Element> placeholder_element() { return m_placeholder_element; }
  113. JS::GCPtr<Element const> placeholder_element() const { return m_placeholder_element; }
  114. virtual bool has_activation_behavior() const override;
  115. virtual void activation_behavior(DOM::Event const&) override;
  116. private:
  117. HTMLInputElement(DOM::Document&, DOM::QualifiedName);
  118. // ^DOM::Node
  119. virtual bool is_html_input_element() const final { return true; }
  120. // ^DOM::EventTarget
  121. virtual void did_lose_focus() override;
  122. virtual void did_receive_focus() override;
  123. virtual void legacy_pre_activation_behavior() override;
  124. virtual void legacy_cancelled_activation_behavior() override;
  125. virtual void legacy_cancelled_activation_behavior_was_not_called() override;
  126. // ^DOM::Element
  127. virtual i32 default_tab_index_value() const override;
  128. virtual void initialize(JS::Realm&) override;
  129. virtual void visit_edges(Cell::Visitor&) override;
  130. static TypeAttributeState parse_type_attribute(StringView);
  131. void create_shadow_tree_if_needed();
  132. void create_text_input_shadow_tree();
  133. void create_color_input_shadow_tree();
  134. WebIDL::ExceptionOr<void> run_input_activation_behavior();
  135. void set_checked_within_group();
  136. void handle_readonly_attribute(Optional<String> const& value);
  137. // https://html.spec.whatwg.org/multipage/input.html#value-sanitization-algorithm
  138. DeprecatedString value_sanitization_algorithm(DeprecatedString) const;
  139. void update_placeholder_visibility();
  140. JS::GCPtr<DOM::Element> m_placeholder_element;
  141. JS::GCPtr<DOM::Text> m_placeholder_text_node;
  142. JS::GCPtr<DOM::Element> m_inner_text_element;
  143. JS::GCPtr<DOM::Element> m_color_well_element;
  144. JS::GCPtr<DOM::Text> m_text_node;
  145. bool m_checked { false };
  146. // https://html.spec.whatwg.org/multipage/input.html#dom-input-indeterminate
  147. bool m_indeterminate { false };
  148. // https://html.spec.whatwg.org/multipage/input.html#concept-input-checked-dirty-flag
  149. bool m_dirty_checkedness { false };
  150. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-fe-dirty
  151. bool m_dirty_value { false };
  152. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:concept-fe-mutable
  153. bool m_is_mutable { true };
  154. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:legacy-pre-activation-behavior
  155. bool m_before_legacy_pre_activation_behavior_checked { false };
  156. bool m_before_legacy_pre_activation_behavior_indeterminate { false };
  157. JS::GCPtr<HTMLInputElement> m_legacy_pre_activation_behavior_checked_element_in_group;
  158. // https://html.spec.whatwg.org/multipage/input.html#concept-input-type-file-selected
  159. JS::GCPtr<FileAPI::FileList> m_selected_files;
  160. TypeAttributeState m_type { TypeAttributeState::Text };
  161. DeprecatedString m_value;
  162. };
  163. }
  164. namespace Web::DOM {
  165. template<>
  166. inline bool Node::fast_is<HTML::HTMLInputElement>() const { return is_html_input_element(); }
  167. }