HTMLSlotElement.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/HTMLSlotElementPrototype.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/DOM/Element.h>
  10. #include <LibWeb/DOM/Text.h>
  11. #include <LibWeb/HTML/HTMLSlotElement.h>
  12. namespace Web::HTML {
  13. GC_DEFINE_ALLOCATOR(HTMLSlotElement);
  14. HTMLSlotElement::HTMLSlotElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  15. : HTMLElement(document, move(qualified_name))
  16. {
  17. }
  18. HTMLSlotElement::~HTMLSlotElement() = default;
  19. void HTMLSlotElement::initialize(JS::Realm& realm)
  20. {
  21. Base::initialize(realm);
  22. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLSlotElement);
  23. }
  24. void HTMLSlotElement::visit_edges(JS::Cell::Visitor& visitor)
  25. {
  26. Base::visit_edges(visitor);
  27. Slot::visit_edges(visitor);
  28. for (auto const& node : m_manually_assigned_nodes)
  29. node.visit([&](auto const& slottable) { visitor.visit(slottable); });
  30. }
  31. // https://html.spec.whatwg.org/multipage/scripting.html#dom-slot-assignednodes
  32. Vector<GC::Root<DOM::Node>> HTMLSlotElement::assigned_nodes(AssignedNodesOptions options) const
  33. {
  34. // 1. If options["flatten"] is false, then return this's assigned nodes.
  35. if (!options.flatten) {
  36. Vector<GC::Root<DOM::Node>> assigned_nodes;
  37. assigned_nodes.ensure_capacity(assigned_nodes_internal().size());
  38. for (auto const& node : assigned_nodes_internal()) {
  39. node.visit([&](auto const& node) {
  40. assigned_nodes.unchecked_append(*node);
  41. });
  42. }
  43. return assigned_nodes;
  44. }
  45. // FIXME: 2. Return the result of finding flattened slottables with this.
  46. return {};
  47. }
  48. // https://html.spec.whatwg.org/multipage/scripting.html#dom-slot-assignedelements
  49. Vector<GC::Root<DOM::Element>> HTMLSlotElement::assigned_elements(AssignedNodesOptions options) const
  50. {
  51. // 1. If options["flatten"] is false, then return this's assigned nodes, filtered to contain only Element nodes.
  52. if (!options.flatten) {
  53. Vector<GC::Root<DOM::Element>> assigned_nodes;
  54. for (auto const& node : assigned_nodes_internal()) {
  55. if (auto const* element = node.get_pointer<GC::Ref<DOM::Element>>())
  56. assigned_nodes.append(*element);
  57. }
  58. return assigned_nodes;
  59. }
  60. // FIXME: 2. Return the result of finding flattened slottables with this, filtered to contain only Element nodes.
  61. return {};
  62. }
  63. // https://html.spec.whatwg.org/multipage/scripting.html#dom-slot-assign
  64. void HTMLSlotElement::assign(Vector<SlottableHandle> nodes)
  65. {
  66. // 1. For each node of this's manually assigned nodes, set node's manual slot assignment to null.
  67. for (auto& node : m_manually_assigned_nodes) {
  68. node.visit([&](auto& node) {
  69. node->set_manual_slot_assignment(nullptr);
  70. });
  71. }
  72. // 2. Let nodesSet be a new ordered set.
  73. Vector<DOM::Slottable> nodes_set;
  74. // 3. For each node of nodes:
  75. for (auto& node_handle : nodes) {
  76. auto& node = node_handle.visit([](auto& node) -> DOM::SlottableMixin& { return *node; });
  77. auto slottable = node_handle.visit([](auto& node) { return node->as_slottable(); });
  78. // 1. If node's manual slot assignment refers to a slot, then remove node from that slot's manually assigned nodes.
  79. if (node.manual_slot_assignment() != nullptr) {
  80. m_manually_assigned_nodes.remove_all_matching([&](auto const& manually_assigned_node) {
  81. return slottable == manually_assigned_node;
  82. });
  83. }
  84. // 2. Set node's manual slot assignment to this.
  85. node.set_manual_slot_assignment(this);
  86. // 3. Append node to nodesSet.
  87. nodes_set.append(slottable);
  88. }
  89. // 4. Set this's manually assigned nodes to nodesSet.
  90. m_manually_assigned_nodes = move(nodes_set);
  91. // 5. Run assign slottables for a tree for this's root.
  92. assign_slottables_for_a_tree(root());
  93. }
  94. // https://dom.spec.whatwg.org/#ref-for-concept-element-attributes-change-ext
  95. void HTMLSlotElement::attribute_changed(FlyString const& local_name, Optional<String> const& old_value, Optional<String> const& value, Optional<FlyString> const& namespace_)
  96. {
  97. Base::attribute_changed(local_name, old_value, value, namespace_);
  98. // 1. If element is a slot, localName is name, and namespace is null, then:
  99. if (local_name == AttributeNames::name && !namespace_.has_value()) {
  100. // 1. If value is oldValue, then return.
  101. if (value == old_value)
  102. return;
  103. // 2. If value is null and oldValue is the empty string, then return.
  104. if (!value.has_value() && old_value == String {})
  105. return;
  106. // 3. If value is the empty string and oldValue is null, then return.
  107. if (value == String {} && !old_value.has_value())
  108. return;
  109. // 4. If value is null or the empty string, then set element’s name to the empty string.
  110. if (!value.has_value())
  111. set_slot_name({});
  112. // 5. Otherwise, set element’s name to value.
  113. else
  114. set_slot_name(*value);
  115. // 6. Run assign slottables for a tree with element’s root.
  116. DOM::assign_slottables_for_a_tree(root());
  117. }
  118. }
  119. }