| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2012 Apple 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 | |
| 6 * are met: | |
| 7 * 1. Redistributions of source code must retain the above copyright | |
| 8 * notice, this list of conditions and the following disclaimer. | |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | |
| 10 * notice, this list of conditions and the following disclaimer in the | |
| 11 * documentation and/or other materials provided with the distribution. | |
| 12 * | |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | |
| 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | |
| 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
| 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 24 */ | |
| 25 | |
| 26 #include "sky/engine/config.h" | |
| 27 #include "sky/engine/core/css/invalidation/StyleSheetInvalidationAnalysis.h" | |
| 28 | |
| 29 #include "sky/engine/core/css/CSSSelectorList.h" | |
| 30 #include "sky/engine/core/css/StyleSheetContents.h" | |
| 31 #include "sky/engine/core/dom/ContainerNode.h" | |
| 32 #include "sky/engine/core/dom/Document.h" | |
| 33 #include "sky/engine/core/dom/ElementTraversal.h" | |
| 34 #include "sky/engine/core/dom/shadow/ShadowRoot.h" | |
| 35 #include "sky/engine/core/html/HTMLStyleElement.h" | |
| 36 | |
| 37 namespace blink { | |
| 38 | |
| 39 StyleSheetInvalidationAnalysis::StyleSheetInvalidationAnalysis(const Vector<RawP
tr<StyleSheetContents> >& sheets) | |
| 40 : m_dirtiesAllStyle(false) | |
| 41 { | |
| 42 for (unsigned i = 0; i < sheets.size() && !m_dirtiesAllStyle; ++i) | |
| 43 analyzeStyleSheet(sheets[i]); | |
| 44 } | |
| 45 | |
| 46 static bool determineSelectorScopes(const CSSSelectorList& selectorList, HashSet
<StringImpl*>& idScopes, HashSet<StringImpl*>& classScopes) | |
| 47 { | |
| 48 for (const CSSSelector* selector = selectorList.first(); selector; selector
= CSSSelectorList::next(*selector)) { | |
| 49 const CSSSelector* scopeSelector = 0; | |
| 50 // This picks the widest scope, not the narrowest, to minimize the numbe
r of found scopes. | |
| 51 for (const CSSSelector* current = selector; current; current = current->
tagHistory()) { | |
| 52 // Prefer ids over classes. | |
| 53 if (current->match() == CSSSelector::Id) | |
| 54 scopeSelector = current; | |
| 55 else if (current->match() == CSSSelector::Class && (!scopeSelector |
| scopeSelector->match() != CSSSelector::Id)) | |
| 56 scopeSelector = current; | |
| 57 } | |
| 58 if (!scopeSelector) | |
| 59 return false; | |
| 60 ASSERT(scopeSelector->match() == CSSSelector::Class || scopeSelector->ma
tch() == CSSSelector::Id); | |
| 61 if (scopeSelector->match() == CSSSelector::Id) | |
| 62 idScopes.add(scopeSelector->value().impl()); | |
| 63 else | |
| 64 classScopes.add(scopeSelector->value().impl()); | |
| 65 } | |
| 66 return true; | |
| 67 } | |
| 68 | |
| 69 static Node* determineScopingNodeForStyleInShadow(HTMLStyleElement* ownerElement
, StyleSheetContents* styleSheetContents) | |
| 70 { | |
| 71 ASSERT(ownerElement && ownerElement->isInShadowTree()); | |
| 72 return ownerElement->containingShadowRoot()->shadowHost(); | |
| 73 } | |
| 74 | |
| 75 static bool ruleAdditionMightRequireDocumentStyleRecalc(StyleRuleBase* rule) | |
| 76 { | |
| 77 // This funciton is conservative. We only return false when we know that | |
| 78 // the added @rule can't require style recalcs. | |
| 79 switch (rule->type()) { | |
| 80 case StyleRule::Keyframes: // Keyframes never cause style invalidations and
are handled during sheet insertion. | |
| 81 return false; | |
| 82 | |
| 83 case StyleRule::Media: // If the media rule doesn't apply, we could avoid re
calc. | |
| 84 case StyleRule::FontFace: // If the fonts aren't in use, we could avoid reca
lc. | |
| 85 case StyleRule::Supports: // If we evaluated the supports-clause we could av
oid recalc. | |
| 86 // FIXME: Unclear if any of the rest need to cause style recalc: | |
| 87 case StyleRule::Filter: | |
| 88 return true; | |
| 89 | |
| 90 // These should all be impossible to reach: | |
| 91 case StyleRule::Unknown: | |
| 92 case StyleRule::Keyframe: | |
| 93 case StyleRule::Style: | |
| 94 break; | |
| 95 } | |
| 96 ASSERT_NOT_REACHED(); | |
| 97 return true; | |
| 98 } | |
| 99 | |
| 100 void StyleSheetInvalidationAnalysis::analyzeStyleSheet(StyleSheetContents* style
SheetContents) | |
| 101 { | |
| 102 // See if all rules on the sheet are scoped to some specific ids or classes. | |
| 103 // Then test if we actually have any of those in the tree at the moment. | |
| 104 if (styleSheetContents->hasSingleOwnerNode()) { | |
| 105 Node* ownerNode = styleSheetContents->singleOwnerNode(); | |
| 106 if (isHTMLStyleElement(ownerNode) && toHTMLStyleElement(*ownerNode).isIn
ShadowTree()) { | |
| 107 m_scopingNodes.append(determineScopingNodeForStyleInShadow(toHTMLSty
leElement(ownerNode), styleSheetContents)); | |
| 108 return; | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 const Vector<RefPtr<StyleRuleBase> >& rules = styleSheetContents->childRules
(); | |
| 113 for (unsigned i = 0; i < rules.size(); i++) { | |
| 114 StyleRuleBase* rule = rules[i].get(); | |
| 115 if (!rule->isStyleRule()) { | |
| 116 if (ruleAdditionMightRequireDocumentStyleRecalc(rule)) { | |
| 117 m_dirtiesAllStyle = true; | |
| 118 return; | |
| 119 } | |
| 120 continue; | |
| 121 } | |
| 122 StyleRule* styleRule = toStyleRule(rule); | |
| 123 if (!determineSelectorScopes(styleRule->selectorList(), m_idScopes, m_cl
assScopes)) { | |
| 124 m_dirtiesAllStyle = true; | |
| 125 return; | |
| 126 } | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 static bool elementMatchesSelectorScopes(const Element* element, const HashSet<S
tringImpl*>& idScopes, const HashSet<StringImpl*>& classScopes) | |
| 131 { | |
| 132 if (!idScopes.isEmpty() && element->hasID() && idScopes.contains(element->id
ForStyleResolution().impl())) | |
| 133 return true; | |
| 134 if (classScopes.isEmpty() || !element->hasClass()) | |
| 135 return false; | |
| 136 const SpaceSplitString& classNames = element->classNames(); | |
| 137 for (unsigned i = 0; i < classNames.size(); ++i) { | |
| 138 if (classScopes.contains(classNames[i].impl())) | |
| 139 return true; | |
| 140 } | |
| 141 return false; | |
| 142 } | |
| 143 | |
| 144 void StyleSheetInvalidationAnalysis::invalidateStyle(Document& document) | |
| 145 { | |
| 146 ASSERT(!m_dirtiesAllStyle); | |
| 147 | |
| 148 if (!m_scopingNodes.isEmpty()) { | |
| 149 for (unsigned i = 0; i < m_scopingNodes.size(); ++i) | |
| 150 m_scopingNodes.at(i)->setNeedsStyleRecalc(SubtreeStyleChange); | |
| 151 } | |
| 152 | |
| 153 if (m_idScopes.isEmpty() && m_classScopes.isEmpty()) | |
| 154 return; | |
| 155 Element* element = ElementTraversal::firstWithin(document); | |
| 156 while (element) { | |
| 157 if (elementMatchesSelectorScopes(element, m_idScopes, m_classScopes)) { | |
| 158 element->setNeedsStyleRecalc(SubtreeStyleChange); | |
| 159 // The whole subtree is now invalidated, we can skip to the next sib
ling. | |
| 160 element = ElementTraversal::nextSkippingChildren(*element); | |
| 161 continue; | |
| 162 } | |
| 163 element = ElementTraversal::next(*element); | |
| 164 } | |
| 165 } | |
| 166 | |
| 167 } | |
| OLD | NEW |