1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 02:47:35 +00:00

LibWeb: Implement manual slottable assignment

This implements manual slottable assignment by way of HTMLSlotElement's
`assign` API. This includes all of the slottable-related AOs needed to
perform the assignment.
This commit is contained in:
Timothy Flynn 2023-09-05 15:05:46 -04:00 committed by Andreas Kling
parent 7870f10aa8
commit e9da74ebe0
5 changed files with 254 additions and 1 deletions

View file

@ -6,6 +6,8 @@
*/
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/DOM/Element.h>
#include <LibWeb/DOM/Text.h>
#include <LibWeb/HTML/HTMLSlotElement.h>
namespace Web::HTML {
@ -27,6 +29,64 @@ void HTMLSlotElement::visit_edges(JS::Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
Slot::visit_edges(visitor);
for (auto const& node : m_manually_assigned_nodes)
node.visit([&](auto const& slottable) { visitor.visit(slottable); });
}
// https://html.spec.whatwg.org/multipage/scripting.html#dom-slot-assignednodes
Vector<JS::Handle<DOM::Node>> HTMLSlotElement::assigned_nodes(AssignedNodesOptions)
{
// FIXME: 1. If options["flatten"] is false, then return this's 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)
{
// FIXME: 1. If options["flatten"] is false, then return this's assigned nodes, filtered to contain only Element nodes.
// FIXME: 2. Return the result of finding flattened slottables with this, filtered to contain only Element nodes.
return {};
}
// https://html.spec.whatwg.org/multipage/scripting.html#dom-slot-assign
void HTMLSlotElement::assign(Vector<SlottableHandle> nodes)
{
// 1. For each node of this's manually assigned nodes, set node's manual slot assignment to null.
for (auto& node : m_manually_assigned_nodes) {
node.visit([&](auto& node) {
node->set_manual_slot_assignment(nullptr);
});
}
// 2. Let nodesSet be a new ordered set.
Vector<DOM::Slottable> nodes_set;
// 3. For each node of nodes:
for (auto& node_handle : nodes) {
auto& node = node_handle.visit([](auto& node) -> DOM::SlottableMixin& { return *node; });
auto slottable = node_handle.visit([](auto& node) { return node->as_slottable(); });
// 1. If node's manual slot assignment refers to a slot, then remove node from that slot's manually assigned nodes.
if (node.manual_slot_assignment() != nullptr) {
m_manually_assigned_nodes.remove_all_matching([&](auto const& manually_assigned_node) {
return slottable == manually_assigned_node;
});
}
// 2. Set node's manual slot assignment to this.
node.set_manual_slot_assignment(this);
// 3. Append node to nodesSet.
nodes_set.append(slottable);
}
// 4. Set this's manually assigned nodes to nodesSet.
m_manually_assigned_nodes = move(nodes_set);
// 5. Run assign slottables for a tree for this's root.
assign_slottables_for_a_tree(root());
}
}

View file

@ -7,11 +7,19 @@
#pragma once
#include <AK/Variant.h>
#include <AK/Vector.h>
#include <LibJS/Heap/Handle.h>
#include <LibWeb/DOM/Slot.h>
#include <LibWeb/DOM/Slottable.h>
#include <LibWeb/HTML/HTMLElement.h>
namespace Web::HTML {
struct AssignedNodesOptions {
bool flatten { false };
};
class HTMLSlotElement final
: public HTMLElement
, public DOM::Slot {
@ -20,11 +28,22 @@ class HTMLSlotElement final
public:
virtual ~HTMLSlotElement() override;
Vector<JS::Handle<DOM::Node>> assigned_nodes(AssignedNodesOptions options = {});
Vector<JS::Handle<DOM::Element>> assigned_elements(AssignedNodesOptions options = {});
using SlottableHandle = Variant<JS::Handle<DOM::Element>, JS::Handle<DOM::Text>>;
void assign(Vector<SlottableHandle> nodes);
ReadonlySpan<DOM::Slottable> manually_assigned_nodes() const { return m_manually_assigned_nodes; }
private:
HTMLSlotElement(DOM::Document&, DOM::QualifiedName);
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(JS::Cell::Visitor&) override;
// https://html.spec.whatwg.org/multipage/scripting.html#manually-assigned-nodes
Vector<DOM::Slottable> m_manually_assigned_nodes;
};
}

View file

@ -1,3 +1,6 @@
#import <DOM/Element.idl>
#import <DOM/Node.idl>
#import <DOM/Text.idl>
#import <HTML/HTMLElement.idl>
// https://html.spec.whatwg.org/multipage/scripting.html#htmlslotelement
@ -7,5 +10,11 @@ interface HTMLSlotElement : HTMLElement {
[HTMLConstructor] constructor();
[CEReactions, Reflect] attribute DOMString name;
sequence<Node> assignedNodes(optional AssignedNodesOptions options = {});
sequence<Element> assignedElements(optional AssignedNodesOptions options = {});
undefined assign((Element or Text)... nodes);
};
dictionary AssignedNodesOptions {
boolean flatten = false;
};