HTMLTextAreaElement.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2024, Bastiaan van der Plaat <bastiaan.v.d.plaat@gmail.com>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/CSS/StyleProperties.h>
  10. #include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>
  11. #include <LibWeb/DOM/Document.h>
  12. #include <LibWeb/DOM/ElementFactory.h>
  13. #include <LibWeb/DOM/Event.h>
  14. #include <LibWeb/DOM/ShadowRoot.h>
  15. #include <LibWeb/DOM/Text.h>
  16. #include <LibWeb/HTML/HTMLTextAreaElement.h>
  17. #include <LibWeb/HTML/Numbers.h>
  18. #include <LibWeb/Namespace.h>
  19. namespace Web::HTML {
  20. JS_DEFINE_ALLOCATOR(HTMLTextAreaElement);
  21. HTMLTextAreaElement::HTMLTextAreaElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  22. : HTMLElement(document, move(qualified_name))
  23. {
  24. }
  25. HTMLTextAreaElement::~HTMLTextAreaElement() = default;
  26. JS::GCPtr<Layout::Node> HTMLTextAreaElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  27. {
  28. // AD-HOC: We rewrite `display: inline` to `display: inline-block`.
  29. // This is required for the internal shadow tree to work correctly in layout.
  30. if (style->display().is_inline_outside() && style->display().is_flow_inside())
  31. style->set_property(CSS::PropertyID::Display, CSS::DisplayStyleValue::create(CSS::Display::from_short(CSS::Display::Short::InlineBlock)));
  32. return Element::create_layout_node_for_display_type(document(), style->display(), style, this);
  33. }
  34. void HTMLTextAreaElement::initialize(JS::Realm& realm)
  35. {
  36. Base::initialize(realm);
  37. set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLTextAreaElementPrototype>(realm, "HTMLTextAreaElement"_fly_string));
  38. }
  39. void HTMLTextAreaElement::visit_edges(Cell::Visitor& visitor)
  40. {
  41. Base::visit_edges(visitor);
  42. visitor.visit(m_placeholder_element);
  43. visitor.visit(m_placeholder_text_node);
  44. visitor.visit(m_inner_text_element);
  45. visitor.visit(m_text_node);
  46. }
  47. void HTMLTextAreaElement::did_receive_focus()
  48. {
  49. auto* browsing_context = document().browsing_context();
  50. if (!browsing_context)
  51. return;
  52. if (!m_text_node)
  53. return;
  54. browsing_context->set_cursor_position(DOM::Position::create(realm(), *m_text_node, 0));
  55. }
  56. void HTMLTextAreaElement::did_lose_focus()
  57. {
  58. // The change event fires when the value is committed, if that makes sense for the control,
  59. // or else when the control loses focus
  60. queue_an_element_task(HTML::Task::Source::UserInteraction, [this] {
  61. auto change_event = DOM::Event::create(realm(), HTML::EventNames::change);
  62. change_event->set_bubbles(true);
  63. dispatch_event(change_event);
  64. });
  65. }
  66. // https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
  67. i32 HTMLTextAreaElement::default_tab_index_value() const
  68. {
  69. // See the base function for the spec comments.
  70. return 0;
  71. }
  72. // https://html.spec.whatwg.org/multipage/form-elements.html#the-textarea-element:concept-form-reset-control
  73. void HTMLTextAreaElement::reset_algorithm()
  74. {
  75. // The reset algorithm for textarea elements is to set the dirty value flag back to false,
  76. m_dirty_value = false;
  77. // and set the raw value of element to its child text content.
  78. m_raw_value = child_text_content();
  79. update_placeholder_visibility();
  80. }
  81. void HTMLTextAreaElement::form_associated_element_was_inserted()
  82. {
  83. create_shadow_tree_if_needed();
  84. }
  85. void HTMLTextAreaElement::form_associated_element_was_removed(DOM::Node*)
  86. {
  87. set_shadow_root(nullptr);
  88. }
  89. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-defaultvalue
  90. String HTMLTextAreaElement::default_value() const
  91. {
  92. // The defaultValue attribute's getter must return the element's child text content.
  93. return child_text_content();
  94. }
  95. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-defaultvalue
  96. void HTMLTextAreaElement::set_default_value(String const& default_value)
  97. {
  98. // The defaultValue attribute's setter must string replace all with the given value within this element.
  99. string_replace_all(default_value);
  100. }
  101. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-value
  102. String HTMLTextAreaElement::value() const
  103. {
  104. // The value IDL attribute must, on getting, return the element's API value.
  105. return m_raw_value;
  106. }
  107. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-output-value
  108. void HTMLTextAreaElement::set_value(String const& value)
  109. {
  110. // FIXME: 1. Let oldAPIValue be this element's API value.
  111. // 2. Set this element's raw value to the new value.
  112. m_raw_value = value;
  113. // 3. Set this element's dirty value flag to true.
  114. m_dirty_value = true;
  115. // 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".
  116. update_placeholder_visibility();
  117. }
  118. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-textlength
  119. u32 HTMLTextAreaElement::text_length() const
  120. {
  121. // The textLength IDL attribute must return the length of the element's API value.
  122. // FIXME: This is inefficient!
  123. auto utf16_data = MUST(AK::utf8_to_utf16(m_raw_value));
  124. return Utf16View { utf16_data }.length_in_code_units();
  125. }
  126. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-checkvalidity
  127. bool HTMLTextAreaElement::check_validity()
  128. {
  129. dbgln("(STUBBED) HTMLTextAreaElement::check_validity(). Called on: {}", debug_description());
  130. return true;
  131. }
  132. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-reportvalidity
  133. bool HTMLTextAreaElement::report_validity()
  134. {
  135. dbgln("(STUBBED) HTMLTextAreaElement::report_validity(). Called on: {}", debug_description());
  136. return true;
  137. }
  138. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-setcustomvalidity
  139. void HTMLTextAreaElement::set_custom_validity(String const& error)
  140. {
  141. dbgln("(STUBBED) HTMLTextAreaElement::set_custom_validity(\"{}\"). Called on: {}", error, debug_description());
  142. }
  143. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-maxlength
  144. WebIDL::Long HTMLTextAreaElement::max_length() const
  145. {
  146. // The maxLength IDL attribute must reflect the maxlength content attribute, limited to only non-negative numbers.
  147. if (auto maxlength_string = get_attribute(HTML::AttributeNames::maxlength); maxlength_string.has_value()) {
  148. if (auto maxlength = parse_non_negative_integer(*maxlength_string); maxlength.has_value())
  149. return *maxlength;
  150. }
  151. return -1;
  152. }
  153. WebIDL::ExceptionOr<void> HTMLTextAreaElement::set_max_length(WebIDL::Long value)
  154. {
  155. // The maxLength IDL attribute must reflect the maxlength content attribute, limited to only non-negative numbers.
  156. return set_attribute(HTML::AttributeNames::maxlength, TRY(convert_non_negative_integer_to_string(realm(), value)));
  157. }
  158. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-minlength
  159. WebIDL::Long HTMLTextAreaElement::min_length() const
  160. {
  161. // The minLength IDL attribute must reflect the minlength content attribute, limited to only non-negative numbers.
  162. if (auto minlength_string = get_attribute(HTML::AttributeNames::minlength); minlength_string.has_value()) {
  163. if (auto minlength = parse_non_negative_integer(*minlength_string); minlength.has_value())
  164. return *minlength;
  165. }
  166. return -1;
  167. }
  168. WebIDL::ExceptionOr<void> HTMLTextAreaElement::set_min_length(WebIDL::Long value)
  169. {
  170. // The minLength IDL attribute must reflect the minlength content attribute, limited to only non-negative numbers.
  171. return set_attribute(HTML::AttributeNames::minlength, TRY(convert_non_negative_integer_to_string(realm(), value)));
  172. }
  173. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-cols
  174. unsigned HTMLTextAreaElement::cols() const
  175. {
  176. // The cols and rows attributes are limited to only positive numbers with fallback. The cols IDL attribute's default value is 20.
  177. if (auto cols_string = get_attribute(HTML::AttributeNames::cols); cols_string.has_value()) {
  178. if (auto cols = parse_non_negative_integer(*cols_string); cols.has_value())
  179. return *cols;
  180. }
  181. return 20;
  182. }
  183. WebIDL::ExceptionOr<void> HTMLTextAreaElement::set_cols(unsigned cols)
  184. {
  185. return set_attribute(HTML::AttributeNames::cols, MUST(String::number(cols)));
  186. }
  187. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-rows
  188. unsigned HTMLTextAreaElement::rows() const
  189. {
  190. // The cols and rows attributes are limited to only positive numbers with fallback. The rows IDL attribute's default value is 2.
  191. if (auto rows_string = get_attribute(HTML::AttributeNames::rows); rows_string.has_value()) {
  192. if (auto rows = parse_non_negative_integer(*rows_string); rows.has_value())
  193. return *rows;
  194. }
  195. return 2;
  196. }
  197. WebIDL::ExceptionOr<void> HTMLTextAreaElement::set_rows(unsigned rows)
  198. {
  199. return set_attribute(HTML::AttributeNames::rows, MUST(String::number(rows)));
  200. }
  201. void HTMLTextAreaElement::create_shadow_tree_if_needed()
  202. {
  203. if (shadow_root_internal())
  204. return;
  205. auto shadow_root = heap().allocate<DOM::ShadowRoot>(realm(), document(), *this, Bindings::ShadowRootMode::Closed);
  206. set_shadow_root(shadow_root);
  207. auto element = MUST(DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML));
  208. MUST(shadow_root->append_child(element));
  209. m_placeholder_element = MUST(DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML));
  210. m_placeholder_element->set_use_pseudo_element(CSS::Selector::PseudoElement::Type::Placeholder);
  211. MUST(element->append_child(*m_placeholder_element));
  212. m_placeholder_text_node = heap().allocate<DOM::Text>(realm(), document(), String {});
  213. m_placeholder_text_node->set_data(get_attribute_value(HTML::AttributeNames::placeholder));
  214. m_placeholder_text_node->set_editable_text_node_owner(Badge<HTMLTextAreaElement> {}, *this);
  215. MUST(m_placeholder_element->append_child(*m_placeholder_text_node));
  216. m_inner_text_element = MUST(DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML));
  217. MUST(element->append_child(*m_inner_text_element));
  218. m_text_node = heap().allocate<DOM::Text>(realm(), document(), String {});
  219. handle_readonly_attribute(attribute(HTML::AttributeNames::readonly));
  220. m_text_node->set_editable_text_node_owner(Badge<HTMLTextAreaElement> {}, *this);
  221. // NOTE: If `children_changed()` was called before now, `m_raw_value` will hold the text content.
  222. // Otherwise, it will get filled in whenever that does get called.
  223. m_text_node->set_text_content(m_raw_value);
  224. handle_maxlength_attribute();
  225. MUST(m_inner_text_element->append_child(*m_text_node));
  226. update_placeholder_visibility();
  227. }
  228. // https://html.spec.whatwg.org/multipage/input.html#attr-input-readonly
  229. void HTMLTextAreaElement::handle_readonly_attribute(Optional<String> const& maybe_value)
  230. {
  231. // 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.
  232. m_is_mutable = !maybe_value.has_value();
  233. if (m_text_node)
  234. m_text_node->set_always_editable(m_is_mutable);
  235. }
  236. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-maxlength
  237. void HTMLTextAreaElement::handle_maxlength_attribute()
  238. {
  239. if (m_text_node) {
  240. auto max_length = this->max_length();
  241. if (max_length >= 0) {
  242. m_text_node->set_max_length(max_length);
  243. } else {
  244. m_text_node->set_max_length({});
  245. }
  246. }
  247. }
  248. void HTMLTextAreaElement::update_placeholder_visibility()
  249. {
  250. if (!m_placeholder_element)
  251. return;
  252. if (!m_text_node)
  253. return;
  254. auto placeholder_text = get_attribute(AttributeNames::placeholder);
  255. if (placeholder_text.has_value() && m_text_node->data().is_empty()) {
  256. MUST(m_placeholder_element->style_for_bindings()->set_property(CSS::PropertyID::Display, "block"sv));
  257. } else {
  258. MUST(m_placeholder_element->style_for_bindings()->set_property(CSS::PropertyID::Display, "none"sv));
  259. }
  260. }
  261. // https://html.spec.whatwg.org/multipage/form-elements.html#the-textarea-element:children-changed-steps
  262. void HTMLTextAreaElement::children_changed()
  263. {
  264. // The children changed steps for textarea elements must, if the element's dirty value flag is false,
  265. // set the element's raw value to its child text content.
  266. if (!m_dirty_value) {
  267. m_raw_value = child_text_content();
  268. if (m_text_node)
  269. m_text_node->set_text_content(m_raw_value);
  270. update_placeholder_visibility();
  271. }
  272. }
  273. void HTMLTextAreaElement::form_associated_element_attribute_changed(FlyString const& name, Optional<String> const& value)
  274. {
  275. if (name == HTML::AttributeNames::placeholder) {
  276. if (m_placeholder_text_node)
  277. m_placeholder_text_node->set_data(value.value_or(String {}));
  278. } else if (name == HTML::AttributeNames::readonly) {
  279. handle_readonly_attribute(value);
  280. } else if (name == HTML::AttributeNames::maxlength) {
  281. handle_maxlength_attribute();
  282. }
  283. }
  284. void HTMLTextAreaElement::did_edit_text_node(Badge<Web::HTML::BrowsingContext>)
  285. {
  286. // 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.
  287. m_dirty_value = true;
  288. update_placeholder_visibility();
  289. }
  290. }