OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright (C) 2014 Google 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 are | |
6 * met: | |
7 * | |
8 * * Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * * Redistributions in binary form must reproduce the above | |
11 * copyright notice, this list of conditions and the following disclaimer | |
12 * in the documentation and/or other materials provided with the | |
13 * distribution. | |
14 * * Neither the name of Google Inc. nor the names of its | |
15 * contributors may be used to endorse or promote products derived from | |
16 * this software without specific prior written permission. | |
17 * | |
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 */ | |
30 | |
31 #include "config.h" | |
32 #include "core/css/analyzer/RuleSetAnalyzer.h" | |
33 | |
34 #include "core/css/CSSSelector.h" | |
35 #include "core/css/CSSSelectorList.h" | |
36 #include "core/css/RuleFeature.h" | |
37 #include "core/css/RuleSet.h" | |
38 #include "core/css/analyzer/DescendantInvalidationSet.h" | |
39 #include "wtf/Forward.h" | |
40 #include "wtf/HashSet.h" | |
41 #include "wtf/text/AtomicString.h" | |
42 #include "wtf/text/StringHash.h" | |
43 | |
44 | |
45 namespace WebCore { | |
46 | |
47 static bool isSkippableComponentForInvalidation(const CSSSelector* selector) | |
48 { | |
49 if (selector->matchesPseudoElement() || selector->pseudoType() == CSSSelecto r::PseudoHost) | |
50 return false; | |
51 return true; | |
52 } | |
53 | |
54 // This method is somewhat conservative in what it acceptss. | |
55 static bool supportsClassDescendantInvalidation(const CSSSelector* selector) | |
56 { | |
57 bool foundDescendantRelation = false; | |
58 bool foundAncestorIdent = false; | |
59 bool foundIdent = false; | |
60 for (const CSSSelector* component = selector; component; component = compone nt->tagHistory()) { | |
61 | |
62 // FIXME: We should allow pseudo elements, but we need to change how the y hook | |
63 // into recalcStyle by moving them to recalcOwnStyle instead of recalcCh ildStyle. | |
64 | |
65 if (component->m_match == CSSSelector::Tag | |
66 || component->m_match == CSSSelector::Id | |
67 || component->m_match == CSSSelector::Class) { | |
68 if (!foundDescendantRelation) | |
69 foundIdent = true; | |
70 else | |
71 foundAncestorIdent = true; | |
72 } else if (!isSkippableComponentForInvalidation(component)) { | |
73 return false; | |
74 } | |
75 // FIXME: We can probably support ChildTree and DescendantTree. | |
76 switch (component->relation()) { | |
77 case CSSSelector::Descendant: | |
78 case CSSSelector::Child: | |
79 foundDescendantRelation = true; | |
80 // Fall through! | |
81 case CSSSelector::SubSelector: | |
82 continue; | |
83 default: | |
84 return false; | |
85 } | |
86 } | |
87 return foundDescendantRelation && foundAncestorIdent && foundIdent; | |
88 } | |
89 | |
90 void ExtractClassIdOrTag(const CSSSelector& selector, HashSet<AtomicString>* cla sses, AtomicString* id, AtomicString* tagName) | |
esprehn
2014/01/14 21:32:56
lower case function names
esprehn
2014/01/14 21:32:56
pass references for the classes, id and tagNames
chrishtr
2014/01/14 23:33:00
Done.
| |
91 { | |
92 if (selector.m_match == CSSSelector::Tag) | |
93 *tagName = selector.tagQName().localName(); | |
94 else if (selector.m_match == CSSSelector::Id) | |
95 *id = selector.value(); | |
96 else if (selector.m_match == CSSSelector::Class) | |
97 classes->add(selector.value()); | |
98 } | |
99 | |
100 bool RuleSetAnalyzer::updateClassInvalidationSets(const CSSSelector* selector) | |
101 { | |
102 if (!selector) { | |
103 return false; | |
104 } | |
esprehn
2014/01/14 21:32:56
remove braces
chrishtr
2014/01/14 23:33:00
Done.
| |
105 if (!supportsClassDescendantInvalidation(selector)) | |
106 return false; | |
107 | |
108 HashSet<AtomicString> classes; | |
109 AtomicString id; | |
110 AtomicString tagName; | |
111 | |
112 const CSSSelector* lastSelector = selector; | |
113 for (; lastSelector->relation() == CSSSelector::SubSelector; lastSelector = lastSelector->tagHistory()) { | |
114 ExtractClassIdOrTag(*selector, &classes, &id, &tagName); | |
115 } | |
116 ExtractClassIdOrTag(*selector, &classes, &id, &tagName); | |
117 | |
118 for ( ; selector; selector = selector->tagHistory()) { | |
119 if (selector->m_match == CSSSelector::Class) { | |
120 DescendantInvalidationSet* invalidationSet = ensureClassInvalidation Set(selector->value()); | |
121 if (!id.isEmpty()) | |
122 invalidationSet->addId(id); | |
123 if (!tagName.isEmpty()) | |
124 invalidationSet->addTagName(tagName); | |
125 for (HashSet<AtomicString>::const_iterator it = classes.begin(); it != classes.end(); ++it) { | |
126 invalidationSet->addClass(*it); | |
127 } | |
128 } | |
129 } | |
130 return true; | |
131 } | |
132 | |
133 PassRefPtr<RuleSetAnalyzer> RuleSetAnalyzer::create() | |
134 { | |
135 return adoptRef(new RuleSetAnalyzer); | |
136 } | |
137 | |
138 void RuleSetAnalyzer::collectFeaturesFromRuleData(RuleFeatureSet& features, cons t RuleData& ruleData) | |
139 { | |
140 bool foundSiblingSelector = false; | |
141 unsigned maxDirectAdjacentSelectors = 0; | |
142 bool selectorUsesClassInvalidationSet= updateClassInvalidationSets(ruleData. selector()); | |
143 for (const CSSSelector* selector = ruleData.selector(); selector; selector = selector->tagHistory()) { | |
144 features.collectFeaturesFromSelector(selector); | |
145 | |
146 if (const CSSSelectorList* selectorList = selector->selectorList()) { | |
147 for (const CSSSelector* subSelector = selectorList->first(); subSele ctor; subSelector = CSSSelectorList::next(subSelector)) { | |
148 // FIXME: Shouldn't this be checking subSelector->isSiblingSelec tor()? | |
149 if (!foundSiblingSelector && selector->isSiblingSelector()) | |
150 foundSiblingSelector = true; | |
151 if (subSelector->isDirectAdjacentSelector()) | |
152 maxDirectAdjacentSelectors++; | |
153 features.collectFeaturesFromSelector(subSelector); | |
154 } | |
155 } else { | |
156 if (!foundSiblingSelector && selector->isSiblingSelector()) | |
157 foundSiblingSelector = true; | |
158 if (selector->isDirectAdjacentSelector()) | |
159 maxDirectAdjacentSelectors++; | |
160 } | |
161 } | |
162 if (!selectorUsesClassInvalidationSet) { | |
163 for (HashSet<AtomicString>::const_iterator it = features.classesInRules. begin(); it != features.classesInRules.end(); ++it) { | |
164 DescendantInvalidationSet* invalidationSet = ensureClassInvalidation Set(*it); | |
165 invalidationSet->setWholeSubtreeInvalid(); | |
166 } | |
167 } | |
168 features.setMaxDirectAdjacentSelectors(maxDirectAdjacentSelectors); | |
169 if (foundSiblingSelector) | |
170 features.siblingRules.append(RuleFeature(ruleData.rule(), ruleData.selec torIndex(), ruleData.hasDocumentSecurityOrigin())); | |
171 if (ruleData.containsUncommonAttributeSelector()) | |
172 features.uncommonAttributeRules.append(RuleFeature(ruleData.rule(), rule Data.selectorIndex(), ruleData.hasDocumentSecurityOrigin())); | |
173 } | |
174 | |
175 DescendantInvalidationSet* RuleSetAnalyzer::ensureClassInvalidationSet(const Ato micString& className) | |
176 { | |
177 InvalidationSetMap::AddResult addResult = classInvalidationSets.add(classNam e, 0); | |
178 if (addResult.isNewEntry) | |
179 addResult.iterator->value = DescendantInvalidationSet::create(); | |
180 return addResult.iterator->value.get(); | |
181 } | |
182 | |
183 DescendantInvalidationSet* RuleSetAnalyzer::getClassInvalidationSet(const Atomic String& className) const | |
esprehn
2014/01/14 21:32:56
::invalidationSetForClass(...), get*() prefixes me
chrishtr
2014/01/14 23:33:00
Done.
| |
184 { | |
185 return classInvalidationSets.get(className); | |
186 } | |
187 | |
188 bool RuleSetAnalyzer::invalidateElementSubtreeForClassChange(const AtomicString& className, Element* element) const | |
189 { | |
190 DescendantInvalidationSet* descendantInvalidationSet = classInvalidationSets .get(className); | |
191 if (descendantInvalidationSet) { | |
esprehn
2014/01/14 21:32:56
We usually combine the get and the if statement:
chrishtr
2014/01/14 23:33:00
Done.
| |
192 descendantInvalidationSet->invalidateElementSubtree(element); | |
193 return true; | |
194 } | |
195 return false; | |
196 } | |
197 | |
198 void RuleSetAnalyzer::combine(const RuleSetAnalyzer& other) | |
199 { | |
200 for (InvalidationSetMap::const_iterator it = other.classInvalidationSets.beg in(); it != other.classInvalidationSets.end(); ++it) { | |
201 ensureClassInvalidationSet(it->key)->combine(*it->value); | |
202 } | |
203 } | |
204 | |
205 } // namespace WebCore | |
OLD | NEW |