Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / dom / shadow / ShadowRoot.cpp
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 #include "config.h"
28 #include "core/dom/shadow/ShadowRoot.h"
29
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"
43
44 namespace blink {
45
46 struct SameSizeAsShadowRoot : public DocumentFragment, public TreeScope, public DoublyLinkedListNode<ShadowRoot> {
47     void* pointers[3];
48     unsigned countersAndFlags[1];
49 };
50
51 COMPILE_ASSERT(sizeof(ShadowRoot) == sizeof(SameSizeAsShadowRoot), shadowroot_should_stay_small);
52
53 ShadowRoot::ShadowRoot(Document& document, ShadowRootType type)
54     : DocumentFragment(0, CreateShadowRoot)
55     , TreeScope(*this, document)
56     , m_prev(nullptr)
57     , m_next(nullptr)
58     , m_numberOfStyles(0)
59     , m_type(type)
60     , m_registeredWithParentShadowRoot(false)
61     , m_descendantInsertionPointsIsValid(false)
62 {
63     ScriptWrappable::init(this);
64 }
65
66 ShadowRoot::~ShadowRoot()
67 {
68 #if !ENABLE(OILPAN)
69     ASSERT(!m_prev);
70     ASSERT(!m_next);
71
72     if (m_shadowRootRareData && m_shadowRootRareData->styleSheets())
73         m_shadowRootRareData->styleSheets()->detachFromDocument();
74
75     document().styleEngine()->didRemoveShadowRoot(this);
76
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();
82
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();
87
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.
90     if (hasRareData())
91         clearRareData();
92 #endif
93 }
94
95 #if !ENABLE(OILPAN)
96 void ShadowRoot::dispose()
97 {
98     removeDetachedChildren();
99 }
100 #endif
101
102 ShadowRoot* ShadowRoot::olderShadowRootForBindings() const
103 {
104     ShadowRoot* older = olderShadowRoot();
105     while (older && !older->shouldExposeToBindings())
106         older = older->olderShadowRoot();
107     ASSERT(!older || older->shouldExposeToBindings());
108     return older;
109 }
110
111 PassRefPtrWillBeRawPtr<Node> ShadowRoot::cloneNode(bool, ExceptionState& exceptionState)
112 {
113     exceptionState.throwDOMException(DataCloneError, "ShadowRoot nodes are not clonable.");
114     return nullptr;
115 }
116
117 String ShadowRoot::innerHTML() const
118 {
119     return createMarkup(this, ChildrenOnly);
120 }
121
122 void ShadowRoot::setInnerHTML(const String& markup, ExceptionState& exceptionState)
123 {
124     if (isOrphan()) {
125         exceptionState.throwDOMException(InvalidAccessError, "The ShadowRoot does not have a host.");
126         return;
127     }
128
129     if (RefPtrWillBeRawPtr<DocumentFragment> fragment = createFragmentForInnerOuterHTML(markup, host(), AllowScriptingContent, "innerHTML", exceptionState))
130         replaceChildrenWithFragment(this, fragment.release(), exceptionState);
131 }
132
133 void ShadowRoot::recalcStyle(StyleRecalcChange change)
134 {
135     // ShadowRoot doesn't support custom callbacks.
136     ASSERT(!hasCustomStyleCallbacks());
137
138     StyleResolverParentScope parentScope(*this);
139
140     if (styleChangeType() >= SubtreeStyleChange)
141         change = Force;
142
143     if (change < Force && hasRareData() && childNeedsStyleRecalc())
144         checkForChildrenAdjacentRuleChanges();
145
146     // There's no style to update so just calling recalcStyle means we're updated.
147     clearNeedsStyleRecalc();
148
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())
159                 lastTextNode = 0;
160         }
161     }
162
163     clearChildNeedsStyleRecalc();
164 }
165
166 void ShadowRoot::attach(const AttachContext& context)
167 {
168     StyleResolverParentScope parentScope(*this);
169     DocumentFragment::attach(context);
170 }
171
172 Node::InsertionNotificationRequest ShadowRoot::insertedInto(ContainerNode* insertionPoint)
173 {
174     DocumentFragment::insertedInto(insertionPoint);
175
176     if (!insertionPoint->inDocument() || !isOldest())
177         return InsertionDone;
178
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;
184
185     if (ShadowRoot* root = host()->containingShadowRoot()) {
186         root->addChildShadowRoot();
187         m_registeredWithParentShadowRoot = true;
188     }
189
190     return InsertionDone;
191 }
192
193 void ShadowRoot::removedFrom(ContainerNode* insertionPoint)
194 {
195     if (insertionPoint->inDocument() && m_registeredWithParentShadowRoot) {
196         ShadowRoot* root = host()->containingShadowRoot();
197         if (!root)
198             root = insertionPoint->containingShadowRoot();
199         if (root)
200             root->removeChildShadowRoot();
201         m_registeredWithParentShadowRoot = false;
202     }
203
204     DocumentFragment::removedFrom(insertionPoint);
205 }
206
207 void ShadowRoot::childrenChanged(const ChildrenChange& change)
208 {
209     ContainerNode::childrenChanged(change);
210
211     if (change.isChildElementChange())
212         checkForSiblingStyleChanges(change.type == ElementRemoved ? SiblingElementRemoved : SiblingElementInserted, change.siblingBeforeChange, change.siblingAfterChange);
213
214     if (InsertionPoint* point = shadowInsertionPointOfYoungerShadowRoot()) {
215         if (ShadowRoot* root = point->containingShadowRoot())
216             root->owner()->setNeedsDistributionRecalc();
217     }
218 }
219
220 void ShadowRoot::registerScopedHTMLStyleChild()
221 {
222     ++m_numberOfStyles;
223 }
224
225 void ShadowRoot::unregisterScopedHTMLStyleChild()
226 {
227     ASSERT(m_numberOfStyles > 0);
228     --m_numberOfStyles;
229 }
230
231 ShadowRootRareData* ShadowRoot::ensureShadowRootRareData()
232 {
233     if (m_shadowRootRareData)
234         return m_shadowRootRareData.get();
235
236     m_shadowRootRareData = adoptPtrWillBeNoop(new ShadowRootRareData);
237     return m_shadowRootRareData.get();
238 }
239
240 bool ShadowRoot::containsShadowElements() const
241 {
242     return m_shadowRootRareData ? m_shadowRootRareData->containsShadowElements() : 0;
243 }
244
245 bool ShadowRoot::containsContentElements() const
246 {
247     return m_shadowRootRareData ? m_shadowRootRareData->containsContentElements() : 0;
248 }
249
250 bool ShadowRoot::containsShadowRoots() const
251 {
252     return m_shadowRootRareData ? m_shadowRootRareData->containsShadowRoots() : 0;
253 }
254
255 unsigned ShadowRoot::descendantShadowElementCount() const
256 {
257     return m_shadowRootRareData ? m_shadowRootRareData->descendantShadowElementCount() : 0;
258 }
259
260 HTMLShadowElement* ShadowRoot::shadowInsertionPointOfYoungerShadowRoot() const
261 {
262     return m_shadowRootRareData ? m_shadowRootRareData->shadowInsertionPointOfYoungerShadowRoot() : 0;
263 }
264
265 void ShadowRoot::setShadowInsertionPointOfYoungerShadowRoot(PassRefPtrWillBeRawPtr<HTMLShadowElement> shadowInsertionPoint)
266 {
267     if (!m_shadowRootRareData && !shadowInsertionPoint)
268         return;
269     ensureShadowRootRareData()->setShadowInsertionPointOfYoungerShadowRoot(shadowInsertionPoint);
270 }
271
272 void ShadowRoot::didAddInsertionPoint(InsertionPoint* insertionPoint)
273 {
274     ensureShadowRootRareData()->didAddInsertionPoint(insertionPoint);
275     invalidateDescendantInsertionPoints();
276 }
277
278 void ShadowRoot::didRemoveInsertionPoint(InsertionPoint* insertionPoint)
279 {
280     m_shadowRootRareData->didRemoveInsertionPoint(insertionPoint);
281     invalidateDescendantInsertionPoints();
282 }
283
284 void ShadowRoot::addChildShadowRoot()
285 {
286     ensureShadowRootRareData()->didAddChildShadowRoot();
287 }
288
289 void ShadowRoot::removeChildShadowRoot()
290 {
291     // FIXME: Why isn't this an ASSERT?
292     if (!m_shadowRootRareData)
293         return;
294     m_shadowRootRareData->didRemoveChildShadowRoot();
295 }
296
297 unsigned ShadowRoot::childShadowRootCount() const
298 {
299     return m_shadowRootRareData ? m_shadowRootRareData->childShadowRootCount() : 0;
300 }
301
302 void ShadowRoot::invalidateDescendantInsertionPoints()
303 {
304     m_descendantInsertionPointsIsValid = false;
305     m_shadowRootRareData->clearDescendantInsertionPoints();
306 }
307
308 const WillBeHeapVector<RefPtrWillBeMember<InsertionPoint> >& ShadowRoot::descendantInsertionPoints()
309 {
310     DEFINE_STATIC_LOCAL(WillBePersistentHeapVector<RefPtrWillBeMember<InsertionPoint> >, emptyList, ());
311     if (m_shadowRootRareData && m_descendantInsertionPointsIsValid)
312         return m_shadowRootRareData->descendantInsertionPoints();
313
314     m_descendantInsertionPointsIsValid = true;
315
316     if (!containsInsertionPoints())
317         return emptyList;
318
319     WillBeHeapVector<RefPtrWillBeMember<InsertionPoint> > insertionPoints;
320     for (InsertionPoint* insertionPoint = Traversal<InsertionPoint>::firstWithin(*this); insertionPoint; insertionPoint = Traversal<InsertionPoint>::next(*insertionPoint, this))
321         insertionPoints.append(insertionPoint);
322
323     ensureShadowRootRareData()->setDescendantInsertionPoints(insertionPoints);
324
325     return m_shadowRootRareData->descendantInsertionPoints();
326 }
327
328 StyleSheetList* ShadowRoot::styleSheets()
329 {
330     if (!ensureShadowRootRareData()->styleSheets())
331         m_shadowRootRareData->setStyleSheets(StyleSheetList::create(this));
332
333     return m_shadowRootRareData->styleSheets();
334 }
335
336 void ShadowRoot::trace(Visitor* visitor)
337 {
338     visitor->trace(m_prev);
339     visitor->trace(m_next);
340     visitor->trace(m_shadowRootRareData);
341     TreeScope::trace(visitor);
342     DocumentFragment::trace(visitor);
343 }
344
345 }