2 * Copyright (C) 2012 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "core/css/StyleInvalidationAnalysis.h"
29 #include "core/css/CSSSelectorList.h"
30 #include "core/css/StyleRuleImport.h"
31 #include "core/css/StyleSheetContents.h"
32 #include "core/dom/ContainerNode.h"
33 #include "core/dom/Document.h"
34 #include "core/dom/ElementTraversal.h"
35 #include "core/dom/shadow/ShadowRoot.h"
36 #include "core/html/HTMLStyleElement.h"
40 StyleInvalidationAnalysis::StyleInvalidationAnalysis(const Vector<StyleSheetContents*>& sheets)
41 : m_dirtiesAllStyle(false)
43 for (unsigned i = 0; i < sheets.size() && !m_dirtiesAllStyle; ++i)
44 analyzeStyleSheet(sheets[i]);
47 static bool determineSelectorScopes(const CSSSelectorList& selectorList, HashSet<StringImpl*>& idScopes, HashSet<StringImpl*>& classScopes)
49 for (const CSSSelector* selector = selectorList.first(); selector; selector = CSSSelectorList::next(*selector)) {
50 const CSSSelector* scopeSelector = 0;
51 // This picks the widest scope, not the narrowest, to minimize the number of found scopes.
52 for (const CSSSelector* current = selector; current; current = current->tagHistory()) {
53 // Prefer ids over classes.
54 if (current->m_match == CSSSelector::Id)
55 scopeSelector = current;
56 else if (current->m_match == CSSSelector::Class && (!scopeSelector || scopeSelector->m_match != CSSSelector::Id))
57 scopeSelector = current;
58 CSSSelector::Relation relation = current->relation();
59 // FIXME: it would be better to use setNeedsStyleRecalc for all shadow hosts matching
60 // scopeSelector. Currently requests full style recalc.
61 if (relation == CSSSelector::DescendantTree || relation == CSSSelector::ChildTree)
63 if (relation != CSSSelector::Descendant && relation != CSSSelector::Child && relation != CSSSelector::SubSelector)
68 ASSERT(scopeSelector->m_match == CSSSelector::Class || scopeSelector->m_match == CSSSelector::Id);
69 if (scopeSelector->m_match == CSSSelector::Id)
70 idScopes.add(scopeSelector->value().impl());
72 classScopes.add(scopeSelector->value().impl());
77 static bool hasDistributedRule(StyleSheetContents* styleSheetContents)
79 const Vector<RefPtr<StyleRuleBase> >& rules = styleSheetContents->childRules();
80 for (unsigned i = 0; i < rules.size(); i++) {
81 const StyleRuleBase* rule = rules[i].get();
82 if (!rule->isStyleRule())
85 const StyleRule* styleRule = toStyleRule(rule);
86 const CSSSelectorList& selectorList = styleRule->selectorList();
87 for (size_t selectorIndex = 0; selectorIndex != kNotFound; selectorIndex = selectorList.indexOfNextSelectorAfter(selectorIndex)) {
88 if (selectorList.hasShadowDistributedAt(selectorIndex))
95 static Node* determineScopingNodeForStyleScoped(HTMLStyleElement* ownerElement, StyleSheetContents* styleSheetContents)
97 ASSERT(ownerElement && ownerElement->isRegisteredAsScoped());
99 if (ownerElement->isInShadowTree()) {
100 if (hasDistributedRule(styleSheetContents)) {
101 ContainerNode* scope = ownerElement;
103 scope = scope->containingShadowRoot()->shadowHost();
104 } while (scope->isInShadowTree());
108 if (ownerElement->isRegisteredAsScoped())
109 return ownerElement->containingShadowRoot()->shadowHost();
112 return ownerElement->isRegisteredInShadowRoot() ? ownerElement->containingShadowRoot()->shadowHost() : ownerElement->parentNode();
115 static bool ruleAdditionMightRequireDocumentStyleRecalc(StyleRuleBase* rule)
117 // This funciton is conservative. We only return false when we know that
118 // the added @rule can't require style recalcs.
119 switch (rule->type()) {
120 case StyleRule::Import: // Whatever we import should do its own analysis, we don't need to invalidate the document here!
121 case StyleRule::Keyframes: // Keyframes never cause style invalidations and are handled during sheet insertion.
122 case StyleRule::Page: // Page rules apply only during printing, we force a full-recalc before printing.
125 case StyleRule::Media: // If the media rule doesn't apply, we could avoid recalc.
126 case StyleRule::FontFace: // If the fonts aren't in use, we could avoid recalc.
127 case StyleRule::Supports: // If we evaluated the supports-clause we could avoid recalc.
128 case StyleRule::Viewport: // If the viewport doesn't match, we could avoid recalcing.
129 // FIXME: Unclear if any of the rest need to cause style recalc:
130 case StyleRule::Filter:
133 // These should all be impossible to reach:
134 case StyleRule::Unknown:
135 case StyleRule::Charset:
136 case StyleRule::Keyframe:
137 case StyleRule::Style:
140 ASSERT_NOT_REACHED();
144 void StyleInvalidationAnalysis::analyzeStyleSheet(StyleSheetContents* styleSheetContents)
146 ASSERT(!styleSheetContents->isLoading());
148 // See if all rules on the sheet are scoped to some specific ids or classes.
149 // Then test if we actually have any of those in the tree at the moment.
150 const Vector<RefPtr<StyleRuleImport> >& importRules = styleSheetContents->importRules();
151 for (unsigned i = 0; i < importRules.size(); ++i) {
152 if (!importRules[i]->styleSheet())
154 analyzeStyleSheet(importRules[i]->styleSheet());
155 if (m_dirtiesAllStyle)
158 if (styleSheetContents->hasSingleOwnerNode()) {
159 Node* ownerNode = styleSheetContents->singleOwnerNode();
160 if (ownerNode && ownerNode->hasTagName(HTMLNames::styleTag) && toHTMLStyleElement(ownerNode)->isRegisteredAsScoped()) {
161 m_scopingNodes.append(determineScopingNodeForStyleScoped(toHTMLStyleElement(ownerNode), styleSheetContents));
166 const Vector<RefPtr<StyleRuleBase> >& rules = styleSheetContents->childRules();
167 for (unsigned i = 0; i < rules.size(); i++) {
168 StyleRuleBase* rule = rules[i].get();
169 if (!rule->isStyleRule()) {
170 if (ruleAdditionMightRequireDocumentStyleRecalc(rule)) {
171 m_dirtiesAllStyle = true;
176 StyleRule* styleRule = toStyleRule(rule);
177 if (!determineSelectorScopes(styleRule->selectorList(), m_idScopes, m_classScopes)) {
178 m_dirtiesAllStyle = true;
184 static bool elementMatchesSelectorScopes(const Element* element, const HashSet<StringImpl*>& idScopes, const HashSet<StringImpl*>& classScopes)
186 if (!idScopes.isEmpty() && element->hasID() && idScopes.contains(element->idForStyleResolution().impl()))
188 if (classScopes.isEmpty() || !element->hasClass())
190 const SpaceSplitString& classNames = element->classNames();
191 for (unsigned i = 0; i < classNames.size(); ++i) {
192 if (classScopes.contains(classNames[i].impl()))
198 void StyleInvalidationAnalysis::invalidateStyle(Document& document)
200 ASSERT(!m_dirtiesAllStyle);
202 if (!m_scopingNodes.isEmpty()) {
203 for (unsigned i = 0; i < m_scopingNodes.size(); ++i)
204 m_scopingNodes.at(i)->setNeedsStyleRecalc(SubtreeStyleChange);
207 if (m_idScopes.isEmpty() && m_classScopes.isEmpty())
209 Element* element = ElementTraversal::firstWithin(document);
211 if (elementMatchesSelectorScopes(element, m_idScopes, m_classScopes)) {
212 element->setNeedsStyleRecalc(SubtreeStyleChange);
213 // The whole subtree is now invalidated, we can skip to the next sibling.
214 element = ElementTraversal::nextSkippingChildren(*element);
217 element = ElementTraversal::next(*element);