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 "core/dom/ContainerNode.h"
31 #include "core/dom/Document.h"
32 #include "core/dom/DocumentFragment.h"
33 #include "core/dom/Element.h"
34 #include "core/dom/TreeScope.h"
35 #include "wtf/DoublyLinkedList.h"
41 class HTMLShadowElement;
43 class ShadowRootRareData;
45 class ShadowRoot FINAL : public DocumentFragment, public TreeScope, public DoublyLinkedListNode<ShadowRoot> {
46 friend class WTF::DoublyLinkedListNode<ShadowRoot>;
48 // FIXME: We will support multiple shadow subtrees, however current implementation does not work well
49 // if a shadow root is dynamically created. So we prohibit multiple shadow subtrees
50 // in several elements for a while.
51 // See https://bugs.webkit.org/show_bug.cgi?id=77503 and related bugs.
53 UserAgentShadowRoot = 0,
57 static PassRefPtr<ShadowRoot> create(Document& document, ShadowRootType type)
59 return adoptRef(new ShadowRoot(document, type));
62 void recalcStyle(StyleRecalcChange);
64 // Disambiguate between Node and TreeScope hierarchies; TreeScope's implementation is simpler.
65 using TreeScope::document;
67 Element* host() const { return toElement(parentOrShadowHostNode()); }
68 ElementShadow* owner() const { return host() ? host()->shadow() : 0; }
70 ShadowRoot* youngerShadowRoot() const { return prev(); }
72 ShadowRoot* olderShadowRootForBindings() const;
73 bool shouldExposeToBindings() const { return type() == AuthorShadowRoot; }
75 bool isYoungest() const { return !youngerShadowRoot(); }
76 bool isOldest() const { return !olderShadowRoot(); }
77 bool isOldestAuthorShadowRoot() const;
79 virtual void attach(const AttachContext& = AttachContext()) OVERRIDE;
81 virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
82 virtual void removedFrom(ContainerNode*) OVERRIDE;
84 virtual void registerScopedHTMLStyleChild() OVERRIDE;
85 virtual void unregisterScopedHTMLStyleChild() OVERRIDE;
87 bool containsShadowElements() const;
88 bool containsContentElements() const;
89 bool containsInsertionPoints() const { return containsShadowElements() || containsContentElements(); }
90 bool containsShadowRoots() const;
92 unsigned descendantShadowElementCount() const;
94 // For Internals, don't use this.
95 unsigned childShadowRootCount() const;
97 HTMLShadowElement* shadowInsertionPointOfYoungerShadowRoot() const;
98 void setShadowInsertionPointOfYoungerShadowRoot(PassRefPtr<HTMLShadowElement>);
100 void didAddInsertionPoint(InsertionPoint*);
101 void didRemoveInsertionPoint(InsertionPoint*);
102 const Vector<RefPtr<InsertionPoint> >& descendantInsertionPoints();
104 ShadowRootType type() const { return static_cast<ShadowRootType>(m_type); }
106 // Make protected methods from base class public here.
107 using TreeScope::setDocument;
108 using TreeScope::setParentTreeScope;
111 Element* activeElement() const;
113 bool applyAuthorStyles() const { return m_applyAuthorStyles; }
114 void setApplyAuthorStyles(bool);
116 bool resetStyleInheritance() const { return m_resetStyleInheritance; }
117 void setResetStyleInheritance(bool);
119 ShadowRoot* olderShadowRoot() const { return next(); }
121 String innerHTML() const;
122 void setInnerHTML(const String&, ExceptionState&);
124 PassRefPtr<Node> cloneNode(bool, ExceptionState&);
125 PassRefPtr<Node> cloneNode(ExceptionState& exceptionState) { return cloneNode(true, exceptionState); }
127 StyleSheetList* styleSheets();
128 bool isActiveForStyling() const;
131 ShadowRoot(Document&, ShadowRootType);
132 virtual ~ShadowRoot();
134 virtual void dispose() OVERRIDE;
135 virtual void childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta) OVERRIDE;
137 ShadowRootRareData* ensureShadowRootRareData();
139 void addChildShadowRoot();
140 void removeChildShadowRoot();
141 void invalidateDescendantInsertionPoints();
143 // ShadowRoots should never be cloned.
144 virtual PassRefPtr<Node> cloneNode(bool) OVERRIDE { return 0; }
146 // FIXME: This shouldn't happen. https://bugs.webkit.org/show_bug.cgi?id=88834
147 bool isOrphan() const { return !host(); }
151 OwnPtr<ShadowRootRareData> m_shadowRootRareData;
152 unsigned m_numberOfStyles : 27;
153 unsigned m_applyAuthorStyles : 1;
154 unsigned m_resetStyleInheritance : 1;
156 unsigned m_registeredWithParentShadowRoot : 1;
157 unsigned m_descendantInsertionPointsIsValid : 1;
160 inline Element* ShadowRoot::activeElement() const
162 return adjustedFocusedElement();
165 DEFINE_NODE_TYPE_CASTS(ShadowRoot, isShadowRoot());
166 DEFINE_TYPE_CASTS(ShadowRoot, TreeScope, treeScope, treeScope->rootNode().isShadowRoot(), treeScope.rootNode().isShadowRoot());