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.
28 #include "core/dom/shadow/ShadowRoot.h"
30 #include "bindings/core/v8/ExceptionState.h"
31 #include "core/css/StyleSheetList.h"
32 #include "core/css/resolver/StyleResolver.h"
33 #include "core/css/resolver/StyleResolverParentScope.h"
34 #include "core/dom/ElementTraversal.h"
35 #include "core/dom/StyleEngine.h"
36 #include "core/dom/Text.h"
37 #include "core/dom/shadow/ElementShadow.h"
38 #include "core/dom/shadow/InsertionPoint.h"
39 #include "core/dom/shadow/ShadowRootRareData.h"
40 #include "core/editing/markup.h"
41 #include "core/html/HTMLShadowElement.h"
42 #include "public/platform/Platform.h"
46 struct SameSizeAsShadowRoot : public DocumentFragment, public TreeScope, public DoublyLinkedListNode<ShadowRoot> {
48 unsigned countersAndFlags[1];
51 COMPILE_ASSERT(sizeof(ShadowRoot) == sizeof(SameSizeAsShadowRoot), shadowroot_should_stay_small);
53 ShadowRoot::ShadowRoot(Document& document, ShadowRootType type)
54 : DocumentFragment(0, CreateShadowRoot)
55 , TreeScope(*this, document)
60 , m_registeredWithParentShadowRoot(false)
61 , m_descendantInsertionPointsIsValid(false)
63 ScriptWrappable::init(this);
66 ShadowRoot::~ShadowRoot()
72 if (m_shadowRootRareData && m_shadowRootRareData->styleSheets())
73 m_shadowRootRareData->styleSheets()->detachFromDocument();
75 document().styleEngine()->didRemoveShadowRoot(this);
77 // We cannot let ContainerNode destructor call willBeDeletedFromDocument()
78 // for this ShadowRoot instance because TreeScope destructor
79 // clears Node::m_treeScope thus ContainerNode is no longer able
80 // to access it Document reference after that.
81 willBeDeletedFromDocument();
83 // We must remove all of our children first before the TreeScope destructor
84 // runs so we don't go through TreeScopeAdopter for each child with a
85 // destructed tree scope in each descendant.
86 removeDetachedChildren();
88 // We must call clearRareData() here since a ShadowRoot class inherits TreeScope
89 // as well as Node. See a comment on TreeScope.h for the reason.
96 void ShadowRoot::dispose()
98 removeDetachedChildren();
102 ShadowRoot* ShadowRoot::olderShadowRootForBindings() const
104 ShadowRoot* older = olderShadowRoot();
105 while (older && !older->shouldExposeToBindings())
106 older = older->olderShadowRoot();
107 ASSERT(!older || older->shouldExposeToBindings());
111 PassRefPtrWillBeRawPtr<Node> ShadowRoot::cloneNode(bool, ExceptionState& exceptionState)
113 exceptionState.throwDOMException(DataCloneError, "ShadowRoot nodes are not clonable.");
117 String ShadowRoot::innerHTML() const
119 return createMarkup(this, ChildrenOnly);
122 void ShadowRoot::setInnerHTML(const String& markup, ExceptionState& exceptionState)
125 exceptionState.throwDOMException(InvalidAccessError, "The ShadowRoot does not have a host.");
129 if (RefPtrWillBeRawPtr<DocumentFragment> fragment = createFragmentForInnerOuterHTML(markup, host(), AllowScriptingContent, "innerHTML", exceptionState))
130 replaceChildrenWithFragment(this, fragment.release(), exceptionState);
133 void ShadowRoot::recalcStyle(StyleRecalcChange change)
135 // ShadowRoot doesn't support custom callbacks.
136 ASSERT(!hasCustomStyleCallbacks());
138 StyleResolverParentScope parentScope(*this);
140 if (styleChangeType() >= SubtreeStyleChange)
143 if (change < Force && hasRareData() && childNeedsStyleRecalc())
144 checkForChildrenAdjacentRuleChanges();
146 // There's no style to update so just calling recalcStyle means we're updated.
147 clearNeedsStyleRecalc();
149 // FIXME: This doesn't handle :hover + div properly like Element::recalcStyle does.
150 Text* lastTextNode = 0;
151 for (Node* child = lastChild(); child; child = child->previousSibling()) {
152 if (child->isTextNode()) {
153 toText(child)->recalcTextStyle(change, lastTextNode);
154 lastTextNode = toText(child);
155 } else if (child->isElementNode()) {
156 if (child->shouldCallRecalcStyle(change))
157 toElement(child)->recalcStyle(change, lastTextNode);
158 if (child->renderer())
163 clearChildNeedsStyleRecalc();
166 void ShadowRoot::attach(const AttachContext& context)
168 StyleResolverParentScope parentScope(*this);
169 DocumentFragment::attach(context);
172 Node::InsertionNotificationRequest ShadowRoot::insertedInto(ContainerNode* insertionPoint)
174 DocumentFragment::insertedInto(insertionPoint);
176 if (!insertionPoint->inDocument() || !isOldest())
177 return InsertionDone;
179 // FIXME: When parsing <video controls>, insertedInto() is called many times without invoking removedFrom.
180 // For now, we check m_registeredWithParentShadowroot. We would like to ASSERT(!m_registeredShadowRoot) here.
181 // https://bugs.webkit.org/show_bug.cig?id=101316
182 if (m_registeredWithParentShadowRoot)
183 return InsertionDone;
185 if (ShadowRoot* root = host()->containingShadowRoot()) {
186 root->addChildShadowRoot();
187 m_registeredWithParentShadowRoot = true;
190 return InsertionDone;
193 void ShadowRoot::removedFrom(ContainerNode* insertionPoint)
195 if (insertionPoint->inDocument() && m_registeredWithParentShadowRoot) {
196 ShadowRoot* root = host()->containingShadowRoot();
198 root = insertionPoint->containingShadowRoot();
200 root->removeChildShadowRoot();
201 m_registeredWithParentShadowRoot = false;
204 DocumentFragment::removedFrom(insertionPoint);
207 void ShadowRoot::childrenChanged(const ChildrenChange& change)
209 ContainerNode::childrenChanged(change);
211 if (change.isChildElementChange())
212 checkForSiblingStyleChanges(change.type == ElementRemoved ? SiblingElementRemoved : SiblingElementInserted, change.siblingBeforeChange, change.siblingAfterChange);
214 if (InsertionPoint* point = shadowInsertionPointOfYoungerShadowRoot()) {
215 if (ShadowRoot* root = point->containingShadowRoot())
216 root->owner()->setNeedsDistributionRecalc();
220 void ShadowRoot::registerScopedHTMLStyleChild()
225 void ShadowRoot::unregisterScopedHTMLStyleChild()
227 ASSERT(m_numberOfStyles > 0);
231 ShadowRootRareData* ShadowRoot::ensureShadowRootRareData()
233 if (m_shadowRootRareData)
234 return m_shadowRootRareData.get();
236 m_shadowRootRareData = adoptPtrWillBeNoop(new ShadowRootRareData);
237 return m_shadowRootRareData.get();
240 bool ShadowRoot::containsShadowElements() const
242 return m_shadowRootRareData ? m_shadowRootRareData->containsShadowElements() : 0;
245 bool ShadowRoot::containsContentElements() const
247 return m_shadowRootRareData ? m_shadowRootRareData->containsContentElements() : 0;
250 bool ShadowRoot::containsShadowRoots() const
252 return m_shadowRootRareData ? m_shadowRootRareData->containsShadowRoots() : 0;
255 unsigned ShadowRoot::descendantShadowElementCount() const
257 return m_shadowRootRareData ? m_shadowRootRareData->descendantShadowElementCount() : 0;
260 HTMLShadowElement* ShadowRoot::shadowInsertionPointOfYoungerShadowRoot() const
262 return m_shadowRootRareData ? m_shadowRootRareData->shadowInsertionPointOfYoungerShadowRoot() : 0;
265 void ShadowRoot::setShadowInsertionPointOfYoungerShadowRoot(PassRefPtrWillBeRawPtr<HTMLShadowElement> shadowInsertionPoint)
267 if (!m_shadowRootRareData && !shadowInsertionPoint)
269 ensureShadowRootRareData()->setShadowInsertionPointOfYoungerShadowRoot(shadowInsertionPoint);
272 void ShadowRoot::didAddInsertionPoint(InsertionPoint* insertionPoint)
274 ensureShadowRootRareData()->didAddInsertionPoint(insertionPoint);
275 invalidateDescendantInsertionPoints();
278 void ShadowRoot::didRemoveInsertionPoint(InsertionPoint* insertionPoint)
280 m_shadowRootRareData->didRemoveInsertionPoint(insertionPoint);
281 invalidateDescendantInsertionPoints();
284 void ShadowRoot::addChildShadowRoot()
286 ensureShadowRootRareData()->didAddChildShadowRoot();
289 void ShadowRoot::removeChildShadowRoot()
291 // FIXME: Why isn't this an ASSERT?
292 if (!m_shadowRootRareData)
294 m_shadowRootRareData->didRemoveChildShadowRoot();
297 unsigned ShadowRoot::childShadowRootCount() const
299 return m_shadowRootRareData ? m_shadowRootRareData->childShadowRootCount() : 0;
302 void ShadowRoot::invalidateDescendantInsertionPoints()
304 m_descendantInsertionPointsIsValid = false;
305 m_shadowRootRareData->clearDescendantInsertionPoints();
308 const WillBeHeapVector<RefPtrWillBeMember<InsertionPoint> >& ShadowRoot::descendantInsertionPoints()
310 DEFINE_STATIC_LOCAL(WillBePersistentHeapVector<RefPtrWillBeMember<InsertionPoint> >, emptyList, ());
311 if (m_shadowRootRareData && m_descendantInsertionPointsIsValid)
312 return m_shadowRootRareData->descendantInsertionPoints();
314 m_descendantInsertionPointsIsValid = true;
316 if (!containsInsertionPoints())
319 WillBeHeapVector<RefPtrWillBeMember<InsertionPoint> > insertionPoints;
320 for (InsertionPoint* insertionPoint = Traversal<InsertionPoint>::firstWithin(*this); insertionPoint; insertionPoint = Traversal<InsertionPoint>::next(*insertionPoint, this))
321 insertionPoints.append(insertionPoint);
323 ensureShadowRootRareData()->setDescendantInsertionPoints(insertionPoints);
325 return m_shadowRootRareData->descendantInsertionPoints();
328 StyleSheetList* ShadowRoot::styleSheets()
330 if (!ensureShadowRootRareData()->styleSheets())
331 m_shadowRootRareData->setStyleSheets(StyleSheetList::create(this));
333 return m_shadowRootRareData->styleSheets();
336 void ShadowRoot::trace(Visitor* visitor)
338 visitor->trace(m_prev);
339 visitor->trace(m_next);
340 visitor->trace(m_shadowRootRareData);
341 TreeScope::trace(visitor);
342 DocumentFragment::trace(visitor);