Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: third_party/WebKit/Source/core/dom/shadow/ElementShadowV0.cpp

Issue 2322213002: Refactor ElementShadow by separating v0 related features as ElementShadowV0 (Closed)
Patch Set: addressed Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved. 2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Neither the name of Google Inc. nor the names of its 10 * * Neither the name of Google Inc. nor the names of its
11 * contributors may be used to endorse or promote products derived from 11 * contributors may be used to endorse or promote products derived from
12 * this software without specific prior written permission. 12 * this software without specific prior written permission.
13 * 13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 18 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */ 25 */
26 26
27 #include "core/dom/shadow/ElementShadowV0.h"
28
29 #include "core/dom/ElementTraversal.h"
30 #include "core/dom/shadow/DistributedNodes.h"
27 #include "core/dom/shadow/ElementShadow.h" 31 #include "core/dom/shadow/ElementShadow.h"
28
29 #include "core/css/StyleSheetList.h"
30 #include "core/dom/ElementTraversal.h"
31 #include "core/dom/NodeTraversal.h"
32 #include "core/dom/StyleChangeReason.h"
33 #include "core/dom/shadow/DistributedNodes.h"
34 #include "core/frame/Deprecation.h"
35 #include "core/html/HTMLContentElement.h" 32 #include "core/html/HTMLContentElement.h"
36 #include "core/html/HTMLShadowElement.h" 33 #include "core/html/HTMLShadowElement.h"
37 #include "core/inspector/InspectorInstrumentation.h" 34 #include "core/inspector/InspectorInstrumentation.h"
38 #include "platform/EventDispatchForbiddenScope.h"
39 #include "platform/ScriptForbiddenScope.h"
40 35
41 namespace blink { 36 namespace blink {
42 37
43 class DistributionPool final { 38 class DistributionPool final {
44 STACK_ALLOCATED(); 39 STACK_ALLOCATED();
45 public: 40 public:
46 explicit DistributionPool(const ContainerNode&); 41 explicit DistributionPool(const ContainerNode&);
47 void clear(); 42 void clear();
48 ~DistributionPool(); 43 ~DistributionPool();
49 void distributeTo(InsertionPoint*, ElementShadow*); 44 void distributeTo(InsertionPoint*, ElementShadowV0*);
50 void populateChildren(const ContainerNode&); 45 void populateChildren(const ContainerNode&);
51 46
52 private: 47 private:
53 void detachNonDistributedNodes(); 48 void detachNonDistributedNodes();
54 HeapVector<Member<Node>, 32> m_nodes; 49 HeapVector<Member<Node>, 32> m_nodes;
55 Vector<bool, 32> m_distributed; 50 Vector<bool, 32> m_distributed;
56 }; 51 };
57 52
58 inline DistributionPool::DistributionPool(const ContainerNode& parent) 53 inline DistributionPool::DistributionPool(const ContainerNode& parent)
59 { 54 {
(...skipping 20 matching lines...) Expand all
80 for (size_t i = 0; i < insertionPoint->distributedNodesSize(); ++i) 75 for (size_t i = 0; i < insertionPoint->distributedNodesSize(); ++i)
81 m_nodes.append(insertionPoint->distributedNodeAt(i)); 76 m_nodes.append(insertionPoint->distributedNodeAt(i));
82 } else { 77 } else {
83 m_nodes.append(child); 78 m_nodes.append(child);
84 } 79 }
85 } 80 }
86 m_distributed.resize(m_nodes.size()); 81 m_distributed.resize(m_nodes.size());
87 m_distributed.fill(false); 82 m_distributed.fill(false);
88 } 83 }
89 84
90 void DistributionPool::distributeTo(InsertionPoint* insertionPoint, ElementShado w* elementShadow) 85 void DistributionPool::distributeTo(InsertionPoint* insertionPoint, ElementShado wV0* elementShadow)
91 { 86 {
92 DistributedNodes distributedNodes; 87 DistributedNodes distributedNodes;
93 88
94 for (size_t i = 0; i < m_nodes.size(); ++i) { 89 for (size_t i = 0; i < m_nodes.size(); ++i) {
95 if (m_distributed[i]) 90 if (m_distributed[i])
96 continue; 91 continue;
97 92
98 if (isHTMLContentElement(*insertionPoint) && !toHTMLContentElement(inser tionPoint)->canSelectNode(m_nodes, i)) 93 if (isHTMLContentElement(*insertionPoint) && !toHTMLContentElement(inser tionPoint)->canSelectNode(m_nodes, i))
99 continue; 94 continue;
100 95
(...skipping 21 matching lines...) Expand all
122 inline void DistributionPool::detachNonDistributedNodes() 117 inline void DistributionPool::detachNonDistributedNodes()
123 { 118 {
124 for (size_t i = 0; i < m_nodes.size(); ++i) { 119 for (size_t i = 0; i < m_nodes.size(); ++i) {
125 if (m_distributed[i]) 120 if (m_distributed[i])
126 continue; 121 continue;
127 if (m_nodes[i]->layoutObject()) 122 if (m_nodes[i]->layoutObject())
128 m_nodes[i]->lazyReattachIfAttached(); 123 m_nodes[i]->lazyReattachIfAttached();
129 } 124 }
130 } 125 }
131 126
132 ElementShadow* ElementShadow::create() 127 ElementShadowV0* ElementShadowV0::create(ElementShadow& elementShadow)
133 { 128 {
134 return new ElementShadow(); 129 return new ElementShadowV0(elementShadow);
135 } 130 }
136 131
137 ElementShadow::ElementShadow() 132 ElementShadowV0::ElementShadowV0(ElementShadow& elementShadow)
138 : m_needsDistributionRecalc(false) 133 : m_elementShadow(&elementShadow)
139 , m_needsSelectFeatureSet(false) 134 , m_needsSelectFeatureSet(false)
140 { 135 {
141 } 136 }
142 137
143 ElementShadow::~ElementShadow() 138 ElementShadowV0::~ElementShadowV0()
144 { 139 {
145 } 140 }
146 141
147 ShadowRoot& ElementShadow::youngestShadowRoot() const 142 ShadowRoot& ElementShadowV0::youngestShadowRoot() const
148 { 143 {
149 ShadowRoot* current = m_shadowRoot; 144 return m_elementShadow->youngestShadowRoot();
150 DCHECK(current);
151 while (current->youngerShadowRoot())
152 current = current->youngerShadowRoot();
153 return *current;
154 } 145 }
155 146
156 ShadowRoot& ElementShadow::addShadowRoot(Element& shadowHost, ShadowRootType typ e) 147 ShadowRoot& ElementShadowV0::oldestShadowRoot() const
157 { 148 {
158 EventDispatchForbiddenScope assertNoEventDispatch; 149 return m_elementShadow->oldestShadowRoot();
159 ScriptForbiddenScope forbidScript;
160
161 if (type == ShadowRootType::V0 && m_shadowRoot) {
162 DCHECK_EQ(m_shadowRoot->type(), ShadowRootType::V0);
163 Deprecation::countDeprecation(shadowHost.document(), UseCounter::Element CreateShadowRootMultiple);
164 }
165
166 if (m_shadowRoot) {
167 // TODO(hayato): Is the order, from the youngest to the oldest, importan t?
168 for (ShadowRoot* root = &youngestShadowRoot(); root; root = root->olderS hadowRoot())
169 root->lazyReattachIfAttached();
170 }
171
172 ShadowRoot* shadowRoot = ShadowRoot::create(shadowHost.document(), type);
173 shadowRoot->setParentOrShadowHostNode(&shadowHost);
174 shadowRoot->setParentTreeScope(shadowHost.treeScope());
175 appendShadowRoot(*shadowRoot);
176 setNeedsDistributionRecalc();
177
178 shadowRoot->insertedInto(&shadowHost);
179 shadowHost.setChildNeedsStyleRecalc();
180 shadowHost.setNeedsStyleRecalc(SubtreeStyleChange, StyleChangeReasonForTraci ng::create(StyleChangeReason::Shadow));
181
182 InspectorInstrumentation::didPushShadowRoot(&shadowHost, shadowRoot);
183
184 return *shadowRoot;
185 } 150 }
186 151
187 void ElementShadow::appendShadowRoot(ShadowRoot& shadowRoot) 152 const InsertionPoint* ElementShadowV0::finalDestinationInsertionPointFor(const N ode* key) const
188 { 153 {
189 if (!m_shadowRoot) {
190 m_shadowRoot = &shadowRoot;
191 return;
192 }
193 ShadowRoot& youngest = youngestShadowRoot();
194 DCHECK(shadowRoot.type() == ShadowRootType::V0);
195 DCHECK(youngest.type() == ShadowRootType::V0);
196 youngest.setYoungerShadowRoot(shadowRoot);
197 shadowRoot.setOlderShadowRoot(youngest);
198 }
199
200 void ElementShadow::attach(const Node::AttachContext& context)
201 {
202 Node::AttachContext childrenContext(context);
203 childrenContext.resolvedStyle = 0;
204
205 for (ShadowRoot* root = &youngestShadowRoot(); root; root = root->olderShado wRoot()) {
206 if (root->needsAttach())
207 root->attachLayoutTree(childrenContext);
208 }
209 }
210
211 void ElementShadow::detach(const Node::AttachContext& context)
212 {
213 Node::AttachContext childrenContext(context);
214 childrenContext.resolvedStyle = 0;
215
216 for (ShadowRoot* root = &youngestShadowRoot(); root; root = root->olderShado wRoot())
217 root->detachLayoutTree(childrenContext);
218 }
219
220 void ElementShadow::setNeedsDistributionRecalc()
221 {
222 if (m_needsDistributionRecalc)
223 return;
224 m_needsDistributionRecalc = true;
225 host().markAncestorsWithChildNeedsDistributionRecalc();
226 if (!isV1())
227 clearDistributionV0();
228 }
229
230 bool ElementShadow::hasSameStyles(const ElementShadow* other) const
231 {
232 ShadowRoot* root = &youngestShadowRoot();
233 ShadowRoot* otherRoot = &other->youngestShadowRoot();
234 while (root || otherRoot) {
235 if (!root || !otherRoot)
236 return false;
237
238 StyleSheetList& list = root->styleSheets();
239 StyleSheetList& otherList = otherRoot->styleSheets();
240
241 if (list.length() != otherList.length())
242 return false;
243
244 for (size_t i = 0; i < list.length(); i++) {
245 if (toCSSStyleSheet(list.item(i))->contents() != toCSSStyleSheet(oth erList.item(i))->contents())
246 return false;
247 }
248 root = root->olderShadowRoot();
249 otherRoot = otherRoot->olderShadowRoot();
250 }
251
252 return true;
253 }
254
255 const InsertionPoint* ElementShadow::finalDestinationInsertionPointFor(const Nod e* key) const
256 {
257 DCHECK(!isV1());
258 DCHECK(key); 154 DCHECK(key);
259 DCHECK(!key->needsDistributionRecalc()); 155 DCHECK(!key->needsDistributionRecalc());
260 NodeToDestinationInsertionPoints::const_iterator it = m_nodeToInsertionPoint s.find(key); 156 NodeToDestinationInsertionPoints::const_iterator it = m_nodeToInsertionPoint s.find(key);
261 return it == m_nodeToInsertionPoints.end() ? nullptr : it->value->last(); 157 return it == m_nodeToInsertionPoints.end() ? nullptr : it->value->last();
262 } 158 }
263 159
264 const DestinationInsertionPoints* ElementShadow::destinationInsertionPointsFor(c onst Node* key) const 160 const DestinationInsertionPoints* ElementShadowV0::destinationInsertionPointsFor (const Node* key) const
265 { 161 {
266 DCHECK(!isV1());
267 DCHECK(key); 162 DCHECK(key);
268 DCHECK(!key->needsDistributionRecalc()); 163 DCHECK(!key->needsDistributionRecalc());
269 NodeToDestinationInsertionPoints::const_iterator it = m_nodeToInsertionPoint s.find(key); 164 NodeToDestinationInsertionPoints::const_iterator it = m_nodeToInsertionPoint s.find(key);
270 return it == m_nodeToInsertionPoints.end() ? nullptr : it->value; 165 return it == m_nodeToInsertionPoints.end() ? nullptr : it->value;
271 } 166 }
272 167
273 void ElementShadow::distribute() 168 void ElementShadowV0::distribute()
274 { 169 {
275 if (isV1())
276 youngestShadowRoot().distributeV1();
277 else
278 distributeV0();
279 }
280
281 void ElementShadow::distributeV0()
282 {
283 DCHECK(!isV1());
284 HeapVector<Member<HTMLShadowElement>, 32> shadowInsertionPoints; 170 HeapVector<Member<HTMLShadowElement>, 32> shadowInsertionPoints;
285 DistributionPool pool(host()); 171 DistributionPool pool(m_elementShadow->host());
286 172
287 for (ShadowRoot* root = &youngestShadowRoot(); root; root = root->olderShado wRoot()) { 173 for (ShadowRoot* root = &youngestShadowRoot(); root; root = root->olderShado wRoot()) {
288 HTMLShadowElement* shadowInsertionPoint = 0; 174 HTMLShadowElement* shadowInsertionPoint = 0;
289 const HeapVector<Member<InsertionPoint>>& insertionPoints = root->descen dantInsertionPoints(); 175 const HeapVector<Member<InsertionPoint>>& insertionPoints = root->descen dantInsertionPoints();
290 for (size_t i = 0; i < insertionPoints.size(); ++i) { 176 for (size_t i = 0; i < insertionPoints.size(); ++i) {
291 InsertionPoint* point = insertionPoints[i]; 177 InsertionPoint* point = insertionPoints[i];
292 if (!point->isActive()) 178 if (!point->isActive())
293 continue; 179 continue;
294 if (isHTMLShadowElement(*point)) { 180 if (isHTMLShadowElement(*point)) {
295 DCHECK(!shadowInsertionPoint); 181 DCHECK(!shadowInsertionPoint);
(...skipping 16 matching lines...) Expand all
312 } else if (root->olderShadowRoot()->type() == root->type()) { 198 } else if (root->olderShadowRoot()->type() == root->type()) {
313 // Only allow reprojecting older shadow roots between the same type to 199 // Only allow reprojecting older shadow roots between the same type to
314 // disallow reprojecting UA elements into author shadows. 200 // disallow reprojecting UA elements into author shadows.
315 DistributionPool olderShadowRootPool(*root->olderShadowRoot()); 201 DistributionPool olderShadowRootPool(*root->olderShadowRoot());
316 olderShadowRootPool.distributeTo(shadowInsertionPoint, this); 202 olderShadowRootPool.distributeTo(shadowInsertionPoint, this);
317 root->olderShadowRoot()->setShadowInsertionPointOfYoungerShadowRoot( shadowInsertionPoint); 203 root->olderShadowRoot()->setShadowInsertionPointOfYoungerShadowRoot( shadowInsertionPoint);
318 } 204 }
319 if (ElementShadow* shadow = shadowWhereNodeCanBeDistributedForV0(*shadow InsertionPoint)) 205 if (ElementShadow* shadow = shadowWhereNodeCanBeDistributedForV0(*shadow InsertionPoint))
320 shadow->setNeedsDistributionRecalc(); 206 shadow->setNeedsDistributionRecalc();
321 } 207 }
322 InspectorInstrumentation::didPerformElementShadowDistribution(&host()); 208 InspectorInstrumentation::didPerformElementShadowDistribution(const_cast<Ele ment*>(&m_elementShadow->host()));
323 } 209 }
324 210
325 void ElementShadow::didDistributeNode(const Node* node, InsertionPoint* insertio nPoint) 211 void ElementShadowV0::didDistributeNode(const Node* node, InsertionPoint* insert ionPoint)
326 { 212 {
327 DCHECK(!isV1());
328 NodeToDestinationInsertionPoints::AddResult result = m_nodeToInsertionPoints .add(node, nullptr); 213 NodeToDestinationInsertionPoints::AddResult result = m_nodeToInsertionPoints .add(node, nullptr);
329 if (result.isNewEntry) 214 if (result.isNewEntry)
330 result.storedValue->value = new DestinationInsertionPoints; 215 result.storedValue->value = new DestinationInsertionPoints;
331 result.storedValue->value->append(insertionPoint); 216 result.storedValue->value->append(insertionPoint);
332 } 217 }
333 218
334 const SelectRuleFeatureSet& ElementShadow::ensureSelectFeatureSet() 219 const SelectRuleFeatureSet& ElementShadowV0::ensureSelectFeatureSet()
335 { 220 {
336 DCHECK(!isV1());
337 if (!m_needsSelectFeatureSet) 221 if (!m_needsSelectFeatureSet)
338 return m_selectFeatures; 222 return m_selectFeatures;
339 223
340 m_selectFeatures.clear(); 224 m_selectFeatures.clear();
341 for (ShadowRoot* root = &oldestShadowRoot(); root; root = root->youngerShado wRoot()) 225 for (const ShadowRoot* root = &oldestShadowRoot(); root; root = root->younge rShadowRoot())
342 collectSelectFeatureSetFrom(*root); 226 collectSelectFeatureSetFrom(*root);
343 m_needsSelectFeatureSet = false; 227 m_needsSelectFeatureSet = false;
344 return m_selectFeatures; 228 return m_selectFeatures;
345 } 229 }
346 230
347 void ElementShadow::collectSelectFeatureSetFrom(ShadowRoot& root) 231 void ElementShadowV0::collectSelectFeatureSetFrom(const ShadowRoot& root)
348 { 232 {
349 DCHECK(!isV1());
350 if (!root.containsShadowRoots() && !root.containsContentElements()) 233 if (!root.containsShadowRoots() && !root.containsContentElements())
351 return; 234 return;
352 235
353 for (Element& element : ElementTraversal::descendantsOf(root)) { 236 for (Element& element : ElementTraversal::descendantsOf(root)) {
354 if (ElementShadow* shadow = element.shadow()) 237 if (ElementShadow* shadow = element.shadow()) {
355 m_selectFeatures.add(shadow->ensureSelectFeatureSet()); 238 if (!shadow->isV1())
239 m_selectFeatures.add(shadow->v0().ensureSelectFeatureSet());
240 }
356 if (!isHTMLContentElement(element)) 241 if (!isHTMLContentElement(element))
357 continue; 242 continue;
358 const CSSSelectorList& list = toHTMLContentElement(element).selectorList (); 243 const CSSSelectorList& list = toHTMLContentElement(element).selectorList ();
359 m_selectFeatures.collectFeaturesFromSelectorList(list); 244 m_selectFeatures.collectFeaturesFromSelectorList(list);
360 } 245 }
361 } 246 }
362 247
363 void ElementShadow::willAffectSelector() 248 void ElementShadowV0::willAffectSelector()
364 { 249 {
365 DCHECK(!isV1()); 250 for (ElementShadow* shadow = m_elementShadow; shadow; shadow = shadow->conta iningShadow()) {
366 for (ElementShadow* shadow = this; shadow; shadow = shadow->containingShadow ()) { 251 if (shadow->isV1() || shadow->v0().needsSelectFeatureSet())
367 if (shadow->needsSelectFeatureSet())
368 break; 252 break;
369 shadow->setNeedsSelectFeatureSet(); 253 shadow->v0().setNeedsSelectFeatureSet();
370 } 254 }
371 setNeedsDistributionRecalc(); 255 m_elementShadow->setNeedsDistributionRecalc();
372 } 256 }
373 257
374 void ElementShadow::clearDistributionV0() 258 void ElementShadowV0::clearDistribution()
375 { 259 {
376 DCHECK(!isV1());
377 m_nodeToInsertionPoints.clear(); 260 m_nodeToInsertionPoints.clear();
378 261
379 for (ShadowRoot* root = &youngestShadowRoot(); root; root = root->olderShado wRoot()) 262 for (ShadowRoot* root = &m_elementShadow->youngestShadowRoot(); root; root = root->olderShadowRoot())
380 root->setShadowInsertionPointOfYoungerShadowRoot(nullptr); 263 root->setShadowInsertionPointOfYoungerShadowRoot(nullptr);
381 } 264 }
382 265
383 DEFINE_TRACE(ElementShadow) 266 DEFINE_TRACE(ElementShadowV0)
384 { 267 {
268 visitor->trace(m_elementShadow);
385 visitor->trace(m_nodeToInsertionPoints); 269 visitor->trace(m_nodeToInsertionPoints);
386 visitor->trace(m_selectFeatures); 270 visitor->trace(m_selectFeatures);
387 visitor->trace(m_shadowRoot);
388 } 271 }
389 272
390 DEFINE_TRACE_WRAPPERS(ElementShadow) 273 DEFINE_TRACE_WRAPPERS(ElementShadowV0)
391 { 274 {
392 visitor->traceWrappers(m_shadowRoot);
393 } 275 }
394 276
395 } // namespace blink 277 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698