2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * Copyright (C) 2015 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Neither the name of Google Inc. nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "ShadowRoot.h"
31 #include "CSSStyleSheet.h"
32 #include "ElementTraversal.h"
33 #include "HTMLSlotElement.h"
34 #include "RenderElement.h"
35 #include "RuntimeEnabledFeatures.h"
36 #include "SlotAssignment.h"
37 #include "StyleResolver.h"
38 #include "StyleScope.h"
43 struct SameSizeAsShadowRoot : public DocumentFragment, public TreeScope {
44 unsigned countersAndFlags[1];
50 COMPILE_ASSERT(sizeof(ShadowRoot) == sizeof(SameSizeAsShadowRoot), shadowroot_should_stay_small);
52 ShadowRoot::ShadowRoot(Document& document, ShadowRootMode type)
53 : DocumentFragment(document, CreateShadowRoot)
54 , TreeScope(*this, document)
56 , m_styleScope(std::make_unique<Style::Scope>(*this))
61 ShadowRoot::ShadowRoot(Document& document, std::unique_ptr<SlotAssignment>&& slotAssignment)
62 : DocumentFragment(document, CreateShadowRoot)
63 , TreeScope(*this, document)
64 , m_type(ShadowRootMode::UserAgent)
65 , m_styleScope(std::make_unique<Style::Scope>(*this))
66 , m_slotAssignment(WTFMove(slotAssignment))
71 ShadowRoot::~ShadowRoot()
74 document().didRemoveInDocumentShadowRoot(*this);
76 // We cannot let ContainerNode destructor call willBeDeletedFrom()
77 // for this ShadowRoot instance because TreeScope destructor
78 // clears Node::m_treeScope thus ContainerNode is no longer able
79 // to access it Document reference after that.
80 willBeDeletedFrom(document());
82 // We must remove all of our children first before the TreeScope destructor
83 // runs so we don't go through Node::setTreeScopeRecursively for each child with a
84 // destructed tree scope in each descendant.
85 removeDetachedChildren();
88 Node::InsertedIntoAncestorResult ShadowRoot::insertedIntoAncestor(InsertionType insertionType, ContainerNode& parentOfInsertedTree)
90 DocumentFragment::insertedIntoAncestor(insertionType, parentOfInsertedTree);
91 if (insertionType.connectedToDocument)
92 document().didInsertInDocumentShadowRoot(*this);
93 return InsertedIntoAncestorResult::Done;
96 void ShadowRoot::removedFromAncestor(RemovalType removalType, ContainerNode& oldParentOfRemovedTree)
98 DocumentFragment::removedFromAncestor(removalType, oldParentOfRemovedTree);
99 if (removalType.disconnectedFromDocument)
100 document().didRemoveInDocumentShadowRoot(*this);
103 void ShadowRoot::moveShadowRootToNewParentScope(TreeScope& newScope, Document& newDocument)
105 setParentTreeScope(newScope);
106 moveShadowRootToNewDocument(newDocument);
109 void ShadowRoot::moveShadowRootToNewDocument(Document& newDocument)
111 setDocumentScope(newDocument);
112 RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(!parentTreeScope() || &parentTreeScope()->documentScope() == &newDocument);
114 // Style scopes are document specific.
115 m_styleScope = std::make_unique<Style::Scope>(*this);
116 RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(&m_styleScope->document() == &newDocument);
119 Style::Scope& ShadowRoot::styleScope()
121 return *m_styleScope;
124 String ShadowRoot::innerHTML() const
126 return createMarkup(*this, ChildrenOnly);
129 ExceptionOr<void> ShadowRoot::setInnerHTML(const String& markup)
132 return Exception { InvalidAccessError };
133 auto fragment = createFragmentForInnerOuterHTML(*host(), markup, AllowScriptingContent);
134 if (fragment.hasException())
135 return fragment.releaseException();
136 return replaceChildrenWithFragment(*this, fragment.releaseReturnValue());
139 bool ShadowRoot::childTypeAllowed(NodeType type) const
143 case PROCESSING_INSTRUCTION_NODE:
146 case CDATA_SECTION_NODE:
153 void ShadowRoot::setResetStyleInheritance(bool value)
155 // If this was ever changed after initialization, child styles would need to be invalidated here.
156 m_resetStyleInheritance = value;
159 Ref<Node> ShadowRoot::cloneNodeInternal(Document&, CloningOperation)
161 RELEASE_ASSERT_NOT_REACHED();
162 return *static_cast<Node*>(nullptr); // ShadowRoots should never be cloned.
165 void ShadowRoot::removeAllEventListeners()
167 DocumentFragment::removeAllEventListeners();
168 for (Node* node = firstChild(); node; node = NodeTraversal::next(*node))
169 node->removeAllEventListeners();
173 HTMLSlotElement* ShadowRoot::findAssignedSlot(const Node& node)
175 ASSERT(node.parentNode() == host());
176 if (!m_slotAssignment)
178 return m_slotAssignment->findAssignedSlot(node, *this);
181 void ShadowRoot::addSlotElementByName(const AtomicString& name, HTMLSlotElement& slot)
183 if (!m_slotAssignment)
184 m_slotAssignment = std::make_unique<SlotAssignment>();
186 return m_slotAssignment->addSlotElementByName(name, slot, *this);
189 void ShadowRoot::removeSlotElementByName(const AtomicString& name, HTMLSlotElement& slot)
191 return m_slotAssignment->removeSlotElementByName(name, slot, *this);
194 const Vector<Node*>* ShadowRoot::assignedNodesForSlot(const HTMLSlotElement& slot)
196 if (!m_slotAssignment)
198 return m_slotAssignment->assignedNodesForSlot(slot, *this);
201 Vector<ShadowRoot*> assignedShadowRootsIfSlotted(const Node& node)
203 Vector<ShadowRoot*> result;
204 for (auto* slot = node.assignedSlot(); slot; slot = slot->assignedSlot()) {
205 ASSERT(slot->containingShadowRoot());
206 result.append(slot->containingShadowRoot());