Slottable.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/MainThreadVM.h>
  7. #include <LibWeb/DOM/Element.h>
  8. #include <LibWeb/DOM/Node.h>
  9. #include <LibWeb/DOM/ShadowRoot.h>
  10. #include <LibWeb/DOM/Slottable.h>
  11. #include <LibWeb/DOM/Text.h>
  12. #include <LibWeb/HTML/HTMLSlotElement.h>
  13. namespace Web::DOM {
  14. SlottableMixin::~SlottableMixin() = default;
  15. void SlottableMixin::visit_edges(JS::Cell::Visitor& visitor)
  16. {
  17. visitor.visit(m_assigned_slot);
  18. visitor.visit(m_manual_slot_assignment);
  19. }
  20. // https://dom.spec.whatwg.org/#dom-slotable-assignedslot
  21. JS::GCPtr<HTML::HTMLSlotElement> SlottableMixin::assigned_slot()
  22. {
  23. auto* node = dynamic_cast<DOM::Node*>(this);
  24. VERIFY(node);
  25. // The assignedSlot getter steps are to return the result of find a slot given this and with the open flag set.
  26. return find_a_slot(node->as_slottable(), OpenFlag::Set);
  27. }
  28. JS::GCPtr<HTML::HTMLSlotElement> assigned_slot_for_node(JS::NonnullGCPtr<Node> node)
  29. {
  30. if (!node->is_slottable())
  31. return nullptr;
  32. return node->as_slottable().visit([](auto const& slottable) {
  33. return slottable->assigned_slot_internal();
  34. });
  35. }
  36. // https://dom.spec.whatwg.org/#slotable-assigned
  37. bool is_an_assigned_slottable(JS::NonnullGCPtr<Node> node)
  38. {
  39. if (!node->is_slottable())
  40. return false;
  41. // A slottable is assigned if its assigned slot is non-null.
  42. return assigned_slot_for_node(node) != nullptr;
  43. }
  44. // https://dom.spec.whatwg.org/#find-a-slot
  45. JS::GCPtr<HTML::HTMLSlotElement> find_a_slot(Slottable const& slottable, OpenFlag open_flag)
  46. {
  47. // 1. If slottable’s parent is null, then return null.
  48. auto* parent = slottable.visit([](auto& node) { return node->parent_element(); });
  49. if (!parent)
  50. return nullptr;
  51. // 2. Let shadow be slottable’s parent’s shadow root.
  52. auto* shadow = parent->shadow_root_internal();
  53. // 3. If shadow is null, then return null.
  54. if (shadow == nullptr)
  55. return nullptr;
  56. // 4. If the open flag is set and shadow’s mode is not "open", then return null.
  57. if (open_flag == OpenFlag::Set && shadow->mode() != Bindings::ShadowRootMode::Open)
  58. return nullptr;
  59. // 5. If shadow’s slot assignment is "manual", then return the slot in shadow’s descendants whose manually assigned
  60. // nodes contains slottable, if any; otherwise null.
  61. if (shadow->slot_assignment() == Bindings::SlotAssignmentMode::Manual) {
  62. JS::GCPtr<HTML::HTMLSlotElement> slot;
  63. shadow->for_each_in_subtree_of_type<HTML::HTMLSlotElement>([&](auto& child) {
  64. if (!child.manually_assigned_nodes().contains_slow(slottable))
  65. return IterationDecision::Continue;
  66. slot = child;
  67. return IterationDecision::Break;
  68. });
  69. return slot;
  70. }
  71. // 6. Return the first slot in tree order in shadow’s descendants whose name is slottable’s name, if any; otherwise null.
  72. auto const& slottable_name = slottable.visit([](auto const& node) { return node->slottable_name(); });
  73. JS::GCPtr<HTML::HTMLSlotElement> slot;
  74. shadow->for_each_in_subtree_of_type<HTML::HTMLSlotElement>([&](auto& child) {
  75. if (child.slot_name() != slottable_name)
  76. return IterationDecision::Continue;
  77. slot = child;
  78. return IterationDecision::Break;
  79. });
  80. return slot;
  81. }
  82. // https://dom.spec.whatwg.org/#find-slotables
  83. Vector<Slottable> find_slottables(JS::NonnullGCPtr<HTML::HTMLSlotElement> slot)
  84. {
  85. // 1. Let result be an empty list.
  86. Vector<Slottable> result;
  87. // 2. Let root be slot’s root.
  88. auto& root = slot->root();
  89. // 3. If root is not a shadow root, then return result.
  90. if (!root.is_shadow_root())
  91. return result;
  92. // 4. Let host be root’s host.
  93. auto& shadow_root = static_cast<ShadowRoot&>(root);
  94. auto* host = shadow_root.host();
  95. // 5. If root’s slot assignment is "manual", then:
  96. if (shadow_root.slot_assignment() == Bindings::SlotAssignmentMode::Manual) {
  97. // 1. Let result be « ».
  98. // 2. For each slottable slottable of slot’s manually assigned nodes, if slottable’s parent is host, append slottable to result.
  99. for (auto const& slottable : slot->manually_assigned_nodes()) {
  100. auto const* parent = slottable.visit([](auto const& node) { return node->parent(); });
  101. if (parent == host)
  102. result.append(slottable);
  103. }
  104. }
  105. // 6. Otherwise, for each slottable child slottable of host, in tree order:
  106. else {
  107. host->for_each_child([&](auto& node) {
  108. if (!node.is_slottable())
  109. return;
  110. auto slottable = node.as_slottable();
  111. // 1. Let foundSlot be the result of finding a slot given slottable.
  112. auto found_slot = find_a_slot(slottable);
  113. // 2. If foundSlot is slot, then append slottable to result.
  114. if (found_slot == slot)
  115. result.append(move(slottable));
  116. });
  117. }
  118. // 7. Return result.
  119. return result;
  120. }
  121. // https://dom.spec.whatwg.org/#assign-slotables
  122. void assign_slottables(JS::NonnullGCPtr<HTML::HTMLSlotElement> slot)
  123. {
  124. // 1. Let slottables be the result of finding slottables for slot.
  125. auto slottables = find_slottables(slot);
  126. // 2. If slottables and slot’s assigned nodes are not identical, then run signal a slot change for slot.
  127. if (slottables != slot->assigned_nodes_internal())
  128. signal_a_slot_change(slot);
  129. // 4. For each slottable in slottables, set slottable’s assigned slot to slot.
  130. for (auto& slottable : slottables) {
  131. slottable.visit([&](auto& node) {
  132. node->set_assigned_slot(slot);
  133. });
  134. }
  135. // 3. Set slot’s assigned nodes to slottables.
  136. // NOTE: We do this step last so that we can move the slottables list.
  137. slot->set_assigned_nodes(move(slottables));
  138. }
  139. // https://dom.spec.whatwg.org/#assign-slotables-for-a-tree
  140. void assign_slottables_for_a_tree(JS::NonnullGCPtr<Node> root)
  141. {
  142. // AD-HOC: This method iterates over the root's entire subtree. That iteration does nothing if the root is not a
  143. // shadow root (see `find_slottables`). This iteration can be very expensive as the HTML parser inserts
  144. // nodes, especially on sites with many elements. So we skip it if we know it's going to be a no-op anyways.
  145. if (!root->is_shadow_root())
  146. return;
  147. // To assign slottables for a tree, given a node root, run assign slottables for each slot slot in root’s inclusive
  148. // descendants, in tree order.
  149. root->for_each_in_inclusive_subtree_of_type<HTML::HTMLSlotElement>([](auto& slot) {
  150. assign_slottables(slot);
  151. return IterationDecision::Continue;
  152. });
  153. }
  154. // https://dom.spec.whatwg.org/#assign-a-slot
  155. void assign_a_slot(Slottable const& slottable)
  156. {
  157. // 1. Let slot be the result of finding a slot with slottable.
  158. auto slot = find_a_slot(slottable);
  159. // 2. If slot is non-null, then run assign slottables for slot.
  160. if (slot != nullptr)
  161. assign_slottables(*slot);
  162. }
  163. // https://dom.spec.whatwg.org/#signal-a-slot-change
  164. void signal_a_slot_change(JS::NonnullGCPtr<HTML::HTMLSlotElement> slottable)
  165. {
  166. // FIXME: 1. Append slot to slot’s relevant agent’s signal slots.
  167. // 2. Queue a mutation observer microtask.
  168. Bindings::queue_mutation_observer_microtask(slottable->document());
  169. }
  170. }