NodeRenderingContext is slow due to ComposedShadowTreeWalker
[WebKit-https.git] / Source / WebCore / dom / ElementShadow.cpp
1 /*
2  * Copyright (C) 2012 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 "ElementShadow.h"
29
30 #include "CSSParser.h"
31 #include "CSSSelectorList.h"
32 #include "ContainerNodeAlgorithms.h"
33 #include "Document.h"
34 #include "Element.h"
35 #include "HTMLContentElement.h"
36 #include "HTMLShadowElement.h"
37 #include "InspectorInstrumentation.h"
38 #include "NodeTraversal.h"
39 #include "ShadowRoot.h"
40 #include "StyleResolver.h"
41
42 namespace WebCore {
43
44 ElementShadow::ElementShadow()
45     : m_shouldCollectSelectFeatureSet(false)
46 {
47 }
48
49 ElementShadow::~ElementShadow()
50 {
51     ASSERT(m_shadowRoots.isEmpty());
52 }
53
54 static bool validateShadowRoot(Document* document, ShadowRoot* shadowRoot, ExceptionCode& ec)
55 {
56     if (!shadowRoot)
57         return true;
58
59     if (shadowRoot->host()) {
60         ec = HIERARCHY_REQUEST_ERR;
61         return false;
62     }
63
64     if (shadowRoot->document() != document) {
65         ec = WRONG_DOCUMENT_ERR;
66         return false;
67     }
68
69     return true;
70 }
71
72 void ElementShadow::addShadowRoot(Element* shadowHost, PassRefPtr<ShadowRoot> shadowRoot, ShadowRoot::ShadowRootType type, ExceptionCode& ec)
73 {
74     ASSERT(shadowHost);
75     ASSERT(shadowRoot);
76
77     if (!validateShadowRoot(shadowHost->document(), shadowRoot.get(), ec))
78         return;
79
80     if (type == ShadowRoot::AuthorShadowRoot)
81         shadowHost->willAddAuthorShadowRoot();
82
83     shadowRoot->setHost(shadowHost);
84     shadowRoot->setParentTreeScope(shadowHost->treeScope());
85     m_shadowRoots.push(shadowRoot.get());
86     setValidityUndetermined();
87     invalidateDistribution(shadowHost);
88     ChildNodeInsertionNotifier(shadowHost).notify(shadowRoot.get());
89
90     // Existence of shadow roots requires the host and its children to do traversal using ComposedShadowTreeWalker.
91     shadowHost->setNeedsShadowTreeWalker();
92
93     // FIXME(94905): ShadowHost should be reattached during recalcStyle.
94     // Set some flag here and recreate shadow hosts' renderer in
95     // Element::recalcStyle.
96     if (shadowHost->attached())
97         shadowHost->lazyReattach();
98
99     InspectorInstrumentation::didPushShadowRoot(shadowHost, shadowRoot.get());
100 }
101
102 void ElementShadow::removeAllShadowRoots()
103 {
104     // Dont protect this ref count.
105     Element* shadowHost = host();
106
107     while (RefPtr<ShadowRoot> oldRoot = m_shadowRoots.head()) {
108         InspectorInstrumentation::willPopShadowRoot(shadowHost, oldRoot.get());
109         shadowHost->document()->removeFocusedNodeOfSubtree(oldRoot.get());
110
111         if (oldRoot->attached())
112             oldRoot->detach();
113
114         m_shadowRoots.removeHead();
115         oldRoot->setHost(0);
116         oldRoot->setPrev(0);
117         oldRoot->setNext(0);
118         shadowHost->document()->adoptIfNeeded(oldRoot.get());
119         ChildNodeRemovalNotifier(shadowHost).notify(oldRoot.get());
120     }
121
122     invalidateDistribution(shadowHost);
123 }
124
125 void ElementShadow::attach()
126 {
127     ensureDistribution();
128     for (ShadowRoot* root = youngestShadowRoot(); root; root = root->olderShadowRoot()) {
129         if (!root->attached())
130             root->attach();
131     }
132 }
133
134 void ElementShadow::detach()
135 {
136     for (ShadowRoot* root = youngestShadowRoot(); root; root = root->olderShadowRoot()) {
137         if (root->attached())
138             root->detach();
139     }
140 }
141
142 bool ElementShadow::childNeedsStyleRecalc()
143 {
144     ASSERT(youngestShadowRoot());
145     for (ShadowRoot* root = youngestShadowRoot(); root; root = root->olderShadowRoot())
146         if (root->childNeedsStyleRecalc())
147             return true;
148
149     return false;
150 }
151
152 bool ElementShadow::needsStyleRecalc()
153 {
154     ASSERT(youngestShadowRoot());
155     for (ShadowRoot* root = youngestShadowRoot(); root; root = root->olderShadowRoot())
156         if (root->needsStyleRecalc())
157             return true;
158
159     return false;
160 }
161
162 void ElementShadow::recalcStyle(Node::StyleChange change)
163 {
164     for (ShadowRoot* root = youngestShadowRoot(); root; root = root->olderShadowRoot())
165         root->recalcStyle(change);
166 }
167
168 void ElementShadow::ensureDistribution()
169 {
170     if (!m_distributor.needsDistribution())
171         return;
172     m_distributor.distribute(host());
173 }
174
175 void ElementShadow::ensureDistributionFromDocument()
176 {
177     Vector<Element*, 8> hosts;
178     for (Element* current = host(); current; current = current->shadowHost())
179         hosts.append(current);
180
181     for (size_t i = hosts.size(); i > 0; --i)
182         hosts[i - 1]->shadow()->ensureDistribution();
183 }
184
185 void ElementShadow::setValidityUndetermined()
186 {
187     m_distributor.setValidity(ContentDistributor::Undetermined);
188 }
189
190 void ElementShadow::invalidateDistribution()
191 {
192     invalidateDistribution(host());
193 }
194
195 void ElementShadow::invalidateDistribution(Element* host)
196 {
197     bool needsInvalidation = m_distributor.needsInvalidation();
198     bool needsReattach = needsInvalidation ? m_distributor.invalidate(host) : false;
199
200     if (needsReattach && host->attached()) {
201         for (Node* n = host->firstChild(); n; n = n->nextSibling())
202             n->lazyReattach();
203         host->setNeedsStyleRecalc();
204     }
205
206     if (needsInvalidation)
207         m_distributor.finishInivalidation();
208 }
209
210 void ElementShadow::setShouldCollectSelectFeatureSet()
211 {
212     if (shouldCollectSelectFeatureSet())
213         return;
214
215     m_shouldCollectSelectFeatureSet = true;
216
217     if (ShadowRoot* parentShadowRoot = host()->containingShadowRoot()) {
218         if (ElementShadow* parentElementShadow = parentShadowRoot->owner())
219             parentElementShadow->setShouldCollectSelectFeatureSet();
220     }
221 }
222
223 void ElementShadow::ensureSelectFeatureSetCollected()
224 {
225     if (!m_shouldCollectSelectFeatureSet)
226         return;
227
228     m_selectFeatures.clear();
229     for (ShadowRoot* root = oldestShadowRoot(); root; root = root->youngerShadowRoot())
230         collectSelectFeatureSetFrom(root);
231     m_shouldCollectSelectFeatureSet = false;
232 }
233
234 void ElementShadow::collectSelectFeatureSetFrom(ShadowRoot* root)
235 {
236     if (root->hasElementShadow()) {
237         for (Element* element = ElementTraversal::firstWithin(root); element; element = ElementTraversal::next(element)) {
238             if (ElementShadow* elementShadow = element->shadow()) {
239                 elementShadow->ensureSelectFeatureSetCollected();
240                 m_selectFeatures.add(elementShadow->m_selectFeatures);
241             }
242         }
243     }
244
245     if (root->hasContentElement()) {
246         for (Element* element = ElementTraversal::firstWithin(root); element; element = ElementTraversal::next(element)) {
247             if (isHTMLContentElement(element)) {
248                 const CSSSelectorList& list = toHTMLContentElement(element)->selectorList();
249                 for (CSSSelector* selector = list.first(); selector; selector = list.next(selector))
250                     m_selectFeatures.collectFeaturesFromSelector(selector);                    
251             }
252         }
253     }
254 }
255
256 void ElementShadow::reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
257 {
258     MemoryClassInfo info(memoryObjectInfo, this, WebCoreMemoryTypes::DOM);
259     info.addMember(m_shadowRoots);
260     ShadowRoot* shadowRoot = m_shadowRoots.head();
261     while (shadowRoot) {
262         info.addMember(shadowRoot);
263         shadowRoot = shadowRoot->next();
264     }
265     info.addMember(m_distributor);
266 }
267
268 void invalidateParentDistributionIfNecessary(Element* element, SelectRuleFeatureSet::SelectRuleFeatureMask updatedFeatures)
269 {
270     ElementShadow* elementShadow = shadowOfParentForDistribution(element);
271     if (!elementShadow)
272         return;
273
274     elementShadow->ensureSelectFeatureSetCollected();
275     if (elementShadow->selectRuleFeatureSet().hasSelectorFor(updatedFeatures))
276         elementShadow->invalidateDistribution();
277 }
278
279 } // namespace