HTMLSlotElement.cpp 5.2 KB

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