HTMLInputElement.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/HTML/FormAssociatedElement.h>
  9. #include <LibWeb/HTML/HTMLElement.h>
  10. namespace Web::HTML {
  11. // https://html.spec.whatwg.org/multipage/input.html#attr-input-type
  12. #define ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTES \
  13. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(hidden, Hidden) \
  14. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(text, Text) \
  15. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(search, Search) \
  16. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(tel, Telephone) \
  17. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(url, URL) \
  18. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(email, Email) \
  19. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(password, Password) \
  20. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(date, Date) \
  21. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(month, Month) \
  22. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(week, Week) \
  23. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(time, Time) \
  24. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE("datetime-local", LocalDateAndTime) \
  25. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(number, Number) \
  26. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(range, Range) \
  27. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(color, Color) \
  28. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(checkbox, Checkbox) \
  29. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(radio, RadioButton) \
  30. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(file, FileUpload) \
  31. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(submit, SubmitButton) \
  32. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(image, ImageButton) \
  33. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(reset, ResetButton) \
  34. __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(button, Button)
  35. class HTMLInputElement final
  36. : public HTMLElement
  37. , public FormAssociatedElement {
  38. FORM_ASSOCIATED_ELEMENT(HTMLElement, HTMLInputElement)
  39. public:
  40. using WrapperType = Bindings::HTMLInputElementWrapper;
  41. HTMLInputElement(DOM::Document&, DOM::QualifiedName);
  42. virtual ~HTMLInputElement() override;
  43. virtual RefPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
  44. enum TypeAttributeState {
  45. #define __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(_, state) state,
  46. ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTES
  47. #undef __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE
  48. };
  49. String type() const;
  50. TypeAttributeState type_state() const { return m_type; }
  51. void set_type(String const&);
  52. String default_value() const { return attribute(HTML::AttributeNames::value); }
  53. String name() const { return attribute(HTML::AttributeNames::name); }
  54. String value() const;
  55. void set_value(String);
  56. bool checked() const { return m_checked; }
  57. enum class ChangeSource {
  58. Programmatic,
  59. User,
  60. };
  61. void set_checked(bool, ChangeSource = ChangeSource::Programmatic);
  62. bool checked_binding() const { return checked(); }
  63. void set_checked_binding(bool);
  64. void did_edit_text_node(Badge<BrowsingContext>);
  65. virtual bool is_focusable() const override;
  66. virtual void parse_attribute(FlyString const&, String const&) override;
  67. virtual void did_remove_attribute(FlyString const&) override;
  68. // ^FormAssociatedElement
  69. // https://html.spec.whatwg.org/multipage/forms.html#category-listed
  70. virtual bool is_listed() const override { return true; }
  71. // https://html.spec.whatwg.org/multipage/forms.html#category-submit
  72. virtual bool is_submittable() const override { return true; }
  73. // https://html.spec.whatwg.org/multipage/forms.html#category-reset
  74. virtual bool is_resettable() const override { return true; }
  75. // https://html.spec.whatwg.org/multipage/forms.html#category-autocapitalize
  76. virtual bool is_auto_capitalize_inheriting() const override { return true; }
  77. virtual void form_associated_element_was_inserted() override;
  78. // ^HTMLElement
  79. // https://html.spec.whatwg.org/multipage/forms.html#category-label
  80. virtual bool is_labelable() const override { return type_state() != TypeAttributeState::Hidden; }
  81. private:
  82. // ^DOM::EventTarget
  83. virtual void did_receive_focus() override;
  84. virtual void legacy_pre_activation_behavior() override;
  85. virtual void legacy_cancelled_activation_behavior() override;
  86. virtual void legacy_cancelled_activation_behavior_was_not_called() override;
  87. static TypeAttributeState parse_type_attribute(StringView);
  88. void create_shadow_tree_if_needed();
  89. void run_input_activation_behavior();
  90. void set_checked_within_group();
  91. // https://html.spec.whatwg.org/multipage/input.html#value-sanitization-algorithm
  92. String value_sanitization_algorithm(String) const;
  93. RefPtr<DOM::Text> m_text_node;
  94. bool m_checked { false };
  95. // https://html.spec.whatwg.org/multipage/input.html#concept-input-checked-dirty-flag
  96. bool m_dirty_checkedness { false };
  97. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-fe-dirty
  98. bool m_dirty_value { false };
  99. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:legacy-pre-activation-behavior
  100. bool m_before_legacy_pre_activation_behavior_checked { false };
  101. RefPtr<HTMLInputElement> m_legacy_pre_activation_behavior_checked_element_in_group;
  102. TypeAttributeState m_type { TypeAttributeState::Text };
  103. String m_value;
  104. };
  105. }