1/*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Neither the name of Google Inc. nor the names of its
11 * contributors may be used to endorse or promote products derived from
12 * this software without specific prior written permission.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include "Document.h"
30#include "DocumentFragment.h"
31#include "Element.h"
32#include "ShadowRootMode.h"
33
34namespace WebCore {
35
36class HTMLSlotElement;
37class SlotAssignment;
38class StyleSheetList;
39
40class ShadowRoot final : public DocumentFragment, public TreeScope {
41 WTF_MAKE_ISO_ALLOCATED(ShadowRoot);
42public:
43 static Ref<ShadowRoot> create(Document& document, ShadowRootMode type)
44 {
45 return adoptRef(*new ShadowRoot(document, type));
46 }
47
48 static Ref<ShadowRoot> create(Document& document, std::unique_ptr<SlotAssignment>&& assignment)
49 {
50 return adoptRef(*new ShadowRoot(document, WTFMove(assignment)));
51 }
52
53 virtual ~ShadowRoot();
54
55 using TreeScope::rootNode;
56
57 Style::Scope& styleScope();
58 StyleSheetList& styleSheets();
59
60 bool resetStyleInheritance() const { return m_resetStyleInheritance; }
61 void setResetStyleInheritance(bool);
62
63 Element* host() const { return m_host; }
64 void setHost(Element* host) { m_host = host; }
65
66 String innerHTML() const;
67 ExceptionOr<void> setInnerHTML(const String&);
68
69 Element* activeElement() const;
70
71 ShadowRootMode mode() const { return m_type; }
72 bool shouldFireSlotchangeEvent() const { return m_type != ShadowRootMode::UserAgent && !m_hasBegunDeletingDetachedChildren; }
73
74 void removeAllEventListeners() override;
75
76 HTMLSlotElement* findAssignedSlot(const Node&);
77
78 void renameSlotElement(HTMLSlotElement&, const AtomString& oldName, const AtomString& newName);
79 void addSlotElementByName(const AtomString&, HTMLSlotElement&);
80 void removeSlotElementByName(const AtomString&, HTMLSlotElement&, ContainerNode& oldParentOfRemovedTree);
81 void slotFallbackDidChange(HTMLSlotElement&);
82 void resolveSlotsBeforeNodeInsertionOrRemoval();
83 void willRemoveAllChildren(ContainerNode&);
84
85 void didRemoveAllChildrenOfShadowHost();
86 void didChangeDefaultSlot();
87 void hostChildElementDidChange(const Element&);
88 void hostChildElementDidChangeSlotAttribute(Element&, const AtomString& oldValue, const AtomString& newValue);
89
90 const Vector<Node*>* assignedNodesForSlot(const HTMLSlotElement&);
91
92 void moveShadowRootToNewParentScope(TreeScope&, Document&);
93 void moveShadowRootToNewDocument(Document&);
94
95protected:
96 ShadowRoot(Document&, ShadowRootMode);
97
98 ShadowRoot(Document&, std::unique_ptr<SlotAssignment>&&);
99
100private:
101 bool childTypeAllowed(NodeType) const override;
102
103 Ref<Node> cloneNodeInternal(Document&, CloningOperation) override;
104
105 Node::InsertedIntoAncestorResult insertedIntoAncestor(InsertionType, ContainerNode&) override;
106 void removedFromAncestor(RemovalType, ContainerNode& insertionPoint) override;
107
108 void childrenChanged(const ChildChange&) override;
109
110 bool m_resetStyleInheritance { false };
111 bool m_hasBegunDeletingDetachedChildren { false };
112 ShadowRootMode m_type { ShadowRootMode::UserAgent };
113
114 Element* m_host { nullptr };
115 RefPtr<StyleSheetList> m_styleSheetList;
116
117 std::unique_ptr<Style::Scope> m_styleScope;
118 std::unique_ptr<SlotAssignment> m_slotAssignment;
119};
120
121inline Element* ShadowRoot::activeElement() const
122{
123 return treeScope().focusedElementInScope();
124}
125
126inline ShadowRoot* Node::shadowRoot() const
127{
128 if (!is<Element>(*this))
129 return nullptr;
130 return downcast<Element>(*this).shadowRoot();
131}
132
133inline ContainerNode* Node::parentOrShadowHostNode() const
134{
135 ASSERT(isMainThreadOrGCThread());
136 if (is<ShadowRoot>(*this))
137 return downcast<ShadowRoot>(*this).host();
138 return parentNode();
139}
140
141inline bool hasShadowRootParent(const Node& node)
142{
143 return node.parentNode() && node.parentNode()->isShadowRoot();
144}
145
146Vector<ShadowRoot*> assignedShadowRootsIfSlotted(const Node&);
147
148} // namespace WebCore
149
150SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::ShadowRoot)
151 static bool isType(const WebCore::Node& node) { return node.isShadowRoot(); }
152SPECIALIZE_TYPE_TRAITS_END()
153