HTMLTextAreaElement.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
  4. * Copyright (c) 2024, Bastiaan van der Plaat <bastiaan.v.d.plaat@gmail.com>
  5. * Copyright (c) 2024, Jelle Raaijmakers <jelle@gmta.nl>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #pragma once
  10. #include <LibCore/Timer.h>
  11. #include <LibWeb/ARIA/Roles.h>
  12. #include <LibWeb/DOM/Text.h>
  13. #include <LibWeb/HTML/FormAssociatedElement.h>
  14. #include <LibWeb/HTML/HTMLElement.h>
  15. #include <LibWeb/WebIDL/Types.h>
  16. namespace Web::HTML {
  17. class HTMLTextAreaElement final
  18. : public HTMLElement
  19. , public FormAssociatedTextControlElement
  20. , public DOM::EditableTextNodeOwner {
  21. WEB_PLATFORM_OBJECT(HTMLTextAreaElement, HTMLElement);
  22. JS_DECLARE_ALLOCATOR(HTMLTextAreaElement);
  23. FORM_ASSOCIATED_ELEMENT(HTMLElement, HTMLTextAreaElement)
  24. public:
  25. virtual ~HTMLTextAreaElement() override;
  26. virtual void adjust_computed_style(CSS::StyleProperties&) override;
  27. String const& type() const
  28. {
  29. static String const textarea = "textarea"_string;
  30. return textarea;
  31. }
  32. // ^DOM::EditableTextNodeOwner
  33. virtual void did_edit_text_node() override;
  34. // ^EventTarget
  35. // https://html.spec.whatwg.org/multipage/interaction.html#the-tabindex-attribute:the-textarea-element
  36. virtual bool is_focusable() const override { return true; }
  37. virtual void did_lose_focus() override;
  38. virtual void did_receive_focus() override;
  39. // ^FormAssociatedElement
  40. // https://html.spec.whatwg.org/multipage/forms.html#category-listed
  41. virtual bool is_listed() const override { return true; }
  42. // https://html.spec.whatwg.org/multipage/forms.html#category-submit
  43. virtual bool is_submittable() const override { return true; }
  44. // https://html.spec.whatwg.org/multipage/forms.html#category-reset
  45. virtual bool is_resettable() const override { return true; }
  46. // https://html.spec.whatwg.org/multipage/forms.html#category-autocapitalize
  47. virtual bool is_auto_capitalize_inheriting() const override { return true; }
  48. // https://html.spec.whatwg.org/multipage/forms.html#category-label
  49. virtual bool is_labelable() const override { return true; }
  50. virtual void reset_algorithm() override;
  51. virtual void clear_algorithm() override;
  52. virtual WebIDL::ExceptionOr<void> cloned(Node&, bool) override;
  53. virtual void form_associated_element_was_inserted() override;
  54. virtual void form_associated_element_was_removed(DOM::Node*) override;
  55. virtual void form_associated_element_attribute_changed(FlyString const&, Optional<String> const&) override;
  56. virtual void children_changed() override;
  57. // https://www.w3.org/TR/html-aria/#el-textarea
  58. virtual Optional<ARIA::Role> default_role() const override { return ARIA::Role::textbox; }
  59. String default_value() const;
  60. void set_default_value(String const&);
  61. String value() const override;
  62. void set_value(String const&);
  63. // https://html.spec.whatwg.org/multipage/form-elements.html#the-textarea-element:concept-fe-api-value-3
  64. String api_value() const;
  65. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-relevant-value
  66. virtual String relevant_value() override { return api_value(); }
  67. virtual WebIDL::ExceptionOr<void> set_relevant_value(String const& value) override;
  68. virtual void set_dirty_value_flag(bool flag) override { m_dirty_value = flag; }
  69. u32 text_length() const;
  70. bool check_validity();
  71. bool report_validity();
  72. void set_custom_validity(String const& error);
  73. WebIDL::Long max_length() const;
  74. WebIDL::ExceptionOr<void> set_max_length(WebIDL::Long);
  75. WebIDL::Long min_length() const;
  76. WebIDL::ExceptionOr<void> set_min_length(WebIDL::Long);
  77. unsigned cols() const;
  78. WebIDL::ExceptionOr<void> set_cols(unsigned);
  79. unsigned rows() const;
  80. WebIDL::ExceptionOr<void> set_rows(unsigned);
  81. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-selectionstart
  82. WebIDL::UnsignedLong selection_start_binding() const;
  83. WebIDL::ExceptionOr<void> set_selection_start_binding(WebIDL::UnsignedLong const&);
  84. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-selectionend
  85. WebIDL::UnsignedLong selection_end_binding() const;
  86. WebIDL::ExceptionOr<void> set_selection_end_binding(WebIDL::UnsignedLong const&);
  87. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-selectiondirection
  88. String selection_direction_binding() const;
  89. void set_selection_direction_binding(String const& direction);
  90. void set_dirty_value_flag(Badge<FormAssociatedElement>, bool flag) { m_dirty_value = flag; }
  91. virtual JS::GCPtr<DOM::Text> form_associated_element_to_text_node() override { return m_text_node; }
  92. private:
  93. HTMLTextAreaElement(DOM::Document&, DOM::QualifiedName);
  94. virtual void initialize(JS::Realm&) override;
  95. virtual void visit_edges(Cell::Visitor&) override;
  96. void set_raw_value(String);
  97. // ^DOM::Element
  98. virtual i32 default_tab_index_value() const override;
  99. void create_shadow_tree_if_needed();
  100. void handle_readonly_attribute(Optional<String> const& value);
  101. void handle_maxlength_attribute();
  102. void queue_firing_input_event();
  103. void update_placeholder_visibility();
  104. JS::GCPtr<DOM::Element> m_placeholder_element;
  105. JS::GCPtr<DOM::Text> m_placeholder_text_node;
  106. JS::GCPtr<DOM::Element> m_inner_text_element;
  107. JS::GCPtr<DOM::Text> m_text_node;
  108. RefPtr<Core::Timer> m_input_event_timer;
  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/form-elements.html#the-textarea-element:concept-fe-mutable
  112. bool m_is_mutable { true };
  113. // https://html.spec.whatwg.org/multipage/form-elements.html#concept-textarea-raw-value
  114. String m_raw_value;
  115. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-fe-api-value
  116. mutable Optional<String> m_api_value;
  117. };
  118. }