HTMLInputElement.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 RefPtr<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. String type() const;
  51. TypeAttributeState type_state() const { return m_type; }
  52. void set_type(String const&);
  53. String default_value() const { return attribute(HTML::AttributeNames::value); }
  54. String name() const { return attribute(HTML::AttributeNames::name); }
  55. String value() const;
  56. WebIDL::ExceptionOr<void> set_value(String);
  57. bool checked() const { return m_checked; }
  58. enum class ChangeSource {
  59. Programmatic,
  60. User,
  61. };
  62. void set_checked(bool, ChangeSource = ChangeSource::Programmatic);
  63. bool checked_binding() const { return checked(); }
  64. void set_checked_binding(bool);
  65. void did_edit_text_node(Badge<BrowsingContext>);
  66. JS::GCPtr<FileAPI::FileList> files();
  67. void set_files(JS::GCPtr<FileAPI::FileList>);
  68. // NOTE: User interaction
  69. // https://html.spec.whatwg.org/multipage/input.html#update-the-file-selection
  70. void update_the_file_selection(JS::NonnullGCPtr<FileAPI::FileList>);
  71. WebIDL::ExceptionOr<void> show_picker();
  72. // ^EventTarget
  73. // https://html.spec.whatwg.org/multipage/interaction.html#the-tabindex-attribute:the-input-element
  74. virtual bool is_focusable() const override { return m_type != TypeAttributeState::Hidden; }
  75. // ^HTMLElement
  76. virtual void parse_attribute(FlyString const&, String const&) override;
  77. virtual void did_remove_attribute(FlyString const&) override;
  78. // ^FormAssociatedElement
  79. // https://html.spec.whatwg.org/multipage/forms.html#category-listed
  80. virtual bool is_listed() const override { return true; }
  81. // https://html.spec.whatwg.org/multipage/forms.html#category-submit
  82. virtual bool is_submittable() const override { return true; }
  83. // https://html.spec.whatwg.org/multipage/forms.html#category-reset
  84. virtual bool is_resettable() const override { return true; }
  85. // https://html.spec.whatwg.org/multipage/forms.html#category-autocapitalize
  86. virtual bool is_auto_capitalize_inheriting() const override { return true; }
  87. virtual void form_associated_element_was_inserted() override;
  88. // ^HTMLElement
  89. // https://html.spec.whatwg.org/multipage/forms.html#category-label
  90. virtual bool is_labelable() const override { return type_state() != TypeAttributeState::Hidden; }
  91. private:
  92. HTMLInputElement(DOM::Document&, DOM::QualifiedName);
  93. // ^DOM::EventTarget
  94. virtual void did_receive_focus() override;
  95. virtual void legacy_pre_activation_behavior() override;
  96. virtual void legacy_cancelled_activation_behavior() override;
  97. virtual void legacy_cancelled_activation_behavior_was_not_called() override;
  98. virtual void visit_edges(Cell::Visitor&) override;
  99. static TypeAttributeState parse_type_attribute(StringView);
  100. void create_shadow_tree_if_needed();
  101. void run_input_activation_behavior();
  102. void set_checked_within_group();
  103. // https://html.spec.whatwg.org/multipage/input.html#value-sanitization-algorithm
  104. String value_sanitization_algorithm(String) const;
  105. JS::GCPtr<DOM::Text> m_text_node;
  106. bool m_checked { false };
  107. // https://html.spec.whatwg.org/multipage/input.html#concept-input-checked-dirty-flag
  108. bool m_dirty_checkedness { false };
  109. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-fe-dirty
  110. bool m_dirty_value { false };
  111. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:legacy-pre-activation-behavior
  112. bool m_before_legacy_pre_activation_behavior_checked { false };
  113. JS::GCPtr<HTMLInputElement> m_legacy_pre_activation_behavior_checked_element_in_group;
  114. // https://html.spec.whatwg.org/multipage/input.html#concept-input-type-file-selected
  115. JS::GCPtr<FileAPI::FileList> m_selected_files;
  116. TypeAttributeState m_type { TypeAttributeState::Text };
  117. String m_value;
  118. };
  119. }