HTMLDetailsElement.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/DOM/ElementFactory.h>
  9. #include <LibWeb/DOM/Event.h>
  10. #include <LibWeb/DOM/ShadowRoot.h>
  11. #include <LibWeb/DOM/Text.h>
  12. #include <LibWeb/HTML/EventLoop/TaskQueue.h>
  13. #include <LibWeb/HTML/HTMLDetailsElement.h>
  14. #include <LibWeb/HTML/HTMLSlotElement.h>
  15. #include <LibWeb/HTML/HTMLSummaryElement.h>
  16. #include <LibWeb/HTML/ToggleEvent.h>
  17. #include <LibWeb/Namespace.h>
  18. namespace Web::HTML {
  19. JS_DEFINE_ALLOCATOR(HTMLDetailsElement);
  20. HTMLDetailsElement::HTMLDetailsElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  21. : HTMLElement(document, move(qualified_name))
  22. {
  23. }
  24. HTMLDetailsElement::~HTMLDetailsElement() = default;
  25. void HTMLDetailsElement::visit_edges(Cell::Visitor& visitor)
  26. {
  27. Base::visit_edges(visitor);
  28. visitor.visit(m_summary_slot);
  29. visitor.visit(m_descendants_slot);
  30. }
  31. void HTMLDetailsElement::initialize(JS::Realm& realm)
  32. {
  33. Base::initialize(realm);
  34. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLDetailsElement);
  35. }
  36. void HTMLDetailsElement::inserted()
  37. {
  38. create_shadow_tree_if_needed().release_value_but_fixme_should_propagate_errors();
  39. update_shadow_tree_slots();
  40. }
  41. void HTMLDetailsElement::removed_from(DOM::Node*)
  42. {
  43. set_shadow_root(nullptr);
  44. }
  45. void HTMLDetailsElement::attribute_changed(FlyString const& name, Optional<String> const& value)
  46. {
  47. Base::attribute_changed(name, value);
  48. // https://html.spec.whatwg.org/multipage/interactive-elements.html#details-notification-task-steps
  49. if (name == HTML::AttributeNames::open) {
  50. // 1. If the open attribute is added, queue a details toggle event task given the details element, "closed", and "open".
  51. if (value.has_value()) {
  52. queue_a_details_toggle_event_task("closed"_string, "open"_string);
  53. }
  54. // 2. Otherwise, queue a details toggle event task given the details element, "open", and "closed".
  55. else {
  56. queue_a_details_toggle_event_task("open"_string, "closed"_string);
  57. }
  58. update_shadow_tree_style();
  59. }
  60. }
  61. void HTMLDetailsElement::children_changed()
  62. {
  63. Base::children_changed();
  64. update_shadow_tree_slots();
  65. }
  66. // https://html.spec.whatwg.org/multipage/interactive-elements.html#queue-a-details-toggle-event-task
  67. void HTMLDetailsElement::queue_a_details_toggle_event_task(String old_state, String new_state)
  68. {
  69. // 1. If element's details toggle task tracker is not null, then:
  70. if (m_details_toggle_task_tracker.has_value()) {
  71. // 1. Set oldState to element's details toggle task tracker's old state.
  72. old_state = move(m_details_toggle_task_tracker->old_state);
  73. // 2. Remove element's details toggle task tracker's task from its task queue.
  74. HTML::main_thread_event_loop().task_queue().remove_tasks_matching([&](auto const& task) {
  75. return task.id() == m_details_toggle_task_tracker->task_id;
  76. });
  77. // 3. Set element's details toggle task tracker to null.
  78. m_details_toggle_task_tracker->task_id = {};
  79. }
  80. // 2. Queue an element task given the DOM manipulation task source and element to run the following steps:
  81. auto task_id = queue_an_element_task(HTML::Task::Source::DOMManipulation, [this, old_state, new_state = move(new_state)]() mutable {
  82. // 1. Fire an event named toggle at element, using ToggleEvent, with the oldState attribute initialized to
  83. // oldState and the newState attribute initialized to newState.
  84. ToggleEventInit event_init {};
  85. event_init.old_state = move(old_state);
  86. event_init.new_state = move(new_state);
  87. dispatch_event(ToggleEvent::create(realm(), HTML::EventNames::toggle, move(event_init)));
  88. // 2. Set element's details toggle task tracker to null.
  89. m_details_toggle_task_tracker = {};
  90. });
  91. // 3. Set element's details toggle task tracker to a struct with task set to the just-queued task and old state set to oldState.
  92. m_details_toggle_task_tracker = ToggleTaskTracker {
  93. .task_id = task_id,
  94. .old_state = move(old_state),
  95. };
  96. }
  97. // https://html.spec.whatwg.org/#the-details-and-summary-elements
  98. WebIDL::ExceptionOr<void> HTMLDetailsElement::create_shadow_tree_if_needed()
  99. {
  100. if (shadow_root_internal())
  101. return {};
  102. auto& realm = this->realm();
  103. // The element is also expected to have an internal shadow tree with two slots.
  104. auto shadow_root = heap().allocate<DOM::ShadowRoot>(realm, document(), *this, Bindings::ShadowRootMode::Closed);
  105. shadow_root->set_slot_assignment(Bindings::SlotAssignmentMode::Manual);
  106. // The first slot is expected to take the details element's first summary element child, if any.
  107. auto summary_slot = TRY(DOM::create_element(document(), HTML::TagNames::slot, Namespace::HTML));
  108. MUST(shadow_root->append_child(summary_slot));
  109. // The second slot is expected to take the details element's remaining descendants, if any.
  110. auto descendants_slot = TRY(DOM::create_element(document(), HTML::TagNames::slot, Namespace::HTML));
  111. MUST(shadow_root->append_child(descendants_slot));
  112. m_summary_slot = static_cast<HTML::HTMLSlotElement&>(*summary_slot);
  113. m_descendants_slot = static_cast<HTML::HTMLSlotElement&>(*descendants_slot);
  114. set_shadow_root(shadow_root);
  115. return {};
  116. }
  117. void HTMLDetailsElement::update_shadow_tree_slots()
  118. {
  119. if (!shadow_root_internal())
  120. return;
  121. Vector<HTMLSlotElement::SlottableHandle> summary_assignment;
  122. Vector<HTMLSlotElement::SlottableHandle> descendants_assignment;
  123. auto* summary = first_child_of_type<HTMLSummaryElement>();
  124. if (summary != nullptr)
  125. summary_assignment.append(JS::make_handle(static_cast<DOM::Element&>(*summary)));
  126. for_each_in_subtree([&](auto& child) {
  127. if (&child == summary)
  128. return IterationDecision::Continue;
  129. if (!child.is_slottable())
  130. return IterationDecision::Continue;
  131. child.as_slottable().visit([&](auto& node) {
  132. descendants_assignment.append(JS::make_handle(node));
  133. });
  134. return IterationDecision::Continue;
  135. });
  136. m_summary_slot->assign(move(summary_assignment));
  137. m_descendants_slot->assign(move(descendants_assignment));
  138. update_shadow_tree_style();
  139. }
  140. // https://html.spec.whatwg.org/#the-details-and-summary-elements:the-details-element-6
  141. void HTMLDetailsElement::update_shadow_tree_style()
  142. {
  143. if (!shadow_root_internal())
  144. return;
  145. if (has_attribute(HTML::AttributeNames::open)) {
  146. MUST(m_descendants_slot->set_attribute(HTML::AttributeNames::style, R"~~~(
  147. display: block;
  148. )~~~"_string));
  149. } else {
  150. // FIXME: Should be `display: block` but we do not support `content-visibility: hidden`.
  151. MUST(m_descendants_slot->set_attribute(HTML::AttributeNames::style, R"~~~(
  152. display: none;
  153. content-visibility: hidden;
  154. )~~~"_string));
  155. }
  156. }
  157. }