2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
30 #include "DocumentFragment.h"
32 #include "ShadowRootMode.h"
36 class HTMLSlotElement;
39 class ShadowRoot final : public DocumentFragment, public TreeScope {
41 static Ref<ShadowRoot> create(Document& document, ShadowRootMode type)
43 return adoptRef(*new ShadowRoot(document, type));
46 static Ref<ShadowRoot> create(Document& document, std::unique_ptr<SlotAssignment>&& assignment)
48 return adoptRef(*new ShadowRoot(document, WTFMove(assignment)));
51 virtual ~ShadowRoot();
53 using TreeScope::rootNode;
55 Style::Scope& styleScope();
57 bool resetStyleInheritance() const { return m_resetStyleInheritance; }
58 void setResetStyleInheritance(bool);
60 Element* host() const { return m_host; }
61 void setHost(Element* host) { m_host = host; }
63 String innerHTML() const;
64 ExceptionOr<void> setInnerHTML(const String&);
66 Element* activeElement() const;
68 ShadowRootMode mode() const { return m_type; }
70 void removeAllEventListeners() override;
72 HTMLSlotElement* findAssignedSlot(const Node&);
74 void addSlotElementByName(const AtomicString&, HTMLSlotElement&);
75 void removeSlotElementByName(const AtomicString&, HTMLSlotElement&);
77 void didRemoveAllChildrenOfShadowHost();
78 void didChangeDefaultSlot();
79 void hostChildElementDidChange(const Element&);
80 void hostChildElementDidChangeSlotAttribute(Element&, const AtomicString& oldValue, const AtomicString& newValue);
82 const Vector<Node*>* assignedNodesForSlot(const HTMLSlotElement&);
84 void moveShadowRootToNewParentScope(TreeScope&, Document&);
85 void moveShadowRootToNewDocument(Document&);
88 ShadowRoot(Document&, ShadowRootMode);
90 ShadowRoot(Document&, std::unique_ptr<SlotAssignment>&&);
92 // FIXME: This shouldn't happen. https://bugs.webkit.org/show_bug.cgi?id=88834
93 bool isOrphan() const { return !m_host; }
96 bool childTypeAllowed(NodeType) const override;
98 Ref<Node> cloneNodeInternal(Document&, CloningOperation) override;
100 Node::InsertedIntoAncestorResult insertedIntoAncestor(InsertionType, ContainerNode&) override;
101 void removedFromAncestor(RemovalType, ContainerNode& insertionPoint) override;
103 bool m_resetStyleInheritance { false };
104 ShadowRootMode m_type { ShadowRootMode::UserAgent };
106 Element* m_host { nullptr };
108 std::unique_ptr<Style::Scope> m_styleScope;
110 std::unique_ptr<SlotAssignment> m_slotAssignment;
113 inline Element* ShadowRoot::activeElement() const
115 return treeScope().focusedElementInScope();
118 inline ShadowRoot* Node::shadowRoot() const
120 if (!is<Element>(*this))
122 return downcast<Element>(*this).shadowRoot();
125 inline ContainerNode* Node::parentOrShadowHostNode() const
127 ASSERT(isMainThreadOrGCThread());
128 if (is<ShadowRoot>(*this))
129 return downcast<ShadowRoot>(*this).host();
133 inline bool hasShadowRootParent(const Node& node)
135 return node.parentNode() && node.parentNode()->isShadowRoot();
138 Vector<ShadowRoot*> assignedShadowRootsIfSlotted(const Node&);
140 } // namespace WebCore
142 SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::ShadowRoot)
143 static bool isType(const WebCore::Node& node) { return node.isShadowRoot(); }
144 SPECIALIZE_TYPE_TRAITS_END()