Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 12 matching lines...) Expand all Loading... | |
| 83 void extractClassIdOrTag(const CSSSelector& selector, HashSet<AtomicString>& cla sses, AtomicString& id, AtomicString& tagName) | 88 void extractClassIdOrTag(const CSSSelector& selector, HashSet<AtomicString>& cla sses, AtomicString& id, AtomicString& tagName) |
| 84 { | 89 { |
| 85 if (selector.m_match == CSSSelector::Tag) | 90 if (selector.m_match == CSSSelector::Tag) |
| 86 tagName = selector.tagQName().localName(); | 91 tagName = selector.tagQName().localName(); |
| 87 else if (selector.m_match == CSSSelector::Id) | 92 else if (selector.m_match == CSSSelector::Id) |
| 88 id = selector.value(); | 93 id = selector.value(); |
| 89 else if (selector.m_match == CSSSelector::Class) | 94 else if (selector.m_match == CSSSelector::Class) |
| 90 classes.add(selector.value()); | 95 classes.add(selector.value()); |
| 91 } | 96 } |
| 92 | 97 |
| 98 RuleFeatureSet::RuleFeatureSet() | |
| 99 { | |
| 100 m_targetedStyleRecalcEnabled = RuntimeEnabledFeatures::targetedStyleRecalcEn abled(); | |
|
esprehn
2014/01/30 05:09:24
This should be in the initializer list.
RuleFeatu
chrishtr
2014/01/30 17:28:15
Done.
| |
| 101 } | |
| 102 | |
| 93 bool RuleFeatureSet::updateClassInvalidationSets(const CSSSelector* selector) | 103 bool RuleFeatureSet::updateClassInvalidationSets(const CSSSelector* selector) |
| 94 { | 104 { |
| 95 if (!selector) | 105 if (!selector) |
| 96 return false; | 106 return false; |
| 97 if (!supportsClassDescendantInvalidation(selector)) | 107 if (!supportsClassDescendantInvalidation(selector)) |
| 98 return false; | 108 return false; |
| 99 | 109 |
| 100 HashSet<AtomicString> classes; | 110 HashSet<AtomicString> classes; |
| 101 AtomicString id; | 111 AtomicString id; |
| 102 AtomicString tagName; | 112 AtomicString tagName; |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 120 } | 130 } |
| 121 } | 131 } |
| 122 return true; | 132 return true; |
| 123 } | 133 } |
| 124 | 134 |
| 125 void RuleFeatureSet::addAttributeInASelector(const AtomicString& attributeName) | 135 void RuleFeatureSet::addAttributeInASelector(const AtomicString& attributeName) |
| 126 { | 136 { |
| 127 m_metadata.attrsInRules.add(attributeName); | 137 m_metadata.attrsInRules.add(attributeName); |
| 128 } | 138 } |
| 129 | 139 |
| 130 | |
| 131 void RuleFeatureSet::collectFeaturesFromRuleData(const RuleData& ruleData) | 140 void RuleFeatureSet::collectFeaturesFromRuleData(const RuleData& ruleData) |
| 132 { | 141 { |
| 133 | |
| 134 FeatureMetadata metadata; | 142 FeatureMetadata metadata; |
| 135 collectFeaturesFromSelector(ruleData.selector(), metadata); | 143 collectFeaturesFromSelector(ruleData.selector(), metadata); |
| 136 m_metadata.add(metadata); | 144 m_metadata.add(metadata); |
| 137 if (RuntimeEnabledFeatures::targetedStyleRecalcEnabled()) { | 145 if (m_targetedStyleRecalcEnabled) { |
| 138 bool selectorUsesClassInvalidationSet = updateClassInvalidationSets(rule Data.selector()); | 146 bool selectorUsesClassInvalidationSet = updateClassInvalidationSets(rule Data.selector()); |
| 139 if (!selectorUsesClassInvalidationSet) { | 147 if (!selectorUsesClassInvalidationSet) { |
| 140 for (HashSet<AtomicString>::const_iterator it = metadata.classesInRu les.begin(); it != metadata.classesInRules.end(); ++it) { | 148 for (HashSet<AtomicString>::const_iterator it = metadata.classesInRu les.begin(); it != metadata.classesInRules.end(); ++it) { |
| 141 DescendantInvalidationSet& invalidationSet = ensureClassInvalida tionSet(*it); | 149 DescendantInvalidationSet& invalidationSet = ensureClassInvalida tionSet(*it); |
| 142 invalidationSet.setWholeSubtreeInvalid(); | 150 invalidationSet.setWholeSubtreeInvalid(); |
| 143 } | 151 } |
| 144 } | 152 } |
| 145 } | 153 } |
| 154 | |
| 146 if (metadata.foundSiblingSelector) | 155 if (metadata.foundSiblingSelector) |
| 147 siblingRules.append(RuleFeature(ruleData.rule(), ruleData.selectorIndex( ), ruleData.hasDocumentSecurityOrigin())); | 156 siblingRules.append(RuleFeature(ruleData.rule(), ruleData.selectorIndex( ), ruleData.hasDocumentSecurityOrigin())); |
| 148 if (ruleData.containsUncommonAttributeSelector()) | 157 if (ruleData.containsUncommonAttributeSelector()) |
| 149 uncommonAttributeRules.append(RuleFeature(ruleData.rule(), ruleData.sele ctorIndex(), ruleData.hasDocumentSecurityOrigin())); | 158 uncommonAttributeRules.append(RuleFeature(ruleData.rule(), ruleData.sele ctorIndex(), ruleData.hasDocumentSecurityOrigin())); |
| 150 } | 159 } |
| 151 | 160 |
| 152 DescendantInvalidationSet& RuleFeatureSet::ensureClassInvalidationSet(const Atom icString& className) | 161 DescendantInvalidationSet& RuleFeatureSet::ensureClassInvalidationSet(const Atom icString& className) |
| 153 { | 162 { |
| 154 InvalidationSetMap::AddResult addResult = m_classInvalidationSets.add(classN ame, 0); | 163 InvalidationSetMap::AddResult addResult = m_classInvalidationSets.add(classN ame, 0); |
| 155 if (addResult.isNewEntry) | 164 if (addResult.isNewEntry) |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 233 uncommonAttributeRules.append(other.uncommonAttributeRules); | 242 uncommonAttributeRules.append(other.uncommonAttributeRules); |
| 234 } | 243 } |
| 235 | 244 |
| 236 void RuleFeatureSet::clear() | 245 void RuleFeatureSet::clear() |
| 237 { | 246 { |
| 238 m_metadata.clear(); | 247 m_metadata.clear(); |
| 239 siblingRules.clear(); | 248 siblingRules.clear(); |
| 240 uncommonAttributeRules.clear(); | 249 uncommonAttributeRules.clear(); |
| 241 } | 250 } |
| 242 | 251 |
| 252 void RuleFeatureSet::scheduleStyleInvalidationForClassChange(const SpaceSplitStr ing& changedClasses, Element* element) | |
| 253 { | |
| 254 if (scheduleStyleInvalidationForClassChangeInternal(changedClasses, element) | |
| 255 && !m_targetedStyleRecalcEnabled) | |
|
esprehn
2014/01/30 05:09:24
I wouldn't wrap these.
chrishtr
2014/01/30 17:28:15
Done.
| |
| 256 element->setNeedsStyleRecalc(); | |
| 257 } | |
| 258 | |
| 259 void RuleFeatureSet::scheduleStyleInvalidationForClassChange(const SpaceSplitStr ing& oldClasses, const SpaceSplitString& newClasses, Element* element) | |
| 260 { | |
| 261 if (scheduleStyleInvalidationForClassChangeInternal(oldClasses, newClasses, element) | |
| 262 && !m_targetedStyleRecalcEnabled) | |
| 263 element->setNeedsStyleRecalc(); | |
| 264 } | |
| 265 | |
| 266 bool RuleFeatureSet::scheduleStyleInvalidationForClassChangeInternal(const Space SplitString& changedClasses, Element* element) | |
| 267 { | |
| 268 unsigned changedSize = changedClasses.size(); | |
| 269 for (unsigned i = 0; i < changedSize; ++i) { | |
| 270 if (m_targetedStyleRecalcEnabled) | |
| 271 addClassToInvalidationSet(changedClasses[i], element); | |
| 272 else if (m_metadata.classesInRules.contains(changedClasses[i])) | |
| 273 return true; | |
| 274 } | |
| 275 return false; | |
| 276 } | |
| 277 | |
| 278 bool RuleFeatureSet::scheduleStyleInvalidationForClassChangeInternal(const Space SplitString& oldClasses, const SpaceSplitString& newClasses, Element* element) | |
|
esprehn
2014/01/30 05:09:24
Having two methods with the same name like this is
chrishtr
2014/01/30 17:28:15
Done.
| |
| 279 { | |
| 280 if (!oldClasses.size()) | |
| 281 return scheduleStyleInvalidationForClassChangeInternal(newClasses, eleme nt); | |
| 282 | |
| 283 // Class vectors tend to be very short. This is faster than using a hash tab le. | |
| 284 BitVector remainingClassBits; | |
| 285 remainingClassBits.ensureSize(oldClasses.size()); | |
| 286 | |
| 287 for (unsigned i = 0; i < newClasses.size(); ++i) { | |
| 288 bool found = false; | |
| 289 for (unsigned j = 0; j < oldClasses.size(); ++j) { | |
| 290 if (newClasses[i] == oldClasses[j]) { | |
| 291 // Mark each class that is still in the newClasses so we can ski p doing | |
| 292 // an n^2 search below when looking for removals. We can't break from | |
| 293 // this loop early since a class can appear more than once. | |
| 294 remainingClassBits.quickSet(j); | |
| 295 found = true; | |
| 296 } | |
| 297 } | |
| 298 // Class was added. | |
| 299 if (!found) { | |
| 300 if (m_targetedStyleRecalcEnabled) | |
| 301 addClassToInvalidationSet(newClasses[i], element); | |
| 302 else if (m_metadata.classesInRules.contains(newClasses[i])) | |
| 303 return true; | |
| 304 } | |
| 305 } | |
| 306 | |
| 307 for (unsigned i = 0; i < oldClasses.size(); ++i) { | |
| 308 if (remainingClassBits.quickGet(i)) | |
| 309 continue; | |
| 310 | |
| 311 // Class was removed. | |
| 312 if (m_targetedStyleRecalcEnabled) | |
| 313 addClassToInvalidationSet(oldClasses[i], element); | |
| 314 else if (m_metadata.classesInRules.contains(oldClasses[i])) | |
| 315 return true; | |
| 316 } | |
| 317 return false; | |
| 318 } | |
| 319 | |
| 320 void RuleFeatureSet::addClassToInvalidationSet(const AtomicString& className, El ement* element) | |
| 321 { | |
| 322 if (DescendantInvalidationSet* invalidationSet = m_classInvalidationSets.get (className)) { | |
| 323 ensurePendingInvalidationList(element).append(invalidationSet); | |
| 324 element->setNeedsStyleInvalidation(); | |
| 325 } | |
| 326 } | |
| 327 | |
| 328 RuleFeatureSet::InvalidationList& RuleFeatureSet::ensurePendingInvalidationList( Element* element) | |
| 329 { | |
| 330 PendingInvalidationMap::AddResult addResult = m_pendingInvalidationMap.add(e lement, 0); | |
| 331 if (addResult.isNewEntry) | |
| 332 addResult.iterator->value = new InvalidationList; | |
| 333 return *addResult.iterator->value; | |
| 334 } | |
| 335 | |
| 336 void RuleFeatureSet::computeStyleInvalidation(Document& document) | |
| 337 { | |
| 338 Vector<AtomicString> invalidationClasses; | |
| 339 for (Node* child = document.firstChild(); child; child = child->nextSibling( )) { | |
|
esprehn
2014/01/30 05:09:24
A document can only have one element child, you do
chrishtr
2014/01/30 17:28:15
Done.
| |
| 340 if (child->isElementNode() && child->childNeedsStyleInvalidation()) { | |
| 341 Element* childElement = toElement(child); | |
| 342 computeStyleInvalidationInternal(childElement, invalidationClasses, false); | |
|
esprehn
2014/01/30 05:09:24
I would name these methods invalidateStyleForClass
chrishtr
2014/01/30 17:28:15
Done.
| |
| 343 } | |
| 344 } | |
| 345 document.clearChildNeedsStyleInvalidation(); | |
| 346 m_pendingInvalidationMap.clear(); | |
| 347 } | |
| 348 | |
| 349 bool RuleFeatureSet::computeStyleInvalidationForChildren(Element* element, Vecto r<AtomicString>& invalidationClasses, bool foundInvalidationSet) | |
| 350 { | |
| 351 bool someChildrenNeedStyleRecalc = false; | |
| 352 for (Node* child = element->firstChild(); child; child = child->nextSibling( )) { | |
| 353 if (child->isElementNode()) { | |
| 354 Element* childElement = toElement(child); | |
| 355 bool childRecalced = computeStyleInvalidationInternal(childElement, invalidationClasses, foundInvalidationSet); | |
|
esprehn
2014/01/30 05:09:24
invalidateSubtree(...)
chrishtr
2014/01/30 17:28:15
Done.
chrishtr
2014/01/30 17:28:15
Done.
| |
| 356 someChildrenNeedStyleRecalc = someChildrenNeedStyleRecalc || childRe calced; | |
| 357 } | |
| 358 } | |
| 359 for (ShadowRoot* root = element->youngestShadowRoot(); root; root = root->ol derShadowRoot()) { | |
|
esprehn
2014/01/30 05:09:24
Usually we do ShadowRoots first, I'd swap these tw
chrishtr
2014/01/30 17:28:15
Done.
| |
| 360 for (Node* child = root->firstChild(); child; child = child->nextSibling ()) { | |
| 361 if (child->isElementNode()) { | |
| 362 Element* childElement = toElement(child); | |
| 363 bool childRecalced = computeStyleInvalidationInternal(childEleme nt, invalidationClasses, foundInvalidationSet); | |
| 364 someChildrenNeedStyleRecalc = someChildrenNeedStyleRecalc || chi ldRecalced; | |
| 365 } | |
| 366 } | |
| 367 } | |
| 368 return someChildrenNeedStyleRecalc; | |
| 369 } | |
| 370 | |
| 371 bool RuleFeatureSet::computeStyleInvalidationInternal(Element* element, Vector<A tomicString>& invalidationClasses, bool foundInvalidationSet) | |
| 372 { | |
| 373 int oldSize = invalidationClasses.size(); | |
| 374 if (element->needsStyleInvalidation()) { | |
| 375 if (InvalidationList* invalidationList = m_pendingInvalidationMap.get(el ement)) { | |
| 376 foundInvalidationSet = true; | |
| 377 bool recalcWholeSubtree = false; | |
| 378 for (InvalidationList::const_iterator it = invalidationList->begin() ; it != invalidationList->end(); ++it) { | |
| 379 if ((*it)->wholeSubtreeInvalid()) { | |
| 380 recalcWholeSubtree = true; | |
| 381 break; | |
| 382 } | |
| 383 (*it)->getClasses(invalidationClasses); | |
| 384 } | |
| 385 if (recalcWholeSubtree) { | |
| 386 element->setNeedsStyleRecalc(SubtreeStyleChange); | |
| 387 invalidationClasses.remove(oldSize, invalidationClasses.size() - oldSize); | |
| 388 element->clearChildNeedsStyleInvalidation(); | |
| 389 return true; | |
| 390 } | |
| 391 } | |
| 392 } | |
| 393 | |
| 394 bool thisElementNeedsStyleRecalc = false; | |
| 395 | |
| 396 if (element->hasClass()) { | |
| 397 const SpaceSplitString& classNames = element->classNames(); | |
| 398 for (Vector<AtomicString>::const_iterator it = invalidationClasses.begin (); it != invalidationClasses.end(); ++it) { | |
| 399 if (classNames.contains(*it)) { | |
| 400 thisElementNeedsStyleRecalc = true; | |
|
esprehn
2014/01/30 05:09:24
element->setNeedsSylRecalc(LocalStyleChange);
and
chrishtr
2014/01/30 17:28:15
I don't like that, as it will cause a bunch of red
ojan
2014/01/30 19:08:39
setNeedsStyleRecalc does the ancestor walk only on
chrishtr
2014/01/30 19:25:48
It's not the ancestor child-needs-recalc bit setti
ojan
2014/01/30 19:51:18
There's something I'm missing. If you just replace
chrishtr
2014/01/30 21:39:44
Yes. I can make that change if you want. The retur
ojan
2014/01/30 22:15:55
Ah. Didn't notice that we're returning this value.
chrishtr
2014/01/30 22:25:53
Ack.
| |
| 401 break; | |
| 402 } | |
| 403 } | |
| 404 } | |
| 405 | |
| 406 // foundInvalidationSet will be true if we are in a subtree of a node with a DescendantInvalidationSet on it. | |
| 407 // We need to check all nodes in the subtree of such a node. | |
| 408 if (foundInvalidationSet || element->childNeedsStyleInvalidation()) { | |
| 409 bool someChildrenNeedStyleRecalc = computeStyleInvalidationForChildren(e lement, invalidationClasses, foundInvalidationSet); | |
| 410 // We only need to possibly recalc style if this node is in the subtree of a node with a DescendantInvalidationSet on it. | |
| 411 if (foundInvalidationSet) | |
| 412 thisElementNeedsStyleRecalc = thisElementNeedsStyleRecalc || someChi ldrenNeedStyleRecalc; | |
|
esprehn
2014/01/30 05:09:24
Get rid of the bools, just call setNeedsStyleRecal
chrishtr
2014/01/30 17:28:15
See above.
| |
| 413 } | |
| 414 | |
| 415 if (thisElementNeedsStyleRecalc) | |
| 416 element->setNeedsStyleRecalc(LocalStyleChange); | |
|
esprehn
2014/01/30 05:09:24
Remove this. If you just mark yourself above and t
chrishtr
2014/01/30 17:28:15
See above.
| |
| 417 | |
| 418 invalidationClasses.remove(oldSize, invalidationClasses.size() - oldSize); | |
| 419 element->clearChildNeedsStyleInvalidation(); | |
| 420 return thisElementNeedsStyleRecalc; | |
| 421 } | |
| 422 | |
| 243 } // namespace WebCore | 423 } // namespace WebCore |
| OLD | NEW |