HTMLTextAreaElement.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/Namespace.h>
  17. namespace Web::HTML {
  18. HTMLTextAreaElement::HTMLTextAreaElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  19. : HTMLElement(document, move(qualified_name))
  20. {
  21. }
  22. HTMLTextAreaElement::~HTMLTextAreaElement() = default;
  23. JS::GCPtr<Layout::Node> HTMLTextAreaElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  24. {
  25. // AD-HOC: We rewrite `display: inline` to `display: inline-block`.
  26. // This is required for the internal shadow tree to work correctly in layout.
  27. if (style->display().is_inline_outside() && style->display().is_flow_inside())
  28. style->set_property(CSS::PropertyID::Display, CSS::DisplayStyleValue::create(CSS::Display::from_short(CSS::Display::Short::InlineBlock)));
  29. return Element::create_layout_node_for_display_type(document(), style->display(), style, this);
  30. }
  31. void HTMLTextAreaElement::initialize(JS::Realm& realm)
  32. {
  33. Base::initialize(realm);
  34. set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLTextAreaElementPrototype>(realm, "HTMLTextAreaElement"_fly_string));
  35. }
  36. void HTMLTextAreaElement::visit_edges(Cell::Visitor& visitor)
  37. {
  38. Base::visit_edges(visitor);
  39. visitor.visit(m_inner_text_element);
  40. visitor.visit(m_text_node);
  41. }
  42. void HTMLTextAreaElement::did_receive_focus()
  43. {
  44. auto* browsing_context = document().browsing_context();
  45. if (!browsing_context)
  46. return;
  47. if (!m_text_node)
  48. return;
  49. browsing_context->set_cursor_position(DOM::Position::create(realm(), *m_text_node, 0));
  50. }
  51. void HTMLTextAreaElement::did_lose_focus()
  52. {
  53. // The change event fires when the value is committed, if that makes sense for the control,
  54. // or else when the control loses focus
  55. queue_an_element_task(HTML::Task::Source::UserInteraction, [this] {
  56. auto change_event = DOM::Event::create(realm(), HTML::EventNames::change);
  57. change_event->set_bubbles(true);
  58. dispatch_event(change_event);
  59. });
  60. }
  61. // https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
  62. i32 HTMLTextAreaElement::default_tab_index_value() const
  63. {
  64. // See the base function for the spec comments.
  65. return 0;
  66. }
  67. // https://html.spec.whatwg.org/multipage/form-elements.html#the-textarea-element:concept-form-reset-control
  68. void HTMLTextAreaElement::reset_algorithm()
  69. {
  70. // The reset algorithm for textarea elements is to set the dirty value flag back to false,
  71. m_dirty = false;
  72. // and set the raw value of element to its child text content.
  73. m_raw_value = child_text_content();
  74. }
  75. void HTMLTextAreaElement::form_associated_element_was_inserted()
  76. {
  77. create_shadow_tree_if_needed();
  78. }
  79. void HTMLTextAreaElement::form_associated_element_was_removed(DOM::Node*)
  80. {
  81. set_shadow_root(nullptr);
  82. }
  83. void HTMLTextAreaElement::create_shadow_tree_if_needed()
  84. {
  85. if (shadow_root_internal())
  86. return;
  87. auto shadow_root = heap().allocate<DOM::ShadowRoot>(realm(), document(), *this, Bindings::ShadowRootMode::Closed);
  88. auto element = MUST(DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML));
  89. m_inner_text_element = MUST(DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML));
  90. m_text_node = heap().allocate<DOM::Text>(realm(), document(), String {});
  91. m_text_node->set_always_editable(true);
  92. m_text_node->set_editable_text_node_owner(Badge<HTMLTextAreaElement> {}, *this);
  93. // NOTE: If `children_changed()` was called before now, `m_raw_value` will hold the text content.
  94. // Otherwise, it will get filled in whenever that does get called.
  95. m_text_node->set_text_content(m_raw_value);
  96. MUST(m_inner_text_element->append_child(*m_text_node));
  97. MUST(element->append_child(*m_inner_text_element));
  98. MUST(shadow_root->append_child(element));
  99. set_shadow_root(shadow_root);
  100. }
  101. // https://html.spec.whatwg.org/multipage/form-elements.html#the-textarea-element:children-changed-steps
  102. void HTMLTextAreaElement::children_changed()
  103. {
  104. // The children changed steps for textarea elements must, if the element's dirty value flag is false,
  105. // set the element's raw value to its child text content.
  106. if (!m_dirty) {
  107. m_raw_value = child_text_content();
  108. if (m_text_node)
  109. m_text_node->set_text_content(m_raw_value);
  110. }
  111. }
  112. void HTMLTextAreaElement::did_edit_text_node(Badge<Web::HTML::BrowsingContext>)
  113. {
  114. // 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.
  115. m_dirty = true;
  116. }
  117. }