HTMLInputElement.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 : public FormAssociatedElement {
  36. public:
  37. using WrapperType = Bindings::HTMLInputElementWrapper;
  38. HTMLInputElement(DOM::Document&, DOM::QualifiedName);
  39. virtual ~HTMLInputElement() override;
  40. virtual RefPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
  41. enum TypeAttributeState {
  42. #define __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(_, state) state,
  43. ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTES
  44. #undef __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE
  45. };
  46. String type() const;
  47. TypeAttributeState type_state() const;
  48. void set_type(String const&);
  49. String default_value() const { return attribute(HTML::AttributeNames::value); }
  50. String name() const { return attribute(HTML::AttributeNames::name); }
  51. String value() const;
  52. void set_value(String);
  53. bool checked() const { return m_checked; }
  54. enum class ChangeSource {
  55. Programmatic,
  56. User,
  57. };
  58. enum class ShouldRunActivationBehavior {
  59. No,
  60. Yes,
  61. };
  62. void set_checked(bool, ChangeSource = ChangeSource::Programmatic, ShouldRunActivationBehavior = ShouldRunActivationBehavior::Yes);
  63. void did_click_button(Badge<Painting::ButtonPaintable>);
  64. void did_click_checkbox(Badge<Painting::CheckBoxPaintable>);
  65. void did_edit_text_node(Badge<BrowsingContext>);
  66. virtual bool is_focusable() const override;
  67. virtual void parse_attribute(FlyString const&, String const&) override;
  68. virtual void did_remove_attribute(FlyString const&) override;
  69. // ^FormAssociatedElement
  70. // https://html.spec.whatwg.org/multipage/forms.html#category-listed
  71. virtual bool is_listed() const override { return true; }
  72. // https://html.spec.whatwg.org/multipage/forms.html#category-submit
  73. virtual bool is_submittable() const override { return true; }
  74. // https://html.spec.whatwg.org/multipage/forms.html#category-reset
  75. virtual bool is_resettable() const override { return true; }
  76. // https://html.spec.whatwg.org/multipage/forms.html#category-autocapitalize
  77. virtual bool is_auto_capitalize_inheriting() const override { return true; }
  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 run_activation_behavior() override;
  85. void create_shadow_tree_if_needed();
  86. void run_input_activation_behavior();
  87. // https://html.spec.whatwg.org/multipage/input.html#value-sanitization-algorithm
  88. String value_sanitization_algorithm(String) const;
  89. RefPtr<DOM::Text> m_text_node;
  90. bool m_checked { false };
  91. // https://html.spec.whatwg.org/multipage/input.html#concept-input-checked-dirty-flag
  92. bool m_dirty_checkedness { false };
  93. };
  94. }