-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathRuleSet.cpp
More file actions
703 lines (611 loc) · 26.2 KB
/
RuleSet.cpp
File metadata and controls
703 lines (611 loc) · 26.2 KB
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
/*
* 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-2021 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 "CSSPositionTryRule.h"
#include "CSSSelector.h"
#include "CSSSelectorList.h"
#include "CSSViewTransitionRule.h"
#include "CommonAtomStrings.h"
#include "HTMLNames.h"
#include "MediaQueryEvaluator.h"
#include "MutableCSSSelector.h"
#include "RuleSetBuilder.h"
#include "SVGElement.h"
#include "ScriptExecutionContext.h"
#include "SecurityOrigin.h"
#include "SelectorChecker.h"
#include "SelectorFilter.h"
#include "StyleResolver.h"
#include "StyleRule.h"
#include "StyleRuleImport.h"
#include "StyleSheetContents.h"
#include "UserAgentParts.h"
#include <ranges>
namespace WebCore {
namespace Style {
using namespace HTMLNames;
RuleSet::RuleSet() = default;
RuleSet::~RuleSet() = default;
void RuleSet::addToRuleSet(const AtomString& key, AtomRuleMap& map, const RuleData& ruleData)
{
if (key.isNull())
return;
auto& rules = map.add(key, nullptr).iterator->value;
if (!rules)
rules = makeUnique<RuleDataVector>();
rules->append(ruleData);
}
static unsigned NODELETE rulesCountForName(const RuleSet::AtomRuleMap& map, const AtomString& name)
{
if (const auto* rules = map.get(name))
return rules->size();
return 0;
}
// FIXME: Maybe we can unify both following functions
static bool NODELETE hasHostOrScopePseudoClassSubjectInSelectorList(const CSSSelectorList* selectorList)
{
if (!selectorList)
return false;
for (auto& selector : *selectorList) {
if (selector.isHostPseudoClass() || selector.isScopePseudoClass())
return true;
if (hasHostOrScopePseudoClassSubjectInSelectorList(selector.selectorList()))
return true;
}
return false;
}
static bool isHostSelectorMatchingInShadowTree(const CSSSelector& startSelector)
{
auto isHostSelectorMatchingInShadowTreeInSelectorList = [](const CSSSelectorList* selectorList) {
if (!selectorList || selectorList->isEmpty())
return false;
for (auto& selector : *selectorList) {
if (isHostSelectorMatchingInShadowTree(selector))
return true;
}
return false;
};
bool hasOnlyOneCompound = true;
bool hasHostInLastCompound = false;
for (auto* selector = &startSelector; selector; selector = selector->precedingInComplexSelector()) {
if (selector->match() == CSSSelector::Match::PseudoClass && selector->pseudoClass() == CSSSelector::PseudoClass::Host)
hasHostInLastCompound = true;
if (isHostSelectorMatchingInShadowTreeInSelectorList(selector->selectorList()))
return true;
if (selector->precedingInComplexSelector() && selector->relation() != CSSSelector::Relation::Subselector) {
hasOnlyOneCompound = false;
hasHostInLastCompound = false;
}
}
return !hasOnlyOneCompound && hasHostInLastCompound;
}
static bool shouldHaveBucketForAttributeName(const CSSSelector& attributeSelector)
{
// Don't make buckets for lazy attributes since we don't want to synchronize.
if (attributeSelector.attribute().localNameLowercase() == HTMLNames::styleAttr->localName())
return false;
if (SVGElement::animatableAttributeForName(attributeSelector.attribute().localName()) != nullQName())
return false;
return true;
}
void RuleSet::addRule(const StyleRule& rule, unsigned selectorIndex, unsigned selectorListIndex)
{
RuleData ruleData(rule, selectorIndex, selectorListIndex, m_ruleCount, IsStartingStyle::No);
// This path is used when building invalidation RuleSets, no need to collect features (nullptr CollectionContext).
addRule(WTF::move(ruleData), 0, 0, 0, nullptr);
}
void RuleSet::addRule(RuleData&& ruleData, CascadeLayerIdentifier cascadeLayerIdentifier, ContainerQueryIdentifier containerQueryIdentifier, ScopeRuleIdentifier scopeRuleIdentifier, RuleFeatureSet::CollectionContext* featureCollectionContext)
{
ASSERT(ruleData.position() == m_ruleCount);
++m_ruleCount;
auto storeIdentifier = [&](auto identifier, auto& container) {
if (identifier) {
auto oldSize = container.size();
container.grow(m_ruleCount);
auto newlyAllocated = container.mutableSpan().subspan(oldSize);
std::ranges::fill(newlyAllocated, 0);
container.last() = identifier;
}
};
storeIdentifier(cascadeLayerIdentifier, m_cascadeLayerIdentifierForRulePosition);
storeIdentifier(containerQueryIdentifier, m_containerQueryIdentifierForRulePosition);
storeIdentifier(scopeRuleIdentifier, m_scopeRuleIdentifierForRulePosition);
const auto& scopeRules = scopeRulesFor(ruleData);
auto computeLinkMatchType = [&] {
auto& selector = ruleData.selector();
// General case: no @scope rule or current rule selector is not :scope.
if (scopeRules.isEmpty() || !selector.hasScope())
return SelectorChecker::determineLinkMatchType(selector);
// When current rule is :scope, we need to take into account the @scope selectors to determine the link match type.
Ref scopeRule = scopeRules.last();
return SelectorChecker::determineLinkMatchType(selector, scopeRule.ptr());
};
ruleData.setLinkMatchType(computeLinkMatchType());
if (featureCollectionContext)
m_features.collectFeatures(*featureCollectionContext, ruleData, scopeRules);
unsigned classBucketSize = 0;
const CSSSelector* idSelector = nullptr;
const CSSSelector* tagSelector = nullptr;
const CSSSelector* classSelector = nullptr;
const CSSSelector* attributeSelector = nullptr;
const CSSSelector* linkSelector = nullptr;
const CSSSelector* focusSelector = nullptr;
const CSSSelector* focusVisibleSelector = nullptr;
const CSSSelector* rootElementSelector = nullptr;
const CSSSelector* hostPseudoClassSelector = nullptr;
const CSSSelector* fullscreenPseudoClassSelector = nullptr;
const CSSSelector* customPseudoElementSelector = nullptr;
const CSSSelector* slottedPseudoElementSelector = nullptr;
const CSSSelector* partPseudoElementSelector = nullptr;
const CSSSelector* pickerPseudoElementSelector = nullptr;
const CSSSelector* namedPseudoElementSelector = nullptr;
const CSSSelector* otherPseudoElementSelector = nullptr;
#if ENABLE(VIDEO)
const CSSSelector* cuePseudoElementSelector = nullptr;
#endif
const CSSSelector* selector = &ruleData.selector();
do {
switch (selector->match()) {
case CSSSelector::Match::Id:
idSelector = selector;
break;
case CSSSelector::Match::Class: {
auto& className = selector->value();
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;
}
}
break;
}
case CSSSelector::Match::Exact:
case CSSSelector::Match::Set:
case CSSSelector::Match::List:
case CSSSelector::Match::Hyphen:
case CSSSelector::Match::Contain:
case CSSSelector::Match::Begin:
case CSSSelector::Match::End:
if (shouldHaveBucketForAttributeName(*selector))
attributeSelector = selector;
break;
case CSSSelector::Match::Tag:
if (selector->tagQName().localName() != starAtom())
tagSelector = selector;
break;
case CSSSelector::Match::PseudoElement:
switch (selector->pseudoElement()) {
case CSSSelector::PseudoElement::Picker:
pickerPseudoElementSelector = selector;
break;
case CSSSelector::PseudoElement::UserAgentPart:
case CSSSelector::PseudoElement::UserAgentPartLegacyAlias:
customPseudoElementSelector = selector;
break;
case CSSSelector::PseudoElement::Slotted:
slottedPseudoElementSelector = selector;
break;
case CSSSelector::PseudoElement::Part:
partPseudoElementSelector = selector;
break;
#if ENABLE(VIDEO)
case CSSSelector::PseudoElement::Cue:
cuePseudoElementSelector = selector;
break;
#endif
case CSSSelector::PseudoElement::ViewTransitionGroup:
case CSSSelector::PseudoElement::ViewTransitionImagePair:
case CSSSelector::PseudoElement::ViewTransitionOld:
case CSSSelector::PseudoElement::ViewTransitionNew:
if (selector->stringList()->first() != starAtom())
namedPseudoElementSelector = selector;
break;
default:
otherPseudoElementSelector = selector;
break;
}
break;
case CSSSelector::Match::PseudoClass:
switch (selector->pseudoClass()) {
case CSSSelector::PseudoClass::Link:
case CSSSelector::PseudoClass::Visited:
case CSSSelector::PseudoClass::AnyLink:
linkSelector = selector;
break;
case CSSSelector::PseudoClass::Focus:
focusSelector = selector;
break;
case CSSSelector::PseudoClass::FocusVisible:
focusVisibleSelector = selector;
break;
case CSSSelector::PseudoClass::Host:
hostPseudoClassSelector = selector;
break;
case CSSSelector::PseudoClass::Root:
rootElementSelector = selector;
break;
#if ENABLE(FULLSCREEN_API)
case CSSSelector::PseudoClass::Fullscreen:
case CSSSelector::PseudoClass::InternalInWindowFullscreen:
case CSSSelector::PseudoClass::InternalFullscreenDocument:
case CSSSelector::PseudoClass::InternalAnimatingFullscreenTransition:
fullscreenPseudoClassSelector = selector;
break;
#endif
case CSSSelector::PseudoClass::Scope:
m_hasHostOrScopePseudoClassRulesInUniversalBucket = true;
break;
default:
if (hasHostOrScopePseudoClassSubjectInSelectorList(selector->selectorList()))
m_hasHostOrScopePseudoClassRulesInUniversalBucket = true;
break;
}
break;
case CSSSelector::Match::Unknown:
case CSSSelector::Match::ForgivingUnknown:
case CSSSelector::Match::ForgivingUnknownNestContaining:
case CSSSelector::Match::HasScope:
case CSSSelector::Match::NestingParent:
case CSSSelector::Match::PagePseudoClass:
break;
}
// We only process the subject (rightmost compound selector).
if (selector->relation() != CSSSelector::Relation::Subselector)
break;
selector = selector->precedingInComplexSelector();
} while (selector);
if (!m_hasHostPseudoClassRulesMatchingInShadowTree)
m_hasHostPseudoClassRulesMatchingInShadowTree = isHostSelectorMatchingInShadowTree(ruleData.selector());
#if ENABLE(VIDEO)
if (cuePseudoElementSelector) {
m_cuePseudoRules.append(ruleData);
return;
}
#endif
if (slottedPseudoElementSelector) {
// ::slotted pseudo elements work accross shadow boundary making filtering difficult.
ruleData.disableSelectorFiltering();
m_slottedPseudoElementRules.append(ruleData);
return;
}
if (partPseudoElementSelector) {
// Filtering doesn't work accross shadow boundaries.
ruleData.disableSelectorFiltering();
m_partPseudoElementRules.append(ruleData);
return;
}
auto* userAgentPartSelector = customPseudoElementSelector ? customPseudoElementSelector : pickerPseudoElementSelector;
if (userAgentPartSelector) {
// FIXME: Custom pseudo elements are handled by the shadow tree's selector filter. It doesn't know about the main DOM.
ruleData.disableSelectorFiltering();
auto* previousSelector = userAgentPartSelector->precedingInComplexSelector();
if (previousSelector && previousSelector->match() == CSSSelector::Match::PseudoElement && previousSelector->pseudoElement() == CSSSelector::PseudoElement::Part) {
// Handle selectors like ::part(foo)::placeholder with the part codepath.
m_partPseudoElementRules.append(ruleData);
return;
}
if (previousSelector && previousSelector->match() == CSSSelector::Match::PseudoElement && previousSelector->pseudoElement() == CSSSelector::PseudoElement::Slotted) {
// Handle selectors like ::slotted(select)::picker(select) with the slotted codepath.
ruleData.disableSelectorFiltering();
m_slottedPseudoElementRules.append(ruleData);
return;
}
if (pickerPseudoElementSelector) [[unlikely]] {
// Look up useragentpart="picker(...)".
addToRuleSet(AtomString(makeString("picker("_s, pickerPseudoElementSelector->stringList()->at(0), ')')), m_userAgentPartRules, ruleData);
return;
}
ASSERT(customPseudoElementSelector);
addToRuleSet(customPseudoElementSelector->value(), m_userAgentPartRules, ruleData);
#if ENABLE(VIDEO)
// <https://w3c.github.io/webvtt/#the-cue-pseudo-element>
// * 8.2.1. The ::cue pseudo-element:
// As a special exception, the properties corresponding to the background shorthand,
// when they would have been applied to the list of WebVTT Node Objects, must instead
// be applied to the WebVTT cue background box.
// To implement this exception, clone rules whose selector matches the `::cue` (a.k.a,
// `user-agent-part="cue"`), and replace the selector with one that matches the cue background
// box (a.k.a. `user-agent-part="internal-cue-background"`).
if (customPseudoElementSelector->value() == UserAgentParts::cue()
&& customPseudoElementSelector->argument() == nullAtom()) {
std::unique_ptr cueBackgroundSelector = makeUnique<MutableCSSSelector>(*customPseudoElementSelector);
cueBackgroundSelector->setMatch(CSSSelector::Match::PseudoElement);
cueBackgroundSelector->setPseudoElement(CSSSelector::PseudoElement::UserAgentPart);
cueBackgroundSelector->setValue(UserAgentParts::internalCueBackground());
Ref cueBackgroundStyleRule = StyleRule::create(ruleData.styleRule().properties().immutableCopyIfNeeded(), ruleData.styleRule().hasDocumentSecurityOrigin(), CSSSelectorList { MutableCSSSelectorList::from(WTF::move(cueBackgroundSelector)) });
// Warning: Recursion!
addRule(WTF::move(cueBackgroundStyleRule), 0, 0);
}
#endif
return;
}
if (hostPseudoClassSelector) {
m_hostPseudoClassRules.append(ruleData);
return;
}
if (idSelector) {
addToRuleSet(idSelector->value(), m_idRules, ruleData);
return;
}
if (classSelector) {
addToRuleSet(classSelector->value(), m_classRules, ruleData);
return;
}
if (attributeSelector) {
addToRuleSet(attributeSelector->attribute().localName(), m_attributeLocalNameRules, ruleData);
addToRuleSet(attributeSelector->attribute().localNameLowercase(), m_attributeLowercaseLocalNameRules, ruleData);
return;
}
if (linkSelector) {
m_linkPseudoClassRules.append(ruleData);
return;
}
if (focusSelector) {
m_focusPseudoClassRules.append(ruleData);
return;
}
if (focusVisibleSelector) {
m_focusVisiblePseudoClassRules.append(ruleData);
return;
}
if (fullscreenPseudoClassSelector) {
m_fullscreenPseudoClassRules.append(ruleData);
return;
}
if (namedPseudoElementSelector) {
addToRuleSet(namedPseudoElementSelector->stringList()->first(), m_namedPseudoElementRules, ruleData);
return;
}
if (rootElementSelector) {
m_rootElementRules.append(ruleData);
return;
}
if (tagSelector) {
addToRuleSet(tagSelector->tagQName().localName(), m_tagLocalNameRules, ruleData);
addToRuleSet(tagSelector->tagLowercaseLocalName(), m_tagLowercaseLocalNameRules, ruleData);
return;
}
auto addUniversalPseudoElement = [&] {
if (!otherPseudoElementSelector)
return false;
// Check this is a simple selector like "::marker" that applies to HTML elements.
if (otherPseudoElementSelector->precedingInComplexSelector())
return false;
bool isHTMLNamespace = false;
auto* last = otherPseudoElementSelector->lastInCompound();
if (last->precedingInComplexSelector() == otherPseudoElementSelector) {
// Check that implicit * is present with the right namespace and nothing else.
if (last->match() != CSSSelector::Match::Tag)
return false;
ASSERT(last->tagQName().localName() == starAtom());
auto& namespaceURI = last->tagQName().namespaceURI();
isHTMLNamespace = namespaceURI == xhtmlNamespaceURI;
if (!isHTMLNamespace && namespaceURI != starAtom())
return false;
} else if (last != otherPseudoElementSelector)
return false;
auto stylePseudoElement = CSSSelector::stylePseudoElementTypeFor(otherPseudoElementSelector->pseudoElement());
if (!stylePseudoElement)
return false;
m_universalPseudoElementRules.append(ruleData);
m_universalHTMLPseudoElementTypes.add(*stylePseudoElement);
if (!isHTMLNamespace)
m_universalPseudoElementTypes.add(*stylePseudoElement);
return true;
};
if (addUniversalPseudoElement())
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::setViewTransitionRule(StyleRuleViewTransition& rule)
{
m_viewTransitionRule = rule;
}
RefPtr<StyleRuleViewTransition> RuleSet::viewTransitionRule() const
{
return m_viewTransitionRule;
}
template<typename Function>
void RuleSet::traverseRuleDatas(Function&& function)
{
auto traverseVector = [&](auto& vector) {
for (auto& ruleData : vector)
function(ruleData);
};
auto traverseMap = [&](auto& map) {
for (auto& ruleDatas : map.values())
traverseVector(*ruleDatas);
};
traverseMap(m_idRules);
traverseMap(m_classRules);
traverseMap(m_attributeLocalNameRules);
traverseMap(m_attributeLowercaseLocalNameRules);
traverseMap(m_tagLocalNameRules);
traverseMap(m_tagLowercaseLocalNameRules);
traverseMap(m_userAgentPartRules);
traverseMap(m_namedPseudoElementRules);
traverseVector(m_linkPseudoClassRules);
#if ENABLE(VIDEO)
traverseVector(m_cuePseudoRules);
#endif
traverseVector(m_hostPseudoClassRules);
traverseVector(m_slottedPseudoElementRules);
traverseVector(m_partPseudoElementRules);
traverseVector(m_focusPseudoClassRules);
traverseVector(m_focusVisiblePseudoClassRules);
traverseVector(m_fullscreenPseudoClassRules);
traverseVector(m_rootElementRules);
traverseVector(m_universalRules);
traverseVector(m_universalPseudoElementRules);
}
template<typename Function> void RuleSet::traverseRuleDatas(Function&& function) const
{
const_cast<RuleSet&>(*this).traverseRuleDatas([&](const RuleData& ruleData) {
function(ruleData);
});
}
std::optional<DynamicMediaQueryEvaluationChanges> RuleSet::evaluateDynamicMediaQueryRules(const MQ::MediaQueryEvaluator& evaluator)
{
auto collectedChanges = evaluateDynamicMediaQueryRules(evaluator, 0);
if (collectedChanges.requiredFullReset)
return { { DynamicMediaQueryEvaluationChanges::Type::ResetStyle } };
if (collectedChanges.changedQueryIndexes.isEmpty())
return { };
auto& ruleSet = m_mediaQueryInvalidationRuleSetCache.ensure(collectedChanges.changedQueryIndexes, [&] {
auto ruleSet = RuleSet::create();
RuleSetBuilder builder(ruleSet, MQ::MediaQueryEvaluator { screenAtom(), MQ::EvaluationResult::True });
for (auto* rules : collectedChanges.affectedRules) {
for (auto& rule : *rules)
builder.addStyleRule(rule);
}
return ruleSet;
}).iterator->value;
return { { DynamicMediaQueryEvaluationChanges::Type::InvalidateStyle, { { ruleSet.copyRef() } } } };
}
RuleSet::CollectedMediaQueryChanges RuleSet::evaluateDynamicMediaQueryRules(const MQ::MediaQueryEvaluator& evaluator, size_t startIndex)
{
CollectedMediaQueryChanges collectedChanges;
HashMap<size_t, bool, DefaultHash<size_t>, WTF::UnsignedWithZeroKeyHashTraits<size_t>> affectedRulePositionsAndResults;
for (size_t i = startIndex; i < m_dynamicMediaQueryRules.size(); ++i) {
auto& dynamicRules = m_dynamicMediaQueryRules[i];
bool result = true;
for (auto& queryList : dynamicRules.mediaQueries) {
if (!evaluator.evaluate(queryList)) {
result = false;
break;
}
}
if (result != dynamicRules.result) {
dynamicRules.result = result;
if (dynamicRules.requiresFullReset) {
collectedChanges.requiredFullReset = true;
continue;
}
for (auto position : dynamicRules.affectedRulePositions)
affectedRulePositionsAndResults.add(position, result);
collectedChanges.changedQueryIndexes.append(i);
collectedChanges.affectedRules.append(&dynamicRules.affectedRules);
}
}
if (affectedRulePositionsAndResults.isEmpty())
return collectedChanges;
traverseRuleDatas([&](RuleData& ruleData) {
if (auto result = affectedRulePositionsAndResults.getOptional(ruleData.position()))
ruleData.setEnabled(*result);
});
return collectedChanges;
}
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_attributeLocalNameRules);
shrinkMapVectorsToFit(m_attributeLowercaseLocalNameRules);
shrinkMapVectorsToFit(m_tagLocalNameRules);
shrinkMapVectorsToFit(m_tagLowercaseLocalNameRules);
shrinkMapVectorsToFit(m_userAgentPartRules);
shrinkMapVectorsToFit(m_namedPseudoElementRules);
m_linkPseudoClassRules.shrinkToFit();
#if ENABLE(VIDEO)
m_cuePseudoRules.shrinkToFit();
#endif
m_hostPseudoClassRules.shrinkToFit();
m_slottedPseudoElementRules.shrinkToFit();
m_partPseudoElementRules.shrinkToFit();
m_focusPseudoClassRules.shrinkToFit();
m_focusVisiblePseudoClassRules.shrinkToFit();
m_fullscreenPseudoClassRules.shrinkToFit();
m_rootElementRules.shrinkToFit();
m_universalRules.shrinkToFit();
m_universalPseudoElementRules.shrinkToFit();
m_pageRules.shrinkToFit();
m_features.shrinkToFit();
for (auto& rule : m_dynamicMediaQueryRules)
rule.shrinkToFit();
m_dynamicMediaQueryRules.shrinkToFit();
m_cascadeLayers.shrinkToFit();
m_cascadeLayerIdentifierForRulePosition.shrinkToFit();
m_containerQueries.shrinkToFit();
m_containerQueryIdentifierForRulePosition.shrinkToFit();
m_resolverMutatingRulesInLayers.shrinkToFit();
}
Vector<Ref<const StyleRuleContainer>> RuleSet::containerQueryRules() const
{
return m_containerQueries.map([](auto& entry) {
return entry.containerRule;
});
}
Vector<Ref<const StyleRuleScope>> RuleSet::scopeRulesFor(const RuleData& ruleData) const
{
if (m_scopeRuleIdentifierForRulePosition.size() <= ruleData.position())
return { };
Vector<Ref<const StyleRuleScope>> queries;
auto identifier = m_scopeRuleIdentifierForRulePosition[ruleData.position()];
while (identifier) {
auto& query = m_scopeRules[identifier - 1];
queries.append(query.scopeRule);
identifier = query.parent;
};
// Order scopes from outermost to innermost.
queries.reverse();
return queries;
}
const RefPtr<const StyleRulePositionTry> RuleSet::positionTryRuleForName(const AtomString& name) const
{
return m_positionTryRules.get(name);
}
String RuleSet::selectorsForDebugging() const
{
TextStream ts;
ts << "RuleSet size " << ruleCount();
ts.nextLine();
traverseRuleDatas([&](auto& ruleData) {
ts << ruleData.selector().selectorText();
ts.nextLine();
});
return ts.release();
}
} // namespace Style
} // namespace WebCore