Coverage Report

Created: 2026-05-16 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/Userland/Libraries/LibWeb/DOM/ShadowRoot.h
Line
Count
Source
1
/*
2
 * Copyright (c) 2020, the SerenityOS developers.
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#pragma once
8
9
#include <LibWeb/Bindings/ShadowRootPrototype.h>
10
#include <LibWeb/DOM/DocumentFragment.h>
11
#include <LibWeb/WebIDL/ObservableArray.h>
12
13
namespace Web::DOM {
14
15
class ShadowRoot final : public DocumentFragment {
16
    WEB_PLATFORM_OBJECT(ShadowRoot, DocumentFragment);
17
    JS_DECLARE_ALLOCATOR(ShadowRoot);
18
19
public:
20
0
    Bindings::ShadowRootMode mode() const { return m_mode; }
21
22
0
    Bindings::SlotAssignmentMode slot_assignment() const { return m_slot_assignment; }
23
0
    void set_slot_assignment(Bindings::SlotAssignmentMode slot_assignment) { m_slot_assignment = slot_assignment; }
24
25
0
    bool delegates_focus() const { return m_delegates_focus; }
26
0
    void set_delegates_focus(bool delegates_focus) { m_delegates_focus = delegates_focus; }
27
28
0
    [[nodiscard]] bool declarative() const { return m_declarative; }
29
0
    void set_declarative(bool declarative) { m_declarative = declarative; }
30
31
0
    [[nodiscard]] bool clonable() const { return m_clonable; }
32
0
    void set_clonable(bool clonable) { m_clonable = clonable; }
33
34
0
    [[nodiscard]] bool serializable() const { return m_serializable; }
35
0
    void set_serializable(bool serializable) { m_serializable = serializable; }
36
37
    void set_onslotchange(WebIDL::CallbackType*);
38
    WebIDL::CallbackType* onslotchange();
39
40
0
    bool available_to_element_internals() const { return m_available_to_element_internals; }
41
0
    void set_available_to_element_internals(bool available_to_element_internals) { m_available_to_element_internals = available_to_element_internals; }
42
43
    // ^EventTarget
44
    virtual EventTarget* get_parent(Event const&) override;
45
46
    WebIDL::ExceptionOr<String> inner_html() const;
47
    WebIDL::ExceptionOr<void> set_inner_html(StringView);
48
49
    WebIDL::ExceptionOr<void> set_html_unsafe(StringView);
50
51
    WebIDL::ExceptionOr<String> get_html(GetHTMLOptions const&) const;
52
53
    CSS::StyleSheetList& style_sheets();
54
    CSS::StyleSheetList const& style_sheets() const;
55
56
0
    CSS::StyleSheetList* style_sheets_for_bindings() { return &style_sheets(); }
57
58
    JS::NonnullGCPtr<WebIDL::ObservableArray> adopted_style_sheets() const;
59
    WebIDL::ExceptionOr<void> set_adopted_style_sheets(JS::Value);
60
61
    void for_each_css_style_sheet(Function<void(CSS::CSSStyleSheet&)>&& callback) const;
62
63
    Vector<JS::NonnullGCPtr<Animations::Animation>> get_animations();
64
65
    virtual void finalize() override;
66
67
protected:
68
    virtual void visit_edges(Cell::Visitor&) override;
69
70
private:
71
    ShadowRoot(Document&, Element& host, Bindings::ShadowRootMode);
72
    virtual void initialize(JS::Realm&) override;
73
74
    // ^Node
75
0
    virtual FlyString node_name() const override { return "#shadow-root"_fly_string; }
76
0
    virtual bool is_shadow_root() const final { return true; }
77
78
    // NOTE: The specification doesn't seem to specify a default value for mode. Assuming closed for now.
79
    Bindings::ShadowRootMode m_mode { Bindings::ShadowRootMode::Closed };
80
    Bindings::SlotAssignmentMode m_slot_assignment { Bindings::SlotAssignmentMode::Named };
81
    bool m_delegates_focus { false };
82
    bool m_available_to_element_internals { false };
83
84
    // https://dom.spec.whatwg.org/#shadowroot-declarative
85
    bool m_declarative { false };
86
87
    // https://dom.spec.whatwg.org/#shadowroot-clonable
88
    bool m_clonable { false };
89
90
    // https://dom.spec.whatwg.org/#shadowroot-serializable
91
    bool m_serializable { false };
92
93
    JS::GCPtr<CSS::StyleSheetList> m_style_sheets;
94
    mutable JS::GCPtr<WebIDL::ObservableArray> m_adopted_style_sheets;
95
};
96
97
template<>
98
0
inline bool Node::fast_is<ShadowRoot>() const { return node_type() == to_underlying(NodeType::DOCUMENT_FRAGMENT_NODE) && is_shadow_root(); }
99
100
// https://dom.spec.whatwg.org/#concept-shadow-including-tree-order
101
// In shadow-including tree order is shadow-including preorder, depth-first traversal of a node tree.
102
// Shadow-including preorder, depth-first traversal of a node tree tree is preorder, depth-first traversal
103
// of tree, with for each shadow host encountered in tree, shadow-including preorder, depth-first traversal
104
// of that element’s shadow root’s node tree just after it is encountered.
105
106
// https://dom.spec.whatwg.org/#concept-shadow-including-descendant
107
// An object A is a shadow-including descendant of an object B, if A is a descendant of B, or A’s root is a
108
// shadow root and A’s root’s host is a shadow-including inclusive descendant of B.
109
110
// https://dom.spec.whatwg.org/#concept-shadow-including-inclusive-descendant
111
// A shadow-including inclusive descendant is an object or one of its shadow-including descendants.
112
113
template<typename Callback>
114
inline TraversalDecision Node::for_each_shadow_including_inclusive_descendant(Callback callback)
115
0
{
116
0
    if (callback(*this) == TraversalDecision::Break)
117
0
        return TraversalDecision::Break;
118
119
0
    if (is_element()) {
120
0
        if (auto shadow_root = static_cast<Element*>(this)->shadow_root()) {
121
0
            if (shadow_root->for_each_shadow_including_inclusive_descendant(callback) == TraversalDecision::Break)
122
0
                return TraversalDecision::Break;
123
0
        }
124
0
    }
125
126
0
    for (auto* child = first_child(); child; child = child->next_sibling()) {
127
0
        if (child->for_each_shadow_including_inclusive_descendant(callback) == TraversalDecision::Break)
128
0
            return TraversalDecision::Break;
129
0
    }
130
131
0
    return TraversalDecision::Continue;
132
0
}
Unexecuted instantiation: Document.cpp:Web::TraversalDecision Web::DOM::Node::for_each_shadow_including_inclusive_descendant<Web::DOM::Document::adopt_node(Web::DOM::Node&)::$_0>(Web::DOM::Document::adopt_node(Web::DOM::Node&)::$_0)
Unexecuted instantiation: Document.cpp:Web::TraversalDecision Web::DOM::Node::for_each_shadow_including_inclusive_descendant<Web::DOM::Document::adopt_node(Web::DOM::Node&)::$_1>(Web::DOM::Document::adopt_node(Web::DOM::Node&)::$_1)
Unexecuted instantiation: Document.cpp:Web::TraversalDecision Web::DOM::Node::for_each_shadow_including_inclusive_descendant<Web::DOM::Document::adopt_node(Web::DOM::Node&)::$_2>(Web::DOM::Document::adopt_node(Web::DOM::Node&)::$_2)
Unexecuted instantiation: Document.cpp:Web::TraversalDecision Web::DOM::Node::for_each_shadow_including_inclusive_descendant<Web::DOM::Document::descendant_navigables()::$_0>(Web::DOM::Document::descendant_navigables()::$_0)
Unexecuted instantiation: Node.cpp:Web::TraversalDecision Web::DOM::Node::for_each_shadow_including_inclusive_descendant<Web::DOM::Node::insert_before(JS::NonnullGCPtr<Web::DOM::Node>, JS::GCPtr<Web::DOM::Node>, bool)::$_0>(Web::DOM::Node::insert_before(JS::NonnullGCPtr<Web::DOM::Node>, JS::GCPtr<Web::DOM::Node>, bool)::$_0)
Unexecuted instantiation: Node.cpp:Web::TraversalDecision Web::DOM::Node::for_each_shadow_including_inclusive_descendant<Web::DOM::Node::remove(bool)::$_2>(Web::DOM::Node::remove(bool)::$_2)
Unexecuted instantiation: CustomElementRegistry.cpp:Web::TraversalDecision Web::DOM::Node::for_each_shadow_including_inclusive_descendant<Web::HTML::CustomElementRegistry::define(AK::String const&, Web::WebIDL::CallbackType*, Web::HTML::ElementDefinitionOptions)::$_3>(Web::HTML::CustomElementRegistry::define(AK::String const&, Web::WebIDL::CallbackType*, Web::HTML::ElementDefinitionOptions)::$_3)
Unexecuted instantiation: CustomElementRegistry.cpp:Web::TraversalDecision Web::DOM::Node::for_each_shadow_including_inclusive_descendant<Web::HTML::CustomElementRegistry::upgrade(JS::NonnullGCPtr<Web::DOM::Node>) const::$_0>(Web::HTML::CustomElementRegistry::upgrade(JS::NonnullGCPtr<Web::DOM::Node>) const::$_0)
Unexecuted instantiation: LayoutState.cpp:Web::TraversalDecision Web::DOM::Node::for_each_shadow_including_inclusive_descendant<Web::Layout::LayoutState::commit(Web::Layout::Box&)::$_2>(Web::Layout::LayoutState::commit(Web::Layout::Box&)::$_2)
133
134
template<typename Callback>
135
inline TraversalDecision Node::for_each_shadow_including_descendant(Callback callback)
136
0
{
137
0
    for (auto* child = first_child(); child; child = child->next_sibling()) {
138
0
        if (child->for_each_shadow_including_inclusive_descendant(callback) == TraversalDecision::Break)
139
0
            return TraversalDecision::Break;
140
0
    }
141
0
    return TraversalDecision::Continue;
142
0
}
Unexecuted instantiation: Document.cpp:Web::TraversalDecision Web::DOM::Node::for_each_shadow_including_descendant<Web::DOM::Document::descendant_navigables()::$_0>(Web::DOM::Document::descendant_navigables()::$_0)
Unexecuted instantiation: Node.cpp:Web::TraversalDecision Web::DOM::Node::for_each_shadow_including_descendant<Web::DOM::Node::remove(bool)::$_2>(Web::DOM::Node::remove(bool)::$_2)
Unexecuted instantiation: CustomElementRegistry.cpp:Web::TraversalDecision Web::DOM::Node::for_each_shadow_including_descendant<Web::HTML::CustomElementRegistry::define(AK::String const&, Web::WebIDL::CallbackType*, Web::HTML::ElementDefinitionOptions)::$_3>(Web::HTML::CustomElementRegistry::define(AK::String const&, Web::WebIDL::CallbackType*, Web::HTML::ElementDefinitionOptions)::$_3)
143
144
}