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

Side by Side Diff: Source/core/css/RuleFeature.cpp

Issue 143873016: Implement style invalidation tree walk for targeted style recalc upon class change. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Addressed comments. Created 6 years, 11 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 | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 2004-2005 Allan Sandfeld Jensen (kde@carewolf.com) 3 * (C) 2004-2005 Allan Sandfeld Jensen (kde@carewolf.com)
4 * Copyright (C) 2006, 2007 Nicholas Shanks (webkit@nickshanks.com) 4 * Copyright (C) 2006, 2007 Nicholas Shanks (webkit@nickshanks.com)
5 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All r ights reserved. 5 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All r ights reserved.
6 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org> 6 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
7 * Copyright (C) 2007, 2008 Eric Seidel <eric@webkit.org> 7 * Copyright (C) 2007, 2008 Eric Seidel <eric@webkit.org>
8 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.t orchmobile.com/) 8 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.t orchmobile.com/)
9 * Copyright (c) 2011, Code Aurora Forum. All rights reserved. 9 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
10 * Copyright (C) Research In Motion Limited 2011. All rights reserved. 10 * Copyright (C) Research In Motion Limited 2011. All rights reserved.
(...skipping 16 matching lines...) Expand all
27 */ 27 */
28 28
29 #include "config.h" 29 #include "config.h"
30 #include "core/css/RuleFeature.h" 30 #include "core/css/RuleFeature.h"
31 31
32 #include "HTMLNames.h" 32 #include "HTMLNames.h"
33 #include "RuntimeEnabledFeatures.h" 33 #include "RuntimeEnabledFeatures.h"
34 #include "core/css/CSSSelector.h" 34 #include "core/css/CSSSelector.h"
35 #include "core/css/CSSSelectorList.h" 35 #include "core/css/CSSSelectorList.h"
36 #include "core/css/RuleSet.h" 36 #include "core/css/RuleSet.h"
37 #include "core/dom/Document.h"
38 #include "core/dom/Element.h"
39 #include "core/dom/Node.h"
40 #include "core/dom/shadow/ElementShadow.h"
41 #include "core/dom/shadow/ShadowRoot.h"
42 #include "wtf/BitVector.h"
37 43
38 namespace WebCore { 44 namespace WebCore {
39 45
40 static bool isSkippableComponentForInvalidation(const CSSSelector* selector) 46 static bool isSkippableComponentForInvalidation(const CSSSelector* selector)
41 { 47 {
42 if (selector->matchesPseudoElement() || selector->pseudoType() == CSSSelecto r::PseudoHost) 48 if (selector->matchesPseudoElement() || selector->pseudoType() == CSSSelecto r::PseudoHost)
43 return false; 49 return false;
44 return true; 50 return true;
45 } 51 }
46 52
47 // This method is somewhat conservative in what it acceptss. 53 // This method is somewhat conservative in what it acceptss.
48 static bool supportsClassDescendantInvalidation(const CSSSelector* selector) 54 static bool supportsClassDescendantInvalidation(const CSSSelector* selector)
49 { 55 {
50 bool foundDescendantRelation = false; 56 bool foundDescendantRelation = false;
51 bool foundAncestorIdent = false; 57 bool foundAncestorIdent = false;
52 bool foundIdent = false; 58 bool foundIdent = false;
53 for (const CSSSelector* component = selector; component; component = compone nt->tagHistory()) { 59 for (const CSSSelector* component = selector; component; component = compone nt->tagHistory()) {
54 60
55 // FIXME: We should allow pseudo elements, but we need to change how the y hook 61 // FIXME: We should allow pseudo elements, but we need to change how the y hook
56 // into recalcStyle by moving them to recalcOwnStyle instead of recalcCh ildStyle. 62 // into recalcStyle by moving them to recalcOwnStyle instead of recalcCh ildStyle.
57 63
58 if (component->m_match == CSSSelector::Tag 64 // FIXME: next up: Tag and Id.
59 || component->m_match == CSSSelector::Id 65 if (component->m_match == CSSSelector::Class) {
60 || component->m_match == CSSSelector::Class) {
61 if (!foundDescendantRelation) 66 if (!foundDescendantRelation)
62 foundIdent = true; 67 foundIdent = true;
63 else 68 else
64 foundAncestorIdent = true; 69 foundAncestorIdent = true;
65 } else if (!isSkippableComponentForInvalidation(component)) { 70 } else if (!isSkippableComponentForInvalidation(component)) {
66 return false; 71 return false;
67 } 72 }
68 // FIXME: We can probably support ChildTree and DescendantTree. 73 // FIXME: We can probably support ChildTree and DescendantTree.
69 switch (component->relation()) { 74 switch (component->relation()) {
70 case CSSSelector::Descendant: 75 case CSSSelector::Descendant:
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 uncommonAttributeRules.append(other.uncommonAttributeRules); 238 uncommonAttributeRules.append(other.uncommonAttributeRules);
234 } 239 }
235 240
236 void RuleFeatureSet::clear() 241 void RuleFeatureSet::clear()
237 { 242 {
238 m_metadata.clear(); 243 m_metadata.clear();
239 siblingRules.clear(); 244 siblingRules.clear();
240 uncommonAttributeRules.clear(); 245 uncommonAttributeRules.clear();
241 } 246 }
242 247
248 void RuleFeatureSet::scheduleStyleInvalidationForClassChange(const SpaceSplitStr ing& changedClasses, Element* element)
249 {
250 if (scheduleStyleInvalidationForClassChangeInternal(changedClasses, element)
251 && !RuntimeEnabledFeatures::targetedStyleRecalcEnabled())
252 element->setNeedsStyleRecalc();
253 }
254
255 void RuleFeatureSet::scheduleStyleInvalidationForClassChange(const SpaceSplitStr ing& oldClasses, const SpaceSplitString& newClasses, Element* element)
256 {
257 if (scheduleStyleInvalidationForClassChangeInternal(oldClasses, newClasses, element)
258 && !RuntimeEnabledFeatures::targetedStyleRecalcEnabled())
259 element->setNeedsStyleRecalc();
260 }
261
262 bool RuleFeatureSet::scheduleStyleInvalidationForClassChangeInternal(const Space SplitString& changedClasses, Element* element)
263 {
264 unsigned changedSize = changedClasses.size();
265 for (unsigned i = 0; i < changedSize; ++i) {
266 if (RuntimeEnabledFeatures::targetedStyleRecalcEnabled())
267 addClassToInvalidationSet(changedClasses[i], element);
268 else if (m_metadata.classesInRules.contains(changedClasses[i]))
269 return true;
270 }
271 return false;
272 }
273
274 bool RuleFeatureSet::scheduleStyleInvalidationForClassChangeInternal(const Space SplitString& oldClasses, const SpaceSplitString& newClasses, Element* element)
275 {
276 if (!oldClasses.size())
277 return scheduleStyleInvalidationForClassChangeInternal(newClasses, eleme nt);
278
279 // Class vectors tend to be very short. This is faster than using a hash tab le.
280 BitVector remainingClassBits;
281 remainingClassBits.ensureSize(oldClasses.size());
282
283 for (unsigned i = 0; i < newClasses.size(); ++i) {
284 bool found = false;
285 for (unsigned j = 0; j < oldClasses.size(); ++j) {
286 if (newClasses[i] == oldClasses[j]) {
287 // Mark each class that is still in the newClasses so we can ski p doing
288 // an n^2 search below when looking for removals. We can't break from
289 // this loop early since a class can appear more than once.
290 remainingClassBits.quickSet(j);
291 found = true;
292 }
293 }
294 // Class was added.
295 if (!found) {
296 if (RuntimeEnabledFeatures::targetedStyleRecalcEnabled())
297 addClassToInvalidationSet(newClasses[i], element);
298 else if (m_metadata.classesInRules.contains(newClasses[i]))
299 return true;
300 }
301 }
302
303 for (unsigned i = 0; i < oldClasses.size(); ++i) {
304 if (remainingClassBits.quickGet(i))
305 continue;
306
307 // Class was removed.
308 if (RuntimeEnabledFeatures::targetedStyleRecalcEnabled())
309 addClassToInvalidationSet(oldClasses[i], element);
310 else if (m_metadata.classesInRules.contains(oldClasses[i]))
311 return true;
312 }
313 return false;
314 }
315
316 void RuleFeatureSet::addClassToInvalidationSet(const AtomicString& className, El ement* element)
317 {
318 if (DescendantInvalidationSet* invalidationSet = m_classInvalidationSets.get (className)) {
319 ensurePendingInvalidationList(element).append(invalidationSet);
320 element->setNeedsStyleInvalidation();
321 }
322 }
323
324 RuleFeatureSet::InvalidationList& RuleFeatureSet::ensurePendingInvalidationList( Element* element)
325 {
326 PendingInvalidationMap::AddResult addResult = m_pendingInvalidationMap.add(e lement, 0);
327 if (addResult.isNewEntry)
328 addResult.iterator->value = new InvalidationList;
329 return *addResult.iterator->value;
330 }
331
332 void RuleFeatureSet::computeStyleInvalidation(Document& document)
333 {
334 Vector<AtomicString> invalidationClasses;
335 for (Node* child = document.firstChild(); child; child = child->nextSibling( )) {
336 if (child->isElementNode() && child->childNeedsStyleInvalidation()) {
337 Element* childElement = toElement(child);
338 computeStyleInvalidationInternal(childElement, invalidationClasses, false);
339 }
340 }
341 document.clearChildNeedsStyleInvalidation();
342 m_pendingInvalidationMap.clear();
343 }
344
345 bool RuleFeatureSet::computeStyleInvalidationForChildren(Element* element, Vecto r<AtomicString>& invalidationClasses, bool foundInvalidationSet)
346 {
347 bool someChildrenNeedStyleRecalc = false;
348 for (Node* child = element->firstChild(); child; child = child->nextSibling( )) {
349 if (child->isElementNode()) {
350 Element* childElement = toElement(child);
351 bool childRecalced = computeStyleInvalidationInternal(childElement, invalidationClasses, foundInvalidationSet);
352 someChildrenNeedStyleRecalc = someChildrenNeedStyleRecalc || childRe calced;
353 }
354 }
355 for (ShadowRoot* root = element->youngestShadowRoot(); root; root = root->ol derShadowRoot()) {
356 for (Node* child = root->firstChild(); child; child = child->nextSibling ()) {
357 if (child->isElementNode()) {
358 Element* childElement = toElement(child);
359 bool childRecalced = computeStyleInvalidationInternal(childEleme nt, invalidationClasses, foundInvalidationSet);
360 someChildrenNeedStyleRecalc = someChildrenNeedStyleRecalc || chi ldRecalced;
361 }
362 }
363 }
364 return someChildrenNeedStyleRecalc;
365 }
366
367 bool RuleFeatureSet::computeStyleInvalidationInternal(Element* element, Vector<A tomicString>& invalidationClasses, bool foundInvalidationSet)
368 {
369 int oldSize = invalidationClasses.size();
370 if (element->needsStyleInvalidation()) {
371 if (InvalidationList* invalidationList = m_pendingInvalidationMap.get(el ement)) {
372 foundInvalidationSet = true;
373 bool recalcWholeSubtree = false;
374 for (InvalidationList::const_iterator it = invalidationList->begin() ; it != invalidationList->end(); ++it) {
375 if ((*it)->wholeSubtreeInvalid()) {
376 recalcWholeSubtree = true;
377 break;
378 }
379 (*it)->getClasses(invalidationClasses);
380 }
381 if (recalcWholeSubtree) {
382 element->setNeedsStyleRecalc(SubtreeStyleChange);
383 invalidationClasses.remove(oldSize, invalidationClasses.size() - oldSize);
384 element->clearChildNeedsStyleInvalidation();
385 return true;
386 }
387 }
388 }
389
390 bool thisElementNeedsStyleRecalc = false;
391
392 if (element->hasClass()) {
393 const SpaceSplitString& classNames = element->classNames();
394 for (Vector<AtomicString>::const_iterator it = invalidationClasses.begin (); it != invalidationClasses.end(); ++it) {
395 if (classNames.contains(*it)) {
396 thisElementNeedsStyleRecalc = true;
397 break;
398 }
399 }
400 }
401
402 // foundInvalidationSet will be true if we are in a subtree of a node with a DescendantInvalidationSet on it.
403 // We need to check all nodes in the subtree of such a node.
404 if (foundInvalidationSet || element->childNeedsStyleInvalidation()) {
405 bool someChildrenNeedStyleRecalc = computeStyleInvalidationForChildren(e lement, invalidationClasses, foundInvalidationSet);
406 // We only need to possibly recalc style if this node is in the subtree of a node with a DescendantInvalidationSet on it.
407 if (foundInvalidationSet)
408 thisElementNeedsStyleRecalc = thisElementNeedsStyleRecalc || someChi ldrenNeedStyleRecalc;
409 }
410
411 if (thisElementNeedsStyleRecalc)
412 element->setNeedsStyleRecalc(LocalStyleChange);
413
414 invalidationClasses.remove(oldSize, invalidationClasses.size() - oldSize);
415 element->clearChildNeedsStyleInvalidation();
416 return thisElementNeedsStyleRecalc;
417 }
418
243 } // namespace WebCore 419 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698