HTMLInputElement.h 8.6 KB

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