Add modern API for overriding the page's specified viewport configuration
[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 static bool isSiblingOrSubject(MatchElement matchElement)
39 {
40     switch (matchElement) {
41     case MatchElement::Subject:
42     case MatchElement::IndirectSibling:
43     case MatchElement::DirectSibling:
44     case MatchElement::AnySibling:
45         return true;
46     case MatchElement::Parent:
47     case MatchElement::Ancestor:
48     case MatchElement::ParentSibling:
49     case MatchElement::AncestorSibling:
50     case MatchElement::Host:
51         return false;
52     }
53     ASSERT_NOT_REACHED();
54     return false;
55 }
56
57 MatchElement RuleFeatureSet::computeNextMatchElement(MatchElement matchElement, CSSSelector::RelationType relation)
58 {
59     if (isSiblingOrSubject(matchElement)) {
60         switch (relation) {
61         case CSSSelector::Subselector:
62             return matchElement;
63         case CSSSelector::DescendantSpace:
64             return MatchElement::Ancestor;
65         case CSSSelector::Child:
66             return MatchElement::Parent;
67         case CSSSelector::IndirectAdjacent:
68             if (matchElement == MatchElement::AnySibling)
69                 return MatchElement::AnySibling;
70             return MatchElement::IndirectSibling;
71         case CSSSelector::DirectAdjacent:
72             if (matchElement == MatchElement::AnySibling)
73                 return MatchElement::AnySibling;
74             return matchElement == MatchElement::Subject ? MatchElement::DirectSibling : MatchElement::IndirectSibling;
75         case CSSSelector::ShadowDescendant:
76             return MatchElement::Host;
77         };
78     }
79     switch (relation) {
80     case CSSSelector::Subselector:
81         return matchElement;
82     case CSSSelector::DescendantSpace:
83     case CSSSelector::Child:
84         return MatchElement::Ancestor;
85     case CSSSelector::IndirectAdjacent:
86     case CSSSelector::DirectAdjacent:
87         return matchElement == MatchElement::Parent ? MatchElement::ParentSibling : MatchElement::AncestorSibling;
88     case CSSSelector::ShadowDescendant:
89         return MatchElement::Host;
90     };
91     ASSERT_NOT_REACHED();
92     return matchElement;
93 };
94
95 MatchElement RuleFeatureSet::computeSubSelectorMatchElement(MatchElement matchElement, const CSSSelector& selector)
96 {
97     ASSERT(selector.selectorList());
98
99     if (selector.match() == CSSSelector::PseudoClass) {
100         auto type = selector.pseudoClassType();
101         // For :nth-child(n of .some-subselector) where an element change may affect other elements similar to sibling combinators.
102         if (type == CSSSelector::PseudoClassNthChild || type == CSSSelector::PseudoClassNthLastChild)
103             return MatchElement::AnySibling;
104
105         // Similarly for :host().
106         if (type == CSSSelector::PseudoClassHost)
107             return MatchElement::Host;
108     }
109     if (selector.match() == CSSSelector::PseudoElement) {
110         // Similarly for ::slotted().
111         if (selector.pseudoElementType() == CSSSelector::PseudoElementSlotted)
112             return MatchElement::Host;
113     }
114
115     return matchElement;
116 };
117
118 void RuleFeatureSet::recursivelyCollectFeaturesFromSelector(SelectorFeatures& selectorFeatures, const CSSSelector& firstSelector, MatchElement matchElement)
119 {
120     const CSSSelector* selector = &firstSelector;
121     do {
122         if (selector->match() == CSSSelector::Id) {
123             idsInRules.add(selector->value());
124             if (matchElement == MatchElement::Parent || matchElement == MatchElement::Ancestor)
125                 idsMatchingAncestorsInRules.add(selector->value());
126         } else if (selector->match() == CSSSelector::Class)
127             selectorFeatures.classes.append(std::make_pair(selector->value(), matchElement));
128         else if (selector->isAttributeSelector()) {
129             auto& canonicalLocalName = selector->attributeCanonicalLocalName();
130             auto& localName = selector->attribute().localName();
131             attributeCanonicalLocalNamesInRules.add(canonicalLocalName);
132             attributeLocalNamesInRules.add(localName);
133             selectorFeatures.attributes.append(std::make_pair(selector, matchElement));
134         } else if (selector->match() == CSSSelector::PseudoElement) {
135             switch (selector->pseudoElementType()) {
136             case CSSSelector::PseudoElementFirstLine:
137                 usesFirstLineRules = true;
138                 break;
139             case CSSSelector::PseudoElementFirstLetter:
140                 usesFirstLetterRules = true;
141                 break;
142             default:
143                 break;
144             }
145         }
146
147         if (!selectorFeatures.hasSiblingSelector && selector->isSiblingSelector())
148             selectorFeatures.hasSiblingSelector = true;
149
150         if (const CSSSelectorList* selectorList = selector->selectorList()) {
151             auto subSelectorMatchElement = computeSubSelectorMatchElement(matchElement, *selector);
152
153             for (const CSSSelector* subSelector = selectorList->first(); subSelector; subSelector = CSSSelectorList::next(subSelector)) {
154                 if (!selectorFeatures.hasSiblingSelector && selector->isSiblingSelector())
155                     selectorFeatures.hasSiblingSelector = true;
156                 recursivelyCollectFeaturesFromSelector(selectorFeatures, *subSelector, subSelectorMatchElement);
157             }
158         }
159
160         matchElement = computeNextMatchElement(matchElement, selector->relation());
161
162         selector = selector->tagHistory();
163     } while (selector);
164 }
165
166 void RuleFeatureSet::collectFeatures(const RuleData& ruleData)
167 {
168     SelectorFeatures selectorFeatures;
169     recursivelyCollectFeaturesFromSelector(selectorFeatures, *ruleData.selector());
170     if (selectorFeatures.hasSiblingSelector)
171         siblingRules.append(RuleFeature(ruleData.rule(), ruleData.selectorIndex(), ruleData.selectorListIndex()));
172     if (ruleData.containsUncommonAttributeSelector())
173         uncommonAttributeRules.append(RuleFeature(ruleData.rule(), ruleData.selectorIndex(), ruleData.selectorListIndex()));
174
175     for (auto& nameAndMatch : selectorFeatures.classes) {
176         classRules.ensure(nameAndMatch.first, [] {
177             return std::make_unique<Vector<RuleFeature>>();
178         }).iterator->value->append(RuleFeature(ruleData.rule(), ruleData.selectorIndex(), ruleData.selectorListIndex(), nameAndMatch.second));
179         if (nameAndMatch.second == MatchElement::Host)
180             classesAffectingHost.add(nameAndMatch.first);
181     }
182     for (auto& selectorAndMatch : selectorFeatures.attributes) {
183         auto* selector = selectorAndMatch.first;
184         auto matchElement = selectorAndMatch.second;
185         attributeRules.ensure(selector->attribute().localName().convertToASCIILowercase(), [] {
186             return std::make_unique<Vector<RuleFeature>>();
187         }).iterator->value->append(RuleFeature(ruleData.rule(), ruleData.selectorIndex(), ruleData.selectorListIndex(), matchElement, selector));
188         if (matchElement == MatchElement::Host)
189             attributesAffectingHost.add(selector->attribute().localName().convertToASCIILowercase());
190     }
191 }
192
193 void RuleFeatureSet::add(const RuleFeatureSet& other)
194 {
195     idsInRules.add(other.idsInRules.begin(), other.idsInRules.end());
196     idsMatchingAncestorsInRules.add(other.idsMatchingAncestorsInRules.begin(), other.idsMatchingAncestorsInRules.end());
197     attributeCanonicalLocalNamesInRules.add(other.attributeCanonicalLocalNamesInRules.begin(), other.attributeCanonicalLocalNamesInRules.end());
198     attributeLocalNamesInRules.add(other.attributeLocalNamesInRules.begin(), other.attributeLocalNamesInRules.end());
199     contentAttributeNamesInRules.add(other.contentAttributeNamesInRules.begin(), other.contentAttributeNamesInRules.end());
200     siblingRules.appendVector(other.siblingRules);
201     uncommonAttributeRules.appendVector(other.uncommonAttributeRules);
202     for (auto& keyValuePair : other.classRules) {
203         classRules.ensure(keyValuePair.key, [] {
204             return std::make_unique<Vector<RuleFeature>>();
205         }).iterator->value->appendVector(*keyValuePair.value);
206     }
207     classesAffectingHost.add(other.classesAffectingHost.begin(), other.classesAffectingHost.end());
208
209     for (auto& keyValuePair : other.attributeRules) {
210         attributeRules.ensure(keyValuePair.key, [] {
211             return std::make_unique<Vector<RuleFeature>>();
212         }).iterator->value->appendVector(*keyValuePair.value);
213     }
214     attributesAffectingHost.add(other.attributesAffectingHost.begin(), other.attributesAffectingHost.end());
215
216     usesFirstLineRules = usesFirstLineRules || other.usesFirstLineRules;
217     usesFirstLetterRules = usesFirstLetterRules || other.usesFirstLetterRules;
218 }
219
220 void RuleFeatureSet::registerContentAttribute(const AtomicString& attributeName)
221 {
222     contentAttributeNamesInRules.add(attributeName.convertToASCIILowercase());
223     attributeCanonicalLocalNamesInRules.add(attributeName);
224     attributeLocalNamesInRules.add(attributeName);
225 }
226
227 void RuleFeatureSet::clear()
228 {
229     idsInRules.clear();
230     idsMatchingAncestorsInRules.clear();
231     attributeCanonicalLocalNamesInRules.clear();
232     attributeLocalNamesInRules.clear();
233     contentAttributeNamesInRules.clear();
234     siblingRules.clear();
235     uncommonAttributeRules.clear();
236     classRules.clear();
237     classesAffectingHost.clear();
238     attributeRules.clear();
239     attributesAffectingHost.clear();
240     usesFirstLineRules = false;
241     usesFirstLetterRules = false;
242 }
243
244 void RuleFeatureSet::shrinkToFit()
245 {
246     siblingRules.shrinkToFit();
247     uncommonAttributeRules.shrinkToFit();
248     for (auto& rules : classRules.values())
249         rules->shrinkToFit();
250     for (auto& rules : attributeRules.values())
251         rules->shrinkToFit();
252 }
253
254 } // namespace WebCore