HTMLTextAreaElement.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/CSS/StyleProperties.h>
  9. #include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/DOM/ElementFactory.h>
  12. #include <LibWeb/DOM/Event.h>
  13. #include <LibWeb/DOM/ShadowRoot.h>
  14. #include <LibWeb/DOM/Text.h>
  15. #include <LibWeb/HTML/HTMLTextAreaElement.h>
  16. #include <LibWeb/HTML/Numbers.h>
  17. #include <LibWeb/Namespace.h>
  18. namespace Web::HTML {
  19. JS_DEFINE_ALLOCATOR(HTMLTextAreaElement);
  20. HTMLTextAreaElement::HTMLTextAreaElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  21. : HTMLElement(document, move(qualified_name))
  22. {
  23. }
  24. HTMLTextAreaElement::~HTMLTextAreaElement() = default;
  25. JS::GCPtr<Layout::Node> HTMLTextAreaElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  26. {
  27. // AD-HOC: We rewrite `display: inline` to `display: inline-block`.
  28. // This is required for the internal shadow tree to work correctly in layout.
  29. if (style->display().is_inline_outside() && style->display().is_flow_inside())
  30. style->set_property(CSS::PropertyID::Display, CSS::DisplayStyleValue::create(CSS::Display::from_short(CSS::Display::Short::InlineBlock)));
  31. return Element::create_layout_node_for_display_type(document(), style->display(), style, this);
  32. }
  33. void HTMLTextAreaElement::initialize(JS::Realm& realm)
  34. {
  35. Base::initialize(realm);
  36. set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLTextAreaElementPrototype>(realm, "HTMLTextAreaElement"_fly_string));
  37. }
  38. void HTMLTextAreaElement::visit_edges(Cell::Visitor& visitor)
  39. {
  40. Base::visit_edges(visitor);
  41. visitor.visit(m_placeholder_element);
  42. visitor.visit(m_placeholder_text_node);
  43. visitor.visit(m_inner_text_element);
  44. visitor.visit(m_text_node);
  45. }
  46. void HTMLTextAreaElement::did_receive_focus()
  47. {
  48. auto* browsing_context = document().browsing_context();
  49. if (!browsing_context)
  50. return;
  51. if (!m_text_node)
  52. return;
  53. browsing_context->set_cursor_position(DOM::Position::create(realm(), *m_text_node, 0));
  54. }
  55. void HTMLTextAreaElement::did_lose_focus()
  56. {
  57. // The change event fires when the value is committed, if that makes sense for the control,
  58. // or else when the control loses focus
  59. queue_an_element_task(HTML::Task::Source::UserInteraction, [this] {
  60. auto change_event = DOM::Event::create(realm(), HTML::EventNames::change);
  61. change_event->set_bubbles(true);
  62. dispatch_event(change_event);
  63. });
  64. }
  65. // https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
  66. i32 HTMLTextAreaElement::default_tab_index_value() const
  67. {
  68. // See the base function for the spec comments.
  69. return 0;
  70. }
  71. // https://html.spec.whatwg.org/multipage/form-elements.html#the-textarea-element:concept-form-reset-control
  72. void HTMLTextAreaElement::reset_algorithm()
  73. {
  74. // The reset algorithm for textarea elements is to set the dirty value flag back to false,
  75. m_dirty_value = false;
  76. // and set the raw value of element to its child text content.
  77. m_raw_value = child_text_content();
  78. update_placeholder_visibility();
  79. }
  80. void HTMLTextAreaElement::form_associated_element_was_inserted()
  81. {
  82. create_shadow_tree_if_needed();
  83. }
  84. void HTMLTextAreaElement::form_associated_element_was_removed(DOM::Node*)
  85. {
  86. set_shadow_root(nullptr);
  87. }
  88. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-defaultvalue
  89. String HTMLTextAreaElement::default_value() const
  90. {
  91. // The defaultValue attribute's getter must return the element's child text content.
  92. return child_text_content();
  93. }
  94. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-defaultvalue
  95. void HTMLTextAreaElement::set_default_value(String const& default_value)
  96. {
  97. // The defaultValue attribute's setter must string replace all with the given value within this element.
  98. string_replace_all(default_value);
  99. }
  100. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-value
  101. String HTMLTextAreaElement::value() const
  102. {
  103. // The value IDL attribute must, on getting, return the element's API value.
  104. return m_raw_value;
  105. }
  106. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-output-value
  107. void HTMLTextAreaElement::set_value(String const& value)
  108. {
  109. // FIXME: 1. Let oldAPIValue be this element's API value.
  110. // 2. Set this element's raw value to the new value.
  111. m_raw_value = value;
  112. // 3. Set this element's dirty value flag to true.
  113. m_dirty_value = true;
  114. // FIXME: 4. If the new API value is different from oldAPIValue, then move the text entry cursor position to the end of the text control, unselecting any selected text and resetting the selection direction to "none".
  115. update_placeholder_visibility();
  116. }
  117. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-textlength
  118. u32 HTMLTextAreaElement::text_length() const
  119. {
  120. // The textLength IDL attribute must return the length of the element's API value.
  121. // FIXME: This is inefficient!
  122. auto utf16_data = MUST(AK::utf8_to_utf16(m_raw_value));
  123. return Utf16View { utf16_data }.length_in_code_units();
  124. }
  125. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-cols
  126. unsigned HTMLTextAreaElement::cols() const
  127. {
  128. // The cols and rows attributes are limited to only positive numbers with fallback. The cols IDL attribute's default value is 20.
  129. if (auto cols_string = get_attribute(HTML::AttributeNames::cols); cols_string.has_value()) {
  130. if (auto cols = parse_non_negative_integer(*cols_string); cols.has_value())
  131. return *cols;
  132. }
  133. return 20;
  134. }
  135. WebIDL::ExceptionOr<void> HTMLTextAreaElement::set_cols(unsigned cols)
  136. {
  137. return set_attribute(HTML::AttributeNames::cols, MUST(String::number(cols)));
  138. }
  139. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-rows
  140. unsigned HTMLTextAreaElement::rows() const
  141. {
  142. // The cols and rows attributes are limited to only positive numbers with fallback. The rows IDL attribute's default value is 2.
  143. if (auto rows_string = get_attribute(HTML::AttributeNames::rows); rows_string.has_value()) {
  144. if (auto rows = parse_non_negative_integer(*rows_string); rows.has_value())
  145. return *rows;
  146. }
  147. return 2;
  148. }
  149. WebIDL::ExceptionOr<void> HTMLTextAreaElement::set_rows(unsigned rows)
  150. {
  151. return set_attribute(HTML::AttributeNames::rows, MUST(String::number(rows)));
  152. }
  153. void HTMLTextAreaElement::create_shadow_tree_if_needed()
  154. {
  155. if (shadow_root_internal())
  156. return;
  157. auto shadow_root = heap().allocate<DOM::ShadowRoot>(realm(), document(), *this, Bindings::ShadowRootMode::Closed);
  158. set_shadow_root(shadow_root);
  159. auto element = MUST(DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML));
  160. MUST(shadow_root->append_child(element));
  161. m_placeholder_element = MUST(DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML));
  162. m_placeholder_element->set_use_pseudo_element(CSS::Selector::PseudoElement::Type::Placeholder);
  163. MUST(element->append_child(*m_placeholder_element));
  164. m_placeholder_text_node = heap().allocate<DOM::Text>(realm(), document(), String {});
  165. m_placeholder_text_node->set_data(get_attribute_value(HTML::AttributeNames::placeholder));
  166. m_placeholder_text_node->set_editable_text_node_owner(Badge<HTMLTextAreaElement> {}, *this);
  167. MUST(m_placeholder_element->append_child(*m_placeholder_text_node));
  168. m_inner_text_element = MUST(DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML));
  169. MUST(element->append_child(*m_inner_text_element));
  170. m_text_node = heap().allocate<DOM::Text>(realm(), document(), String {});
  171. handle_readonly_attribute(attribute(HTML::AttributeNames::readonly));
  172. m_text_node->set_editable_text_node_owner(Badge<HTMLTextAreaElement> {}, *this);
  173. // NOTE: If `children_changed()` was called before now, `m_raw_value` will hold the text content.
  174. // Otherwise, it will get filled in whenever that does get called.
  175. m_text_node->set_text_content(m_raw_value);
  176. MUST(m_inner_text_element->append_child(*m_text_node));
  177. update_placeholder_visibility();
  178. }
  179. // https://html.spec.whatwg.org/multipage/input.html#attr-input-readonly
  180. void HTMLTextAreaElement::handle_readonly_attribute(Optional<String> const& maybe_value)
  181. {
  182. // The readonly attribute is a boolean attribute that controls whether or not the user can edit the form control. When specified, the element is not mutable.
  183. m_is_mutable = !maybe_value.has_value();
  184. if (m_text_node)
  185. m_text_node->set_always_editable(m_is_mutable);
  186. }
  187. void HTMLTextAreaElement::update_placeholder_visibility()
  188. {
  189. if (!m_placeholder_element)
  190. return;
  191. if (!m_text_node)
  192. return;
  193. auto placeholder_text = get_attribute(AttributeNames::placeholder);
  194. if (placeholder_text.has_value() && m_text_node->data().is_empty()) {
  195. MUST(m_placeholder_element->style_for_bindings()->set_property(CSS::PropertyID::Display, "block"sv));
  196. } else {
  197. MUST(m_placeholder_element->style_for_bindings()->set_property(CSS::PropertyID::Display, "none"sv));
  198. }
  199. }
  200. // https://html.spec.whatwg.org/multipage/form-elements.html#the-textarea-element:children-changed-steps
  201. void HTMLTextAreaElement::children_changed()
  202. {
  203. // The children changed steps for textarea elements must, if the element's dirty value flag is false,
  204. // set the element's raw value to its child text content.
  205. if (!m_dirty_value) {
  206. m_raw_value = child_text_content();
  207. if (m_text_node)
  208. m_text_node->set_text_content(m_raw_value);
  209. update_placeholder_visibility();
  210. }
  211. }
  212. void HTMLTextAreaElement::form_associated_element_attribute_changed(FlyString const& name, Optional<String> const& value)
  213. {
  214. if (name == HTML::AttributeNames::placeholder) {
  215. if (m_placeholder_text_node)
  216. m_placeholder_text_node->set_data(value.value_or(String {}));
  217. } else if (name == HTML::AttributeNames::readonly) {
  218. handle_readonly_attribute(value);
  219. }
  220. }
  221. void HTMLTextAreaElement::did_edit_text_node(Badge<Web::HTML::BrowsingContext>)
  222. {
  223. // A textarea element's dirty value flag must be set to true whenever the user interacts with the control in a way that changes the raw value.
  224. m_dirty_value = true;
  225. update_placeholder_visibility();
  226. }
  227. }