Procházet zdrojové kódy

LibWeb: Implement (most of) the assigned slot/slottable lookup APIs

This implements looking up a slottable's assigned slot, and a slot's
list of assigned slottables. For the latter, only unflattened lookups
are implemented so far.
Timothy Flynn před 1 rokem
rodič
revize
683ce8e615

+ 5 - 2
Userland/Libraries/LibWeb/DOM/Slottable.cpp

@@ -25,8 +25,11 @@ void SlottableMixin::visit_edges(JS::Cell::Visitor& visitor)
 // https://dom.spec.whatwg.org/#dom-slotable-assignedslot
 JS::GCPtr<HTML::HTMLSlotElement> SlottableMixin::assigned_slot()
 {
-    // FIXME: The assignedSlot getter steps are to return the result of find a slot given this and with the open flag set.
-    return nullptr;
+    auto* node = dynamic_cast<DOM::Node*>(this);
+    VERIFY(node);
+
+    // The assignedSlot getter steps are to return the result of find a slot given this and with the open flag set.
+    return find_a_slot(node->as_slottable(), OpenFlag::Set);
 }
 
 JS::GCPtr<HTML::HTMLSlotElement> assigned_slot_for_node(JS::NonnullGCPtr<Node> node)

+ 28 - 4
Userland/Libraries/LibWeb/HTML/HTMLSlotElement.cpp

@@ -62,17 +62,41 @@ void HTMLSlotElement::visit_edges(JS::Cell::Visitor& visitor)
 }
 
 // https://html.spec.whatwg.org/multipage/scripting.html#dom-slot-assignednodes
-Vector<JS::Handle<DOM::Node>> HTMLSlotElement::assigned_nodes(AssignedNodesOptions)
+Vector<JS::Handle<DOM::Node>> HTMLSlotElement::assigned_nodes(AssignedNodesOptions options)
 {
-    // FIXME: 1. If options["flatten"] is false, then return this's assigned nodes.
+    // 1. If options["flatten"] is false, then return this's assigned nodes.
+    if (!options.flatten) {
+        Vector<JS::Handle<DOM::Node>> assigned_nodes;
+        assigned_nodes.ensure_capacity(assigned_nodes_internal().size());
+
+        for (auto const& node : assigned_nodes_internal()) {
+            node.visit([&](auto const& node) {
+                assigned_nodes.unchecked_append(*node);
+            });
+        }
+
+        return assigned_nodes;
+    }
+
     // FIXME: 2. Return the result of finding flattened slottables with this.
     return {};
 }
 
 // https://html.spec.whatwg.org/multipage/scripting.html#dom-slot-assignedelements
-Vector<JS::Handle<DOM::Element>> HTMLSlotElement::assigned_elements(AssignedNodesOptions)
+Vector<JS::Handle<DOM::Element>> HTMLSlotElement::assigned_elements(AssignedNodesOptions options)
 {
-    // FIXME: 1. If options["flatten"] is false, then return this's assigned nodes, filtered to contain only Element nodes.
+    // 1. If options["flatten"] is false, then return this's assigned nodes, filtered to contain only Element nodes.
+    if (!options.flatten) {
+        Vector<JS::Handle<DOM::Element>> assigned_nodes;
+
+        for (auto const& node : assigned_nodes_internal()) {
+            if (auto const* element = node.get_pointer<JS::NonnullGCPtr<DOM::Element>>())
+                assigned_nodes.append(*element);
+        }
+
+        return assigned_nodes;
+    }
+
     // FIXME: 2. Return the result of finding flattened slottables with this, filtered to contain only Element nodes.
     return {};
 }