HTMLInputElement.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. FORM_ASSOCIATED_ELEMENT(HTMLElement, HTMLInputElement)
  44. public:
  45. virtual ~HTMLInputElement() override;
  46. virtual JS::GCPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
  47. enum class TypeAttributeState {
  48. #define __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(_, state) state,
  49. ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTES
  50. #undef __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE
  51. };
  52. StringView type() const;
  53. TypeAttributeState type_state() const { return m_type; }
  54. WebIDL::ExceptionOr<void> set_type(String const&);
  55. DeprecatedString default_value() const { return deprecated_attribute(HTML::AttributeNames::value); }
  56. DeprecatedString name() const { return deprecated_attribute(HTML::AttributeNames::name); }
  57. virtual DeprecatedString value() const override;
  58. WebIDL::ExceptionOr<void> set_value(String const&);
  59. Optional<DeprecatedString> placeholder_value() const;
  60. bool checked() const { return m_checked; }
  61. enum class ChangeSource {
  62. Programmatic,
  63. User,
  64. };
  65. void set_checked(bool, ChangeSource = ChangeSource::Programmatic);
  66. bool checked_binding() const { return checked(); }
  67. void set_checked_binding(bool);
  68. bool indeterminate() const { return m_indeterminate; }
  69. void set_indeterminate(bool);
  70. bool is_mutable() const { return m_is_mutable; }
  71. JS::GCPtr<FileAPI::FileList> files();
  72. void set_files(JS::GCPtr<FileAPI::FileList>);
  73. // NOTE: User interaction
  74. // https://html.spec.whatwg.org/multipage/input.html#update-the-file-selection
  75. void update_the_file_selection(JS::NonnullGCPtr<FileAPI::FileList>);
  76. WebIDL::ExceptionOr<bool> check_validity();
  77. WebIDL::ExceptionOr<bool> report_validity();
  78. void set_custom_validity(String const&);
  79. WebIDL::ExceptionOr<void> select();
  80. WebIDL::ExceptionOr<void> set_selection_range(u32 start, u32 end, Optional<String> const& direction = {});
  81. WebIDL::ExceptionOr<void> show_picker();
  82. // ^DOM::EditableTextNodeOwner
  83. virtual void did_edit_text_node(Badge<BrowsingContext>) override;
  84. // ^EventTarget
  85. // https://html.spec.whatwg.org/multipage/interaction.html#the-tabindex-attribute:the-input-element
  86. virtual bool is_focusable() const override { return m_type != TypeAttributeState::Hidden; }
  87. // ^HTMLElement
  88. virtual void attribute_changed(DeprecatedFlyString const&, DeprecatedString const&) override;
  89. // ^FormAssociatedElement
  90. // https://html.spec.whatwg.org/multipage/forms.html#category-listed
  91. virtual bool is_listed() const override { return true; }
  92. // https://html.spec.whatwg.org/multipage/forms.html#category-submit
  93. virtual bool is_submittable() const override { return true; }
  94. // https://html.spec.whatwg.org/multipage/forms.html#category-reset
  95. virtual bool is_resettable() const override { return true; }
  96. // https://html.spec.whatwg.org/multipage/forms.html#category-autocapitalize
  97. virtual bool is_auto_capitalize_inheriting() const override { return true; }
  98. // https://html.spec.whatwg.org/multipage/forms.html#concept-button
  99. virtual bool is_button() const override;
  100. // https://html.spec.whatwg.org/multipage/forms.html#concept-submit-button
  101. virtual bool is_submit_button() const override;
  102. virtual void reset_algorithm() override;
  103. virtual void form_associated_element_was_inserted() override;
  104. // ^HTMLElement
  105. // https://html.spec.whatwg.org/multipage/forms.html#category-label
  106. virtual bool is_labelable() const override { return type_state() != TypeAttributeState::Hidden; }
  107. virtual Optional<ARIA::Role> default_role() const override;
  108. JS::GCPtr<Element> placeholder_element() { return m_placeholder_element; }
  109. JS::GCPtr<Element const> placeholder_element() const { return m_placeholder_element; }
  110. private:
  111. HTMLInputElement(DOM::Document&, DOM::QualifiedName);
  112. // ^DOM::Node
  113. virtual bool is_html_input_element() const final { return true; }
  114. // ^DOM::EventTarget
  115. virtual void did_lose_focus() override;
  116. virtual void did_receive_focus() override;
  117. virtual void legacy_pre_activation_behavior() override;
  118. virtual void legacy_cancelled_activation_behavior() override;
  119. virtual void legacy_cancelled_activation_behavior_was_not_called() override;
  120. // ^DOM::Element
  121. virtual i32 default_tab_index_value() const override;
  122. virtual void initialize(JS::Realm&) override;
  123. virtual void visit_edges(Cell::Visitor&) override;
  124. static TypeAttributeState parse_type_attribute(StringView);
  125. void create_shadow_tree_if_needed();
  126. WebIDL::ExceptionOr<void> run_input_activation_behavior();
  127. void set_checked_within_group();
  128. void handle_readonly_attribute(DeprecatedFlyString const& value);
  129. // https://html.spec.whatwg.org/multipage/input.html#value-sanitization-algorithm
  130. DeprecatedString value_sanitization_algorithm(DeprecatedString) const;
  131. void update_placeholder_visibility();
  132. JS::GCPtr<DOM::Element> m_placeholder_element;
  133. JS::GCPtr<DOM::Text> m_placeholder_text_node;
  134. JS::GCPtr<DOM::Element> m_inner_text_element;
  135. JS::GCPtr<DOM::Text> m_text_node;
  136. bool m_checked { false };
  137. // https://html.spec.whatwg.org/multipage/input.html#dom-input-indeterminate
  138. bool m_indeterminate { false };
  139. // https://html.spec.whatwg.org/multipage/input.html#concept-input-checked-dirty-flag
  140. bool m_dirty_checkedness { false };
  141. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-fe-dirty
  142. bool m_dirty_value { false };
  143. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:concept-fe-mutable
  144. bool m_is_mutable { true };
  145. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:legacy-pre-activation-behavior
  146. bool m_before_legacy_pre_activation_behavior_checked { false };
  147. bool m_before_legacy_pre_activation_behavior_indeterminate { false };
  148. JS::GCPtr<HTMLInputElement> m_legacy_pre_activation_behavior_checked_element_in_group;
  149. // https://html.spec.whatwg.org/multipage/input.html#concept-input-type-file-selected
  150. JS::GCPtr<FileAPI::FileList> m_selected_files;
  151. TypeAttributeState m_type { TypeAttributeState::Text };
  152. DeprecatedString m_value;
  153. };
  154. }
  155. namespace Web::DOM {
  156. template<>
  157. inline bool Node::fast_is<HTML::HTMLInputElement>() const { return is_html_input_element(); }
  158. }