Use AtomicString in RuleSet and RuleFeature
[WebKit-https.git] / Source / WebCore / css / RuleFeature.cpp
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-2012, 2014 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 "RuleFeature.h"
31
32 #include "CSSSelector.h"
33 #include "CSSSelectorList.h"
34 #include "RuleSet.h"
35
36 namespace WebCore {
37
38 void RuleFeatureSet::recursivelyCollectFeaturesFromSelector(SelectorFeatures& selectorFeatures, const CSSSelector& firstSelector, bool matchesAncestor)
39 {
40     const CSSSelector* selector = &firstSelector;
41     do {
42         if (selector->match() == CSSSelector::Id) {
43             idsInRules.add(selector->value());
44             if (matchesAncestor)
45                 idsMatchingAncestorsInRules.add(selector->value());
46         } else if (selector->match() == CSSSelector::Class) {
47             classesInRules.add(selector->value());
48             if (matchesAncestor)
49                 selectorFeatures.classesMatchingAncestors.append(selector->value());
50         } else if (selector->isAttributeSelector()) {
51             auto& canonicalLocalName = selector->attributeCanonicalLocalName();
52             auto& localName = selector->attribute().localName();
53             attributeCanonicalLocalNamesInRules.add(canonicalLocalName);
54             attributeLocalNamesInRules.add(localName);
55             if (matchesAncestor)
56                 selectorFeatures.attributeSelectorsMatchingAncestors.append(selector);
57         } else if (selector->match() == CSSSelector::PseudoElement) {
58             switch (selector->pseudoElementType()) {
59             case CSSSelector::PseudoElementFirstLine:
60                 usesFirstLineRules = true;
61                 break;
62             case CSSSelector::PseudoElementFirstLetter:
63                 usesFirstLetterRules = true;
64                 break;
65             default:
66                 break;
67             }
68         }
69
70         if (!selectorFeatures.hasSiblingSelector && selector->isSiblingSelector())
71             selectorFeatures.hasSiblingSelector = true;
72
73         if (const CSSSelectorList* selectorList = selector->selectorList()) {
74             for (const CSSSelector* subSelector = selectorList->first(); subSelector; subSelector = CSSSelectorList::next(subSelector)) {
75                 if (!selectorFeatures.hasSiblingSelector && selector->isSiblingSelector())
76                     selectorFeatures.hasSiblingSelector = true;
77                 recursivelyCollectFeaturesFromSelector(selectorFeatures, *subSelector, matchesAncestor);
78             }
79         }
80         if (selector->hasDescendantOrChildRelation())
81             matchesAncestor = true;
82
83         selector = selector->tagHistory();
84     } while (selector);
85 }
86
87 static RuleFeatureSet::AttributeRules::SelectorKey makeAttributeSelectorKey(const CSSSelector& selector)
88 {
89     bool caseInsensitive = selector.attributeValueMatchingIsCaseInsensitive();
90     unsigned matchAndCase = static_cast<unsigned>(selector.match()) << 1 | (caseInsensitive ? 1 : 0);
91     return std::make_pair(selector.attributeCanonicalLocalName().impl(), std::make_pair(selector.value().impl(), matchAndCase));
92 }
93
94 void RuleFeatureSet::collectFeatures(const RuleData& ruleData)
95 {
96     SelectorFeatures selectorFeatures;
97     recursivelyCollectFeaturesFromSelector(selectorFeatures, *ruleData.selector());
98     if (selectorFeatures.hasSiblingSelector)
99         siblingRules.append(RuleFeature(ruleData.rule(), ruleData.selectorIndex(), ruleData.hasDocumentSecurityOrigin()));
100     if (ruleData.containsUncommonAttributeSelector())
101         uncommonAttributeRules.append(RuleFeature(ruleData.rule(), ruleData.selectorIndex(), ruleData.hasDocumentSecurityOrigin()));
102     for (auto& className : selectorFeatures.classesMatchingAncestors) {
103         auto addResult = ancestorClassRules.ensure(className, [] {
104             return std::make_unique<Vector<RuleFeature>>();
105         });
106         addResult.iterator->value->append(RuleFeature(ruleData.rule(), ruleData.selectorIndex(), ruleData.hasDocumentSecurityOrigin()));
107     }
108     for (auto* selector : selectorFeatures.attributeSelectorsMatchingAncestors) {
109         // Hashing by attributeCanonicalLocalName makes this HTML specific.
110         auto addResult = ancestorAttributeRulesForHTML.ensure(selector->attributeCanonicalLocalName(), [] {
111             return std::make_unique<AttributeRules>();
112         });
113         auto& rules = *addResult.iterator->value;
114         rules.features.append(RuleFeature(ruleData.rule(), ruleData.selectorIndex(), ruleData.hasDocumentSecurityOrigin()));
115         // Deduplicate selectors.
116         rules.selectors.add(makeAttributeSelectorKey(*selector), selector);
117     }
118 }
119
120 void RuleFeatureSet::add(const RuleFeatureSet& other)
121 {
122     idsInRules.add(other.idsInRules.begin(), other.idsInRules.end());
123     idsMatchingAncestorsInRules.add(other.idsMatchingAncestorsInRules.begin(), other.idsMatchingAncestorsInRules.end());
124     classesInRules.add(other.classesInRules.begin(), other.classesInRules.end());
125     attributeCanonicalLocalNamesInRules.add(other.attributeCanonicalLocalNamesInRules.begin(), other.attributeCanonicalLocalNamesInRules.end());
126     attributeLocalNamesInRules.add(other.attributeLocalNamesInRules.begin(), other.attributeLocalNamesInRules.end());
127     siblingRules.appendVector(other.siblingRules);
128     uncommonAttributeRules.appendVector(other.uncommonAttributeRules);
129     for (auto& keyValuePair : other.ancestorClassRules) {
130         auto addResult = ancestorClassRules.ensure(keyValuePair.key, [] {
131             return std::make_unique<Vector<RuleFeature>>();
132         });
133         addResult.iterator->value->appendVector(*keyValuePair.value);
134     }
135     for (auto& keyValuePair : other.ancestorAttributeRulesForHTML) {
136         auto addResult = ancestorAttributeRulesForHTML.ensure(keyValuePair.key, [] {
137             return std::make_unique<AttributeRules>();
138         });
139         auto& rules = *addResult.iterator->value;
140         rules.features.appendVector(keyValuePair.value->features);
141         for (auto& selectorPair : keyValuePair.value->selectors)
142             rules.selectors.add(selectorPair.key, selectorPair.value);
143     }
144     usesFirstLineRules = usesFirstLineRules || other.usesFirstLineRules;
145     usesFirstLetterRules = usesFirstLetterRules || other.usesFirstLetterRules;
146 }
147
148 void RuleFeatureSet::clear()
149 {
150     idsInRules.clear();
151     idsMatchingAncestorsInRules.clear();
152     classesInRules.clear();
153     attributeCanonicalLocalNamesInRules.clear();
154     attributeLocalNamesInRules.clear();
155     siblingRules.clear();
156     uncommonAttributeRules.clear();
157     ancestorClassRules.clear();
158     ancestorAttributeRulesForHTML.clear();
159     usesFirstLineRules = false;
160     usesFirstLetterRules = false;
161 }
162
163 void RuleFeatureSet::shrinkToFit()
164 {
165     siblingRules.shrinkToFit();
166     uncommonAttributeRules.shrinkToFit();
167     for (auto& rules : ancestorClassRules.values())
168         rules->shrinkToFit();
169     for (auto& rules : ancestorAttributeRulesForHTML.values())
170         rules->features.shrinkToFit();
171 }
172
173 } // namespace WebCore