HTMLSlotElement.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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/Element.h>
  9. #include <LibWeb/DOM/Text.h>
  10. #include <LibWeb/HTML/HTMLSlotElement.h>
  11. namespace Web::HTML {
  12. HTMLSlotElement::HTMLSlotElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  13. : HTMLElement(document, move(qualified_name))
  14. {
  15. }
  16. HTMLSlotElement::~HTMLSlotElement() = default;
  17. void HTMLSlotElement::initialize(JS::Realm& realm)
  18. {
  19. Base::initialize(realm);
  20. set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLSlotElementPrototype>(realm, "HTMLSlotElement"));
  21. }
  22. void HTMLSlotElement::visit_edges(JS::Cell::Visitor& visitor)
  23. {
  24. Base::visit_edges(visitor);
  25. Slot::visit_edges(visitor);
  26. for (auto const& node : m_manually_assigned_nodes)
  27. node.visit([&](auto const& slottable) { visitor.visit(slottable); });
  28. }
  29. // https://html.spec.whatwg.org/multipage/scripting.html#dom-slot-assignednodes
  30. Vector<JS::Handle<DOM::Node>> HTMLSlotElement::assigned_nodes(AssignedNodesOptions)
  31. {
  32. // FIXME: 1. If options["flatten"] is false, then return this's assigned nodes.
  33. // FIXME: 2. Return the result of finding flattened slottables with this.
  34. return {};
  35. }
  36. // https://html.spec.whatwg.org/multipage/scripting.html#dom-slot-assignedelements
  37. Vector<JS::Handle<DOM::Element>> HTMLSlotElement::assigned_elements(AssignedNodesOptions)
  38. {
  39. // FIXME: 1. If options["flatten"] is false, then return this's assigned nodes, filtered to contain only Element nodes.
  40. // FIXME: 2. Return the result of finding flattened slottables with this, filtered to contain only Element nodes.
  41. return {};
  42. }
  43. // https://html.spec.whatwg.org/multipage/scripting.html#dom-slot-assign
  44. void HTMLSlotElement::assign(Vector<SlottableHandle> nodes)
  45. {
  46. // 1. For each node of this's manually assigned nodes, set node's manual slot assignment to null.
  47. for (auto& node : m_manually_assigned_nodes) {
  48. node.visit([&](auto& node) {
  49. node->set_manual_slot_assignment(nullptr);
  50. });
  51. }
  52. // 2. Let nodesSet be a new ordered set.
  53. Vector<DOM::Slottable> nodes_set;
  54. // 3. For each node of nodes:
  55. for (auto& node_handle : nodes) {
  56. auto& node = node_handle.visit([](auto& node) -> DOM::SlottableMixin& { return *node; });
  57. auto slottable = node_handle.visit([](auto& node) { return node->as_slottable(); });
  58. // 1. If node's manual slot assignment refers to a slot, then remove node from that slot's manually assigned nodes.
  59. if (node.manual_slot_assignment() != nullptr) {
  60. m_manually_assigned_nodes.remove_all_matching([&](auto const& manually_assigned_node) {
  61. return slottable == manually_assigned_node;
  62. });
  63. }
  64. // 2. Set node's manual slot assignment to this.
  65. node.set_manual_slot_assignment(this);
  66. // 3. Append node to nodesSet.
  67. nodes_set.append(slottable);
  68. }
  69. // 4. Set this's manually assigned nodes to nodesSet.
  70. m_manually_assigned_nodes = move(nodes_set);
  71. // 5. Run assign slottables for a tree for this's root.
  72. assign_slottables_for_a_tree(root());
  73. }
  74. }