1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
/*
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 2004-2005 Allan Sandfeld Jensen (kde@carewolf.com)
* Copyright (C) 2006, 2007 Nicholas Shanks (webkit@nickshanks.com)
* Copyright (C) 2005-2014 Apple Inc. All rights reserved.
* Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
* Copyright (C) 2007, 2008 Eric Seidel <eric@webkit.org>
* Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
* Copyright (c) 2011, Code Aurora Forum. All rights reserved.
* Copyright (C) Research In Motion Limited 2011. All rights reserved.
* Copyright (C) 2012 Google Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "RuleSet.h"
#include "CSSFontSelector.h"
#include "CSSKeyframesRule.h"
#include "CSSSelector.h"
#include "CSSSelectorList.h"
#include "HTMLNames.h"
#include "MediaQueryEvaluator.h"
#include "SecurityOrigin.h"
#include "SelectorChecker.h"
#include "SelectorFilter.h"
#include "StyleResolver.h"
#include "StyleRule.h"
#include "StyleRuleImport.h"
#include "StyleSheetContents.h"
#if ENABLE(VIDEO_TRACK)
#include "TextTrackCue.h"
#endif
namespace WebCore {
using namespace HTMLNames;
// -----------------------------------------------------------------
static inline MatchBasedOnRuleHash computeMatchBasedOnRuleHash(const CSSSelector& selector)
{
if (selector.tagHistory())
return MatchBasedOnRuleHash::None;
if (selector.match() == CSSSelector::Tag) {
const QualifiedName& tagQualifiedName = selector.tagQName();
const AtomicString& selectorNamespace = tagQualifiedName.namespaceURI();
if (selectorNamespace == starAtom || selectorNamespace == xhtmlNamespaceURI) {
if (tagQualifiedName == anyQName())
return MatchBasedOnRuleHash::Universal;
return MatchBasedOnRuleHash::ClassC;
}
return MatchBasedOnRuleHash::None;
}
if (SelectorChecker::isCommonPseudoClassSelector(&selector))
return MatchBasedOnRuleHash::ClassB;
#if ENABLE(SHADOW_DOM)
if (selector.match() == CSSSelector::PseudoClass && selector.pseudoClassType() == CSSSelector::PseudoClassHost)
return MatchBasedOnRuleHash::ClassB;
#endif
if (selector.match() == CSSSelector::Id)
return MatchBasedOnRuleHash::ClassA;
if (selector.match() == CSSSelector::Class)
return MatchBasedOnRuleHash::ClassB;
return MatchBasedOnRuleHash::None;
}
static bool selectorCanMatchPseudoElement(const CSSSelector& rootSelector)
{
const CSSSelector* selector = &rootSelector;
do {
if (selector->matchesPseudoElement())
return true;
if (const CSSSelectorList* selectorList = selector->selectorList()) {
for (const CSSSelector* subSelector = selectorList->first(); subSelector; subSelector = CSSSelectorList::next(subSelector)) {
if (selectorCanMatchPseudoElement(*subSelector))
return true;
}
}
selector = selector->tagHistory();
} while (selector);
return false;
}
static inline bool isCommonAttributeSelectorAttribute(const QualifiedName& attribute)
{
// These are explicitly tested for equality in canShareStyleWithElement.
return attribute == typeAttr || attribute == readonlyAttr;
}
static bool containsUncommonAttributeSelector(const CSSSelector& rootSelector, bool matchesRightmostElement)
{
const CSSSelector* selector = &rootSelector;
do {
if (selector->isAttributeSelector()) {
// FIXME: considering non-rightmost simple selectors is necessary because of the style sharing of cousins.
// It is a primitive solution which disable a lot of style sharing on pages that rely on attributes for styling.
// We should investigate better ways of doing this.
if (!isCommonAttributeSelectorAttribute(selector->attribute()) || !matchesRightmostElement)
return true;
}
if (const CSSSelectorList* selectorList = selector->selectorList()) {
for (const CSSSelector* subSelector = selectorList->first(); subSelector; subSelector = CSSSelectorList::next(subSelector)) {
if (containsUncommonAttributeSelector(*subSelector, matchesRightmostElement))
return true;
}
}
if (selector->relation() != CSSSelector::SubSelector)
matchesRightmostElement = false;
selector = selector->tagHistory();
} while (selector);
return false;
}
static inline bool containsUncommonAttributeSelector(const CSSSelector& rootSelector)
{
return containsUncommonAttributeSelector(rootSelector, true);
}
static inline PropertyWhitelistType determinePropertyWhitelistType(const AddRuleFlags addRuleFlags, const CSSSelector* selector)
{
if (addRuleFlags & RuleIsInRegionRule)
return PropertyWhitelistRegion;
#if ENABLE(VIDEO_TRACK)
for (const CSSSelector* component = selector; component; component = component->tagHistory()) {
if (component->match() == CSSSelector::PseudoElement && (component->pseudoElementType() == CSSSelector::PseudoElementCue || component->value() == TextTrackCue::cueShadowPseudoId()))
return PropertyWhitelistCue;
}
#else
UNUSED_PARAM(selector);
#endif
return PropertyWhitelistNone;
}
RuleData::RuleData(StyleRule* rule, unsigned selectorIndex, unsigned position, AddRuleFlags addRuleFlags)
: m_rule(rule)
, m_selectorIndex(selectorIndex)
, m_hasDocumentSecurityOrigin(addRuleFlags & RuleHasDocumentSecurityOrigin)
, m_position(position)
, m_matchBasedOnRuleHash(static_cast<unsigned>(computeMatchBasedOnRuleHash(*selector())))
, m_canMatchPseudoElement(selectorCanMatchPseudoElement(*selector()))
, m_containsUncommonAttributeSelector(WebCore::containsUncommonAttributeSelector(*selector()))
, m_linkMatchType(SelectorChecker::determineLinkMatchType(selector()))
, m_propertyWhitelistType(determinePropertyWhitelistType(addRuleFlags, selector()))
#if ENABLE(CSS_SELECTOR_JIT) && CSS_SELECTOR_JIT_PROFILING
, m_compiledSelectorUseCount(0)
#endif
{
ASSERT(m_position == position);
ASSERT(m_selectorIndex == selectorIndex);
SelectorFilter::collectIdentifierHashes(selector(), m_descendantSelectorIdentifierHashes, maximumIdentifierCount);
}
RuleSet::RuleSet()
{
}
RuleSet::~RuleSet()
{
}
void RuleSet::addToRuleSet(AtomicStringImpl* key, AtomRuleMap& map, const RuleData& ruleData)
{
if (!key)
return;
auto& rules = map.add(key, nullptr).iterator->value;
if (!rules)
rules = std::make_unique<RuleDataVector>();
rules->append(ruleData);
}
static unsigned rulesCountForName(const RuleSet::AtomRuleMap& map, AtomicStringImpl* name)
{
if (const auto* rules = map.get(name))
return rules->size();
return 0;
}
void RuleSet::addRule(StyleRule* rule, unsigned selectorIndex, AddRuleFlags addRuleFlags)
{
RuleData ruleData(rule, selectorIndex, m_ruleCount++, addRuleFlags);
m_features.collectFeatures(ruleData);
unsigned classBucketSize = 0;
const CSSSelector* tagSelector = nullptr;
const CSSSelector* classSelector = nullptr;
const CSSSelector* linkSelector = nullptr;
const CSSSelector* focusSelector = nullptr;
const CSSSelector* selector = ruleData.selector();
do {
if (selector->match() == CSSSelector::Id) {
addToRuleSet(selector->value().impl(), m_idRules, ruleData);
return;
}
#if ENABLE(VIDEO_TRACK)
if (selector->match() == CSSSelector::PseudoElement && selector->pseudoElementType() == CSSSelector::PseudoElementCue) {
m_cuePseudoRules.append(ruleData);
return;
}
#endif
if (selector->isCustomPseudoElement()) {
// FIXME: Custom pseudo elements are handled by the shadow tree's selector filter. It doesn't know about the main DOM.
ruleData.disableSelectorFiltering();
addToRuleSet(selector->value().impl(), m_shadowPseudoElementRules, ruleData);
return;
}
if (selector->match() == CSSSelector::Class) {
AtomicStringImpl* className = selector->value().impl();
if (!classSelector) {
classSelector = selector;
classBucketSize = rulesCountForName(m_classRules, className);
} else if (classBucketSize) {
unsigned newClassBucketSize = rulesCountForName(m_classRules, className);
if (newClassBucketSize < classBucketSize) {
classSelector = selector;
classBucketSize = newClassBucketSize;
}
}
}
if (selector->match() == CSSSelector::Tag && selector->tagQName().localName() != starAtom)
tagSelector = selector;
if (SelectorChecker::isCommonPseudoClassSelector(selector)) {
switch (selector->pseudoClassType()) {
case CSSSelector::PseudoClassLink:
case CSSSelector::PseudoClassVisited:
case CSSSelector::PseudoClassAnyLink:
case CSSSelector::PseudoClassAnyLinkDeprecated:
linkSelector = selector;
break;
case CSSSelector::PseudoClassFocus:
focusSelector = selector;
break;
default:
ASSERT_NOT_REACHED();
}
}
#if ENABLE(SHADOW_DOM)
if (selector->match() == CSSSelector::PseudoClass && selector->pseudoClassType() == CSSSelector::PseudoClassHost) {
m_hostPseudoClassRules.append(ruleData);
return;
}
#endif
if (selector->relation() != CSSSelector::SubSelector)
break;
selector = selector->tagHistory();
} while (selector);
if (classSelector) {
addToRuleSet(classSelector->value().impl(), m_classRules, ruleData);
return;
}
if (linkSelector) {
m_linkPseudoClassRules.append(ruleData);
return;
}
if (focusSelector) {
m_focusPseudoClassRules.append(ruleData);
return;
}
if (tagSelector) {
addToRuleSet(tagSelector->tagQName().localName().impl(), m_tagLocalNameRules, ruleData);
addToRuleSet(tagSelector->tagLowercaseLocalName().impl(), m_tagLowercaseLocalNameRules, ruleData);
return;
}
// If we didn't find a specialized map to stick it in, file under universal rules.
m_universalRules.append(ruleData);
}
void RuleSet::addPageRule(StyleRulePage* rule)
{
m_pageRules.append(rule);
}
void RuleSet::addRegionRule(StyleRuleRegion* regionRule, bool hasDocumentSecurityOrigin)
{
auto regionRuleSet = std::make_unique<RuleSet>();
// The region rule set should take into account the position inside the parent rule set.
// Otherwise, the rules inside region block might be incorrectly positioned before other similar rules from
// the stylesheet that contains the region block.
regionRuleSet->m_ruleCount = m_ruleCount;
// Collect the region rules into a rule set
// FIXME: Should this add other types of rules? (i.e. use addChildRules() directly?)
const Vector<RefPtr<StyleRuleBase>>& childRules = regionRule->childRules();
AddRuleFlags addRuleFlags = hasDocumentSecurityOrigin ? RuleHasDocumentSecurityOrigin : RuleHasNoSpecialState;
addRuleFlags = static_cast<AddRuleFlags>(addRuleFlags | RuleIsInRegionRule);
for (auto& childRule : childRules) {
if (is<StyleRule>(*childRule))
regionRuleSet->addStyleRule(downcast<StyleRule>(childRule.get()), addRuleFlags);
}
// Update the "global" rule count so that proper order is maintained
m_ruleCount = regionRuleSet->m_ruleCount;
m_regionSelectorsAndRuleSets.append(RuleSetSelectorPair(regionRule->selectorList().first(), WTFMove(regionRuleSet)));
}
void RuleSet::addChildRules(const Vector<RefPtr<StyleRuleBase>>& rules, const MediaQueryEvaluator& medium, StyleResolver* resolver, bool hasDocumentSecurityOrigin, bool isInitiatingElementInUserAgentShadowTree, AddRuleFlags addRuleFlags)
{
for (auto& rule : rules) {
if (is<StyleRule>(*rule))
addStyleRule(downcast<StyleRule>(rule.get()), addRuleFlags);
else if (is<StyleRulePage>(*rule))
addPageRule(downcast<StyleRulePage>(rule.get()));
else if (is<StyleRuleMedia>(*rule)) {
auto& mediaRule = downcast<StyleRuleMedia>(*rule);
if ((!mediaRule.mediaQueries() || medium.eval(mediaRule.mediaQueries(), resolver)))
addChildRules(mediaRule.childRules(), medium, resolver, hasDocumentSecurityOrigin, isInitiatingElementInUserAgentShadowTree, addRuleFlags);
} else if (is<StyleRuleFontFace>(*rule) && resolver) {
// Add this font face to our set.
resolver->document().fontSelector().addFontFaceRule(downcast<StyleRuleFontFace>(*rule.get()), isInitiatingElementInUserAgentShadowTree);
resolver->invalidateMatchedPropertiesCache();
} else if (is<StyleRuleKeyframes>(*rule) && resolver)
resolver->addKeyframeStyle(downcast<StyleRuleKeyframes>(rule.get()));
else if (is<StyleRuleSupports>(*rule) && downcast<StyleRuleSupports>(*rule).conditionIsSupported())
addChildRules(downcast<StyleRuleSupports>(*rule).childRules(), medium, resolver, hasDocumentSecurityOrigin, isInitiatingElementInUserAgentShadowTree, addRuleFlags);
#if ENABLE(CSS_REGIONS)
else if (is<StyleRuleRegion>(*rule) && resolver) {
addRegionRule(downcast<StyleRuleRegion>(rule.get()), hasDocumentSecurityOrigin);
}
#endif
#if ENABLE(CSS_DEVICE_ADAPTATION)
else if (is<StyleRuleViewport>(*rule) && resolver) {
resolver->viewportStyleResolver()->addViewportRule(downcast<StyleRuleViewport>(rule.get()));
}
#endif
}
}
void RuleSet::addRulesFromSheet(StyleSheetContents& sheet, const MediaQueryEvaluator& medium, StyleResolver* resolver)
{
for (auto& rule : sheet.importRules()) {
if (rule->styleSheet() && (!rule->mediaQueries() || medium.eval(rule->mediaQueries(), resolver)))
addRulesFromSheet(*rule->styleSheet(), medium, resolver);
}
bool hasDocumentSecurityOrigin = resolver && resolver->document().securityOrigin()->canRequest(sheet.baseURL());
AddRuleFlags addRuleFlags = static_cast<AddRuleFlags>((hasDocumentSecurityOrigin ? RuleHasDocumentSecurityOrigin : 0));
// FIXME: Skip Content Security Policy check when stylesheet is in a user agent shadow tree.
// See <https://bugs.webkit.org/show_bug.cgi?id=146663>.
bool isInitiatingElementInUserAgentShadowTree = false;
addChildRules(sheet.childRules(), medium, resolver, hasDocumentSecurityOrigin, isInitiatingElementInUserAgentShadowTree, addRuleFlags);
if (m_autoShrinkToFitEnabled)
shrinkToFit();
}
void RuleSet::addStyleRule(StyleRule* rule, AddRuleFlags addRuleFlags)
{
for (size_t selectorIndex = 0; selectorIndex != notFound; selectorIndex = rule->selectorList().indexOfNextSelectorAfter(selectorIndex))
addRule(rule, selectorIndex, addRuleFlags);
}
bool RuleSet::hasShadowPseudoElementRules() const
{
if (!m_shadowPseudoElementRules.isEmpty())
return true;
#if ENABLE(VIDEO_TRACK)
if (!m_cuePseudoRules.isEmpty())
return true;
#endif
return false;
}
void RuleSet::copyShadowPseudoElementRulesFrom(const RuleSet& other)
{
for (auto& keyValuePair : other.m_shadowPseudoElementRules)
m_shadowPseudoElementRules.add(keyValuePair.key, std::make_unique<RuleDataVector>(*keyValuePair.value));
#if ENABLE(VIDEO_TRACK)
// FIXME: We probably shouldn't treat WebVTT as author stylable user agent shadow tree.
for (auto& cue : other.m_cuePseudoRules)
m_cuePseudoRules.append(cue);
#endif
}
static inline void shrinkMapVectorsToFit(RuleSet::AtomRuleMap& map)
{
for (auto& vector : map.values())
vector->shrinkToFit();
}
void RuleSet::shrinkToFit()
{
shrinkMapVectorsToFit(m_idRules);
shrinkMapVectorsToFit(m_classRules);
shrinkMapVectorsToFit(m_tagLocalNameRules);
shrinkMapVectorsToFit(m_tagLowercaseLocalNameRules);
shrinkMapVectorsToFit(m_shadowPseudoElementRules);
m_linkPseudoClassRules.shrinkToFit();
#if ENABLE(VIDEO_TRACK)
m_cuePseudoRules.shrinkToFit();
#endif
m_focusPseudoClassRules.shrinkToFit();
m_universalRules.shrinkToFit();
m_pageRules.shrinkToFit();
m_features.shrinkToFit();
m_regionSelectorsAndRuleSets.shrinkToFit();
}
} // namespace WebCore
|