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 #ifndef ShadowRoot_h
28 #define ShadowRoot_h
29
30 #include "core/dom/ContainerNode.h"
31 #include "core/dom/DocumentFragment.h"
32 #include "core/dom/Element.h"
33 #include "core/dom/TreeScope.h"
34 #include "wtf/DoublyLinkedList.h"
35
36 namespace blink {
37
38 class Document;
39 class ElementShadow;
40 class ExceptionState;
41 class HTMLShadowElement;
42 class InsertionPoint;
43 class ShadowRootRareData;
44 class StyleSheetList;
45
46 class ShadowRoot FINAL : public DocumentFragment, public TreeScope, public DoublyLinkedListNode<ShadowRoot> {
47 DEFINE_WRAPPERTYPEINFO();
48 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(ShadowRoot);
49 friend class WTF::DoublyLinkedListNode<ShadowRoot>;
50 public:
51 // FIXME: We will support multiple shadow subtrees, however current implementation does not work well
52 // if a shadow root is dynamically created. So we prohibit multiple shadow subtrees
53 // in several elements for a while.
54 // See https://bugs.webkit.org/show_bug.cgi?id=77503 and related bugs.
55 enum ShadowRootType {
56 UserAgentShadowRoot = 0,
57 AuthorShadowRoot
58 };
59
create(Document & document,ShadowRootType type)60 static PassRefPtrWillBeRawPtr<ShadowRoot> create(Document& document, ShadowRootType type)
61 {
62 return adoptRefWillBeNoop(new ShadowRoot(document, type));
63 }
64
65 void recalcStyle(StyleRecalcChange);
66
67 // Disambiguate between Node and TreeScope hierarchies; TreeScope's implementation is simpler.
68 using TreeScope::document;
69 using TreeScope::getElementById;
70
host()71 Element* host() const { return toElement(parentOrShadowHostNode()); }
owner()72 ElementShadow* owner() const { return host() ? host()->shadow() : 0; }
73
youngerShadowRoot()74 ShadowRoot* youngerShadowRoot() const { return prev(); }
75
76 ShadowRoot* olderShadowRootForBindings() const;
shouldExposeToBindings()77 bool shouldExposeToBindings() const { return type() == AuthorShadowRoot; }
78
isYoungest()79 bool isYoungest() const { return !youngerShadowRoot(); }
isOldest()80 bool isOldest() const { return !olderShadowRoot(); }
81 bool isOldestAuthorShadowRoot() const;
82
83 virtual void attach(const AttachContext& = AttachContext()) OVERRIDE;
84
85 virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
86 virtual void removedFrom(ContainerNode*) OVERRIDE;
87
88 void registerScopedHTMLStyleChild();
89 void unregisterScopedHTMLStyleChild();
90
91 bool containsShadowElements() const;
92 bool containsContentElements() const;
containsInsertionPoints()93 bool containsInsertionPoints() const { return containsShadowElements() || containsContentElements(); }
94 bool containsShadowRoots() const;
95
96 unsigned descendantShadowElementCount() const;
97
98 // For Internals, don't use this.
99 unsigned childShadowRootCount() const;
numberOfStyles()100 unsigned numberOfStyles() const { return m_numberOfStyles; }
101
102 HTMLShadowElement* shadowInsertionPointOfYoungerShadowRoot() const;
103 void setShadowInsertionPointOfYoungerShadowRoot(PassRefPtrWillBeRawPtr<HTMLShadowElement>);
104
105 void didAddInsertionPoint(InsertionPoint*);
106 void didRemoveInsertionPoint(InsertionPoint*);
107 const WillBeHeapVector<RefPtrWillBeMember<InsertionPoint> >& descendantInsertionPoints();
108
type()109 ShadowRootType type() const { return static_cast<ShadowRootType>(m_type); }
110
111 // Make protected methods from base class public here.
112 using TreeScope::setDocument;
113 using TreeScope::setParentTreeScope;
114
115 public:
116 Element* activeElement() const;
117
olderShadowRoot()118 ShadowRoot* olderShadowRoot() const { return next(); }
119
120 String innerHTML() const;
121 void setInnerHTML(const String&, ExceptionState&);
122
123 PassRefPtrWillBeRawPtr<Node> cloneNode(bool, ExceptionState&);
cloneNode(ExceptionState & exceptionState)124 PassRefPtrWillBeRawPtr<Node> cloneNode(ExceptionState& exceptionState) { return cloneNode(true, exceptionState); }
125
126 StyleSheetList* styleSheets();
127
128 virtual void trace(Visitor*) OVERRIDE;
129
130 private:
131 ShadowRoot(Document&, ShadowRootType);
132 virtual ~ShadowRoot();
133
134 #if !ENABLE(OILPAN)
135 virtual void dispose() OVERRIDE;
136 #endif
137
138 virtual void childrenChanged(const ChildrenChange&) OVERRIDE;
139
140 ShadowRootRareData* ensureShadowRootRareData();
141
142 void addChildShadowRoot();
143 void removeChildShadowRoot();
144 void invalidateDescendantInsertionPoints();
145
146 // ShadowRoots should never be cloned.
cloneNode(bool)147 virtual PassRefPtrWillBeRawPtr<Node> cloneNode(bool) OVERRIDE { return nullptr; }
148
149 // FIXME: This shouldn't happen. https://bugs.webkit.org/show_bug.cgi?id=88834
isOrphan()150 bool isOrphan() const { return !host(); }
151
152 RawPtrWillBeMember<ShadowRoot> m_prev;
153 RawPtrWillBeMember<ShadowRoot> m_next;
154 OwnPtrWillBeMember<ShadowRootRareData> m_shadowRootRareData;
155 unsigned m_numberOfStyles : 27;
156 unsigned m_type : 1;
157 unsigned m_registeredWithParentShadowRoot : 1;
158 unsigned m_descendantInsertionPointsIsValid : 1;
159 };
160
activeElement()161 inline Element* ShadowRoot::activeElement() const
162 {
163 return adjustedFocusedElement();
164 }
165
166 DEFINE_NODE_TYPE_CASTS(ShadowRoot, isShadowRoot());
167 DEFINE_TYPE_CASTS(ShadowRoot, TreeScope, treeScope, treeScope->rootNode().isShadowRoot(), treeScope.rootNode().isShadowRoot());
168
169 } // namespace blink
170
171 #endif // ShadowRoot_h
172