• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  *           (C) 2004-2005 Allan Sandfeld Jensen (kde@carewolf.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 rights reserved.
6  * Copyright (C) 2007 Alexey Proskuryakov <ap@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.torchmobile.com/)
9  * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
10  * Copyright (C) Research In Motion Limited 2011. All rights reserved.
11  * Copyright (C) 2012 Google Inc. All rights reserved.
12  *
13  * This library is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU Library General Public
15  * License as published by the Free Software Foundation; either
16  * version 2 of the License, or (at your option) any later version.
17  *
18  * This library is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  * Library General Public License for more details.
22  *
23  * You should have received a copy of the GNU Library General Public License
24  * along with this library; see the file COPYING.LIB.  If not, write to
25  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26  * Boston, MA 02110-1301, USA.
27  */
28 
29 #include "config.h"
30 #include "core/css/RuleSet.h"
31 
32 #include "core/HTMLNames.h"
33 #include "core/css/CSSFontSelector.h"
34 #include "core/css/CSSSelector.h"
35 #include "core/css/CSSSelectorList.h"
36 #include "core/css/SelectorChecker.h"
37 #include "core/css/SelectorFilter.h"
38 #include "core/css/StyleRuleImport.h"
39 #include "core/css/StyleSheetContents.h"
40 #include "core/html/track/TextTrackCue.h"
41 #include "platform/RuntimeEnabledFeatures.h"
42 #include "platform/TraceEvent.h"
43 #include "platform/heap/HeapTerminatedArrayBuilder.h"
44 #include "platform/weborigin/SecurityOrigin.h"
45 
46 #include "wtf/TerminatedArrayBuilder.h"
47 
48 namespace WebCore {
49 
50 using namespace HTMLNames;
51 
52 // -----------------------------------------------------------------
53 
isSelectorMatchingHTMLBasedOnRuleHash(const CSSSelector & selector)54 static inline bool isSelectorMatchingHTMLBasedOnRuleHash(const CSSSelector& selector)
55 {
56     if (selector.match() == CSSSelector::Tag) {
57         const AtomicString& selectorNamespace = selector.tagQName().namespaceURI();
58         if (selectorNamespace != starAtom && selectorNamespace != xhtmlNamespaceURI)
59             return false;
60         if (selector.relation() == CSSSelector::SubSelector) {
61             ASSERT(selector.tagHistory());
62             return isSelectorMatchingHTMLBasedOnRuleHash(*selector.tagHistory());
63         }
64         return true;
65     }
66     if (SelectorChecker::isCommonPseudoClassSelector(selector))
67         return true;
68     return selector.match() == CSSSelector::Id || selector.match() == CSSSelector::Class;
69 }
70 
selectorListContainsUncommonAttributeSelector(const CSSSelector * selector)71 static inline bool selectorListContainsUncommonAttributeSelector(const CSSSelector* selector)
72 {
73     const CSSSelectorList* selectorList = selector->selectorList();
74     if (!selectorList)
75         return false;
76     for (const CSSSelector* selector = selectorList->first(); selector; selector = CSSSelectorList::next(*selector)) {
77         for (const CSSSelector* component = selector; component; component = component->tagHistory()) {
78             if (component->isAttributeSelector())
79                 return true;
80         }
81     }
82     return false;
83 }
84 
isCommonAttributeSelectorAttribute(const QualifiedName & attribute)85 static inline bool isCommonAttributeSelectorAttribute(const QualifiedName& attribute)
86 {
87     // These are explicitly tested for equality in canShareStyleWithElement.
88     return attribute == typeAttr || attribute == readonlyAttr;
89 }
90 
containsUncommonAttributeSelector(const CSSSelector & selector)91 static inline bool containsUncommonAttributeSelector(const CSSSelector& selector)
92 {
93     const CSSSelector* current = &selector;
94     for (; current; current = current->tagHistory()) {
95         // Allow certain common attributes (used in the default style) in the selectors that match the current element.
96         if (current->isAttributeSelector() && !isCommonAttributeSelectorAttribute(current->attribute()))
97             return true;
98         if (selectorListContainsUncommonAttributeSelector(current))
99             return true;
100         if (current->relation() != CSSSelector::SubSelector) {
101             current = current->tagHistory();
102             break;
103         }
104     }
105 
106     for (; current; current = current->tagHistory()) {
107         if (current->isAttributeSelector())
108             return true;
109         if (selectorListContainsUncommonAttributeSelector(current))
110             return true;
111     }
112     return false;
113 }
114 
determinePropertyWhitelistType(const AddRuleFlags addRuleFlags,const CSSSelector & selector)115 static inline PropertyWhitelistType determinePropertyWhitelistType(const AddRuleFlags addRuleFlags, const CSSSelector& selector)
116 {
117     for (const CSSSelector* component = &selector; component; component = component->tagHistory()) {
118         if (component->pseudoType() == CSSSelector::PseudoCue || (component->match() == CSSSelector::PseudoElement && component->value() == TextTrackCue::cueShadowPseudoId()))
119             return PropertyWhitelistCue;
120         if (component->pseudoType() == CSSSelector::PseudoFirstLetter)
121             return PropertyWhitelistFirstLetter;
122     }
123     return PropertyWhitelistNone;
124 }
125 
RuleData(StyleRule * rule,unsigned selectorIndex,unsigned position,AddRuleFlags addRuleFlags)126 RuleData::RuleData(StyleRule* rule, unsigned selectorIndex, unsigned position, AddRuleFlags addRuleFlags)
127     : m_rule(rule)
128     , m_selectorIndex(selectorIndex)
129     , m_isLastInArray(false)
130     , m_position(position)
131     , m_specificity(selector().specificity())
132     , m_hasMultipartSelector(!!selector().tagHistory())
133     , m_hasRightmostSelectorMatchingHTMLBasedOnRuleHash(isSelectorMatchingHTMLBasedOnRuleHash(selector()))
134     , m_containsUncommonAttributeSelector(WebCore::containsUncommonAttributeSelector(selector()))
135     , m_linkMatchType(SelectorChecker::determineLinkMatchType(selector()))
136     , m_hasDocumentSecurityOrigin(addRuleFlags & RuleHasDocumentSecurityOrigin)
137     , m_propertyWhitelistType(determinePropertyWhitelistType(addRuleFlags, selector()))
138 {
139     ASSERT(m_position == position);
140     ASSERT(m_selectorIndex == selectorIndex);
141     SelectorFilter::collectIdentifierHashes(selector(), m_descendantSelectorIdentifierHashes, maximumIdentifierCount);
142 }
143 
addToRuleSet(const AtomicString & key,PendingRuleMap & map,const RuleData & ruleData)144 void RuleSet::addToRuleSet(const AtomicString& key, PendingRuleMap& map, const RuleData& ruleData)
145 {
146     OwnPtrWillBeMember<WillBeHeapLinkedStack<RuleData> >& rules = map.add(key, nullptr).storedValue->value;
147     if (!rules)
148         rules = adoptPtrWillBeNoop(new WillBeHeapLinkedStack<RuleData>);
149     rules->push(ruleData);
150 }
151 
extractValuesforSelector(const CSSSelector * selector,AtomicString & id,AtomicString & className,AtomicString & customPseudoElementName,AtomicString & tagName)152 static void extractValuesforSelector(const CSSSelector* selector, AtomicString& id, AtomicString& className, AtomicString& customPseudoElementName, AtomicString& tagName)
153 {
154     switch (selector->match()) {
155     case CSSSelector::Id:
156         id = selector->value();
157         break;
158     case CSSSelector::Class:
159         className = selector->value();
160         break;
161     case CSSSelector::Tag:
162         if (selector->tagQName().localName() != starAtom)
163             tagName = selector->tagQName().localName();
164         break;
165     default:
166         break;
167     }
168     if (selector->isCustomPseudoElement())
169         customPseudoElementName = selector->value();
170 }
171 
findBestRuleSetAndAdd(const CSSSelector & component,RuleData & ruleData)172 bool RuleSet::findBestRuleSetAndAdd(const CSSSelector& component, RuleData& ruleData)
173 {
174     AtomicString id;
175     AtomicString className;
176     AtomicString customPseudoElementName;
177     AtomicString tagName;
178 
179 #ifndef NDEBUG
180     m_allRules.append(ruleData);
181 #endif
182 
183     const CSSSelector* it = &component;
184     for (; it && it->relation() == CSSSelector::SubSelector; it = it->tagHistory()) {
185         extractValuesforSelector(it, id, className, customPseudoElementName, tagName);
186     }
187     // FIXME: this null check should not be necessary. See crbug.com/358475
188     if (it)
189         extractValuesforSelector(it, id, className, customPseudoElementName, tagName);
190 
191     // Prefer rule sets in order of most likely to apply infrequently.
192     if (!id.isEmpty()) {
193         addToRuleSet(id, ensurePendingRules()->idRules, ruleData);
194         return true;
195     }
196     if (!className.isEmpty()) {
197         addToRuleSet(className, ensurePendingRules()->classRules, ruleData);
198         return true;
199     }
200     if (!customPseudoElementName.isEmpty()) {
201         // Custom pseudos come before ids and classes in the order of tagHistory, and have a relation of
202         // ShadowPseudo between them. Therefore we should never be a situation where extractValuesforSelector
203         // finsd id and className in addition to custom pseudo.
204         ASSERT(id.isEmpty() && className.isEmpty());
205         addToRuleSet(customPseudoElementName, ensurePendingRules()->shadowPseudoElementRules, ruleData);
206         return true;
207     }
208 
209     if (component.pseudoType() == CSSSelector::PseudoCue) {
210         m_cuePseudoRules.append(ruleData);
211         return true;
212     }
213 
214     if (SelectorChecker::isCommonPseudoClassSelector(component)) {
215         switch (component.pseudoType()) {
216         case CSSSelector::PseudoLink:
217         case CSSSelector::PseudoVisited:
218         case CSSSelector::PseudoAnyLink:
219             m_linkPseudoClassRules.append(ruleData);
220             return true;
221         case CSSSelector::PseudoFocus:
222             m_focusPseudoClassRules.append(ruleData);
223             return true;
224         default:
225             ASSERT_NOT_REACHED();
226             return true;
227         }
228     }
229 
230     if (!tagName.isEmpty()) {
231         addToRuleSet(tagName, ensurePendingRules()->tagRules, ruleData);
232         return true;
233     }
234 
235     return false;
236 }
237 
addRule(StyleRule * rule,unsigned selectorIndex,AddRuleFlags addRuleFlags)238 void RuleSet::addRule(StyleRule* rule, unsigned selectorIndex, AddRuleFlags addRuleFlags)
239 {
240     RuleData ruleData(rule, selectorIndex, m_ruleCount++, addRuleFlags);
241     m_features.collectFeaturesFromRuleData(ruleData);
242 
243     if (!findBestRuleSetAndAdd(ruleData.selector(), ruleData)) {
244         // If we didn't find a specialized map to stick it in, file under universal rules.
245         m_universalRules.append(ruleData);
246     }
247 }
248 
addPageRule(StyleRulePage * rule)249 void RuleSet::addPageRule(StyleRulePage* rule)
250 {
251     ensurePendingRules(); // So that m_pageRules.shrinkToFit() gets called.
252     m_pageRules.append(rule);
253 }
254 
addViewportRule(StyleRuleViewport * rule)255 void RuleSet::addViewportRule(StyleRuleViewport* rule)
256 {
257     ensurePendingRules(); // So that m_viewportRules.shrinkToFit() gets called.
258     m_viewportRules.append(rule);
259 }
260 
addFontFaceRule(StyleRuleFontFace * rule)261 void RuleSet::addFontFaceRule(StyleRuleFontFace* rule)
262 {
263     ensurePendingRules(); // So that m_fontFaceRules.shrinkToFit() gets called.
264     m_fontFaceRules.append(rule);
265 }
266 
addKeyframesRule(StyleRuleKeyframes * rule)267 void RuleSet::addKeyframesRule(StyleRuleKeyframes* rule)
268 {
269     ensurePendingRules(); // So that m_keyframesRules.shrinkToFit() gets called.
270     m_keyframesRules.append(rule);
271 }
272 
addChildRules(const WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase>> & rules,const MediaQueryEvaluator & medium,AddRuleFlags addRuleFlags)273 void RuleSet::addChildRules(const WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase> >& rules, const MediaQueryEvaluator& medium, AddRuleFlags addRuleFlags)
274 {
275     for (unsigned i = 0; i < rules.size(); ++i) {
276         StyleRuleBase* rule = rules[i].get();
277 
278         if (rule->isStyleRule()) {
279             StyleRule* styleRule = toStyleRule(rule);
280 
281             const CSSSelectorList& selectorList = styleRule->selectorList();
282             for (size_t selectorIndex = 0; selectorIndex != kNotFound; selectorIndex = selectorList.indexOfNextSelectorAfter(selectorIndex)) {
283                 if (selectorList.selectorCrossesTreeScopes(selectorIndex)) {
284                     m_treeBoundaryCrossingRules.append(MinimalRuleData(styleRule, selectorIndex, addRuleFlags));
285                 } else if (selectorList.hasShadowDistributedAt(selectorIndex)) {
286                     m_shadowDistributedRules.append(MinimalRuleData(styleRule, selectorIndex, addRuleFlags));
287                 } else {
288                     addRule(styleRule, selectorIndex, addRuleFlags);
289                 }
290             }
291         } else if (rule->isPageRule()) {
292             addPageRule(toStyleRulePage(rule));
293         } else if (rule->isMediaRule()) {
294             StyleRuleMedia* mediaRule = toStyleRuleMedia(rule);
295             if ((!mediaRule->mediaQueries() || medium.eval(mediaRule->mediaQueries(), &m_viewportDependentMediaQueryResults)))
296                 addChildRules(mediaRule->childRules(), medium, addRuleFlags);
297         } else if (rule->isFontFaceRule()) {
298             addFontFaceRule(toStyleRuleFontFace(rule));
299         } else if (rule->isKeyframesRule()) {
300             addKeyframesRule(toStyleRuleKeyframes(rule));
301         } else if (rule->isViewportRule()) {
302             addViewportRule(toStyleRuleViewport(rule));
303         } else if (rule->isSupportsRule() && toStyleRuleSupports(rule)->conditionIsSupported()) {
304             addChildRules(toStyleRuleSupports(rule)->childRules(), medium, addRuleFlags);
305         }
306     }
307 }
308 
addRulesFromSheet(StyleSheetContents * sheet,const MediaQueryEvaluator & medium,AddRuleFlags addRuleFlags)309 void RuleSet::addRulesFromSheet(StyleSheetContents* sheet, const MediaQueryEvaluator& medium, AddRuleFlags addRuleFlags)
310 {
311     TRACE_EVENT0("webkit", "RuleSet::addRulesFromSheet");
312 
313     ASSERT(sheet);
314 
315     addRuleFlags = static_cast<AddRuleFlags>(addRuleFlags | RuleCanUseFastCheckSelector);
316     const WillBeHeapVector<RefPtrWillBeMember<StyleRuleImport> >& importRules = sheet->importRules();
317     for (unsigned i = 0; i < importRules.size(); ++i) {
318         StyleRuleImport* importRule = importRules[i].get();
319         if (importRule->styleSheet() && (!importRule->mediaQueries() || medium.eval(importRule->mediaQueries(), &m_viewportDependentMediaQueryResults)))
320             addRulesFromSheet(importRule->styleSheet(), medium, addRuleFlags);
321     }
322 
323     addChildRules(sheet->childRules(), medium, addRuleFlags);
324 }
325 
addStyleRule(StyleRule * rule,AddRuleFlags addRuleFlags)326 void RuleSet::addStyleRule(StyleRule* rule, AddRuleFlags addRuleFlags)
327 {
328     for (size_t selectorIndex = 0; selectorIndex != kNotFound; selectorIndex = rule->selectorList().indexOfNextSelectorAfter(selectorIndex))
329         addRule(rule, selectorIndex, addRuleFlags);
330 }
331 
compactPendingRules(PendingRuleMap & pendingMap,CompactRuleMap & compactMap)332 void RuleSet::compactPendingRules(PendingRuleMap& pendingMap, CompactRuleMap& compactMap)
333 {
334     PendingRuleMap::iterator end = pendingMap.end();
335     for (PendingRuleMap::iterator it = pendingMap.begin(); it != end; ++it) {
336         OwnPtrWillBeRawPtr<WillBeHeapLinkedStack<RuleData> > pendingRules = it->value.release();
337         CompactRuleMap::ValueType* compactRules = compactMap.add(it->key, nullptr).storedValue;
338 
339         WillBeHeapTerminatedArrayBuilder<RuleData> builder(compactRules->value.release());
340         builder.grow(pendingRules->size());
341         while (!pendingRules->isEmpty()) {
342             builder.append(pendingRules->peek());
343             pendingRules->pop();
344         }
345 
346         compactRules->value = builder.release();
347     }
348 }
349 
compactRules()350 void RuleSet::compactRules()
351 {
352     ASSERT(m_pendingRules);
353     OwnPtrWillBeRawPtr<PendingRuleMaps> pendingRules = m_pendingRules.release();
354     compactPendingRules(pendingRules->idRules, m_idRules);
355     compactPendingRules(pendingRules->classRules, m_classRules);
356     compactPendingRules(pendingRules->tagRules, m_tagRules);
357     compactPendingRules(pendingRules->shadowPseudoElementRules, m_shadowPseudoElementRules);
358     m_linkPseudoClassRules.shrinkToFit();
359     m_cuePseudoRules.shrinkToFit();
360     m_focusPseudoClassRules.shrinkToFit();
361     m_universalRules.shrinkToFit();
362     m_pageRules.shrinkToFit();
363     m_viewportRules.shrinkToFit();
364     m_fontFaceRules.shrinkToFit();
365     m_keyframesRules.shrinkToFit();
366     m_treeBoundaryCrossingRules.shrinkToFit();
367     m_shadowDistributedRules.shrinkToFit();
368 }
369 
trace(Visitor * visitor)370 void MinimalRuleData::trace(Visitor* visitor)
371 {
372     visitor->trace(m_rule);
373 }
374 
trace(Visitor * visitor)375 void RuleData::trace(Visitor* visitor)
376 {
377     visitor->trace(m_rule);
378 }
379 
trace(Visitor * visitor)380 void RuleSet::PendingRuleMaps::trace(Visitor* visitor)
381 {
382     visitor->trace(idRules);
383     visitor->trace(classRules);
384     visitor->trace(tagRules);
385     visitor->trace(shadowPseudoElementRules);
386 }
387 
trace(Visitor * visitor)388 void RuleSet::trace(Visitor* visitor)
389 {
390 #if ENABLE(OILPAN)
391     visitor->trace(m_idRules);
392     visitor->trace(m_classRules);
393     visitor->trace(m_tagRules);
394     visitor->trace(m_shadowPseudoElementRules);
395     visitor->trace(m_linkPseudoClassRules);
396     visitor->trace(m_cuePseudoRules);
397     visitor->trace(m_focusPseudoClassRules);
398     visitor->trace(m_universalRules);
399     visitor->trace(m_features);
400     visitor->trace(m_pageRules);
401     visitor->trace(m_viewportRules);
402     visitor->trace(m_fontFaceRules);
403     visitor->trace(m_keyframesRules);
404     visitor->trace(m_treeBoundaryCrossingRules);
405     visitor->trace(m_shadowDistributedRules);
406     visitor->trace(m_viewportDependentMediaQueryResults);
407     visitor->trace(m_pendingRules);
408 #ifndef NDEBUG
409     visitor->trace(m_allRules);
410 #endif
411 #endif
412 }
413 
414 #ifndef NDEBUG
show()415 void RuleSet::show()
416 {
417     for (WillBeHeapVector<RuleData>::const_iterator it = m_allRules.begin(); it != m_allRules.end(); ++it)
418         it->selector().show();
419 }
420 #endif
421 
422 } // namespace WebCore
423