File: RuleSetBuilder.cpp

package info (click to toggle)
webkit2gtk 2.48.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 429,764 kB
  • sloc: cpp: 3,697,587; javascript: 194,444; ansic: 169,997; python: 46,499; asm: 19,295; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; sh: 2,098; java: 1,993; lex: 1,327; pascal: 366; makefile: 298
file content (600 lines) | stat: -rw-r--r-- 23,714 bytes parent folder | download | duplicates (6)
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
/*
 * 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-2025 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 "RuleSetBuilder.h"

#include "CSSCounterStyleRegistry.h"
#include "CSSCounterStyleRule.h"
#include "CSSFontSelector.h"
#include "CSSKeyframesRule.h"
#include "CSSPositionTryRule.h"
#include "CSSSelectorParser.h"
#include "CSSViewTransitionRule.h"
#include "CustomPropertyRegistry.h"
#include "Document.h"
#include "DocumentInlines.h"
#include "MediaQueryEvaluator.h"
#include "MutableCSSSelector.h"
#include "StyleResolver.h"
#include "StyleRuleImport.h"
#include "StyleScope.h"
#include "StyleSheetContents.h"
#include <wtf/CryptographicallyRandomNumber.h>

namespace WebCore {
namespace Style {

RuleSetBuilder::RuleSetBuilder(RuleSet& ruleSet, const MQ::MediaQueryEvaluator& evaluator, Resolver* resolver, ShrinkToFit shrinkToFit, ShouldResolveNesting shouldResolveNesting)
    : m_ruleSet(&ruleSet)
    , m_mediaQueryCollector({ evaluator })
    , m_resolver(resolver)
    , m_shrinkToFit(shrinkToFit)
    , m_builderShouldResolveNesting(shouldResolveNesting)
{
}

RuleSetBuilder::RuleSetBuilder(const MQ::MediaQueryEvaluator& evaluator)
    : m_mediaQueryCollector({ evaluator, true })
{
}

RuleSetBuilder::~RuleSetBuilder()
{
    if (!m_ruleSet)
        return;

    updateCascadeLayerPriorities();
    updateDynamicMediaQueries();
    addMutatingRulesToResolver();

    if (m_shrinkToFit == ShrinkToFit::Enable)
        m_ruleSet->shrinkToFit();
}

void RuleSetBuilder::addRulesFromSheet(const StyleSheetContents& sheet, const MQ::MediaQueryList& sheetQuery)
{
    auto canUseDynamicMediaQueryEvaluation = [&] {
        if (!m_resolver)
            return false;

        RuleSetBuilder dynamicEvaluationScanner(m_mediaQueryCollector.evaluator);
        if (dynamicEvaluationScanner.m_mediaQueryCollector.pushAndEvaluate(sheetQuery))
            dynamicEvaluationScanner.addRulesFromSheetContents(sheet);
        dynamicEvaluationScanner.m_mediaQueryCollector.pop(sheetQuery);

        return !dynamicEvaluationScanner.requiresStaticMediaQueryEvaluation;
    };

    m_mediaQueryCollector.collectDynamic = canUseDynamicMediaQueryEvaluation();

    if (m_mediaQueryCollector.pushAndEvaluate(sheetQuery))
        addRulesFromSheetContents(sheet);
    m_mediaQueryCollector.pop(sheetQuery);
}

void RuleSetBuilder::addChildRules(const Vector<Ref<StyleRuleBase>>& rules)
{
    for (auto& rule : rules) {
        if (requiresStaticMediaQueryEvaluation)
            return;
        addChildRule(rule);
    }
}

void RuleSetBuilder::addChildRule(Ref<StyleRuleBase> rule)
{
    switch (rule->type()) {
    case StyleRuleType::StyleWithNesting:
        if (m_ruleSet)
            addStyleRule(uncheckedDowncast<StyleRuleWithNesting>(rule));
        return;

    case StyleRuleType::Style:
        if (m_ruleSet)
            addStyleRule(uncheckedDowncast<StyleRule>(rule));
        return;

    case StyleRuleType::NestedDeclarations:
        if (m_ruleSet)
            addStyleRule(uncheckedDowncast<StyleRuleNestedDeclarations>(rule));
        return;

    case StyleRuleType::Scope: {
        auto scopeRule = uncheckedDowncast<StyleRuleScope>(WTFMove(rule));
        auto previousScopeIdentifier = m_currentScopeIdentifier;
        if (m_ruleSet) {
            m_ruleSet->m_scopeRules.append({ scopeRule.copyRef(), previousScopeIdentifier });
            m_currentScopeIdentifier = m_ruleSet->m_scopeRules.size();
        }

        // https://drafts.csswg.org/css-nesting/#nesting-at-scope
        // For the purposes of the style rules in its body and its own <scope-end> selector,
        // the @scope rule is treated as an ancestor style rule, matching the elements matched by its <scope-start> selector.
        if (m_shouldResolveNestingForSheet) {
            const CSSSelectorList* parentResolvedSelectorList = nullptr;
            if (m_selectorListStack.size())
                parentResolvedSelectorList = m_selectorListStack.last();
            if (!scopeRule->originalScopeStart().isEmpty())
                scopeRule->setScopeStart(CSSSelectorParser::resolveNestingParent(scopeRule->originalScopeStart(), parentResolvedSelectorList));
            if (!scopeRule->originalScopeEnd().isEmpty())
                scopeRule->setScopeEnd(CSSSelectorParser::resolveNestingParent(scopeRule->originalScopeEnd(), &scopeRule->scopeStart()));
        }

        const auto& scopeStart = scopeRule->scopeStart();
        // If <scope-start> is empty, it doesn't create a nesting context (the nesting selector might eventually be replaced by :scope)
        if (!scopeStart.isEmpty())
            m_selectorListStack.append(&scopeStart);
        m_ancestorStack.append(CSSParserEnum::NestedContextType::Scope);
        addChildRules(scopeRule->childRules());
        m_ancestorStack.removeLast();
        if (!scopeStart.isEmpty())
            m_selectorListStack.removeLast();

        if (m_ruleSet)
            m_currentScopeIdentifier = previousScopeIdentifier;
        return;
    }

    case StyleRuleType::Page:
        if (m_ruleSet)
            m_ruleSet->addPageRule(uncheckedDowncast<StyleRulePage>(rule));
        return;

    case StyleRuleType::Media: {
        auto mediaRule = uncheckedDowncast<StyleRuleMedia>(WTFMove(rule));
        if (m_mediaQueryCollector.pushAndEvaluate(mediaRule->mediaQueries()))
            addChildRules(mediaRule->childRules());
        m_mediaQueryCollector.pop(mediaRule->mediaQueries());
        return;
    }

    case StyleRuleType::Container: {
        auto containerRule = uncheckedDowncast<StyleRuleContainer>(WTFMove(rule));
        auto previousContainerQueryIdentifier = m_currentContainerQueryIdentifier;
        if (m_ruleSet) {
            m_ruleSet->m_containerQueries.append({ containerRule.copyRef(), previousContainerQueryIdentifier });
            m_currentContainerQueryIdentifier = m_ruleSet->m_containerQueries.size();
        }
        addChildRules(containerRule->childRules());
        if (m_ruleSet)
            m_currentContainerQueryIdentifier = previousContainerQueryIdentifier;
        return;
    }

    case StyleRuleType::LayerBlock:
    case StyleRuleType::LayerStatement: {
        disallowDynamicMediaQueryEvaluationIfNeeded();

        auto layerRule = uncheckedDowncast<StyleRuleLayer>(WTFMove(rule));
        if (layerRule->isStatement()) {
            // Statement syntax just registers the layers.
            registerLayers(layerRule->nameList());
            return;
        }
        // Block syntax.
        pushCascadeLayer(layerRule->name());
        addChildRules(layerRule->childRules());
        popCascadeLayer(layerRule->name());
        return;
    }

    case StyleRuleType::StartingStyle: {
        SetForScope startingStyleScope { m_isStartingStyle, IsStartingStyle::Yes };
        auto startingStyleRule = uncheckedDowncast<StyleRuleStartingStyle>(WTFMove(rule));
        addChildRules(startingStyleRule->childRules());
        return;
    }

    case StyleRuleType::CounterStyle:
    case StyleRuleType::FontFace:
    case StyleRuleType::FontPaletteValues:
    case StyleRuleType::FontFeatureValues:
    case StyleRuleType::Keyframes:
    case StyleRuleType::Property:
    case StyleRuleType::ViewTransition:
    case StyleRuleType::PositionTry:
        disallowDynamicMediaQueryEvaluationIfNeeded();
        if (m_resolver)
            m_collectedResolverMutatingRules.append({ rule, m_currentCascadeLayerIdentifier });
        return;

    case StyleRuleType::Supports: {
        auto supportsRule = uncheckedDowncast<StyleRuleSupports>(WTFMove(rule));
        if (supportsRule->conditionIsSupported())
            addChildRules(supportsRule->childRules());
        return;
    }

    case StyleRuleType::Import:
    case StyleRuleType::Margin:
    case StyleRuleType::Namespace:
    case StyleRuleType::FontFeatureValuesBlock:
        return;

    case StyleRuleType::Charset:
    case StyleRuleType::Keyframe:
        ASSERT_NOT_REACHED();
        return;
    }
}

void RuleSetBuilder::addRulesFromSheetContents(const StyleSheetContents& sheet)
{
    auto nestingResolveScope = SetForScope { m_shouldResolveNestingForSheet, m_builderShouldResolveNesting == ShouldResolveNesting::Yes && !sheet.hasResolvedNesting() };

    for (auto& rule : sheet.layerRulesBeforeImportRules())
        registerLayers(rule->nameList());

    for (auto& rule : sheet.importRules()) {
        if (!rule->styleSheet())
            continue;

        if (!rule->supportsMatches())
            continue;

        if (m_mediaQueryCollector.pushAndEvaluate(rule->mediaQueries())) {
            auto& cascadeLayerName = rule->cascadeLayerName();
            if (cascadeLayerName) {
                disallowDynamicMediaQueryEvaluationIfNeeded();
                pushCascadeLayer(*cascadeLayerName);
            }

            addRulesFromSheetContents(*rule->styleSheet());

            if (cascadeLayerName)
                popCascadeLayer(*cascadeLayerName);
        }
        m_mediaQueryCollector.pop(rule->mediaQueries());
    }

    addChildRules(sheet.childRules());

    if (m_shouldResolveNestingForSheet)
        sheet.setHasResolvedNesting(true);
}

void RuleSetBuilder::resolveSelectorListWithNesting(StyleRuleWithNesting& rule)
{
    ASSERT(m_shouldResolveNestingForSheet);

    const CSSSelectorList* parentResolvedSelectorList = nullptr;
    if (m_selectorListStack.size())
        parentResolvedSelectorList = m_selectorListStack.last();

    // If it's a top-level rule without a nesting parent selector, keep the selector list as is.
    if (!rule.originalSelectorList().hasExplicitNestingParent() && !parentResolvedSelectorList)
        return;

    auto resolvedSelectorList = CSSSelectorParser::resolveNestingParent(rule.originalSelectorList(), parentResolvedSelectorList);
    rule.wrapperAdoptSelectorList(WTFMove(resolvedSelectorList));
}

void RuleSetBuilder::addStyleRuleWithSelectorList(const CSSSelectorList& selectorList, const StyleRule& rule)
{
    if (!selectorList.isEmpty()) {
        unsigned selectorListIndex = 0;
        for (size_t selectorIndex = 0; selectorIndex != notFound; selectorIndex = selectorList.indexOfNextSelectorAfter(selectorIndex)) {
            RuleData ruleData(rule, selectorIndex, selectorListIndex, m_ruleSet->ruleCount(), m_isStartingStyle);
            m_mediaQueryCollector.addRuleIfNeeded(ruleData);
            m_ruleSet->addRule(WTFMove(ruleData), m_currentCascadeLayerIdentifier, m_currentContainerQueryIdentifier, m_currentScopeIdentifier);
            ++selectorListIndex;
        }
    }
}

void RuleSetBuilder::addStyleRule(StyleRuleWithNesting& rule)
{
    if (m_shouldResolveNestingForSheet)
        resolveSelectorListWithNesting(rule);

    const auto& selectorList = rule.selectorList();
    addStyleRuleWithSelectorList(selectorList, rule);

    // Process nested rules
    m_selectorListStack.append(&selectorList);
    m_ancestorStack.append(CSSParserEnum::NestedContextType::Style);
    for (auto& nestedRule : rule.nestedRules())
        addChildRule(nestedRule);
    m_ancestorStack.removeLast();
    m_selectorListStack.removeLast();
}

void RuleSetBuilder::addStyleRule(const StyleRule& rule)
{
    addStyleRuleWithSelectorList(rule.selectorList(), rule);
}

void RuleSetBuilder::addStyleRule(StyleRuleNestedDeclarations& rule)
{
    auto whereScopeSelector = [] {
        auto scopeSelector = makeUnique<MutableCSSSelector>();
        scopeSelector->setMatch(CSSSelector::Match::PseudoClass);
        scopeSelector->setPseudoClass(CSSSelector::PseudoClass::Scope);
        auto whereSelector = makeUnique<MutableCSSSelector>();
        whereSelector->setMatch(CSSSelector::Match::PseudoClass);
        whereSelector->setPseudoClass(CSSSelector::PseudoClass::Where);
        whereSelector->setSelectorList(makeUnique<CSSSelectorList>(MutableCSSSelectorList::from(WTFMove(scopeSelector))));
        return whereSelector;
    };

    auto selectorList = [&] {
        ASSERT(m_selectorListStack.size());
        ASSERT(m_ancestorStack.size());
        if (m_ancestorStack.last() == CSSParserEnum::NestedContextType::Style)
            return *m_selectorListStack.last();
        ASSERT(m_ancestorStack.last() == CSSParserEnum::NestedContextType::Scope);
        return CSSSelectorList { MutableCSSSelectorList::from(whereScopeSelector()) };
    };

    if (m_shouldResolveNestingForSheet)
        rule.wrapperAdoptSelectorList(selectorList());

    addStyleRuleWithSelectorList(rule.selectorList(), rule);
}

void RuleSetBuilder::disallowDynamicMediaQueryEvaluationIfNeeded()
{
    bool isScanningForDynamicEvaluation = !m_ruleSet;
    if (isScanningForDynamicEvaluation && !m_mediaQueryCollector.dynamicContextStack.isEmpty())
        requiresStaticMediaQueryEvaluation = true;
}

void RuleSetBuilder::registerLayers(const Vector<CascadeLayerName>& names)
{
    for (auto& name : names) {
        pushCascadeLayer(name);
        popCascadeLayer(name);
    }
}

void RuleSetBuilder::pushCascadeLayer(const CascadeLayerName& name)
{
    if (!m_ruleSet)
        return;

    if (m_cascadeLayerIdentifierMap.isEmpty() && !m_ruleSet->m_cascadeLayers.isEmpty()) {
        // For incremental build, reconstruct the name->identifier map.
        RuleSet::CascadeLayerIdentifier identifier = 0;
        for (auto& layer : m_ruleSet->m_cascadeLayers)
            m_cascadeLayerIdentifierMap.add(layer.resolvedName, ++identifier);
    }

    auto nameResolvingAnonymous = [&] {
        if (name.isEmpty()) {
            // Make unique name for an anonymous layer.
            return CascadeLayerName { makeAtomString("anon_"_s, cryptographicallyRandomNumber<uint64_t>()) };
        }
        return name;
    };

    // For hierarchical names we register the containing layers individually first.
    for (auto& nameSegment : nameResolvingAnonymous()) {
        m_resolvedCascadeLayerName.append(nameSegment);
        m_currentCascadeLayerIdentifier = m_cascadeLayerIdentifierMap.ensure(m_resolvedCascadeLayerName, [&] {
            // Previously unseen layer.
            m_ruleSet->m_cascadeLayers.append({ m_resolvedCascadeLayerName, m_currentCascadeLayerIdentifier });
            return m_ruleSet->m_cascadeLayers.size();
        }).iterator->value;
    }
}

void RuleSetBuilder::popCascadeLayer(const CascadeLayerName& name)
{
    if (!m_ruleSet)
        return;

    for (auto size = name.isEmpty() ? 1 : name.size(); size--;) {
        m_resolvedCascadeLayerName.removeLast();
        m_currentCascadeLayerIdentifier = m_ruleSet->cascadeLayerForIdentifier(m_currentCascadeLayerIdentifier).parentIdentifier;
    }
}

void RuleSetBuilder::updateCascadeLayerPriorities()
{
    if (m_cascadeLayerIdentifierMap.isEmpty())
        return;

    auto compare = [&](auto a, auto b) {
        while (true) {
            // Identifiers are in parse order.
            auto aParent = m_ruleSet->cascadeLayerForIdentifier(a).parentIdentifier;
            auto bParent = m_ruleSet->cascadeLayerForIdentifier(b).parentIdentifier;

            // For sibling layers, the later layer in parse order has a higher priority.
            if (aParent == bParent)
                return a < b;

            // For nested layers, the parent layer has a higher priority.
            if (aParent == b)
                return true;
            if (a == bParent)
                return false;

            // Traverse to parent. Parent layer identifiers are always lower.
            if (aParent > bParent)
                a = aParent;
            else
                b = bParent;
        }
    };

    auto layerCount = m_ruleSet->m_cascadeLayers.size();

    Vector<RuleSet::CascadeLayerIdentifier> layersInPriorityOrder(layerCount, [](size_t i) {
        return i + 1;
    });

    std::sort(layersInPriorityOrder.begin(), layersInPriorityOrder.end(), compare);

    // Priorities matter only relative to each other, so assign them enforcing these constraints:
    // - Layers must get a priority greater than RuleSet::cascadeLayerPriorityForPresentationalHints.
    // - Layers must get a priority smaller than RuleSet::cascadeLayerPriorityForUnlayered.
    // - A layer must get at least the same priority as the previous one.
    // - A layer should get more priority than the previous one, but this may be impossible if there are too many layers.
    //   In that case, the last layers will get the maximum priority for layers, RuleSet::cascadeLayerPriorityForUnlayered - 1.
    for (unsigned i = 0; i < layerCount; ++i) {
        auto priority = std::min<unsigned>(i + RuleSet::cascadeLayerPriorityForPresentationalHints + 1, RuleSet::cascadeLayerPriorityForUnlayered - 1);
        m_ruleSet->cascadeLayerForIdentifier(layersInPriorityOrder[i]).priority = priority;
    }
}

void RuleSetBuilder::addMutatingRulesToResolver()
{
    if (!m_resolver)
        return;

    auto compareLayers = [&](const auto& a, const auto& b) {
        auto aPriority = m_ruleSet->cascadeLayerPriorityForIdentifier(a.layerIdentifier);
        auto bPriority = m_ruleSet->cascadeLayerPriorityForIdentifier(b.layerIdentifier);
        return aPriority < bPriority;
    };

    // The order may change so we need to reprocess resolver mutating rules from earlier stylesheets.
    auto rulesToAdd = std::exchange(m_ruleSet->m_resolverMutatingRulesInLayers, { });
    rulesToAdd.appendVector(WTFMove(m_collectedResolverMutatingRules));

    if (!m_cascadeLayerIdentifierMap.isEmpty())
        std::stable_sort(rulesToAdd.begin(), rulesToAdd.end(), compareLayers);

    for (auto& collectedRule : rulesToAdd) {
        if (collectedRule.layerIdentifier)
            m_ruleSet->m_resolverMutatingRulesInLayers.append(collectedRule);

        auto& rule = collectedRule.rule;
        if (auto* styleRuleFontFace = dynamicDowncast<StyleRuleFontFace>(rule.get())) {
            m_resolver->document().protectedFontSelector()->addFontFaceRule(*styleRuleFontFace, false);
            m_resolver->invalidateMatchedDeclarationsCache();
            continue;
        }
        if (auto* styleRuleFontPaletteValues = dynamicDowncast<StyleRuleFontPaletteValues>(rule.get())) {
            m_resolver->document().protectedFontSelector()->addFontPaletteValuesRule(*styleRuleFontPaletteValues);
            m_resolver->invalidateMatchedDeclarationsCache();
            continue;
        }
        if (auto* styleRuleFontFeatureValues = dynamicDowncast<StyleRuleFontFeatureValues>(rule.get())) {
            m_resolver->document().protectedFontSelector()->addFontFeatureValuesRule(*styleRuleFontFeatureValues);
            m_resolver->invalidateMatchedDeclarationsCache();
            continue;
        }
        if (auto* styleRuleKeyframes = dynamicDowncast<StyleRuleKeyframes>(rule.get())) {
            m_resolver->addKeyframeStyle(*styleRuleKeyframes);
            continue;
        }
        if (auto* styleRuleCounterStyle = dynamicDowncast<StyleRuleCounterStyle>(rule.get())) {
            if (m_resolver->scopeType() == Resolver::ScopeType::ShadowTree)
                continue;
            auto& registry = m_resolver->document().styleScope().counterStyleRegistry();
            registry.addCounterStyle(styleRuleCounterStyle->descriptors());
            continue;
        }
        if (auto* styleRuleProperty = dynamicDowncast<StyleRuleProperty>(rule.get())) {
            // "A @property is invalid if it occurs in a stylesheet inside of a shadow tree, and must be ignored."
            // https://drafts.css-houdini.org/css-properties-values-api/#at-property-rule
            if (m_resolver->scopeType() == Resolver::ScopeType::ShadowTree)
                continue;
            auto& registry = m_resolver->document().styleScope().customPropertyRegistry();
            registry.registerFromStylesheet(styleRuleProperty->descriptor());
            continue;
        }
        if (auto* styleRuleViewTransition = dynamicDowncast<StyleRuleViewTransition>(rule.get())) {
            if (m_ruleSet)
                m_ruleSet->setViewTransitionRule(*styleRuleViewTransition);
        }
    }
}

void RuleSetBuilder::updateDynamicMediaQueries()
{
    if (m_mediaQueryCollector.allDynamicDependencies.contains(MQ::MediaQueryDynamicDependency::Viewport))
        m_ruleSet->m_hasViewportDependentMediaQueries = true;

    if (!m_mediaQueryCollector.dynamicMediaQueryRules.isEmpty()) {
        auto firstNewIndex = m_ruleSet->m_dynamicMediaQueryRules.size();
        m_ruleSet->m_dynamicMediaQueryRules.appendVector(WTFMove(m_mediaQueryCollector.dynamicMediaQueryRules));

        // Set the initial values.
        m_ruleSet->evaluateDynamicMediaQueryRules(m_mediaQueryCollector.evaluator, firstNewIndex);
    }
}

RuleSetBuilder::MediaQueryCollector::~MediaQueryCollector() = default;

bool RuleSetBuilder::MediaQueryCollector::pushAndEvaluate(const MQ::MediaQueryList& mediaQueries)
{
    if (mediaQueries.isEmpty())
        return true;

    auto dynamicDependencies = evaluator.collectDynamicDependencies(mediaQueries);

    allDynamicDependencies.add(dynamicDependencies);

    if (!dynamicDependencies.isEmpty()) {
        dynamicContextStack.append({ mediaQueries });
        if (collectDynamic)
            return true;
    }

    return evaluator.evaluate(mediaQueries);
}

void RuleSetBuilder::MediaQueryCollector::pop(const MQ::MediaQueryList& mediaQueries)
{
    if (dynamicContextStack.isEmpty() || &dynamicContextStack.last().queries != &mediaQueries)
        return;

    if (!dynamicContextStack.last().affectedRulePositions.isEmpty() || !collectDynamic) {
        RuleSet::DynamicMediaQueryRules rules;
        rules.mediaQueries = WTF::map(dynamicContextStack, [](auto& context) {
            return context.queries;
        });

        if (collectDynamic) {
            rules.affectedRulePositions.appendVector(dynamicContextStack.last().affectedRulePositions);
            rules.affectedRules = copyToVector(dynamicContextStack.last().affectedRules);
        } else
            rules.requiresFullReset = true;

        dynamicMediaQueryRules.append(WTFMove(rules));
    }

    dynamicContextStack.removeLast();
}

void RuleSetBuilder::MediaQueryCollector::addRuleIfNeeded(const RuleData& ruleData)
{
    if (dynamicContextStack.isEmpty())
        return;

    auto& context = dynamicContextStack.last();
    context.affectedRulePositions.append(ruleData.position());
    context.affectedRules.add(ruleData.styleRule());
}

}
}