Coverage Report

Created: 2026-07-25 07:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/Userland/Libraries/LibWeb/DOM/Slottable.cpp
Line
Count
Source
1
/*
2
 * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#include <LibWeb/Bindings/MainThreadVM.h>
8
#include <LibWeb/DOM/Element.h>
9
#include <LibWeb/DOM/Node.h>
10
#include <LibWeb/DOM/ShadowRoot.h>
11
#include <LibWeb/DOM/Slottable.h>
12
#include <LibWeb/DOM/Text.h>
13
#include <LibWeb/HTML/HTMLSlotElement.h>
14
15
namespace Web::DOM {
16
17
0
SlottableMixin::~SlottableMixin() = default;
18
19
void SlottableMixin::visit_edges(JS::Cell::Visitor& visitor)
20
0
{
21
0
    visitor.visit(m_assigned_slot);
22
0
    visitor.visit(m_manual_slot_assignment);
23
0
}
24
25
// https://dom.spec.whatwg.org/#dom-slotable-assignedslot
26
JS::GCPtr<HTML::HTMLSlotElement> SlottableMixin::assigned_slot()
27
0
{
28
0
    auto* node = dynamic_cast<DOM::Node*>(this);
29
0
    VERIFY(node);
30
31
    // The assignedSlot getter steps are to return the result of find a slot given this and with the open flag set.
32
0
    return find_a_slot(node->as_slottable(), OpenFlag::Set);
33
0
}
34
35
JS::GCPtr<HTML::HTMLSlotElement> assigned_slot_for_node(JS::NonnullGCPtr<Node> node)
36
0
{
37
0
    if (!node->is_slottable())
38
0
        return nullptr;
39
40
0
    return node->as_slottable().visit([](auto const& slottable) {
41
0
        return slottable->assigned_slot_internal();
42
0
    });
Unexecuted instantiation: Slottable.cpp:auto Web::DOM::assigned_slot_for_node(JS::NonnullGCPtr<Web::DOM::Node>)::$_0::operator()<JS::NonnullGCPtr<Web::DOM::Element> >(JS::NonnullGCPtr<Web::DOM::Element> const&) const
Unexecuted instantiation: Slottable.cpp:auto Web::DOM::assigned_slot_for_node(JS::NonnullGCPtr<Web::DOM::Node>)::$_0::operator()<JS::NonnullGCPtr<Web::DOM::Text> >(JS::NonnullGCPtr<Web::DOM::Text> const&) const
43
0
}
44
45
// https://dom.spec.whatwg.org/#slotable-assigned
46
bool is_an_assigned_slottable(JS::NonnullGCPtr<Node> node)
47
0
{
48
0
    if (!node->is_slottable())
49
0
        return false;
50
51
    // A slottable is assigned if its assigned slot is non-null.
52
0
    return assigned_slot_for_node(node) != nullptr;
53
0
}
54
55
// https://dom.spec.whatwg.org/#find-a-slot
56
JS::GCPtr<HTML::HTMLSlotElement> find_a_slot(Slottable const& slottable, OpenFlag open_flag)
57
0
{
58
    // 1. If slottable’s parent is null, then return null.
59
0
    auto* parent = slottable.visit([](auto& node) { return node->parent_element(); });
Unexecuted instantiation: Slottable.cpp:auto Web::DOM::find_a_slot(AK::Variant<JS::NonnullGCPtr<Web::DOM::Element>, JS::NonnullGCPtr<Web::DOM::Text> > const&, Web::DOM::OpenFlag)::$_0::operator()<JS::NonnullGCPtr<Web::DOM::Element> const>(JS::NonnullGCPtr<Web::DOM::Element> const&) const
Unexecuted instantiation: Slottable.cpp:auto Web::DOM::find_a_slot(AK::Variant<JS::NonnullGCPtr<Web::DOM::Element>, JS::NonnullGCPtr<Web::DOM::Text> > const&, Web::DOM::OpenFlag)::$_0::operator()<JS::NonnullGCPtr<Web::DOM::Text> const>(JS::NonnullGCPtr<Web::DOM::Text> const&) const
60
0
    if (!parent)
61
0
        return nullptr;
62
63
    // 2. Let shadow be slottable’s parent’s shadow root.
64
0
    auto shadow = parent->shadow_root();
65
66
    // 3. If shadow is null, then return null.
67
0
    if (shadow == nullptr)
68
0
        return nullptr;
69
70
    // 4. If the open flag is set and shadow’s mode is not "open", then return null.
71
0
    if (open_flag == OpenFlag::Set && shadow->mode() != Bindings::ShadowRootMode::Open)
72
0
        return nullptr;
73
74
    // 5. If shadow’s slot assignment is "manual", then return the slot in shadow’s descendants whose manually assigned
75
    //    nodes contains slottable, if any; otherwise null.
76
0
    if (shadow->slot_assignment() == Bindings::SlotAssignmentMode::Manual) {
77
0
        JS::GCPtr<HTML::HTMLSlotElement> slot;
78
79
0
        shadow->for_each_in_subtree_of_type<HTML::HTMLSlotElement>([&](auto& child) {
80
0
            if (!child.manually_assigned_nodes().contains_slow(slottable))
81
0
                return TraversalDecision::Continue;
82
83
0
            slot = child;
84
0
            return TraversalDecision::Break;
85
0
        });
86
87
0
        return slot;
88
0
    }
89
90
    // 6. Return the first slot in tree order in shadow’s descendants whose name is slottable’s name, if any; otherwise null.
91
0
    auto const& slottable_name = slottable.visit([](auto const& node) { return node->slottable_name(); });
Unexecuted instantiation: Slottable.cpp:auto Web::DOM::find_a_slot(AK::Variant<JS::NonnullGCPtr<Web::DOM::Element>, JS::NonnullGCPtr<Web::DOM::Text> > const&, Web::DOM::OpenFlag)::$_1::operator()<JS::NonnullGCPtr<Web::DOM::Element> >(JS::NonnullGCPtr<Web::DOM::Element> const&) const
Unexecuted instantiation: Slottable.cpp:auto Web::DOM::find_a_slot(AK::Variant<JS::NonnullGCPtr<Web::DOM::Element>, JS::NonnullGCPtr<Web::DOM::Text> > const&, Web::DOM::OpenFlag)::$_1::operator()<JS::NonnullGCPtr<Web::DOM::Text> >(JS::NonnullGCPtr<Web::DOM::Text> const&) const
92
0
    JS::GCPtr<HTML::HTMLSlotElement> slot;
93
94
0
    shadow->for_each_in_subtree_of_type<HTML::HTMLSlotElement>([&](auto& child) {
95
0
        if (child.slot_name() != slottable_name)
96
0
            return TraversalDecision::Continue;
97
98
0
        slot = child;
99
0
        return TraversalDecision::Break;
100
0
    });
101
102
0
    return slot;
103
0
}
104
105
// https://dom.spec.whatwg.org/#find-slotables
106
Vector<Slottable> find_slottables(JS::NonnullGCPtr<HTML::HTMLSlotElement> slot)
107
0
{
108
    // 1. Let result be an empty list.
109
0
    Vector<Slottable> result;
110
111
    // 2. Let root be slot’s root.
112
0
    auto& root = slot->root();
113
114
    // 3. If root is not a shadow root, then return result.
115
0
    if (!root.is_shadow_root())
116
0
        return result;
117
118
    // 4. Let host be root’s host.
119
0
    auto& shadow_root = static_cast<ShadowRoot&>(root);
120
0
    auto* host = shadow_root.host();
121
122
    // 5. If root’s slot assignment is "manual", then:
123
0
    if (shadow_root.slot_assignment() == Bindings::SlotAssignmentMode::Manual) {
124
        // 1. Let result be « ».
125
        // 2. For each slottable slottable of slot’s manually assigned nodes, if slottable’s parent is host, append slottable to result.
126
0
        for (auto const& slottable : slot->manually_assigned_nodes()) {
127
0
            auto const* parent = slottable.visit([](auto const& node) { return node->parent(); });
Unexecuted instantiation: Slottable.cpp:auto Web::DOM::find_slottables(JS::NonnullGCPtr<Web::HTML::HTMLSlotElement>)::$_0::operator()<JS::NonnullGCPtr<Web::DOM::Element> >(JS::NonnullGCPtr<Web::DOM::Element> const&) const
Unexecuted instantiation: Slottable.cpp:auto Web::DOM::find_slottables(JS::NonnullGCPtr<Web::HTML::HTMLSlotElement>)::$_0::operator()<JS::NonnullGCPtr<Web::DOM::Text> >(JS::NonnullGCPtr<Web::DOM::Text> const&) const
128
129
0
            if (parent == host)
130
0
                result.append(slottable);
131
0
        }
132
0
    }
133
    // 6. Otherwise, for each slottable child slottable of host, in tree order:
134
0
    else {
135
0
        host->for_each_child([&](auto& node) {
136
0
            if (!node.is_slottable())
137
0
                return IterationDecision::Continue;
138
139
0
            auto slottable = node.as_slottable();
140
141
            // 1. Let foundSlot be the result of finding a slot given slottable.
142
0
            auto found_slot = find_a_slot(slottable);
143
144
            // 2. If foundSlot is slot, then append slottable to result.
145
0
            if (found_slot == slot)
146
0
                result.append(move(slottable));
147
148
0
            return IterationDecision::Continue;
149
0
        });
150
0
    }
151
152
    // 7. Return result.
153
0
    return result;
154
0
}
155
156
// https://dom.spec.whatwg.org/#assign-slotables
157
void assign_slottables(JS::NonnullGCPtr<HTML::HTMLSlotElement> slot)
158
0
{
159
    // 1. Let slottables be the result of finding slottables for slot.
160
0
    auto slottables = find_slottables(slot);
161
162
    // 2. If slottables and slot’s assigned nodes are not identical, then run signal a slot change for slot.
163
0
    if (slottables != slot->assigned_nodes_internal())
164
0
        signal_a_slot_change(slot);
165
166
    // 4. For each slottable in slottables, set slottable’s assigned slot to slot.
167
0
    for (auto& slottable : slottables) {
168
0
        slottable.visit([&](auto& node) {
169
0
            node->set_assigned_slot(slot);
170
0
        });
Unexecuted instantiation: Slottable.cpp:auto Web::DOM::assign_slottables(JS::NonnullGCPtr<Web::HTML::HTMLSlotElement>)::$_0::operator()<JS::NonnullGCPtr<Web::DOM::Element> >(JS::NonnullGCPtr<Web::DOM::Element>&) const
Unexecuted instantiation: Slottable.cpp:auto Web::DOM::assign_slottables(JS::NonnullGCPtr<Web::HTML::HTMLSlotElement>)::$_0::operator()<JS::NonnullGCPtr<Web::DOM::Text> >(JS::NonnullGCPtr<Web::DOM::Text>&) const
171
0
    }
172
173
    // 3. Set slot’s assigned nodes to slottables.
174
    // NOTE: We do this step last so that we can move the slottables list.
175
0
    slot->set_assigned_nodes(move(slottables));
176
0
}
177
178
// https://dom.spec.whatwg.org/#assign-slotables-for-a-tree
179
void assign_slottables_for_a_tree(JS::NonnullGCPtr<Node> root)
180
0
{
181
    // AD-HOC: This method iterates over the root's entire subtree. That iteration does nothing if the root is not a
182
    //         shadow root (see `find_slottables`). This iteration can be very expensive as the HTML parser inserts
183
    //         nodes, especially on sites with many elements. So we skip it if we know it's going to be a no-op anyways.
184
0
    if (!root->is_shadow_root())
185
0
        return;
186
187
    // To assign slottables for a tree, given a node root, run assign slottables for each slot slot in root’s inclusive
188
    // descendants, in tree order.
189
0
    root->for_each_in_inclusive_subtree_of_type<HTML::HTMLSlotElement>([](auto& slot) {
190
0
        assign_slottables(slot);
191
0
        return TraversalDecision::Continue;
192
0
    });
193
0
}
194
195
// https://dom.spec.whatwg.org/#assign-a-slot
196
void assign_a_slot(Slottable const& slottable)
197
0
{
198
    // 1. Let slot be the result of finding a slot with slottable.
199
0
    auto slot = find_a_slot(slottable);
200
201
    // 2. If slot is non-null, then run assign slottables for slot.
202
0
    if (slot != nullptr)
203
0
        assign_slottables(*slot);
204
0
}
205
206
// https://dom.spec.whatwg.org/#signal-a-slot-change
207
void signal_a_slot_change(JS::NonnullGCPtr<HTML::HTMLSlotElement> slottable)
208
0
{
209
    // FIXME: 1. Append slot to slot’s relevant agent’s signal slots.
210
211
    // 2. Queue a mutation observer microtask.
212
0
    Bindings::queue_mutation_observer_microtask(slottable->document());
213
0
}
214
215
}