HTMLTextAreaElement.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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_inner_text_element);
  42. visitor.visit(m_text_node);
  43. }
  44. void HTMLTextAreaElement::did_receive_focus()
  45. {
  46. auto* browsing_context = document().browsing_context();
  47. if (!browsing_context)
  48. return;
  49. if (!m_text_node)
  50. return;
  51. browsing_context->set_cursor_position(DOM::Position::create(realm(), *m_text_node, 0));
  52. }
  53. void HTMLTextAreaElement::did_lose_focus()
  54. {
  55. // The change event fires when the value is committed, if that makes sense for the control,
  56. // or else when the control loses focus
  57. queue_an_element_task(HTML::Task::Source::UserInteraction, [this] {
  58. auto change_event = DOM::Event::create(realm(), HTML::EventNames::change);
  59. change_event->set_bubbles(true);
  60. dispatch_event(change_event);
  61. });
  62. }
  63. // https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
  64. i32 HTMLTextAreaElement::default_tab_index_value() const
  65. {
  66. // See the base function for the spec comments.
  67. return 0;
  68. }
  69. // https://html.spec.whatwg.org/multipage/form-elements.html#the-textarea-element:concept-form-reset-control
  70. void HTMLTextAreaElement::reset_algorithm()
  71. {
  72. // The reset algorithm for textarea elements is to set the dirty value flag back to false,
  73. m_dirty = false;
  74. // and set the raw value of element to its child text content.
  75. m_raw_value = child_text_content();
  76. }
  77. void HTMLTextAreaElement::form_associated_element_was_inserted()
  78. {
  79. create_shadow_tree_if_needed();
  80. }
  81. void HTMLTextAreaElement::form_associated_element_was_removed(DOM::Node*)
  82. {
  83. set_shadow_root(nullptr);
  84. }
  85. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-cols
  86. unsigned HTMLTextAreaElement::cols() const
  87. {
  88. // The cols and rows attributes are limited to only positive numbers with fallback. The cols IDL attribute's default value is 20.
  89. if (auto cols_string = get_attribute(HTML::AttributeNames::cols); cols_string.has_value()) {
  90. if (auto cols = parse_non_negative_integer(*cols_string); cols.has_value())
  91. return *cols;
  92. }
  93. return 20;
  94. }
  95. WebIDL::ExceptionOr<void> HTMLTextAreaElement::set_cols(unsigned value)
  96. {
  97. return set_attribute(HTML::AttributeNames::cols, MUST(String::number(value)));
  98. }
  99. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-rows
  100. unsigned HTMLTextAreaElement::rows() const
  101. {
  102. // The cols and rows attributes are limited to only positive numbers with fallback. The rows IDL attribute's default value is 2.
  103. if (auto rows_string = get_attribute(HTML::AttributeNames::rows); rows_string.has_value()) {
  104. if (auto rows = parse_non_negative_integer(*rows_string); rows.has_value())
  105. return *rows;
  106. }
  107. return 2;
  108. }
  109. WebIDL::ExceptionOr<void> HTMLTextAreaElement::set_rows(unsigned value)
  110. {
  111. return set_attribute(HTML::AttributeNames::rows, MUST(String::number(value)));
  112. }
  113. void HTMLTextAreaElement::create_shadow_tree_if_needed()
  114. {
  115. if (shadow_root_internal())
  116. return;
  117. auto shadow_root = heap().allocate<DOM::ShadowRoot>(realm(), document(), *this, Bindings::ShadowRootMode::Closed);
  118. auto element = MUST(DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML));
  119. m_inner_text_element = MUST(DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML));
  120. m_text_node = heap().allocate<DOM::Text>(realm(), document(), String {});
  121. m_text_node->set_always_editable(true);
  122. m_text_node->set_editable_text_node_owner(Badge<HTMLTextAreaElement> {}, *this);
  123. // NOTE: If `children_changed()` was called before now, `m_raw_value` will hold the text content.
  124. // Otherwise, it will get filled in whenever that does get called.
  125. m_text_node->set_text_content(m_raw_value);
  126. MUST(m_inner_text_element->append_child(*m_text_node));
  127. MUST(element->append_child(*m_inner_text_element));
  128. MUST(shadow_root->append_child(element));
  129. set_shadow_root(shadow_root);
  130. }
  131. // https://html.spec.whatwg.org/multipage/form-elements.html#the-textarea-element:children-changed-steps
  132. void HTMLTextAreaElement::children_changed()
  133. {
  134. // The children changed steps for textarea elements must, if the element's dirty value flag is false,
  135. // set the element's raw value to its child text content.
  136. if (!m_dirty) {
  137. m_raw_value = child_text_content();
  138. if (m_text_node)
  139. m_text_node->set_text_content(m_raw_value);
  140. }
  141. }
  142. void HTMLTextAreaElement::did_edit_text_node(Badge<Web::HTML::BrowsingContext>)
  143. {
  144. // 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.
  145. m_dirty = true;
  146. }
  147. }