Coverage Report

Created: 2025-11-02 07:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp
Line
Count
Source
1
/*
2
 * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
3
 * Copyright (c) 2021, the SerenityOS developers.
4
 * Copyright (c) 2021-2024, Sam Atkins <sam@ladybird.org>
5
 * Copyright (c) 2024, Matthew Olsson <mattco@serenityos.org>
6
 *
7
 * SPDX-License-Identifier: BSD-2-Clause
8
 */
9
10
#include <AK/BinarySearch.h>
11
#include <AK/Debug.h>
12
#include <AK/Error.h>
13
#include <AK/Find.h>
14
#include <AK/Function.h>
15
#include <AK/HashMap.h>
16
#include <AK/Math.h>
17
#include <AK/QuickSort.h>
18
#include <AK/TemporaryChange.h>
19
#include <LibGfx/Font/Font.h>
20
#include <LibGfx/Font/FontDatabase.h>
21
#include <LibGfx/Font/FontStyleMapping.h>
22
#include <LibGfx/Font/OpenType/Font.h>
23
#include <LibGfx/Font/ScaledFont.h>
24
#include <LibGfx/Font/VectorFont.h>
25
#include <LibGfx/Font/WOFF/Font.h>
26
#include <LibGfx/Font/WOFF2/Font.h>
27
#include <LibWeb/Animations/AnimationEffect.h>
28
#include <LibWeb/Animations/DocumentTimeline.h>
29
#include <LibWeb/CSS/AnimationEvent.h>
30
#include <LibWeb/CSS/CSSAnimation.h>
31
#include <LibWeb/CSS/CSSFontFaceRule.h>
32
#include <LibWeb/CSS/CSSImportRule.h>
33
#include <LibWeb/CSS/CSSLayerBlockRule.h>
34
#include <LibWeb/CSS/CSSLayerStatementRule.h>
35
#include <LibWeb/CSS/CSSNestedDeclarations.h>
36
#include <LibWeb/CSS/CSSStyleRule.h>
37
#include <LibWeb/CSS/CSSTransition.h>
38
#include <LibWeb/CSS/Interpolation.h>
39
#include <LibWeb/CSS/Parser/Parser.h>
40
#include <LibWeb/CSS/SelectorEngine.h>
41
#include <LibWeb/CSS/StyleComputer.h>
42
#include <LibWeb/CSS/StyleSheet.h>
43
#include <LibWeb/CSS/StyleValues/AngleStyleValue.h>
44
#include <LibWeb/CSS/StyleValues/BorderRadiusStyleValue.h>
45
#include <LibWeb/CSS/StyleValues/CSSColorValue.h>
46
#include <LibWeb/CSS/StyleValues/CSSKeywordValue.h>
47
#include <LibWeb/CSS/StyleValues/CustomIdentStyleValue.h>
48
#include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>
49
#include <LibWeb/CSS/StyleValues/EasingStyleValue.h>
50
#include <LibWeb/CSS/StyleValues/FilterValueListStyleValue.h>
51
#include <LibWeb/CSS/StyleValues/FrequencyStyleValue.h>
52
#include <LibWeb/CSS/StyleValues/GridTrackPlacementStyleValue.h>
53
#include <LibWeb/CSS/StyleValues/GridTrackSizeListStyleValue.h>
54
#include <LibWeb/CSS/StyleValues/IntegerStyleValue.h>
55
#include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
56
#include <LibWeb/CSS/StyleValues/MathDepthStyleValue.h>
57
#include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
58
#include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
59
#include <LibWeb/CSS/StyleValues/PositionStyleValue.h>
60
#include <LibWeb/CSS/StyleValues/RatioStyleValue.h>
61
#include <LibWeb/CSS/StyleValues/RectStyleValue.h>
62
#include <LibWeb/CSS/StyleValues/ShorthandStyleValue.h>
63
#include <LibWeb/CSS/StyleValues/StringStyleValue.h>
64
#include <LibWeb/CSS/StyleValues/StyleValueList.h>
65
#include <LibWeb/CSS/StyleValues/TimeStyleValue.h>
66
#include <LibWeb/CSS/StyleValues/TransformationStyleValue.h>
67
#include <LibWeb/CSS/StyleValues/TransitionStyleValue.h>
68
#include <LibWeb/CSS/StyleValues/UnresolvedStyleValue.h>
69
#include <LibWeb/DOM/Attr.h>
70
#include <LibWeb/DOM/Document.h>
71
#include <LibWeb/DOM/Element.h>
72
#include <LibWeb/DOM/ShadowRoot.h>
73
#include <LibWeb/HTML/HTMLBRElement.h>
74
#include <LibWeb/HTML/HTMLHtmlElement.h>
75
#include <LibWeb/HTML/Parser/HTMLParser.h>
76
#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
77
#include <LibWeb/HighResolutionTime/TimeOrigin.h>
78
#include <LibWeb/Layout/Node.h>
79
#include <LibWeb/Namespace.h>
80
#include <LibWeb/Painting/PaintableBox.h>
81
#include <LibWeb/Platform/FontPlugin.h>
82
#include <LibWeb/ReferrerPolicy/AbstractOperations.h>
83
#include <math.h>
84
#include <stdio.h>
85
86
namespace AK {
87
88
// traits for FontFaceKey
89
template<>
90
struct Traits<Web::CSS::FontFaceKey> : public DefaultTraits<Web::CSS::FontFaceKey> {
91
0
    static unsigned hash(Web::CSS::FontFaceKey const& key) { return pair_int_hash(key.family_name.hash(), pair_int_hash(key.weight, key.slope)); }
92
};
93
94
}
95
96
namespace Web::CSS {
97
98
PropertyOwningCSSStyleDeclaration const& MatchingRule::declaration() const
99
0
{
100
0
    if (rule->type() == CSSRule::Type::Style)
101
0
        return static_cast<CSSStyleRule const&>(*rule).declaration();
102
0
    if (rule->type() == CSSRule::Type::NestedDeclarations)
103
0
        return static_cast<CSSNestedDeclarations const&>(*rule).declaration();
104
0
    VERIFY_NOT_REACHED();
105
0
}
106
107
SelectorList const& MatchingRule::absolutized_selectors() const
108
0
{
109
0
    if (rule->type() == CSSRule::Type::Style)
110
0
        return static_cast<CSSStyleRule const&>(*rule).absolutized_selectors();
111
0
    if (rule->type() == CSSRule::Type::NestedDeclarations)
112
0
        return static_cast<CSSStyleRule const&>(*rule->parent_rule()).absolutized_selectors();
113
0
    VERIFY_NOT_REACHED();
114
0
}
115
116
FlyString const& MatchingRule::qualified_layer_name() const
117
0
{
118
0
    if (rule->type() == CSSRule::Type::Style)
119
0
        return static_cast<CSSStyleRule const&>(*rule).qualified_layer_name();
120
0
    if (rule->type() == CSSRule::Type::NestedDeclarations)
121
0
        return static_cast<CSSStyleRule const&>(*rule->parent_rule()).qualified_layer_name();
122
0
    VERIFY_NOT_REACHED();
123
0
}
124
125
static DOM::Element const* element_to_inherit_style_from(DOM::Element const*, Optional<CSS::Selector::PseudoElement::Type>);
126
127
StyleComputer::StyleComputer(DOM::Document& document)
128
0
    : m_document(document)
129
0
    , m_default_font_metrics(16, Gfx::FontDatabase::default_font().pixel_metrics())
130
0
    , m_root_element_font_metrics(m_default_font_metrics)
131
0
{
132
0
    m_qualified_layer_names_in_order.append({});
133
0
}
134
135
0
StyleComputer::~StyleComputer() = default;
136
137
FontLoader::FontLoader(StyleComputer& style_computer, FlyString family_name, Vector<Gfx::UnicodeRange> unicode_ranges, Vector<URL::URL> urls, Function<void(FontLoader const&)> on_load, Function<void()> on_fail)
138
0
    : m_style_computer(style_computer)
139
0
    , m_family_name(move(family_name))
140
0
    , m_unicode_ranges(move(unicode_ranges))
141
0
    , m_urls(move(urls))
142
0
    , m_on_load(move(on_load))
143
0
    , m_on_fail(move(on_fail))
144
0
{
145
0
}
146
147
0
FontLoader::~FontLoader() = default;
148
149
void FontLoader::resource_did_load()
150
0
{
151
0
    auto result = try_load_font();
152
0
    if (result.is_error()) {
153
0
        dbgln("Failed to parse font: {}", result.error());
154
0
        start_loading_next_url();
155
0
        return;
156
0
    }
157
0
    m_vector_font = result.release_value();
158
0
    m_style_computer.did_load_font(m_family_name);
159
0
    if (m_on_load)
160
0
        m_on_load(*this);
161
0
}
162
163
void FontLoader::resource_did_fail()
164
0
{
165
0
    if (m_on_fail) {
166
0
        m_on_fail();
167
0
    }
168
0
}
169
170
RefPtr<Gfx::Font> FontLoader::font_with_point_size(float point_size)
171
0
{
172
0
    if (!m_vector_font) {
173
0
        if (!resource())
174
0
            start_loading_next_url();
175
0
        return nullptr;
176
0
    }
177
0
    return m_vector_font->scaled_font(point_size);
178
0
}
179
180
void FontLoader::start_loading_next_url()
181
0
{
182
0
    if (resource() && resource()->is_pending())
183
0
        return;
184
0
    if (m_urls.is_empty())
185
0
        return;
186
0
    LoadRequest request;
187
0
    request.set_url(m_urls.take_first());
188
189
    // HACK: We're crudely computing the referer value and shoving it into the
190
    //       request until fetch infrastructure is used here.
191
0
    auto referrer_url = ReferrerPolicy::strip_url_for_use_as_referrer(m_style_computer.document().url());
192
0
    if (referrer_url.has_value() && !request.headers().contains("Referer"))
193
0
        request.set_header("Referer", referrer_url->serialize());
194
195
0
    set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, request));
196
0
}
197
198
ErrorOr<NonnullRefPtr<Gfx::VectorFont>> FontLoader::try_load_font()
199
0
{
200
    // FIXME: This could maybe use the format() provided in @font-face as well, since often the mime type is just application/octet-stream and we have to try every format
201
0
    auto const& mime_type = resource()->mime_type();
202
0
    if (mime_type == "font/ttf"sv || mime_type == "application/x-font-ttf"sv) {
203
0
        if (auto result = OpenType::Font::try_load_from_externally_owned_memory(resource()->encoded_data()); !result.is_error()) {
204
0
            return result;
205
0
        }
206
0
    }
207
0
    if (mime_type == "font/woff"sv || mime_type == "application/font-woff"sv) {
208
0
        if (auto result = WOFF::Font::try_load_from_externally_owned_memory(resource()->encoded_data()); !result.is_error()) {
209
0
            return result;
210
0
        }
211
0
    }
212
0
    if (mime_type == "font/woff2"sv || mime_type == "application/font-woff2"sv) {
213
0
        if (auto result = WOFF2::Font::try_load_from_externally_owned_memory(resource()->encoded_data()); !result.is_error()) {
214
0
            return result;
215
0
        }
216
0
    }
217
218
    // We don't have the luxury of knowing the MIME type, so we have to try all formats.
219
0
    auto ttf = OpenType::Font::try_load_from_externally_owned_memory(resource()->encoded_data());
220
0
    if (!ttf.is_error())
221
0
        return ttf.release_value();
222
0
    auto woff = WOFF::Font::try_load_from_externally_owned_memory(resource()->encoded_data());
223
0
    if (!woff.is_error())
224
0
        return woff.release_value();
225
0
    auto woff2 = WOFF2::Font::try_load_from_externally_owned_memory(resource()->encoded_data());
226
0
    if (!woff2.is_error())
227
0
        return woff2.release_value();
228
0
    return Error::from_string_literal("Automatic format detection failed");
229
0
}
230
231
struct StyleComputer::MatchingFontCandidate {
232
    FontFaceKey key;
233
    Variant<FontLoaderList*, Gfx::Typeface const*> loader_or_typeface;
234
235
    [[nodiscard]] RefPtr<Gfx::FontCascadeList const> font_with_point_size(float point_size) const
236
0
    {
237
0
        RefPtr<Gfx::FontCascadeList> font_list = Gfx::FontCascadeList::create();
238
0
        if (auto* loader_list = loader_or_typeface.get_pointer<FontLoaderList*>(); loader_list) {
239
0
            for (auto const& loader : **loader_list) {
240
0
                if (auto font = loader->font_with_point_size(point_size); font)
241
0
                    font_list->add(*font, loader->unicode_ranges());
242
0
            }
243
0
            return font_list;
244
0
        }
245
246
0
        if (auto font = loader_or_typeface.get<Gfx::Typeface const*>()->get_font(point_size))
247
0
            font_list->add(*font);
248
0
        return font_list;
249
0
    }
250
};
251
252
static CSSStyleSheet& default_stylesheet(DOM::Document const& document)
253
0
{
254
0
    static JS::Handle<CSSStyleSheet> sheet;
255
0
    if (!sheet.cell()) {
256
0
        extern String default_stylesheet_source;
257
0
        sheet = JS::make_handle(parse_css_stylesheet(CSS::Parser::ParsingContext(document), default_stylesheet_source));
258
0
    }
259
0
    return *sheet;
260
0
}
261
262
static CSSStyleSheet& quirks_mode_stylesheet(DOM::Document const& document)
263
0
{
264
0
    static JS::Handle<CSSStyleSheet> sheet;
265
0
    if (!sheet.cell()) {
266
0
        extern String quirks_mode_stylesheet_source;
267
0
        sheet = JS::make_handle(parse_css_stylesheet(CSS::Parser::ParsingContext(document), quirks_mode_stylesheet_source));
268
0
    }
269
0
    return *sheet;
270
0
}
271
272
static CSSStyleSheet& mathml_stylesheet(DOM::Document const& document)
273
0
{
274
0
    static JS::Handle<CSSStyleSheet> sheet;
275
0
    if (!sheet.cell()) {
276
0
        extern String mathml_stylesheet_source;
277
0
        sheet = JS::make_handle(parse_css_stylesheet(CSS::Parser::ParsingContext(document), mathml_stylesheet_source));
278
0
    }
279
0
    return *sheet;
280
0
}
281
282
static CSSStyleSheet& svg_stylesheet(DOM::Document const& document)
283
0
{
284
0
    static JS::Handle<CSSStyleSheet> sheet;
285
0
    if (!sheet.cell()) {
286
0
        extern String svg_stylesheet_source;
287
0
        sheet = JS::make_handle(parse_css_stylesheet(CSS::Parser::ParsingContext(document), svg_stylesheet_source));
288
0
    }
289
0
    return *sheet;
290
0
}
291
292
Optional<String> StyleComputer::user_agent_style_sheet_source(StringView name)
293
0
{
294
0
    extern String default_stylesheet_source;
295
0
    extern String quirks_mode_stylesheet_source;
296
0
    extern String mathml_stylesheet_source;
297
0
    extern String svg_stylesheet_source;
298
299
0
    if (name == "CSS/Default.css"sv)
300
0
        return default_stylesheet_source;
301
0
    if (name == "CSS/QuirksMode.css"sv)
302
0
        return quirks_mode_stylesheet_source;
303
0
    if (name == "MathML/Default.css"sv)
304
0
        return mathml_stylesheet_source;
305
0
    if (name == "SVG/Default.css"sv)
306
0
        return svg_stylesheet_source;
307
0
    return {};
308
0
}
309
310
template<typename Callback>
311
void StyleComputer::for_each_stylesheet(CascadeOrigin cascade_origin, Callback callback) const
312
0
{
313
0
    if (cascade_origin == CascadeOrigin::UserAgent) {
314
0
        callback(default_stylesheet(document()), {});
315
0
        if (document().in_quirks_mode())
316
0
            callback(quirks_mode_stylesheet(document()), {});
317
0
        callback(mathml_stylesheet(document()), {});
318
0
        callback(svg_stylesheet(document()), {});
319
0
    }
320
0
    if (cascade_origin == CascadeOrigin::User) {
321
0
        if (m_user_style_sheet)
322
0
            callback(*m_user_style_sheet, {});
323
0
    }
324
0
    if (cascade_origin == CascadeOrigin::Author) {
325
0
        document().for_each_active_css_style_sheet([&](auto& sheet, auto shadow_root) {
326
0
            callback(sheet, shadow_root);
327
0
        });
Unexecuted instantiation: StyleComputer.cpp:auto Web::CSS::StyleComputer::for_each_stylesheet<Web::CSS::StyleComputer::make_rule_cache_for_cascade_origin(Web::CSS::CascadeOrigin)::$_0>(Web::CSS::CascadeOrigin, Web::CSS::StyleComputer::make_rule_cache_for_cascade_origin(Web::CSS::CascadeOrigin)::$_0) const::{lambda(auto:1&, auto:2)#1}::operator()<Web::CSS::CSSStyleSheet, JS::GCPtr<Web::DOM::ShadowRoot> >(Web::CSS::CSSStyleSheet&, JS::GCPtr<Web::DOM::ShadowRoot>) const
Unexecuted instantiation: StyleComputer.cpp:auto Web::CSS::StyleComputer::for_each_stylesheet<Web::CSS::StyleComputer::build_qualified_layer_names_cache()::$_1>(Web::CSS::CascadeOrigin, Web::CSS::StyleComputer::build_qualified_layer_names_cache()::$_1) const::{lambda(auto:1&, auto:2)#1}::operator()<Web::CSS::CSSStyleSheet, JS::GCPtr<Web::DOM::ShadowRoot> >(Web::CSS::CSSStyleSheet&, JS::GCPtr<Web::DOM::ShadowRoot>) const
328
0
    }
329
0
}
Unexecuted instantiation: StyleComputer.cpp:void Web::CSS::StyleComputer::for_each_stylesheet<Web::CSS::StyleComputer::make_rule_cache_for_cascade_origin(Web::CSS::CascadeOrigin)::$_0>(Web::CSS::CascadeOrigin, Web::CSS::StyleComputer::make_rule_cache_for_cascade_origin(Web::CSS::CascadeOrigin)::$_0) const
Unexecuted instantiation: StyleComputer.cpp:void Web::CSS::StyleComputer::for_each_stylesheet<Web::CSS::StyleComputer::build_qualified_layer_names_cache()::$_1>(Web::CSS::CascadeOrigin, Web::CSS::StyleComputer::build_qualified_layer_names_cache()::$_1) const
330
331
StyleComputer::RuleCache const& StyleComputer::rule_cache_for_cascade_origin(CascadeOrigin cascade_origin) const
332
0
{
333
0
    switch (cascade_origin) {
334
0
    case CascadeOrigin::Author:
335
0
        return *m_author_rule_cache;
336
0
    case CascadeOrigin::User:
337
0
        return *m_user_rule_cache;
338
0
    case CascadeOrigin::UserAgent:
339
0
        return *m_user_agent_rule_cache;
340
0
    default:
341
0
        TODO();
342
0
    }
343
0
}
344
345
[[nodiscard]] static bool filter_namespace_rule(DOM::Element const& element, MatchingRule const& rule)
346
0
{
347
    // FIXME: Filter out non-default namespace using prefixes
348
0
    if (auto namespace_rule = rule.sheet->default_namespace_rule()) {
349
0
        if (namespace_rule->namespace_uri() != element.namespace_uri())
350
0
            return false;
351
0
    }
352
0
    return true;
353
0
}
354
355
[[nodiscard]] static bool filter_layer(FlyString const& qualified_layer_name, MatchingRule const& rule)
356
0
{
357
0
    if (rule.rule && rule.qualified_layer_name() != qualified_layer_name)
358
0
        return false;
359
0
    return true;
360
0
}
361
362
bool StyleComputer::should_reject_with_ancestor_filter(Selector const& selector) const
363
0
{
364
0
    for (u32 hash : selector.ancestor_hashes()) {
365
0
        if (hash == 0)
366
0
            break;
367
0
        if (!m_ancestor_filter.may_contain(hash))
368
0
            return true;
369
0
    }
370
0
    return false;
371
0
}
372
373
Vector<MatchingRule> StyleComputer::collect_matching_rules(DOM::Element const& element, CascadeOrigin cascade_origin, Optional<CSS::Selector::PseudoElement::Type> pseudo_element, FlyString const& qualified_layer_name) const
374
0
{
375
0
    auto const& root_node = element.root();
376
0
    auto shadow_root = is<DOM::ShadowRoot>(root_node) ? static_cast<DOM::ShadowRoot const*>(&root_node) : nullptr;
377
378
0
    JS::GCPtr<DOM::Element const> shadow_host;
379
0
    if (element.is_shadow_host())
380
0
        shadow_host = element;
381
0
    else if (shadow_root)
382
0
        shadow_host = shadow_root->host();
383
384
0
    auto const& rule_cache = rule_cache_for_cascade_origin(cascade_origin);
385
386
0
    bool is_hovered = SelectorEngine::matches_hover_pseudo_class(element);
387
388
0
    Vector<MatchingRule, 512> rules_to_run;
389
0
    auto add_rules_to_run = [&](Vector<MatchingRule> const& rules) {
390
0
        rules_to_run.grow_capacity(rules_to_run.size() + rules.size());
391
0
        if (pseudo_element.has_value()) {
392
0
            for (auto const& rule : rules) {
393
0
                if (rule.must_be_hovered && !is_hovered)
394
0
                    continue;
395
0
                if (rule.contains_pseudo_element && filter_namespace_rule(element, rule) && filter_layer(qualified_layer_name, rule))
396
0
                    rules_to_run.unchecked_append(rule);
397
0
            }
398
0
        } else {
399
0
            for (auto const& rule : rules) {
400
0
                if (rule.must_be_hovered && !is_hovered)
401
0
                    continue;
402
0
                if (!rule.contains_pseudo_element && filter_namespace_rule(element, rule) && filter_layer(qualified_layer_name, rule))
403
0
                    rules_to_run.unchecked_append(rule);
404
0
            }
405
0
        }
406
0
    };
407
408
0
    for (auto const& class_name : element.class_names()) {
409
0
        if (auto it = rule_cache.rules_by_class.find(class_name); it != rule_cache.rules_by_class.end())
410
0
            add_rules_to_run(it->value);
411
0
    }
412
0
    if (auto id = element.id(); id.has_value()) {
413
0
        if (auto it = rule_cache.rules_by_id.find(id.value()); it != rule_cache.rules_by_id.end())
414
0
            add_rules_to_run(it->value);
415
0
    }
416
0
    if (auto it = rule_cache.rules_by_tag_name.find(element.local_name()); it != rule_cache.rules_by_tag_name.end())
417
0
        add_rules_to_run(it->value);
418
0
    if (pseudo_element.has_value()) {
419
0
        if (CSS::Selector::PseudoElement::is_known_pseudo_element_type(pseudo_element.value())) {
420
0
            add_rules_to_run(rule_cache.rules_by_pseudo_element.at(to_underlying(pseudo_element.value())));
421
0
        } else {
422
            // NOTE: We don't cache rules for unknown pseudo-elements. They can't match anything anyway.
423
0
        }
424
0
    }
425
426
0
    if (element.is_document_element())
427
0
        add_rules_to_run(rule_cache.root_rules);
428
429
0
    element.for_each_attribute([&](auto& name, auto&) {
430
0
        if (auto it = rule_cache.rules_by_attribute_name.find(name); it != rule_cache.rules_by_attribute_name.end()) {
431
0
            add_rules_to_run(it->value);
432
0
        }
433
0
    });
434
435
0
    add_rules_to_run(rule_cache.other_rules);
436
437
0
    size_t maximum_match_count = 0;
438
439
0
    for (auto& rule_to_run : rules_to_run) {
440
        // FIXME: This needs to be revised when adding support for the ::shadow selector, as it needs to cross shadow boundaries.
441
0
        auto rule_root = rule_to_run.shadow_root;
442
0
        auto from_user_agent_or_user_stylesheet = rule_to_run.cascade_origin == CascadeOrigin::UserAgent || rule_to_run.cascade_origin == CascadeOrigin::User;
443
444
        // NOTE: Inside shadow trees, we only match rules that are defined in the shadow tree's style sheets.
445
        //       The key exception is the shadow tree's *shadow host*, which needs to match :host rules from inside the shadow root.
446
        //       Also note that UA or User style sheets don't have a scope, so they are always relevant.
447
        // FIXME: We should reorganize the data so that the document-level StyleComputer doesn't cache *all* rules,
448
        //        but instead we'd have some kind of "style scope" at the document level, and also one for each shadow root.
449
        //        Then we could only evaluate rules from the current style scope.
450
0
        bool rule_is_relevant_for_current_scope = rule_root == shadow_root
451
0
            || (element.is_shadow_host() && rule_root == element.shadow_root())
452
0
            || from_user_agent_or_user_stylesheet;
453
454
0
        if (!rule_is_relevant_for_current_scope) {
455
0
            rule_to_run.skip = true;
456
0
            continue;
457
0
        }
458
459
0
        auto const& selector = rule_to_run.absolutized_selectors()[rule_to_run.selector_index];
460
0
        if (should_reject_with_ancestor_filter(*selector)) {
461
0
            rule_to_run.skip = true;
462
0
            continue;
463
0
        }
464
465
0
        ++maximum_match_count;
466
0
    }
467
468
0
    if (maximum_match_count == 0)
469
0
        return {};
470
471
0
    Vector<MatchingRule> matching_rules;
472
0
    matching_rules.ensure_capacity(maximum_match_count);
473
474
0
    for (auto const& rule_to_run : rules_to_run) {
475
0
        if (rule_to_run.skip)
476
0
            continue;
477
478
        // NOTE: When matching an element against a rule from outside the shadow root's style scope,
479
        //       we have to pass in null for the shadow host, otherwise combinator traversal will
480
        //       be confined to the element itself (since it refuses to cross the shadow boundary).
481
0
        auto rule_root = rule_to_run.shadow_root;
482
0
        auto shadow_host_to_use = shadow_host;
483
0
        if (element.is_shadow_host() && rule_root != element.shadow_root())
484
0
            shadow_host_to_use = nullptr;
485
486
0
        auto const& selector = rule_to_run.absolutized_selectors()[rule_to_run.selector_index];
487
488
0
        if (rule_to_run.can_use_fast_matches) {
489
0
            if (!SelectorEngine::fast_matches(selector, *rule_to_run.sheet, element, shadow_host_to_use))
490
0
                continue;
491
0
        } else {
492
0
            if (!SelectorEngine::matches(selector, *rule_to_run.sheet, element, shadow_host_to_use, pseudo_element))
493
0
                continue;
494
0
        }
495
0
        matching_rules.append(rule_to_run);
496
0
    }
497
0
    return matching_rules;
498
0
}
499
500
static void sort_matching_rules(Vector<MatchingRule>& matching_rules)
501
0
{
502
0
    quick_sort(matching_rules, [&](MatchingRule& a, MatchingRule& b) {
503
0
        auto const& a_selector = a.absolutized_selectors()[a.selector_index];
504
0
        auto const& b_selector = b.absolutized_selectors()[b.selector_index];
505
0
        auto a_specificity = a_selector->specificity();
506
0
        auto b_specificity = b_selector->specificity();
507
0
        if (a_specificity == b_specificity) {
508
0
            if (a.style_sheet_index == b.style_sheet_index)
509
0
                return a.rule_index < b.rule_index;
510
0
            return a.style_sheet_index < b.style_sheet_index;
511
0
        }
512
0
        return a_specificity < b_specificity;
513
0
    });
514
0
}
515
516
void StyleComputer::for_each_property_expanding_shorthands(PropertyID property_id, CSSStyleValue const& value, AllowUnresolved allow_unresolved, Function<void(PropertyID, CSSStyleValue const&)> const& set_longhand_property)
517
0
{
518
0
    auto map_logical_property_to_real_property = [](PropertyID property_id) -> Optional<PropertyID> {
519
        // FIXME: Honor writing-mode, direction and text-orientation.
520
0
        switch (property_id) {
521
0
        case PropertyID::MarginBlockStart:
522
0
            return PropertyID::MarginTop;
523
0
        case PropertyID::MarginBlockEnd:
524
0
            return PropertyID::MarginBottom;
525
0
        case PropertyID::MarginInlineStart:
526
0
            return PropertyID::MarginLeft;
527
0
        case PropertyID::MarginInlineEnd:
528
0
            return PropertyID::MarginRight;
529
0
        case PropertyID::PaddingBlockStart:
530
0
            return PropertyID::PaddingTop;
531
0
        case PropertyID::PaddingBlockEnd:
532
0
            return PropertyID::PaddingBottom;
533
0
        case PropertyID::PaddingInlineStart:
534
0
            return PropertyID::PaddingLeft;
535
0
        case PropertyID::PaddingInlineEnd:
536
0
            return PropertyID::PaddingRight;
537
0
        case PropertyID::InlineSize:
538
0
            return PropertyID::Width;
539
0
        case PropertyID::InsetBlockStart:
540
0
            return PropertyID::Top;
541
0
        case PropertyID::InsetBlockEnd:
542
0
            return PropertyID::Bottom;
543
0
        case PropertyID::InsetInlineStart:
544
0
            return PropertyID::Left;
545
0
        case PropertyID::InsetInlineEnd:
546
0
            return PropertyID::Right;
547
0
        default:
548
0
            return {};
549
0
        }
550
0
    };
551
552
0
    struct StartAndEndPropertyIDs {
553
0
        PropertyID start;
554
0
        PropertyID end;
555
0
    };
556
0
    auto map_logical_property_to_real_properties = [](PropertyID property_id) -> Optional<StartAndEndPropertyIDs> {
557
        // FIXME: Honor writing-mode, direction and text-orientation.
558
0
        switch (property_id) {
559
0
        case PropertyID::MarginBlock:
560
0
            return StartAndEndPropertyIDs { PropertyID::MarginTop, PropertyID::MarginBottom };
561
0
        case PropertyID::MarginInline:
562
0
            return StartAndEndPropertyIDs { PropertyID::MarginLeft, PropertyID::MarginRight };
563
0
        case PropertyID::PaddingBlock:
564
0
            return StartAndEndPropertyIDs { PropertyID::PaddingTop, PropertyID::PaddingBottom };
565
0
        case PropertyID::PaddingInline:
566
0
            return StartAndEndPropertyIDs { PropertyID::PaddingLeft, PropertyID::PaddingRight };
567
0
        case PropertyID::InsetBlock:
568
0
            return StartAndEndPropertyIDs { PropertyID::Top, PropertyID::Bottom };
569
0
        case PropertyID::InsetInline:
570
0
            return StartAndEndPropertyIDs { PropertyID::Left, PropertyID::Right };
571
0
        default:
572
0
            return {};
573
0
        }
574
0
    };
575
576
0
    if (auto real_property_id = map_logical_property_to_real_property(property_id); real_property_id.has_value()) {
577
0
        for_each_property_expanding_shorthands(real_property_id.value(), value, allow_unresolved, set_longhand_property);
578
0
        return;
579
0
    }
580
581
0
    if (auto real_property_ids = map_logical_property_to_real_properties(property_id); real_property_ids.has_value()) {
582
0
        if (value.is_value_list() && value.as_value_list().size() == 2) {
583
0
            auto const& start = value.as_value_list().values()[0];
584
0
            auto const& end = value.as_value_list().values()[1];
585
0
            for_each_property_expanding_shorthands(real_property_ids->start, start, allow_unresolved, set_longhand_property);
586
0
            for_each_property_expanding_shorthands(real_property_ids->end, end, allow_unresolved, set_longhand_property);
587
0
            return;
588
0
        }
589
0
        for_each_property_expanding_shorthands(real_property_ids->start, value, allow_unresolved, set_longhand_property);
590
0
        for_each_property_expanding_shorthands(real_property_ids->end, value, allow_unresolved, set_longhand_property);
591
0
        return;
592
0
    }
593
594
0
    if (value.is_shorthand()) {
595
0
        auto& shorthand_value = value.as_shorthand();
596
0
        auto& properties = shorthand_value.sub_properties();
597
0
        auto& values = shorthand_value.values();
598
0
        for (size_t i = 0; i < properties.size(); ++i)
599
0
            for_each_property_expanding_shorthands(properties[i], values[i], allow_unresolved, set_longhand_property);
600
0
        return;
601
0
    }
602
603
0
    auto assign_edge_values = [&](PropertyID top_property, PropertyID right_property, PropertyID bottom_property, PropertyID left_property, auto const& values) {
604
0
        if (values.size() == 4) {
605
0
            set_longhand_property(top_property, values[0]);
606
0
            set_longhand_property(right_property, values[1]);
607
0
            set_longhand_property(bottom_property, values[2]);
608
0
            set_longhand_property(left_property, values[3]);
609
0
        } else if (values.size() == 3) {
610
0
            set_longhand_property(top_property, values[0]);
611
0
            set_longhand_property(right_property, values[1]);
612
0
            set_longhand_property(bottom_property, values[2]);
613
0
            set_longhand_property(left_property, values[1]);
614
0
        } else if (values.size() == 2) {
615
0
            set_longhand_property(top_property, values[0]);
616
0
            set_longhand_property(right_property, values[1]);
617
0
            set_longhand_property(bottom_property, values[0]);
618
0
            set_longhand_property(left_property, values[1]);
619
0
        } else if (values.size() == 1) {
620
0
            set_longhand_property(top_property, values[0]);
621
0
            set_longhand_property(right_property, values[0]);
622
0
            set_longhand_property(bottom_property, values[0]);
623
0
            set_longhand_property(left_property, values[0]);
624
0
        }
625
0
    };
626
627
0
    if (property_id == CSS::PropertyID::Border) {
628
0
        for_each_property_expanding_shorthands(CSS::PropertyID::BorderTop, value, allow_unresolved, set_longhand_property);
629
0
        for_each_property_expanding_shorthands(CSS::PropertyID::BorderRight, value, allow_unresolved, set_longhand_property);
630
0
        for_each_property_expanding_shorthands(CSS::PropertyID::BorderBottom, value, allow_unresolved, set_longhand_property);
631
0
        for_each_property_expanding_shorthands(CSS::PropertyID::BorderLeft, value, allow_unresolved, set_longhand_property);
632
        // FIXME: Also reset border-image, in line with the spec: https://www.w3.org/TR/css-backgrounds-3/#border-shorthands
633
0
        return;
634
0
    }
635
636
0
    if (property_id == CSS::PropertyID::BorderStyle) {
637
0
        if (value.is_value_list()) {
638
0
            auto const& values_list = value.as_value_list();
639
0
            assign_edge_values(PropertyID::BorderTopStyle, PropertyID::BorderRightStyle, PropertyID::BorderBottomStyle, PropertyID::BorderLeftStyle, values_list.values());
640
0
            return;
641
0
        }
642
643
0
        set_longhand_property(CSS::PropertyID::BorderTopStyle, value);
644
0
        set_longhand_property(CSS::PropertyID::BorderRightStyle, value);
645
0
        set_longhand_property(CSS::PropertyID::BorderBottomStyle, value);
646
0
        set_longhand_property(CSS::PropertyID::BorderLeftStyle, value);
647
0
        return;
648
0
    }
649
650
0
    if (property_id == CSS::PropertyID::BorderWidth) {
651
0
        if (value.is_value_list()) {
652
0
            auto const& values_list = value.as_value_list();
653
0
            assign_edge_values(PropertyID::BorderTopWidth, PropertyID::BorderRightWidth, PropertyID::BorderBottomWidth, PropertyID::BorderLeftWidth, values_list.values());
654
0
            return;
655
0
        }
656
657
0
        set_longhand_property(CSS::PropertyID::BorderTopWidth, value);
658
0
        set_longhand_property(CSS::PropertyID::BorderRightWidth, value);
659
0
        set_longhand_property(CSS::PropertyID::BorderBottomWidth, value);
660
0
        set_longhand_property(CSS::PropertyID::BorderLeftWidth, value);
661
0
        return;
662
0
    }
663
664
0
    if (property_id == CSS::PropertyID::BorderColor) {
665
0
        if (value.is_value_list()) {
666
0
            auto const& values_list = value.as_value_list();
667
0
            assign_edge_values(PropertyID::BorderTopColor, PropertyID::BorderRightColor, PropertyID::BorderBottomColor, PropertyID::BorderLeftColor, values_list.values());
668
0
            return;
669
0
        }
670
671
0
        set_longhand_property(CSS::PropertyID::BorderTopColor, value);
672
0
        set_longhand_property(CSS::PropertyID::BorderRightColor, value);
673
0
        set_longhand_property(CSS::PropertyID::BorderBottomColor, value);
674
0
        set_longhand_property(CSS::PropertyID::BorderLeftColor, value);
675
0
        return;
676
0
    }
677
678
0
    if (property_id == CSS::PropertyID::BackgroundPosition) {
679
0
        if (value.is_position()) {
680
0
            auto const& position = value.as_position();
681
0
            set_longhand_property(CSS::PropertyID::BackgroundPositionX, position.edge_x());
682
0
            set_longhand_property(CSS::PropertyID::BackgroundPositionY, position.edge_y());
683
0
        } else if (value.is_value_list()) {
684
            // Expand background-position layer list into separate lists for x and y positions:
685
0
            auto const& values_list = value.as_value_list();
686
0
            StyleValueVector x_positions {};
687
0
            StyleValueVector y_positions {};
688
0
            x_positions.ensure_capacity(values_list.size());
689
0
            y_positions.ensure_capacity(values_list.size());
690
0
            for (auto& layer : values_list.values()) {
691
0
                if (layer->is_position()) {
692
0
                    auto const& position = layer->as_position();
693
0
                    x_positions.unchecked_append(position.edge_x());
694
0
                    y_positions.unchecked_append(position.edge_y());
695
0
                } else {
696
0
                    x_positions.unchecked_append(layer);
697
0
                    y_positions.unchecked_append(layer);
698
0
                }
699
0
            }
700
0
            set_longhand_property(CSS::PropertyID::BackgroundPositionX, StyleValueList::create(move(x_positions), values_list.separator()));
701
0
            set_longhand_property(CSS::PropertyID::BackgroundPositionY, StyleValueList::create(move(y_positions), values_list.separator()));
702
0
        } else {
703
0
            set_longhand_property(CSS::PropertyID::BackgroundPositionX, value);
704
0
            set_longhand_property(CSS::PropertyID::BackgroundPositionY, value);
705
0
        }
706
707
0
        return;
708
0
    }
709
710
0
    if (property_id == CSS::PropertyID::Inset) {
711
0
        if (value.is_value_list()) {
712
0
            auto const& values_list = value.as_value_list();
713
0
            assign_edge_values(PropertyID::Top, PropertyID::Right, PropertyID::Bottom, PropertyID::Left, values_list.values());
714
0
            return;
715
0
        }
716
717
0
        set_longhand_property(CSS::PropertyID::Top, value);
718
0
        set_longhand_property(CSS::PropertyID::Right, value);
719
0
        set_longhand_property(CSS::PropertyID::Bottom, value);
720
0
        set_longhand_property(CSS::PropertyID::Left, value);
721
0
        return;
722
0
    }
723
724
0
    if (property_id == CSS::PropertyID::Margin) {
725
0
        if (value.is_value_list()) {
726
0
            auto const& values_list = value.as_value_list();
727
0
            assign_edge_values(PropertyID::MarginTop, PropertyID::MarginRight, PropertyID::MarginBottom, PropertyID::MarginLeft, values_list.values());
728
0
            return;
729
0
        }
730
731
0
        set_longhand_property(CSS::PropertyID::MarginTop, value);
732
0
        set_longhand_property(CSS::PropertyID::MarginRight, value);
733
0
        set_longhand_property(CSS::PropertyID::MarginBottom, value);
734
0
        set_longhand_property(CSS::PropertyID::MarginLeft, value);
735
0
        return;
736
0
    }
737
738
0
    if (property_id == CSS::PropertyID::Padding) {
739
0
        if (value.is_value_list()) {
740
0
            auto const& values_list = value.as_value_list();
741
0
            assign_edge_values(PropertyID::PaddingTop, PropertyID::PaddingRight, PropertyID::PaddingBottom, PropertyID::PaddingLeft, values_list.values());
742
0
            return;
743
0
        }
744
745
0
        set_longhand_property(CSS::PropertyID::PaddingTop, value);
746
0
        set_longhand_property(CSS::PropertyID::PaddingRight, value);
747
0
        set_longhand_property(CSS::PropertyID::PaddingBottom, value);
748
0
        set_longhand_property(CSS::PropertyID::PaddingLeft, value);
749
0
        return;
750
0
    }
751
752
0
    if (property_id == CSS::PropertyID::Gap) {
753
0
        if (value.is_value_list()) {
754
0
            auto const& values_list = value.as_value_list();
755
0
            set_longhand_property(CSS::PropertyID::RowGap, values_list.values()[0]);
756
0
            set_longhand_property(CSS::PropertyID::ColumnGap, values_list.values()[1]);
757
0
            return;
758
0
        }
759
0
        set_longhand_property(CSS::PropertyID::RowGap, value);
760
0
        set_longhand_property(CSS::PropertyID::ColumnGap, value);
761
0
        return;
762
0
    }
763
764
0
    if (property_id == CSS::PropertyID::MaxInlineSize || property_id == CSS::PropertyID::MinInlineSize) {
765
        // FIXME: Use writing-mode to determine if we should set width or height.
766
0
        bool is_horizontal = true;
767
768
0
        if (is_horizontal) {
769
0
            if (property_id == CSS::PropertyID::MaxInlineSize) {
770
0
                set_longhand_property(CSS::PropertyID::MaxWidth, value);
771
0
            } else {
772
0
                set_longhand_property(CSS::PropertyID::MinWidth, value);
773
0
            }
774
0
        } else {
775
0
            if (property_id == CSS::PropertyID::MaxInlineSize) {
776
0
                set_longhand_property(CSS::PropertyID::MaxHeight, value);
777
0
            } else {
778
0
                set_longhand_property(CSS::PropertyID::MinHeight, value);
779
0
            }
780
0
        }
781
0
        return;
782
0
    }
783
784
0
    if (property_id == CSS::PropertyID::Transition) {
785
0
        if (!value.is_transition()) {
786
            // Handle `none` as a shorthand for `all 0s ease 0s`.
787
0
            set_longhand_property(CSS::PropertyID::TransitionProperty, CSSKeywordValue::create(Keyword::All));
788
0
            set_longhand_property(CSS::PropertyID::TransitionDuration, TimeStyleValue::create(CSS::Time::make_seconds(0)));
789
0
            set_longhand_property(CSS::PropertyID::TransitionDelay, TimeStyleValue::create(CSS::Time::make_seconds(0)));
790
0
            set_longhand_property(CSS::PropertyID::TransitionTimingFunction, CSSKeywordValue::create(Keyword::Ease));
791
0
            return;
792
0
        }
793
0
        auto const& transitions = value.as_transition().transitions();
794
0
        Array<Vector<ValueComparingNonnullRefPtr<CSSStyleValue const>>, 4> transition_values;
795
0
        for (auto const& transition : transitions) {
796
0
            transition_values[0].append(*transition.property_name);
797
0
            transition_values[1].append(transition.duration.as_style_value());
798
0
            transition_values[2].append(transition.delay.as_style_value());
799
0
            if (transition.easing)
800
0
                transition_values[3].append(*transition.easing);
801
0
        }
802
803
0
        set_longhand_property(CSS::PropertyID::TransitionProperty, StyleValueList::create(move(transition_values[0]), StyleValueList::Separator::Comma));
804
0
        set_longhand_property(CSS::PropertyID::TransitionDuration, StyleValueList::create(move(transition_values[1]), StyleValueList::Separator::Comma));
805
0
        set_longhand_property(CSS::PropertyID::TransitionDelay, StyleValueList::create(move(transition_values[2]), StyleValueList::Separator::Comma));
806
0
        set_longhand_property(CSS::PropertyID::TransitionTimingFunction, StyleValueList::create(move(transition_values[3]), StyleValueList::Separator::Comma));
807
0
        return;
808
0
    }
809
810
0
    if (property_id == CSS::PropertyID::Float) {
811
0
        auto keyword = value.to_keyword();
812
813
        // FIXME: Honor writing-mode, direction and text-orientation.
814
0
        if (keyword == Keyword::InlineStart) {
815
0
            set_longhand_property(CSS::PropertyID::Float, CSSKeywordValue::create(Keyword::Left));
816
0
            return;
817
0
        } else if (keyword == Keyword::InlineEnd) {
818
0
            set_longhand_property(CSS::PropertyID::Float, CSSKeywordValue::create(Keyword::Right));
819
0
            return;
820
0
        }
821
0
    }
822
823
0
    if (property_is_shorthand(property_id)) {
824
        // ShorthandStyleValue was handled already.
825
        // That means if we got here, that `value` must be a CSS-wide keyword, which we should apply to our longhand properties.
826
        // We don't directly call `set_longhand_property()` because the longhands might have longhands of their own.
827
        // (eg `grid` -> `grid-template` -> `grid-template-areas` & `grid-template-rows` & `grid-template-columns`)
828
        // Forget this requirement if we're ignoring unresolved values and the value is unresolved.
829
0
        VERIFY(value.is_css_wide_keyword() || (allow_unresolved == AllowUnresolved::Yes && value.is_unresolved()));
830
0
        for (auto longhand : longhands_for_shorthand(property_id))
831
0
            for_each_property_expanding_shorthands(longhand, value, allow_unresolved, set_longhand_property);
832
0
        return;
833
0
    }
834
835
0
    set_longhand_property(property_id, value);
836
0
}
837
838
void StyleComputer::set_property_expanding_shorthands(StyleProperties& style, PropertyID property_id, CSSStyleValue const& value, CSSStyleDeclaration const* declaration, StyleProperties const& style_for_revert, StyleProperties const& style_for_revert_layer, Important important)
839
0
{
840
0
    auto revert_shorthand = [&](PropertyID shorthand_id, StyleProperties const& style_for_revert) {
841
0
        auto previous_value = style_for_revert.m_data->m_property_values[to_underlying(shorthand_id)];
842
0
        if (!previous_value)
843
0
            previous_value = CSSKeywordValue::create(Keyword::Initial);
844
845
0
        style.set_property(shorthand_id, *previous_value, StyleProperties::Inherited::No, important);
846
0
        if (shorthand_id == CSS::PropertyID::AnimationName)
847
0
            style.set_animation_name_source(style_for_revert.animation_name_source());
848
0
        if (shorthand_id == CSS::PropertyID::TransitionProperty)
849
0
            style.set_transition_property_source(style_for_revert.transition_property_source());
850
0
    };
851
852
0
    for_each_property_expanding_shorthands(property_id, value, AllowUnresolved::No, [&](PropertyID shorthand_id, CSSStyleValue const& shorthand_value) {
853
0
        if (shorthand_value.is_revert()) {
854
0
            revert_shorthand(shorthand_id, style_for_revert);
855
0
        } else if (shorthand_value.is_revert_layer()) {
856
0
            revert_shorthand(shorthand_id, style_for_revert_layer);
857
0
        } else {
858
0
            style.set_property(shorthand_id, shorthand_value, StyleProperties::Inherited::No, important);
859
0
            if (shorthand_id == CSS::PropertyID::AnimationName)
860
0
                style.set_animation_name_source(declaration);
861
0
            if (shorthand_id == CSS::PropertyID::TransitionProperty)
862
0
                style.set_transition_property_source(declaration);
863
0
        }
864
0
    });
865
0
}
866
867
void StyleComputer::set_all_properties(DOM::Element& element, Optional<CSS::Selector::PseudoElement::Type> pseudo_element, StyleProperties& style, CSSStyleValue const& value, DOM::Document& document, CSS::CSSStyleDeclaration const* declaration, StyleProperties const& style_for_revert, StyleProperties const& style_for_revert_layer, Important important) const
868
0
{
869
0
    for (auto i = to_underlying(CSS::first_longhand_property_id); i <= to_underlying(CSS::last_longhand_property_id); ++i) {
870
0
        auto property_id = (CSS::PropertyID)i;
871
872
0
        if (value.is_revert()) {
873
0
            style.revert_property(property_id, style_for_revert);
874
0
            continue;
875
0
        }
876
877
0
        if (value.is_revert_layer()) {
878
0
            style.revert_property(property_id, style_for_revert_layer);
879
0
            continue;
880
0
        }
881
882
0
        if (value.is_unset()) {
883
0
            if (is_inherited_property(property_id)) {
884
0
                style.set_property(
885
0
                    property_id,
886
0
                    get_inherit_value(document.realm(), property_id, &element, pseudo_element),
887
0
                    StyleProperties::Inherited::Yes,
888
0
                    important);
889
0
            } else {
890
0
                style.set_property(
891
0
                    property_id,
892
0
                    property_initial_value(document.realm(), property_id),
893
0
                    StyleProperties::Inherited::No,
894
0
                    important);
895
0
            }
896
0
            continue;
897
0
        }
898
899
0
        NonnullRefPtr<CSSStyleValue> property_value = value;
900
0
        if (property_value->is_unresolved())
901
0
            property_value = Parser::Parser::resolve_unresolved_style_value(Parser::ParsingContext { document }, element, pseudo_element, property_id, property_value->as_unresolved());
902
0
        if (!property_value->is_unresolved())
903
0
            set_property_expanding_shorthands(style, property_id, property_value, declaration, style_for_revert, style_for_revert_layer);
904
905
0
        style.set_property_important(property_id, important);
906
907
0
        set_property_expanding_shorthands(style, property_id, value, declaration, style_for_revert, style_for_revert_layer, important);
908
0
    }
909
0
}
910
911
void StyleComputer::cascade_declarations(StyleProperties& style, DOM::Element& element, Optional<CSS::Selector::PseudoElement::Type> pseudo_element, Vector<MatchingRule> const& matching_rules, CascadeOrigin cascade_origin, Important important, StyleProperties const& style_for_revert, StyleProperties const& style_for_revert_layer) const
912
0
{
913
0
    for (auto const& match : matching_rules) {
914
0
        for (auto const& property : match.declaration().properties()) {
915
0
            if (important != property.important)
916
0
                continue;
917
918
0
            if (property.property_id == CSS::PropertyID::All) {
919
0
                set_all_properties(element, pseudo_element, style, property.value, m_document, &match.declaration(), style_for_revert, style_for_revert_layer, important);
920
0
                continue;
921
0
            }
922
923
0
            auto property_value = property.value;
924
0
            if (property.value->is_unresolved())
925
0
                property_value = Parser::Parser::resolve_unresolved_style_value(Parser::ParsingContext { document() }, element, pseudo_element, property.property_id, property.value->as_unresolved());
926
0
            if (!property_value->is_unresolved())
927
0
                set_property_expanding_shorthands(style, property.property_id, property_value, &match.declaration(), style_for_revert, style_for_revert_layer, important);
928
0
        }
929
0
    }
930
931
0
    if (cascade_origin == CascadeOrigin::Author && !pseudo_element.has_value()) {
932
0
        if (auto const inline_style = element.inline_style()) {
933
0
            for (auto const& property : inline_style->properties()) {
934
0
                if (important != property.important)
935
0
                    continue;
936
937
0
                if (property.property_id == CSS::PropertyID::All) {
938
0
                    set_all_properties(element, pseudo_element, style, property.value, m_document, inline_style, style_for_revert, style_for_revert_layer, important);
939
0
                    continue;
940
0
                }
941
942
0
                auto property_value = property.value;
943
0
                if (property.value->is_unresolved())
944
0
                    property_value = Parser::Parser::resolve_unresolved_style_value(Parser::ParsingContext { document() }, element, pseudo_element, property.property_id, property.value->as_unresolved());
945
0
                if (!property_value->is_unresolved())
946
0
                    set_property_expanding_shorthands(style, property.property_id, property_value, inline_style, style_for_revert, style_for_revert_layer, important);
947
0
            }
948
0
        }
949
0
    }
950
0
}
951
952
static void cascade_custom_properties(DOM::Element& element, Optional<CSS::Selector::PseudoElement::Type> pseudo_element, Vector<MatchingRule> const& matching_rules, HashMap<FlyString, StyleProperty>& custom_properties)
953
0
{
954
0
    size_t needed_capacity = 0;
955
0
    for (auto const& matching_rule : matching_rules)
956
0
        needed_capacity += matching_rule.declaration().custom_properties().size();
957
958
0
    if (!pseudo_element.has_value()) {
959
0
        if (auto const inline_style = element.inline_style())
960
0
            needed_capacity += inline_style->custom_properties().size();
961
0
    }
962
963
0
    custom_properties.ensure_capacity(custom_properties.size() + needed_capacity);
964
965
0
    for (auto const& matching_rule : matching_rules) {
966
0
        for (auto const& it : matching_rule.declaration().custom_properties()) {
967
0
            auto style_value = it.value.value;
968
0
            if (style_value->is_revert_layer())
969
0
                continue;
970
0
            custom_properties.set(it.key, it.value);
971
0
        }
972
0
    }
973
974
0
    if (!pseudo_element.has_value()) {
975
0
        if (auto const inline_style = element.inline_style()) {
976
0
            for (auto const& it : inline_style->custom_properties())
977
0
                custom_properties.set(it.key, it.value);
978
0
        }
979
0
    }
980
0
}
981
982
void StyleComputer::collect_animation_into(DOM::Element& element, Optional<CSS::Selector::PseudoElement::Type> pseudo_element, JS::NonnullGCPtr<Animations::KeyframeEffect> effect, StyleProperties& style_properties, AnimationRefresh refresh) const
983
0
{
984
0
    auto animation = effect->associated_animation();
985
0
    if (!animation)
986
0
        return;
987
988
0
    auto output_progress = effect->transformed_progress();
989
0
    if (!output_progress.has_value())
990
0
        return;
991
992
0
    if (!effect->key_frame_set())
993
0
        return;
994
995
0
    auto& keyframes = effect->key_frame_set()->keyframes_by_key;
996
997
0
    auto key = static_cast<u64>(output_progress.value() * 100.0 * Animations::KeyframeEffect::AnimationKeyFrameKeyScaleFactor);
998
0
    auto matching_keyframe_it = keyframes.find_largest_not_above_iterator(key);
999
0
    if (matching_keyframe_it.is_end()) {
1000
        if constexpr (LIBWEB_CSS_ANIMATION_DEBUG) {
1001
            dbgln("    Did not find any start keyframe for the current state ({}) :(", key);
1002
            dbgln("    (have {} keyframes)", keyframes.size());
1003
            for (auto it = keyframes.begin(); it != keyframes.end(); ++it)
1004
                dbgln("        - {}", it.key());
1005
        }
1006
0
        return;
1007
0
    }
1008
1009
0
    auto keyframe_start = matching_keyframe_it.key();
1010
0
    auto keyframe_values = *matching_keyframe_it;
1011
1012
0
    auto initial_keyframe_it = matching_keyframe_it;
1013
0
    auto keyframe_end_it = ++matching_keyframe_it;
1014
0
    if (keyframe_end_it.is_end())
1015
0
        keyframe_end_it = initial_keyframe_it;
1016
1017
0
    auto keyframe_end = keyframe_end_it.key();
1018
0
    auto keyframe_end_values = *keyframe_end_it;
1019
1020
0
    auto progress_in_keyframe = [&] {
1021
0
        if (keyframe_start == keyframe_end)
1022
0
            return 0.f;
1023
0
        return static_cast<float>(key - keyframe_start) / static_cast<float>(keyframe_end - keyframe_start);
1024
0
    }();
1025
1026
    if constexpr (LIBWEB_CSS_ANIMATION_DEBUG) {
1027
        auto valid_properties = keyframe_values.properties.size();
1028
        dbgln("Animation {} contains {} properties to interpolate, progress = {}%", animation->id(), valid_properties, progress_in_keyframe * 100);
1029
    }
1030
1031
0
    for (auto const& it : keyframe_values.properties) {
1032
0
        auto resolve_property = [&](auto& property) {
1033
0
            return property.visit(
1034
0
                [&](Animations::KeyframeEffect::KeyFrameSet::UseInitial) -> RefPtr<CSSStyleValue const> {
1035
0
                    if (refresh == AnimationRefresh::Yes)
1036
0
                        return {};
1037
0
                    return style_properties.maybe_null_property(it.key);
1038
0
                },
1039
0
                [&](RefPtr<CSSStyleValue const> value) -> RefPtr<CSSStyleValue const> {
1040
0
                    if (value->is_unresolved())
1041
0
                        return Parser::Parser::resolve_unresolved_style_value(Parser::ParsingContext { element.document() }, element, pseudo_element, it.key, value->as_unresolved());
1042
0
                    return value;
1043
0
                });
1044
0
        };
1045
1046
0
        auto resolved_start_property = resolve_property(it.value);
1047
1048
0
        auto const& end_property = keyframe_end_values.properties.get(it.key);
1049
0
        if (!end_property.has_value()) {
1050
0
            if (resolved_start_property) {
1051
0
                style_properties.set_animated_property(it.key, *resolved_start_property);
1052
0
                dbgln_if(LIBWEB_CSS_ANIMATION_DEBUG, "No end property for property {}, using {}", string_from_property_id(it.key), resolved_start_property->to_string());
1053
0
            }
1054
0
            continue;
1055
0
        }
1056
1057
0
        auto resolved_end_property = resolve_property(end_property.value());
1058
1059
0
        if (resolved_end_property && !resolved_start_property)
1060
0
            resolved_start_property = CSS::property_initial_value(document().realm(), it.key);
1061
1062
0
        if (!resolved_start_property || !resolved_end_property)
1063
0
            continue;
1064
1065
0
        auto start = resolved_start_property.release_nonnull();
1066
0
        auto end = resolved_end_property.release_nonnull();
1067
1068
0
        if (style_properties.is_property_important(it.key)) {
1069
0
            continue;
1070
0
        }
1071
1072
0
        if (auto next_value = interpolate_property(*effect->target(), it.key, *start, *end, progress_in_keyframe)) {
1073
0
            dbgln_if(LIBWEB_CSS_ANIMATION_DEBUG, "Interpolated value for property {} at {}: {} -> {} = {}", string_from_property_id(it.key), progress_in_keyframe, start->to_string(), end->to_string(), next_value->to_string());
1074
0
            style_properties.set_animated_property(it.key, *next_value);
1075
0
        } else {
1076
            // If interpolate_property() fails, the element should not be rendered
1077
0
            dbgln_if(LIBWEB_CSS_ANIMATION_DEBUG, "Interpolated value for property {} at {}: {} -> {} is invalid", string_from_property_id(it.key), progress_in_keyframe, start->to_string(), end->to_string());
1078
0
            style_properties.set_animated_property(PropertyID::Visibility, CSSKeywordValue::create(Keyword::Hidden));
1079
0
        }
1080
0
    }
1081
0
}
1082
1083
static void apply_animation_properties(DOM::Document& document, StyleProperties& style, Animations::Animation& animation)
1084
0
{
1085
0
    auto& effect = verify_cast<Animations::KeyframeEffect>(*animation.effect());
1086
1087
0
    Optional<CSS::Time> duration;
1088
0
    if (auto duration_value = style.maybe_null_property(PropertyID::AnimationDuration); duration_value) {
1089
0
        if (duration_value->is_time()) {
1090
0
            duration = duration_value->as_time().time();
1091
0
        } else if (duration_value->is_keyword() && duration_value->as_keyword().keyword() == Keyword::Auto) {
1092
            // We use empty optional to represent "auto".
1093
0
            duration = {};
1094
0
        }
1095
0
    }
1096
1097
0
    CSS::Time delay { 0, CSS::Time::Type::S };
1098
0
    if (auto delay_value = style.maybe_null_property(PropertyID::AnimationDelay); delay_value && delay_value->is_time())
1099
0
        delay = delay_value->as_time().time();
1100
1101
0
    double iteration_count = 1.0;
1102
0
    if (auto iteration_count_value = style.maybe_null_property(PropertyID::AnimationIterationCount); iteration_count_value) {
1103
0
        if (iteration_count_value->is_keyword() && iteration_count_value->to_keyword() == Keyword::Infinite)
1104
0
            iteration_count = HUGE_VAL;
1105
0
        else if (iteration_count_value->is_number())
1106
0
            iteration_count = iteration_count_value->as_number().number();
1107
0
    }
1108
1109
0
    CSS::AnimationFillMode fill_mode { CSS::AnimationFillMode::None };
1110
0
    if (auto fill_mode_property = style.maybe_null_property(PropertyID::AnimationFillMode); fill_mode_property && fill_mode_property->is_keyword()) {
1111
0
        if (auto fill_mode_value = keyword_to_animation_fill_mode(fill_mode_property->to_keyword()); fill_mode_value.has_value())
1112
0
            fill_mode = *fill_mode_value;
1113
0
    }
1114
1115
0
    CSS::AnimationDirection direction { CSS::AnimationDirection::Normal };
1116
0
    if (auto direction_property = style.maybe_null_property(PropertyID::AnimationDirection); direction_property && direction_property->is_keyword()) {
1117
0
        if (auto direction_value = keyword_to_animation_direction(direction_property->to_keyword()); direction_value.has_value())
1118
0
            direction = *direction_value;
1119
0
    }
1120
1121
0
    CSS::AnimationPlayState play_state { CSS::AnimationPlayState::Running };
1122
0
    if (auto play_state_property = style.maybe_null_property(PropertyID::AnimationPlayState); play_state_property && play_state_property->is_keyword()) {
1123
0
        if (auto play_state_value = keyword_to_animation_play_state(play_state_property->to_keyword()); play_state_value.has_value())
1124
0
            play_state = *play_state_value;
1125
0
    }
1126
1127
0
    CSS::EasingStyleValue::Function timing_function { CSS::EasingStyleValue::CubicBezier::ease() };
1128
0
    if (auto timing_property = style.maybe_null_property(PropertyID::AnimationTimingFunction); timing_property && timing_property->is_easing())
1129
0
        timing_function = timing_property->as_easing().function();
1130
1131
0
    auto iteration_duration = duration.has_value()
1132
0
        ? Variant<double, String> { duration.release_value().to_milliseconds() }
1133
0
        : "auto"_string;
1134
0
    effect.set_iteration_duration(iteration_duration);
1135
0
    effect.set_start_delay(delay.to_milliseconds());
1136
0
    effect.set_iteration_count(iteration_count);
1137
0
    effect.set_timing_function(move(timing_function));
1138
0
    effect.set_fill_mode(Animations::css_fill_mode_to_bindings_fill_mode(fill_mode));
1139
0
    effect.set_playback_direction(Animations::css_animation_direction_to_bindings_playback_direction(direction));
1140
1141
0
    if (play_state != effect.last_css_animation_play_state()) {
1142
0
        if (play_state == CSS::AnimationPlayState::Running && animation.play_state() == Bindings::AnimationPlayState::Paused) {
1143
0
            HTML::TemporaryExecutionContext context(document.relevant_settings_object());
1144
0
            animation.play().release_value_but_fixme_should_propagate_errors();
1145
0
        } else if (play_state == CSS::AnimationPlayState::Paused && animation.play_state() != Bindings::AnimationPlayState::Paused) {
1146
0
            HTML::TemporaryExecutionContext context(document.relevant_settings_object());
1147
0
            animation.pause().release_value_but_fixme_should_propagate_errors();
1148
0
        }
1149
1150
0
        effect.set_last_css_animation_play_state(play_state);
1151
0
    }
1152
0
}
1153
1154
static void apply_dimension_attribute(StyleProperties& style, DOM::Element const& element, FlyString const& attribute_name, CSS::PropertyID property_id)
1155
0
{
1156
0
    auto attribute = element.attribute(attribute_name);
1157
0
    if (!attribute.has_value())
1158
0
        return;
1159
1160
0
    auto parsed_value = HTML::parse_dimension_value(*attribute);
1161
0
    if (!parsed_value)
1162
0
        return;
1163
1164
0
    style.set_property(property_id, parsed_value.release_nonnull());
1165
0
}
1166
1167
static void compute_transitioned_properties(StyleProperties const& style, DOM::Element& element, Optional<Selector::PseudoElement::Type> pseudo_element)
1168
0
{
1169
    // FIXME: Implement transitioning for pseudo-elements
1170
0
    (void)pseudo_element;
1171
1172
0
    auto const source_declaration = style.transition_property_source();
1173
0
    if (!source_declaration)
1174
0
        return;
1175
0
    if (!element.computed_css_values())
1176
0
        return;
1177
0
    if (source_declaration == element.cached_transition_property_source())
1178
0
        return;
1179
    // Reparse this transition property
1180
0
    element.clear_transitions();
1181
0
    element.set_cached_transition_property_source(*source_declaration);
1182
1183
0
    auto transition_properties_value = style.property(PropertyID::TransitionProperty);
1184
0
    auto transition_properties = transition_properties_value->is_value_list()
1185
0
        ? transition_properties_value->as_value_list().values()
1186
0
        : StyleValueVector { transition_properties_value };
1187
1188
0
    Vector<Vector<PropertyID>> properties;
1189
1190
0
    for (size_t i = 0; i < transition_properties.size(); i++) {
1191
0
        auto property_value = transition_properties[i];
1192
0
        Vector<PropertyID> properties_for_this_transition;
1193
1194
0
        if (property_value->is_keyword()) {
1195
0
            auto keyword = property_value->as_keyword().keyword();
1196
0
            if (keyword == Keyword::None)
1197
0
                continue;
1198
0
            if (keyword == Keyword::All) {
1199
0
                for (auto prop = first_property_id; prop != last_property_id; prop = static_cast<PropertyID>(to_underlying(prop) + 1))
1200
0
                    properties_for_this_transition.append(prop);
1201
0
            }
1202
0
        } else {
1203
0
            auto maybe_property = property_id_from_string(property_value->as_custom_ident().custom_ident());
1204
0
            if (!maybe_property.has_value())
1205
0
                continue;
1206
1207
0
            auto transition_property = maybe_property.release_value();
1208
0
            if (property_is_shorthand(transition_property)) {
1209
0
                for (auto const& prop : longhands_for_shorthand(transition_property))
1210
0
                    properties_for_this_transition.append(prop);
1211
0
            } else {
1212
0
                properties_for_this_transition.append(transition_property);
1213
0
            }
1214
0
        }
1215
1216
0
        properties.append(move(properties_for_this_transition));
1217
0
    }
1218
1219
0
    auto normalize_transition_length_list = [&properties, &style](PropertyID property, auto make_default_value) {
1220
0
        auto style_value = style.maybe_null_property(property);
1221
0
        StyleValueVector list;
1222
1223
0
        if (!style_value || !style_value->is_value_list() || style_value->as_value_list().size() == 0) {
1224
0
            auto default_value = make_default_value();
1225
0
            for (size_t i = 0; i < properties.size(); i++)
1226
0
                list.append(default_value);
1227
0
            return list;
1228
0
        }
1229
1230
0
        auto const& value_list = style_value->as_value_list();
1231
0
        for (size_t i = 0; i < properties.size(); i++)
1232
0
            list.append(value_list.value_at(i, true));
1233
1234
0
        return list;
1235
0
    };
Unexecuted instantiation: StyleComputer.cpp:auto Web::CSS::compute_transitioned_properties(Web::CSS::StyleProperties const&, Web::DOM::Element&, AK::Optional<Web::CSS::Selector::PseudoElement::Type>)::$_0::operator()<Web::CSS::compute_transitioned_properties(Web::CSS::StyleProperties const&, Web::DOM::Element&, AK::Optional<Web::CSS::Selector::PseudoElement::Type>)::$_1>(Web::CSS::PropertyID, Web::CSS::compute_transitioned_properties(Web::CSS::StyleProperties const&, Web::DOM::Element&, AK::Optional<Web::CSS::Selector::PseudoElement::Type>)::$_1) const
Unexecuted instantiation: StyleComputer.cpp:auto Web::CSS::compute_transitioned_properties(Web::CSS::StyleProperties const&, Web::DOM::Element&, AK::Optional<Web::CSS::Selector::PseudoElement::Type>)::$_0::operator()<Web::CSS::compute_transitioned_properties(Web::CSS::StyleProperties const&, Web::DOM::Element&, AK::Optional<Web::CSS::Selector::PseudoElement::Type>)::$_2>(Web::CSS::PropertyID, Web::CSS::compute_transitioned_properties(Web::CSS::StyleProperties const&, Web::DOM::Element&, AK::Optional<Web::CSS::Selector::PseudoElement::Type>)::$_2) const
Unexecuted instantiation: StyleComputer.cpp:auto Web::CSS::compute_transitioned_properties(Web::CSS::StyleProperties const&, Web::DOM::Element&, AK::Optional<Web::CSS::Selector::PseudoElement::Type>)::$_0::operator()<Web::CSS::compute_transitioned_properties(Web::CSS::StyleProperties const&, Web::DOM::Element&, AK::Optional<Web::CSS::Selector::PseudoElement::Type>)::$_3>(Web::CSS::PropertyID, Web::CSS::compute_transitioned_properties(Web::CSS::StyleProperties const&, Web::DOM::Element&, AK::Optional<Web::CSS::Selector::PseudoElement::Type>)::$_3) const
1236
1237
0
    auto delays = normalize_transition_length_list(
1238
0
        PropertyID::TransitionDelay,
1239
0
        [] { return TimeStyleValue::create(Time::make_seconds(0.0)); });
1240
0
    auto durations = normalize_transition_length_list(
1241
0
        PropertyID::TransitionDuration,
1242
0
        [] { return TimeStyleValue::create(Time::make_seconds(0.0)); });
1243
0
    auto timing_functions = normalize_transition_length_list(
1244
0
        PropertyID::TransitionTimingFunction,
1245
0
        [] { return EasingStyleValue::create(EasingStyleValue::CubicBezier::ease()); });
1246
1247
0
    element.add_transitioned_properties(move(properties), move(delays), move(durations), move(timing_functions));
1248
0
}
1249
1250
// https://drafts.csswg.org/css-transitions/#starting
1251
void StyleComputer::start_needed_transitions(StyleProperties const& previous_style, StyleProperties& new_style, DOM::Element& element, Optional<Selector::PseudoElement::Type> pseudo_element) const
1252
0
{
1253
    // FIXME: Implement transitions for pseudo-elements
1254
0
    if (pseudo_element.has_value())
1255
0
        return;
1256
1257
    // https://drafts.csswg.org/css-transitions/#transition-combined-duration
1258
0
    auto combined_duration = [](Animations::Animatable::TransitionAttributes const& transition_attributes) {
1259
        // Define the combined duration of the transition as the sum of max(matching transition duration, 0s) and the matching transition delay.
1260
0
        return max(transition_attributes.duration, 0) + transition_attributes.delay;
1261
0
    };
1262
1263
    // For each element and property, the implementation must act as follows:
1264
0
    auto style_change_event_time = m_document->timeline()->current_time().value();
1265
1266
0
    for (auto i = to_underlying(CSS::first_longhand_property_id); i <= to_underlying(CSS::last_longhand_property_id); ++i) {
1267
0
        auto property_id = static_cast<CSS::PropertyID>(i);
1268
0
        auto matching_transition_properties = element.property_transition_attributes(property_id);
1269
0
        auto before_change_value = previous_style.property(property_id, StyleProperties::WithAnimationsApplied::No);
1270
0
        auto after_change_value = new_style.property(property_id, StyleProperties::WithAnimationsApplied::No);
1271
1272
0
        auto existing_transition = element.property_transition(property_id);
1273
0
        bool has_running_transition = existing_transition && !existing_transition->is_finished();
1274
0
        bool has_completed_transition = existing_transition && existing_transition->is_finished();
1275
1276
0
        auto start_a_transition = [&](auto start_time, auto end_time, auto start_value, auto end_value, auto reversing_adjusted_start_value, auto reversing_shortening_factor) {
1277
0
            dbgln_if(CSS_TRANSITIONS_DEBUG, "Starting a transition of {} from {} to {}", string_from_property_id(property_id), start_value->to_string(), end_value->to_string());
1278
1279
0
            auto transition = CSSTransition::start_a_transition(element, property_id, document().transition_generation(),
1280
0
                start_time, end_time, start_value, end_value, reversing_adjusted_start_value, reversing_shortening_factor);
1281
            // Immediately set the property's value to the transition's current value, to prevent single-frame jumps.
1282
0
            new_style.set_animated_property(property_id, transition->value_at_time(style_change_event_time));
1283
0
        };
1284
1285
        // 1. If all of the following are true:
1286
0
        if (
1287
            // - the element does not have a running transition for the property,
1288
0
            (!has_running_transition) &&
1289
            // - the before-change style is different from the after-change style for that property, and the values for the property are transitionable,
1290
0
            (!before_change_value->equals(after_change_value) && property_values_are_transitionable(property_id, before_change_value, after_change_value)) &&
1291
            // - the element does not have a completed transition for the property
1292
            //   or the end value of the completed transition is different from the after-change style for the property,
1293
0
            (!has_completed_transition || !existing_transition->transition_end_value()->equals(after_change_value)) &&
1294
            // - there is a matching transition-property value, and
1295
0
            (matching_transition_properties.has_value()) &&
1296
            // - the combined duration is greater than 0s,
1297
0
            (combined_duration(matching_transition_properties.value()) > 0)) {
1298
1299
0
            dbgln_if(CSS_TRANSITIONS_DEBUG, "Transition step 1.");
1300
1301
            // then implementations must remove the completed transition (if present) from the set of completed transitions
1302
0
            if (has_completed_transition)
1303
0
                element.remove_transition(property_id);
1304
            // and start a transition whose:
1305
1306
            // - start time is the time of the style change event plus the matching transition delay,
1307
0
            auto start_time = style_change_event_time + matching_transition_properties->delay;
1308
1309
            // - end time is the start time plus the matching transition duration,
1310
0
            auto end_time = start_time + matching_transition_properties->duration;
1311
1312
            // - start value is the value of the transitioning property in the before-change style,
1313
0
            auto start_value = before_change_value;
1314
1315
            // - end value is the value of the transitioning property in the after-change style,
1316
0
            auto end_value = after_change_value;
1317
1318
            // - reversing-adjusted start value is the same as the start value, and
1319
0
            auto reversing_adjusted_start_value = start_value;
1320
1321
            // - reversing shortening factor is 1.
1322
0
            double reversing_shortening_factor = 1;
1323
1324
0
            start_a_transition(start_time, end_time, start_value, end_value, reversing_adjusted_start_value, reversing_shortening_factor);
1325
0
        }
1326
1327
        // 2. Otherwise, if the element has a completed transition for the property
1328
        //    and the end value of the completed transition is different from the after-change style for the property,
1329
        //    then implementations must remove the completed transition from the set of completed transitions.
1330
0
        else if (has_completed_transition && !existing_transition->transition_end_value()->equals(after_change_value)) {
1331
0
            dbgln_if(CSS_TRANSITIONS_DEBUG, "Transition step 2.");
1332
0
            element.remove_transition(property_id);
1333
0
        }
1334
1335
        // 3. If the element has a running transition or completed transition for the property,
1336
        //    and there is not a matching transition-property value,
1337
0
        if (existing_transition && !matching_transition_properties.has_value()) {
1338
            // then implementations must cancel the running transition or remove the completed transition from the set of completed transitions.
1339
0
            dbgln_if(CSS_TRANSITIONS_DEBUG, "Transition step 3.");
1340
0
            if (has_running_transition)
1341
0
                existing_transition->cancel();
1342
0
            else
1343
0
                element.remove_transition(property_id);
1344
0
        }
1345
1346
        // 4. If the element has a running transition for the property,
1347
        //    there is a matching transition-property value,
1348
        //    and the end value of the running transition is not equal to the value of the property in the after-change style, then:
1349
0
        if (has_running_transition && matching_transition_properties.has_value() && !existing_transition->transition_end_value()->equals(after_change_value)) {
1350
0
            dbgln_if(CSS_TRANSITIONS_DEBUG, "Transition step 4. existing end value = {}, after change value = {}", existing_transition->transition_end_value()->to_string(), after_change_value->to_string());
1351
            // 1. If the current value of the property in the running transition is equal to the value of the property in the after-change style,
1352
            //    or if these two values are not transitionable,
1353
            //    then implementations must cancel the running transition.
1354
0
            auto current_value = existing_transition->value_at_time(style_change_event_time);
1355
0
            if (current_value->equals(after_change_value) || !property_values_are_transitionable(property_id, current_value, after_change_value)) {
1356
0
                dbgln_if(CSS_TRANSITIONS_DEBUG, "Transition step 4.1");
1357
0
                existing_transition->cancel();
1358
0
            }
1359
1360
            // 2. Otherwise, if the combined duration is less than or equal to 0s,
1361
            //    or if the current value of the property in the running transition is not transitionable with the value of the property in the after-change style,
1362
            //    then implementations must cancel the running transition.
1363
0
            else if ((combined_duration(matching_transition_properties.value()) <= 0)
1364
0
                || !property_values_are_transitionable(property_id, current_value, after_change_value)) {
1365
0
                dbgln_if(CSS_TRANSITIONS_DEBUG, "Transition step 4.2");
1366
0
                existing_transition->cancel();
1367
0
            }
1368
1369
            // 3. Otherwise, if the reversing-adjusted start value of the running transition is the same as the value of the property in the after-change style
1370
            //    (see the section on reversing of transitions for why these case exists),
1371
0
            else if (existing_transition->reversing_adjusted_start_value()->equals(after_change_value)) {
1372
0
                dbgln_if(CSS_TRANSITIONS_DEBUG, "Transition step 4.3");
1373
                // implementations must cancel the running transition and start a new transition whose:
1374
0
                existing_transition->cancel();
1375
                // AD-HOC: Remove the cancelled transition, otherwise it breaks the invariant that there is only one
1376
                // running or completed transition for a property at once.
1377
0
                element.remove_transition(property_id);
1378
1379
                // - reversing-adjusted start value is the end value of the running transition,
1380
0
                auto reversing_adjusted_start_value = existing_transition->transition_end_value();
1381
1382
                // - reversing shortening factor is the absolute value, clamped to the range [0, 1], of the sum of:
1383
                //   1. the output of the timing function of the old transition at the time of the style change event,
1384
                //      times the reversing shortening factor of the old transition
1385
0
                auto term_1 = existing_transition->timing_function_output_at_time(style_change_event_time) * existing_transition->reversing_shortening_factor();
1386
                //   2. 1 minus the reversing shortening factor of the old transition.
1387
0
                auto term_2 = 1 - existing_transition->reversing_shortening_factor();
1388
0
                double reversing_shortening_factor = clamp(abs(term_1 + term_2), 0.0, 1.0);
1389
1390
                // - start time is the time of the style change event plus:
1391
                //   1. if the matching transition delay is nonnegative, the matching transition delay, or
1392
                //   2. if the matching transition delay is negative, the product of the new transition’s reversing shortening factor and the matching transition delay,
1393
0
                auto start_time = style_change_event_time
1394
0
                    + (matching_transition_properties->delay >= 0
1395
0
                            ? (matching_transition_properties->delay)
1396
0
                            : (reversing_shortening_factor * matching_transition_properties->delay));
1397
1398
                // - end time is the start time plus the product of the matching transition duration and the new transition’s reversing shortening factor,
1399
0
                auto end_time = start_time + (matching_transition_properties->duration * reversing_shortening_factor);
1400
1401
                // - start value is the current value of the property in the running transition,
1402
0
                auto start_value = current_value;
1403
1404
                // - end value is the value of the property in the after-change style,
1405
0
                auto end_value = after_change_value;
1406
1407
0
                start_a_transition(start_time, end_time, start_value, end_value, reversing_adjusted_start_value, reversing_shortening_factor);
1408
0
            }
1409
1410
            // 4. Otherwise,
1411
0
            else {
1412
0
                dbgln_if(CSS_TRANSITIONS_DEBUG, "Transition step 4.4");
1413
                // implementations must cancel the running transition and start a new transition whose:
1414
0
                existing_transition->cancel();
1415
                // AD-HOC: Remove the cancelled transition, otherwise it breaks the invariant that there is only one
1416
                // running or completed transition for a property at once.
1417
0
                element.remove_transition(property_id);
1418
1419
                // - start time is the time of the style change event plus the matching transition delay,
1420
0
                auto start_time = style_change_event_time + matching_transition_properties->delay;
1421
1422
                // - end time is the start time plus the matching transition duration,
1423
0
                auto end_time = start_time + matching_transition_properties->duration;
1424
1425
                // - start value is the current value of the property in the running transition,
1426
0
                auto start_value = current_value;
1427
1428
                // - end value is the value of the property in the after-change style,
1429
0
                auto end_value = after_change_value;
1430
1431
                // - reversing-adjusted start value is the same as the start value, and
1432
0
                auto reversing_adjusted_start_value = start_value;
1433
1434
                // - reversing shortening factor is 1.
1435
0
                double reversing_shortening_factor = 1;
1436
1437
0
                start_a_transition(start_time, end_time, start_value, end_value, reversing_adjusted_start_value, reversing_shortening_factor);
1438
0
            }
1439
0
        }
1440
0
    }
1441
0
}
1442
1443
// https://www.w3.org/TR/css-cascade/#cascading
1444
// https://drafts.csswg.org/css-cascade-5/#layering
1445
void StyleComputer::compute_cascaded_values(StyleProperties& style, DOM::Element& element, Optional<CSS::Selector::PseudoElement::Type> pseudo_element, bool& did_match_any_pseudo_element_rules, ComputeStyleMode mode) const
1446
0
{
1447
    // First, we collect all the CSS rules whose selectors match `element`:
1448
0
    MatchingRuleSet matching_rule_set;
1449
0
    matching_rule_set.user_agent_rules = collect_matching_rules(element, CascadeOrigin::UserAgent, pseudo_element);
1450
0
    sort_matching_rules(matching_rule_set.user_agent_rules);
1451
0
    matching_rule_set.user_rules = collect_matching_rules(element, CascadeOrigin::User, pseudo_element);
1452
0
    sort_matching_rules(matching_rule_set.user_rules);
1453
    // @layer-ed author rules
1454
0
    for (auto const& layer_name : m_qualified_layer_names_in_order) {
1455
0
        auto layer_rules = collect_matching_rules(element, CascadeOrigin::Author, pseudo_element, layer_name);
1456
0
        sort_matching_rules(layer_rules);
1457
0
        matching_rule_set.author_rules.append({ layer_name, layer_rules });
1458
0
    }
1459
    // Un-@layer-ed author rules
1460
0
    auto unlayered_author_rules = collect_matching_rules(element, CascadeOrigin::Author, pseudo_element);
1461
0
    sort_matching_rules(unlayered_author_rules);
1462
0
    matching_rule_set.author_rules.append({ {}, unlayered_author_rules });
1463
1464
0
    if (mode == ComputeStyleMode::CreatePseudoElementStyleIfNeeded) {
1465
0
        VERIFY(pseudo_element.has_value());
1466
0
        if (matching_rule_set.author_rules.is_empty() && matching_rule_set.user_rules.is_empty() && matching_rule_set.user_agent_rules.is_empty()) {
1467
0
            did_match_any_pseudo_element_rules = false;
1468
0
            return;
1469
0
        }
1470
0
        did_match_any_pseudo_element_rules = true;
1471
0
    }
1472
1473
    // Then we resolve all the CSS custom properties ("variables") for this element:
1474
    // FIXME: Also resolve !important custom properties, in a second cascade.
1475
1476
0
    HashMap<FlyString, CSS::StyleProperty> custom_properties;
1477
0
    for (auto& layer : matching_rule_set.author_rules) {
1478
0
        cascade_custom_properties(element, pseudo_element, layer.rules, custom_properties);
1479
0
    }
1480
0
    element.set_custom_properties(pseudo_element, move(custom_properties));
1481
1482
    // Then we apply the declarations from the matched rules in cascade order:
1483
1484
    // Normal user agent declarations
1485
0
    auto previous_origin_style = style.clone();
1486
0
    auto previous_layer_style = style.clone();
1487
0
    cascade_declarations(style, element, pseudo_element, matching_rule_set.user_agent_rules, CascadeOrigin::UserAgent, Important::No, previous_origin_style, previous_layer_style);
1488
1489
    // Normal user declarations
1490
0
    previous_origin_style = style.clone();
1491
0
    previous_layer_style = style.clone();
1492
0
    cascade_declarations(style, element, pseudo_element, matching_rule_set.user_rules, CascadeOrigin::User, Important::No, previous_origin_style, previous_layer_style);
1493
1494
    // Author presentational hints
1495
    // The spec calls this a special "Author presentational hint origin":
1496
    // "For the purpose of cascading this author presentational hint origin is treated as an independent origin;
1497
    // however for the purpose of the revert keyword (but not for the revert-layer keyword) it is considered
1498
    // part of the author origin."
1499
    // https://drafts.csswg.org/css-cascade-5/#author-presentational-hint-origin
1500
0
    previous_origin_style = style.clone();
1501
0
    if (!pseudo_element.has_value()) {
1502
0
        element.apply_presentational_hints(style);
1503
1504
0
        if (element.supports_dimension_attributes()) {
1505
0
            apply_dimension_attribute(style, element, HTML::AttributeNames::width, CSS::PropertyID::Width);
1506
0
            apply_dimension_attribute(style, element, HTML::AttributeNames::height, CSS::PropertyID::Height);
1507
0
        }
1508
1509
        // SVG presentation attributes are parsed as CSS values, so we need to handle potential custom properties here.
1510
0
        if (element.is_svg_element()) {
1511
            // FIXME: This is not very efficient, we should only resolve the custom properties that are actually used.
1512
0
            for (auto i = to_underlying(CSS::first_property_id); i <= to_underlying(CSS::last_property_id); ++i) {
1513
0
                auto property_id = (CSS::PropertyID)i;
1514
0
                auto& property = style.m_data->m_property_values[i];
1515
0
                if (property && property->is_unresolved())
1516
0
                    property = Parser::Parser::resolve_unresolved_style_value(Parser::ParsingContext { document() }, element, pseudo_element, property_id, property->as_unresolved());
1517
0
            }
1518
0
        }
1519
0
    }
1520
1521
    // Normal author declarations, ordered by @layer, with un-@layer-ed rules last
1522
0
    for (auto const& layer : matching_rule_set.author_rules) {
1523
0
        previous_layer_style = style.clone();
1524
0
        cascade_declarations(style, element, pseudo_element, layer.rules, CascadeOrigin::Author, Important::No, previous_origin_style, previous_layer_style);
1525
0
    }
1526
1527
    // Animation declarations [css-animations-2]
1528
0
    auto animation_name = [&]() -> Optional<String> {
1529
0
        auto animation_name = style.maybe_null_property(PropertyID::AnimationName);
1530
0
        if (animation_name.is_null())
1531
0
            return OptionalNone {};
1532
0
        if (animation_name->is_string())
1533
0
            return animation_name->as_string().string_value().to_string();
1534
0
        return animation_name->to_string();
1535
0
    }();
1536
1537
0
    if (animation_name.has_value()) {
1538
0
        if (auto source_declaration = style.animation_name_source()) {
1539
0
            auto& realm = element.realm();
1540
1541
0
            if (source_declaration != element.cached_animation_name_source(pseudo_element)) {
1542
                // This animation name is new, so we need to create a new animation for it.
1543
0
                if (auto existing_animation = element.cached_animation_name_animation(pseudo_element))
1544
0
                    existing_animation->cancel(Animations::Animation::ShouldInvalidate::No);
1545
0
                element.set_cached_animation_name_source(source_declaration, pseudo_element);
1546
1547
0
                auto effect = Animations::KeyframeEffect::create(realm);
1548
0
                auto animation = CSSAnimation::create(realm);
1549
0
                animation->set_id(animation_name.release_value());
1550
0
                animation->set_timeline(m_document->timeline());
1551
0
                animation->set_owning_element(element);
1552
0
                animation->set_effect(effect);
1553
0
                apply_animation_properties(m_document, style, animation);
1554
0
                if (pseudo_element.has_value())
1555
0
                    effect->set_pseudo_element(Selector::PseudoElement { pseudo_element.value() });
1556
1557
0
                auto const& rule_cache = rule_cache_for_cascade_origin(CascadeOrigin::Author);
1558
0
                if (auto keyframe_set = rule_cache.rules_by_animation_keyframes.get(animation->id()); keyframe_set.has_value())
1559
0
                    effect->set_key_frame_set(keyframe_set.value());
1560
1561
0
                effect->set_target(&element);
1562
0
                element.set_cached_animation_name_animation(animation, pseudo_element);
1563
1564
0
                HTML::TemporaryExecutionContext context(m_document->relevant_settings_object());
1565
0
                animation->play().release_value_but_fixme_should_propagate_errors();
1566
0
            } else {
1567
                // The animation hasn't changed, but some properties of the animation may have
1568
0
                apply_animation_properties(m_document, style, *element.cached_animation_name_animation(pseudo_element));
1569
0
            }
1570
0
        }
1571
0
    } else {
1572
        // If the element had an existing animation, cancel it
1573
0
        if (auto existing_animation = element.cached_animation_name_animation(pseudo_element)) {
1574
0
            existing_animation->cancel(Animations::Animation::ShouldInvalidate::No);
1575
0
            element.set_cached_animation_name_animation({}, pseudo_element);
1576
0
            element.set_cached_animation_name_source({}, pseudo_element);
1577
0
        }
1578
0
    }
1579
1580
0
    auto animations = element.get_animations_internal({ .subtree = false });
1581
0
    for (auto& animation : animations) {
1582
0
        if (auto effect = animation->effect(); effect && effect->is_keyframe_effect()) {
1583
0
            auto& keyframe_effect = *static_cast<Animations::KeyframeEffect*>(effect.ptr());
1584
0
            if (keyframe_effect.pseudo_element_type() == pseudo_element)
1585
0
                collect_animation_into(element, pseudo_element, keyframe_effect, style);
1586
0
        }
1587
0
    }
1588
1589
    // Important author declarations, with un-@layer-ed rules first, followed by each @layer in reverse order.
1590
0
    previous_origin_style = style.clone();
1591
0
    for (auto const& layer : matching_rule_set.author_rules.in_reverse()) {
1592
0
        previous_layer_style = style.clone();
1593
0
        cascade_declarations(style, element, pseudo_element, layer.rules, CascadeOrigin::Author, Important::Yes, previous_origin_style, previous_layer_style);
1594
0
    }
1595
1596
    // Important user declarations
1597
0
    previous_origin_style = style.clone();
1598
0
    previous_layer_style = style.clone();
1599
0
    cascade_declarations(style, element, pseudo_element, matching_rule_set.user_rules, CascadeOrigin::User, Important::Yes, previous_origin_style, previous_layer_style);
1600
1601
    // Important user agent declarations
1602
0
    previous_origin_style = style.clone();
1603
0
    previous_layer_style = style.clone();
1604
0
    cascade_declarations(style, element, pseudo_element, matching_rule_set.user_agent_rules, CascadeOrigin::UserAgent, Important::Yes, previous_origin_style, previous_layer_style);
1605
1606
    // Transition declarations [css-transitions-1]
1607
    // Note that we have to do these after finishing computing the style,
1608
    // so they're not done here, but as the final step in compute_style_impl()
1609
0
}
1610
1611
DOM::Element const* element_to_inherit_style_from(DOM::Element const* element, Optional<CSS::Selector::PseudoElement::Type> pseudo_element)
1612
0
{
1613
    // Pseudo-elements treat their originating element as their parent.
1614
0
    DOM::Element const* parent_element = nullptr;
1615
0
    if (pseudo_element.has_value()) {
1616
0
        parent_element = element;
1617
0
    } else if (element) {
1618
0
        parent_element = element->parent_or_shadow_host_element();
1619
0
    }
1620
0
    return parent_element;
1621
0
}
1622
1623
NonnullRefPtr<CSSStyleValue const> StyleComputer::get_inherit_value(JS::Realm& initial_value_context_realm, CSS::PropertyID property_id, DOM::Element const* element, Optional<CSS::Selector::PseudoElement::Type> pseudo_element)
1624
0
{
1625
0
    auto* parent_element = element_to_inherit_style_from(element, pseudo_element);
1626
1627
0
    if (!parent_element || !parent_element->computed_css_values())
1628
0
        return property_initial_value(initial_value_context_realm, property_id);
1629
0
    return parent_element->computed_css_values()->property(property_id);
1630
0
}
1631
1632
void StyleComputer::compute_defaulted_property_value(StyleProperties& style, DOM::Element const* element, CSS::PropertyID property_id, Optional<CSS::Selector::PseudoElement::Type> pseudo_element) const
1633
0
{
1634
    // FIXME: If we don't know the correct initial value for a property, we fall back to `initial`.
1635
1636
0
    auto& value_slot = style.m_data->m_property_values[to_underlying(property_id)];
1637
0
    if (!value_slot) {
1638
0
        if (is_inherited_property(property_id)) {
1639
0
            style.set_property(
1640
0
                property_id,
1641
0
                get_inherit_value(document().realm(), property_id, element, pseudo_element),
1642
0
                StyleProperties::Inherited::Yes,
1643
0
                Important::No);
1644
0
        } else {
1645
0
            style.set_property(property_id, property_initial_value(document().realm(), property_id));
1646
0
        }
1647
0
        return;
1648
0
    }
1649
1650
0
    if (value_slot->is_initial()) {
1651
0
        value_slot = property_initial_value(document().realm(), property_id);
1652
0
        return;
1653
0
    }
1654
1655
0
    if (value_slot->is_inherit()) {
1656
0
        value_slot = get_inherit_value(document().realm(), property_id, element, pseudo_element);
1657
0
        style.set_property_inherited(property_id, StyleProperties::Inherited::Yes);
1658
0
        return;
1659
0
    }
1660
1661
    // https://www.w3.org/TR/css-cascade-4/#inherit-initial
1662
    // If the cascaded value of a property is the unset keyword,
1663
0
    if (value_slot->is_unset()) {
1664
0
        if (is_inherited_property(property_id)) {
1665
            // then if it is an inherited property, this is treated as inherit,
1666
0
            value_slot = get_inherit_value(document().realm(), property_id, element, pseudo_element);
1667
0
            style.set_property_inherited(property_id, StyleProperties::Inherited::Yes);
1668
0
        } else {
1669
            // and if it is not, this is treated as initial.
1670
0
            value_slot = property_initial_value(document().realm(), property_id);
1671
0
        }
1672
0
    }
1673
0
}
1674
1675
// https://www.w3.org/TR/css-cascade/#defaulting
1676
void StyleComputer::compute_defaulted_values(StyleProperties& style, DOM::Element const* element, Optional<CSS::Selector::PseudoElement::Type> pseudo_element) const
1677
0
{
1678
    // Walk the list of all known CSS properties and:
1679
    // - Add them to `style` if they are missing.
1680
    // - Resolve `inherit` and `initial` as needed.
1681
0
    for (auto i = to_underlying(CSS::first_longhand_property_id); i <= to_underlying(CSS::last_longhand_property_id); ++i) {
1682
0
        auto property_id = (CSS::PropertyID)i;
1683
0
        compute_defaulted_property_value(style, element, property_id, pseudo_element);
1684
0
    }
1685
1686
    // https://www.w3.org/TR/css-color-4/#resolving-other-colors
1687
    // In the color property, the used value of currentcolor is the inherited value.
1688
0
    auto color = style.property(CSS::PropertyID::Color);
1689
0
    if (color->to_keyword() == Keyword::Currentcolor) {
1690
0
        color = get_inherit_value(document().realm(), CSS::PropertyID::Color, element, pseudo_element);
1691
0
        style.set_property(CSS::PropertyID::Color, color);
1692
0
    }
1693
0
}
1694
1695
Length::FontMetrics StyleComputer::calculate_root_element_font_metrics(StyleProperties const& style) const
1696
0
{
1697
0
    auto root_value = style.property(CSS::PropertyID::FontSize);
1698
1699
0
    auto font_pixel_metrics = style.first_available_computed_font().pixel_metrics();
1700
0
    Length::FontMetrics font_metrics { m_default_font_metrics.font_size, font_pixel_metrics };
1701
0
    font_metrics.font_size = root_value->as_length().length().to_px(viewport_rect(), font_metrics, font_metrics);
1702
0
    font_metrics.line_height = style.compute_line_height(viewport_rect(), font_metrics, font_metrics);
1703
1704
0
    return font_metrics;
1705
0
}
1706
1707
RefPtr<Gfx::FontCascadeList const> StyleComputer::find_matching_font_weight_ascending(Vector<MatchingFontCandidate> const& candidates, int target_weight, float font_size_in_pt, bool inclusive)
1708
0
{
1709
0
    using Fn = AK::Function<bool(MatchingFontCandidate const&)>;
1710
0
    auto pred = inclusive ? Fn([&](auto const& matching_font_candidate) { return matching_font_candidate.key.weight >= target_weight; })
1711
0
                          : Fn([&](auto const& matching_font_candidate) { return matching_font_candidate.key.weight > target_weight; });
1712
0
    auto it = find_if(candidates.begin(), candidates.end(), pred);
1713
0
    for (; it != candidates.end(); ++it) {
1714
0
        if (auto found_font = it->font_with_point_size(font_size_in_pt))
1715
0
            return found_font;
1716
0
    }
1717
0
    return {};
1718
0
}
1719
1720
RefPtr<Gfx::FontCascadeList const> StyleComputer::find_matching_font_weight_descending(Vector<MatchingFontCandidate> const& candidates, int target_weight, float font_size_in_pt, bool inclusive)
1721
0
{
1722
0
    using Fn = AK::Function<bool(MatchingFontCandidate const&)>;
1723
0
    auto pred = inclusive ? Fn([&](auto const& matching_font_candidate) { return matching_font_candidate.key.weight <= target_weight; })
1724
0
                          : Fn([&](auto const& matching_font_candidate) { return matching_font_candidate.key.weight < target_weight; });
1725
0
    auto it = find_if(candidates.rbegin(), candidates.rend(), pred);
1726
0
    for (; it != candidates.rend(); ++it) {
1727
0
        if (auto found_font = it->font_with_point_size(font_size_in_pt))
1728
0
            return found_font;
1729
0
    }
1730
0
    return {};
1731
0
}
1732
1733
// Partial implementation of the font-matching algorithm: https://www.w3.org/TR/css-fonts-4/#font-matching-algorithm
1734
// FIXME: This should be replaced by the full CSS font selection algorithm.
1735
RefPtr<Gfx::FontCascadeList const> StyleComputer::font_matching_algorithm(FontFaceKey const& key, float font_size_in_pt) const
1736
0
{
1737
    // If a font family match occurs, the user agent assembles the set of font faces in that family and then
1738
    // narrows the set to a single face using other font properties in the order given below.
1739
0
    Vector<MatchingFontCandidate> matching_family_fonts;
1740
0
    for (auto const& font_key_and_loader : m_loaded_fonts) {
1741
0
        if (font_key_and_loader.key.family_name.equals_ignoring_ascii_case(key.family_name))
1742
0
            matching_family_fonts.empend(font_key_and_loader.key, const_cast<FontLoaderList*>(&font_key_and_loader.value));
1743
0
    }
1744
0
    Gfx::FontDatabase::the().for_each_typeface_with_family_name(key.family_name, [&](Gfx::Typeface const& typeface) {
1745
0
        matching_family_fonts.empend(
1746
0
            FontFaceKey {
1747
0
                .family_name = typeface.family(),
1748
0
                .weight = static_cast<int>(typeface.weight()),
1749
0
                .slope = typeface.slope(),
1750
0
            },
1751
0
            &typeface);
1752
0
    });
1753
0
    quick_sort(matching_family_fonts, [](auto const& a, auto const& b) {
1754
0
        return a.key.weight < b.key.weight;
1755
0
    });
1756
    // FIXME: 1. font-stretch is tried first.
1757
    // FIXME: 2. font-style is tried next.
1758
    // We don't have complete support of italic and oblique fonts, so matching on font-style can be simplified to:
1759
    // If a matching slope is found, all faces which don't have that matching slope are excluded from the matching set.
1760
0
    auto style_it = find_if(matching_family_fonts.begin(), matching_family_fonts.end(),
1761
0
        [&](auto const& matching_font_candidate) { return matching_font_candidate.key.slope == key.slope; });
1762
0
    if (style_it != matching_family_fonts.end()) {
1763
0
        matching_family_fonts.remove_all_matching([&](auto const& matching_font_candidate) {
1764
0
            return matching_font_candidate.key.slope != key.slope;
1765
0
        });
1766
0
    }
1767
    // 3. font-weight is matched next.
1768
    // If the desired weight is inclusively between 400 and 500, weights greater than or equal to the target weight
1769
    // are checked in ascending order until 500 is hit and checked, followed by weights less than the target weight
1770
    // in descending order, followed by weights greater than 500, until a match is found.
1771
0
    if (key.weight >= 400 && key.weight <= 500) {
1772
0
        auto it = find_if(matching_family_fonts.begin(), matching_family_fonts.end(),
1773
0
            [&](auto const& matching_font_candidate) { return matching_font_candidate.key.weight >= key.weight; });
1774
0
        for (; it != matching_family_fonts.end() && it->key.weight <= 500; ++it) {
1775
0
            if (auto found_font = it->font_with_point_size(font_size_in_pt))
1776
0
                return found_font;
1777
0
        }
1778
0
        if (auto found_font = find_matching_font_weight_descending(matching_family_fonts, key.weight, font_size_in_pt, false))
1779
0
            return found_font;
1780
0
        for (; it != matching_family_fonts.end(); ++it) {
1781
0
            if (auto found_font = it->font_with_point_size(font_size_in_pt))
1782
0
                return found_font;
1783
0
        }
1784
0
    }
1785
    // If the desired weight is less than 400, weights less than or equal to the desired weight are checked in descending order
1786
    // followed by weights above the desired weight in ascending order until a match is found.
1787
0
    if (key.weight < 400) {
1788
0
        if (auto found_font = find_matching_font_weight_descending(matching_family_fonts, key.weight, font_size_in_pt, true))
1789
0
            return found_font;
1790
0
        if (auto found_font = find_matching_font_weight_ascending(matching_family_fonts, key.weight, font_size_in_pt, false))
1791
0
            return found_font;
1792
0
    }
1793
    // If the desired weight is greater than 500, weights greater than or equal to the desired weight are checked in ascending order
1794
    // followed by weights below the desired weight in descending order until a match is found.
1795
0
    if (key.weight > 500) {
1796
0
        if (auto found_font = find_matching_font_weight_ascending(matching_family_fonts, key.weight, font_size_in_pt, true))
1797
0
            return found_font;
1798
0
        if (auto found_font = find_matching_font_weight_descending(matching_family_fonts, key.weight, font_size_in_pt, false))
1799
0
            return found_font;
1800
0
    }
1801
0
    return {};
1802
0
}
1803
1804
RefPtr<Gfx::FontCascadeList const> StyleComputer::compute_font_for_style_values(DOM::Element const* element, Optional<CSS::Selector::PseudoElement::Type> pseudo_element, CSSStyleValue const& font_family, CSSStyleValue const& font_size, CSSStyleValue const& font_style, CSSStyleValue const& font_weight, CSSStyleValue const& font_stretch, int math_depth) const
1805
0
{
1806
0
    auto* parent_element = element_to_inherit_style_from(element, pseudo_element);
1807
1808
0
    auto width = font_stretch.to_font_width();
1809
1810
0
    auto weight = font_weight.to_font_weight();
1811
0
    bool bold = weight > Gfx::FontWeight::Regular;
1812
1813
    // FIXME: Should be based on "user's default font size"
1814
0
    CSSPixels font_size_in_px = 16;
1815
1816
0
    Gfx::FontPixelMetrics font_pixel_metrics;
1817
0
    if (parent_element && parent_element->computed_css_values())
1818
0
        font_pixel_metrics = parent_element->computed_css_values()->first_available_computed_font().pixel_metrics();
1819
0
    else
1820
0
        font_pixel_metrics = Platform::FontPlugin::the().default_font().pixel_metrics();
1821
0
    auto parent_font_size = [&]() -> CSSPixels {
1822
0
        if (!parent_element || !parent_element->computed_css_values())
1823
0
            return font_size_in_px;
1824
0
        auto value = parent_element->computed_css_values()->property(CSS::PropertyID::FontSize);
1825
0
        if (value->is_length()) {
1826
0
            auto length = value->as_length().length();
1827
0
            if (length.is_absolute() || length.is_relative()) {
1828
0
                Length::FontMetrics font_metrics { font_size_in_px, font_pixel_metrics };
1829
0
                return length.to_px(viewport_rect(), font_metrics, m_root_element_font_metrics);
1830
0
            }
1831
0
        }
1832
0
        return font_size_in_px;
1833
0
    };
1834
0
    Length::FontMetrics font_metrics { parent_font_size(), font_pixel_metrics };
1835
1836
0
    if (font_size.is_keyword()) {
1837
        // https://w3c.github.io/csswg-drafts/css-fonts/#absolute-size-mapping
1838
0
        auto get_absolute_size_mapping = [](Keyword keyword) -> CSSPixelFraction {
1839
0
            switch (keyword) {
1840
0
            case Keyword::XxSmall:
1841
0
                return CSSPixels(3) / 5;
1842
0
            case Keyword::XSmall:
1843
0
                return CSSPixels(3) / 4;
1844
0
            case Keyword::Small:
1845
0
                return CSSPixels(8) / 9;
1846
0
            case Keyword::Medium:
1847
0
                return 1;
1848
0
            case Keyword::Large:
1849
0
                return CSSPixels(6) / 5;
1850
0
            case Keyword::XLarge:
1851
0
                return CSSPixels(3) / 2;
1852
0
            case Keyword::XxLarge:
1853
0
                return 2;
1854
0
            case Keyword::XxxLarge:
1855
0
                return 3;
1856
0
            case Keyword::Smaller:
1857
0
                return CSSPixels(4) / 5;
1858
0
            case Keyword::Larger:
1859
0
                return CSSPixels(5) / 4;
1860
0
            default:
1861
0
                return 1;
1862
0
            }
1863
0
        };
1864
1865
0
        auto const keyword = font_size.to_keyword();
1866
1867
0
        if (keyword == Keyword::Math) {
1868
0
            auto math_scaling_factor = [&]() {
1869
                // https://w3c.github.io/mathml-core/#the-math-script-level-property
1870
                // If the specified value font-size is math then the computed value of font-size is obtained by multiplying
1871
                // the inherited value of font-size by a nonzero scale factor calculated by the following procedure:
1872
                // 1. Let A be the inherited math-depth value, B the computed math-depth value, C be 0.71 and S be 1.0
1873
0
                int inherited_math_depth = parent_element && parent_element->computed_css_values()
1874
0
                    ? parent_element->computed_css_values()->math_depth()
1875
0
                    : InitialValues::math_depth();
1876
0
                int computed_math_depth = math_depth;
1877
0
                auto size_ratio = 0.71;
1878
0
                auto scale = 1.0;
1879
                // 2. If A = B then return S.
1880
0
                bool invert_scale_factor = false;
1881
0
                if (inherited_math_depth == computed_math_depth) {
1882
0
                    return scale;
1883
0
                }
1884
                //    If B < A, swap A and B and set InvertScaleFactor to true.
1885
0
                else if (computed_math_depth < inherited_math_depth) {
1886
0
                    AK::swap(inherited_math_depth, computed_math_depth);
1887
0
                    invert_scale_factor = true;
1888
0
                }
1889
                //    Otherwise B > A and set InvertScaleFactor to false.
1890
0
                else {
1891
0
                    invert_scale_factor = false;
1892
0
                }
1893
                // 3. Let E be B - A > 0.
1894
0
                double e = (computed_math_depth - inherited_math_depth) > 0;
1895
                // FIXME: 4. If the inherited first available font has an OpenType MATH table:
1896
                //    - If A ≤ 0 and B ≥ 2 then multiply S by scriptScriptPercentScaleDown and decrement E by 2.
1897
                //    - Otherwise if A = 1 then multiply S by scriptScriptPercentScaleDown / scriptPercentScaleDown and decrement E by 1.
1898
                //    - Otherwise if B = 1 then multiply S by scriptPercentScaleDown and decrement E by 1.
1899
                // 5. Multiply S by C^E.
1900
0
                scale *= AK::pow(size_ratio, e);
1901
                // 6. Return S if InvertScaleFactor is false and 1/S otherwise.
1902
0
                if (!invert_scale_factor)
1903
0
                    return scale;
1904
0
                return 1.0 / scale;
1905
0
            };
1906
0
            font_size_in_px = parent_font_size().scale_by(math_scaling_factor());
1907
0
        } else {
1908
            // https://w3c.github.io/csswg-drafts/css-fonts/#valdef-font-size-relative-size
1909
            // TODO: If the parent element has a keyword font size in the absolute size keyword mapping table,
1910
            //       larger may compute the font size to the next entry in the table,
1911
            //       and smaller may compute the font size to the previous entry in the table.
1912
0
            if (keyword == Keyword::Smaller || keyword == Keyword::Larger) {
1913
0
                if (parent_element && parent_element->computed_css_values()) {
1914
0
                    font_size_in_px = CSSPixels::nearest_value_for(parent_element->computed_css_values()->first_available_computed_font().pixel_metrics().size);
1915
0
                }
1916
0
            }
1917
0
            font_size_in_px *= get_absolute_size_mapping(keyword);
1918
0
        }
1919
0
    } else {
1920
0
        Length::ResolutionContext const length_resolution_context {
1921
0
            .viewport_rect = viewport_rect(),
1922
0
            .font_metrics = font_metrics,
1923
0
            .root_font_metrics = m_root_element_font_metrics,
1924
0
        };
1925
1926
0
        Optional<Length> maybe_length;
1927
0
        if (font_size.is_percentage()) {
1928
            // Percentages refer to parent element's font size
1929
0
            maybe_length = Length::make_px(CSSPixels::nearest_value_for(font_size.as_percentage().percentage().as_fraction() * parent_font_size().to_double()));
1930
1931
0
        } else if (font_size.is_length()) {
1932
0
            maybe_length = font_size.as_length().length();
1933
0
        } else if (font_size.is_math()) {
1934
0
            if (font_size.as_math().contains_percentage()) {
1935
0
                maybe_length = font_size.as_math().resolve_length_percentage(length_resolution_context, Length::make_px(parent_font_size()));
1936
0
            } else {
1937
0
                maybe_length = font_size.as_math().resolve_length(length_resolution_context);
1938
0
            }
1939
0
        }
1940
0
        if (maybe_length.has_value()) {
1941
0
            font_size_in_px = maybe_length.value().to_px(length_resolution_context);
1942
0
        }
1943
0
    }
1944
1945
0
    auto slope = font_style.to_font_slope();
1946
1947
    // FIXME: Implement the full font-matching algorithm: https://www.w3.org/TR/css-fonts-4/#font-matching-algorithm
1948
1949
    // Note: This is modified by the find_font() lambda
1950
0
    bool monospace = false;
1951
1952
0
    float const font_size_in_pt = font_size_in_px * 0.75f;
1953
1954
0
    auto find_font = [&](FlyString const& family) -> RefPtr<Gfx::FontCascadeList const> {
1955
0
        FontFaceKey key {
1956
0
            .family_name = family,
1957
0
            .weight = weight,
1958
0
            .slope = slope,
1959
0
        };
1960
1961
0
        auto result = Gfx::FontCascadeList::create();
1962
0
        if (auto it = m_loaded_fonts.find(key); it != m_loaded_fonts.end()) {
1963
0
            auto const& loaders = it->value;
1964
0
            for (auto const& loader : loaders) {
1965
0
                if (auto found_font = loader->font_with_point_size(font_size_in_pt))
1966
0
                    result->add(*found_font, loader->unicode_ranges());
1967
0
            }
1968
0
            return result;
1969
0
        }
1970
1971
0
        if (auto found_font = font_matching_algorithm(key, font_size_in_pt); found_font && !found_font->is_empty()) {
1972
0
            return found_font;
1973
0
        }
1974
1975
0
        if (auto found_font = Gfx::FontDatabase::the().get(family, font_size_in_pt, weight, width, slope, Gfx::Font::AllowInexactSizeMatch::Yes)) {
1976
0
            result->add(*found_font);
1977
0
            return result;
1978
0
        }
1979
1980
0
        return {};
1981
0
    };
1982
1983
0
    auto find_generic_font = [&](Keyword font_id) -> RefPtr<Gfx::FontCascadeList const> {
1984
0
        Platform::GenericFont generic_font {};
1985
0
        switch (font_id) {
1986
0
        case Keyword::Monospace:
1987
0
        case Keyword::UiMonospace:
1988
0
            generic_font = Platform::GenericFont::Monospace;
1989
0
            monospace = true;
1990
0
            break;
1991
0
        case Keyword::Serif:
1992
0
            generic_font = Platform::GenericFont::Serif;
1993
0
            break;
1994
0
        case Keyword::Fantasy:
1995
0
            generic_font = Platform::GenericFont::Fantasy;
1996
0
            break;
1997
0
        case Keyword::SansSerif:
1998
0
            generic_font = Platform::GenericFont::SansSerif;
1999
0
            break;
2000
0
        case Keyword::Cursive:
2001
0
            generic_font = Platform::GenericFont::Cursive;
2002
0
            break;
2003
0
        case Keyword::UiSerif:
2004
0
            generic_font = Platform::GenericFont::UiSerif;
2005
0
            break;
2006
0
        case Keyword::UiSansSerif:
2007
0
            generic_font = Platform::GenericFont::UiSansSerif;
2008
0
            break;
2009
0
        case Keyword::UiRounded:
2010
0
            generic_font = Platform::GenericFont::UiRounded;
2011
0
            break;
2012
0
        default:
2013
0
            return {};
2014
0
        }
2015
0
        return find_font(Platform::FontPlugin::the().generic_font_name(generic_font));
2016
0
    };
2017
2018
0
    auto font_list = Gfx::FontCascadeList::create();
2019
0
    if (font_family.is_value_list()) {
2020
0
        auto const& family_list = static_cast<StyleValueList const&>(font_family).values();
2021
0
        for (auto const& family : family_list) {
2022
0
            RefPtr<Gfx::FontCascadeList const> other_font_list;
2023
0
            if (family->is_keyword()) {
2024
0
                other_font_list = find_generic_font(family->to_keyword());
2025
0
            } else if (family->is_string()) {
2026
0
                other_font_list = find_font(family->as_string().string_value());
2027
0
            } else if (family->is_custom_ident()) {
2028
0
                other_font_list = find_font(family->as_custom_ident().custom_ident());
2029
0
            }
2030
0
            if (other_font_list)
2031
0
                font_list->extend(*other_font_list);
2032
0
        }
2033
0
    } else if (font_family.is_keyword()) {
2034
0
        if (auto other_font_list = find_generic_font(font_family.to_keyword()))
2035
0
            font_list->extend(*other_font_list);
2036
0
    } else if (font_family.is_string()) {
2037
0
        if (auto other_font_list = find_font(font_family.as_string().string_value()))
2038
0
            font_list->extend(*other_font_list);
2039
0
    } else if (font_family.is_custom_ident()) {
2040
0
        if (auto other_font_list = find_font(font_family.as_custom_ident().custom_ident()))
2041
0
            font_list->extend(*other_font_list);
2042
0
    }
2043
2044
0
    auto found_font = StyleProperties::font_fallback(monospace, bold);
2045
0
    font_list->add(found_font->with_size(font_size_in_pt));
2046
2047
0
    return font_list;
2048
0
}
2049
2050
void StyleComputer::compute_font(StyleProperties& style, DOM::Element const* element, Optional<CSS::Selector::PseudoElement::Type> pseudo_element) const
2051
0
{
2052
    // To compute the font, first ensure that we've defaulted the relevant CSS font properties.
2053
    // FIXME: This should be more sophisticated.
2054
0
    compute_defaulted_property_value(style, element, CSS::PropertyID::FontFamily, pseudo_element);
2055
0
    compute_defaulted_property_value(style, element, CSS::PropertyID::FontSize, pseudo_element);
2056
0
    compute_defaulted_property_value(style, element, CSS::PropertyID::FontWidth, pseudo_element);
2057
0
    compute_defaulted_property_value(style, element, CSS::PropertyID::FontStyle, pseudo_element);
2058
0
    compute_defaulted_property_value(style, element, CSS::PropertyID::FontWeight, pseudo_element);
2059
0
    compute_defaulted_property_value(style, element, CSS::PropertyID::LineHeight, pseudo_element);
2060
2061
0
    auto font_family = style.property(CSS::PropertyID::FontFamily);
2062
0
    auto font_size = style.property(CSS::PropertyID::FontSize);
2063
0
    auto font_style = style.property(CSS::PropertyID::FontStyle);
2064
0
    auto font_weight = style.property(CSS::PropertyID::FontWeight);
2065
0
    auto font_width = style.property(CSS::PropertyID::FontWidth);
2066
2067
0
    auto font_list = compute_font_for_style_values(element, pseudo_element, font_family, font_size, font_style, font_weight, font_width, style.math_depth());
2068
0
    VERIFY(font_list);
2069
0
    VERIFY(!font_list->is_empty());
2070
2071
0
    RefPtr<Gfx::Font const> const found_font = font_list->first();
2072
2073
0
    style.set_property(CSS::PropertyID::FontSize, LengthStyleValue::create(CSS::Length::make_px(CSSPixels::nearest_value_for(found_font->pixel_size()))));
2074
0
    style.set_property(CSS::PropertyID::FontWeight, NumberStyleValue::create(font_weight->to_font_weight()));
2075
2076
0
    style.set_computed_font_list(*font_list);
2077
2078
0
    if (element && is<HTML::HTMLHtmlElement>(*element)) {
2079
0
        const_cast<StyleComputer&>(*this).m_root_element_font_metrics = calculate_root_element_font_metrics(style);
2080
0
    }
2081
0
}
2082
2083
Gfx::Font const& StyleComputer::initial_font() const
2084
0
{
2085
    // FIXME: This is not correct.
2086
0
    return StyleProperties::font_fallback(false, false);
2087
0
}
2088
2089
void StyleComputer::absolutize_values(StyleProperties& style) const
2090
0
{
2091
0
    Length::FontMetrics font_metrics {
2092
0
        m_root_element_font_metrics.font_size,
2093
0
        style.first_available_computed_font().pixel_metrics()
2094
0
    };
2095
2096
0
    auto font_size = style.property(CSS::PropertyID::FontSize)->as_length().length().to_px(viewport_rect(), font_metrics, m_root_element_font_metrics);
2097
0
    font_metrics.font_size = font_size;
2098
2099
    // NOTE: Percentage line-height values are relative to the font-size of the element.
2100
    //       We have to resolve them right away, so that the *computed* line-height is ready for inheritance.
2101
    //       We can't simply absolutize *all* percentage values against the font size,
2102
    //       because most percentages are relative to containing block metrics.
2103
0
    auto& line_height_value_slot = style.m_data->m_property_values[to_underlying(CSS::PropertyID::LineHeight)];
2104
0
    if (line_height_value_slot && line_height_value_slot->is_percentage()) {
2105
0
        line_height_value_slot = LengthStyleValue::create(
2106
0
            Length::make_px(CSSPixels::nearest_value_for(font_size * static_cast<double>(line_height_value_slot->as_percentage().percentage().as_fraction()))));
2107
0
    }
2108
2109
0
    auto line_height = style.compute_line_height(viewport_rect(), font_metrics, m_root_element_font_metrics);
2110
0
    font_metrics.line_height = line_height;
2111
2112
    // NOTE: line-height might be using lh which should be resolved against the parent line height (like we did here already)
2113
0
    if (line_height_value_slot && line_height_value_slot->is_length())
2114
0
        line_height_value_slot = LengthStyleValue::create(Length::make_px(line_height));
2115
2116
0
    for (size_t i = 0; i < style.m_data->m_property_values.size(); ++i) {
2117
0
        auto& value_slot = style.m_data->m_property_values[i];
2118
0
        if (!value_slot)
2119
0
            continue;
2120
0
        value_slot = value_slot->absolutized(viewport_rect(), font_metrics, m_root_element_font_metrics);
2121
0
    }
2122
2123
0
    style.set_line_height({}, line_height);
2124
0
}
2125
2126
void StyleComputer::resolve_effective_overflow_values(StyleProperties& style) const
2127
0
{
2128
    // https://www.w3.org/TR/css-overflow-3/#overflow-control
2129
    // The visible/clip values of overflow compute to auto/hidden (respectively) if one of overflow-x or
2130
    // overflow-y is neither visible nor clip.
2131
0
    auto overflow_x = keyword_to_overflow(style.property(PropertyID::OverflowX)->to_keyword());
2132
0
    auto overflow_y = keyword_to_overflow(style.property(PropertyID::OverflowY)->to_keyword());
2133
0
    auto overflow_x_is_visible_or_clip = overflow_x == Overflow::Visible || overflow_x == Overflow::Clip;
2134
0
    auto overflow_y_is_visible_or_clip = overflow_y == Overflow::Visible || overflow_y == Overflow::Clip;
2135
0
    if (!overflow_x_is_visible_or_clip || !overflow_y_is_visible_or_clip) {
2136
0
        if (overflow_x == CSS::Overflow::Visible)
2137
0
            style.set_property(CSS::PropertyID::OverflowX, CSSKeywordValue::create(Keyword::Auto));
2138
0
        if (overflow_x == CSS::Overflow::Clip)
2139
0
            style.set_property(CSS::PropertyID::OverflowX, CSSKeywordValue::create(Keyword::Hidden));
2140
0
        if (overflow_y == CSS::Overflow::Visible)
2141
0
            style.set_property(CSS::PropertyID::OverflowY, CSSKeywordValue::create(Keyword::Auto));
2142
0
        if (overflow_y == CSS::Overflow::Clip)
2143
0
            style.set_property(CSS::PropertyID::OverflowY, CSSKeywordValue::create(Keyword::Hidden));
2144
0
    }
2145
0
}
2146
2147
enum class BoxTypeTransformation {
2148
    None,
2149
    Blockify,
2150
    Inlinify,
2151
};
2152
2153
static BoxTypeTransformation required_box_type_transformation(StyleProperties const& style, DOM::Element const& element, Optional<CSS::Selector::PseudoElement::Type> const& pseudo_element)
2154
0
{
2155
    // NOTE: We never blockify <br> elements. They are always inline.
2156
    //       There is currently no way to express in CSS how a <br> element really behaves.
2157
    //       Spec issue: https://github.com/whatwg/html/issues/2291
2158
0
    if (is<HTML::HTMLBRElement>(element))
2159
0
        return BoxTypeTransformation::None;
2160
2161
    // Absolute positioning or floating an element blockifies the box’s display type. [CSS2]
2162
0
    if (style.position() == CSS::Positioning::Absolute || style.position() == CSS::Positioning::Fixed || style.float_() != CSS::Float::None)
2163
0
        return BoxTypeTransformation::Blockify;
2164
2165
    // FIXME: Containment in a ruby container inlinifies the box’s display type, as described in [CSS-RUBY-1].
2166
2167
    // NOTE: If we're computing style for a pseudo-element, the effective parent will be the originating element itself, not its parent.
2168
0
    auto const* parent = pseudo_element.has_value() ? &element : element.parent_element();
2169
2170
    // A parent with a grid or flex display value blockifies the box’s display type. [CSS-GRID-1] [CSS-FLEXBOX-1]
2171
0
    if (parent && parent->computed_css_values()) {
2172
0
        auto const& parent_display = parent->computed_css_values()->display();
2173
0
        if (parent_display.is_grid_inside() || parent_display.is_flex_inside())
2174
0
            return BoxTypeTransformation::Blockify;
2175
0
    }
2176
2177
0
    return BoxTypeTransformation::None;
2178
0
}
2179
2180
// https://drafts.csswg.org/css-display/#transformations
2181
void StyleComputer::transform_box_type_if_needed(StyleProperties& style, DOM::Element const& element, Optional<CSS::Selector::PseudoElement::Type> pseudo_element) const
2182
0
{
2183
    // 2.7. Automatic Box Type Transformations
2184
2185
    // Some layout effects require blockification or inlinification of the box type,
2186
    // which sets the box’s computed outer display type to block or inline (respectively).
2187
    // (This has no effect on display types that generate no box at all, such as none or contents.)
2188
2189
0
    auto display = style.display();
2190
2191
0
    if (display.is_none() || (display.is_contents() && !element.is_document_element()))
2192
0
        return;
2193
2194
    // https://drafts.csswg.org/css-display/#root
2195
    // The root element’s display type is always blockified, and its principal box always establishes an independent formatting context.
2196
0
    if (element.is_document_element() && !display.is_block_outside()) {
2197
0
        style.set_property(CSS::PropertyID::Display, DisplayStyleValue::create(Display::from_short(CSS::Display::Short::Block)));
2198
0
        return;
2199
0
    }
2200
2201
0
    auto new_display = display;
2202
2203
0
    if (display.is_math_inside()) {
2204
        // https://w3c.github.io/mathml-core/#new-display-math-value
2205
        // For elements that are not MathML elements, if the specified value of display is inline math or block math
2206
        // then the computed value is block flow and inline flow respectively.
2207
0
        if (element.namespace_uri() != Namespace::MathML)
2208
0
            new_display = CSS::Display { display.outside(), CSS::DisplayInside::Flow };
2209
        // For the mtable element the computed value is block table and inline table respectively.
2210
0
        else if (element.tag_name().equals_ignoring_ascii_case("mtable"sv))
2211
0
            new_display = CSS::Display { display.outside(), CSS::DisplayInside::Table };
2212
        // For the mtr element, the computed value is table-row.
2213
0
        else if (element.tag_name().equals_ignoring_ascii_case("mtr"sv))
2214
0
            new_display = CSS::Display { CSS::DisplayInternal::TableRow };
2215
        // For the mtd element, the computed value is table-cell.
2216
0
        else if (element.tag_name().equals_ignoring_ascii_case("mtd"sv))
2217
0
            new_display = CSS::Display { CSS::DisplayInternal::TableCell };
2218
0
    }
2219
2220
0
    switch (required_box_type_transformation(style, element, pseudo_element)) {
2221
0
    case BoxTypeTransformation::None:
2222
0
        break;
2223
0
    case BoxTypeTransformation::Blockify:
2224
0
        if (display.is_block_outside())
2225
0
            return;
2226
        // If a layout-internal box is blockified, its inner display type converts to flow so that it becomes a block container.
2227
0
        if (display.is_internal()) {
2228
0
            new_display = CSS::Display::from_short(CSS::Display::Short::Block);
2229
0
        } else {
2230
0
            VERIFY(display.is_outside_and_inside());
2231
2232
            // For legacy reasons, if an inline block box (inline flow-root) is blockified, it becomes a block box (losing its flow-root nature).
2233
            // For consistency, a run-in flow-root box also blockifies to a block box.
2234
0
            if (display.is_inline_block()) {
2235
0
                new_display = CSS::Display { CSS::DisplayOutside::Block, CSS::DisplayInside::Flow, display.list_item() };
2236
0
            } else {
2237
0
                new_display = CSS::Display { CSS::DisplayOutside::Block, display.inside(), display.list_item() };
2238
0
            }
2239
0
        }
2240
0
        break;
2241
0
    case BoxTypeTransformation::Inlinify:
2242
0
        if (display.is_inline_outside()) {
2243
            // FIXME: If an inline box (inline flow) is inlinified, it recursively inlinifies all of its in-flow children,
2244
            //        so that no block-level descendants break up the inline formatting context in which it participates.
2245
0
            if (display.is_flow_inside()) {
2246
0
                dbgln("FIXME: Inlinify inline box children recursively");
2247
0
            }
2248
0
            break;
2249
0
        }
2250
0
        if (display.is_internal()) {
2251
            // Inlinification has no effect on layout-internal boxes. (However, placement in such an inline context will typically cause them
2252
            // to be wrapped in an appropriately-typed anonymous inline-level box.)
2253
0
        } else {
2254
0
            VERIFY(display.is_outside_and_inside());
2255
2256
            // If a block box (block flow) is inlinified, its inner display type is set to flow-root so that it remains a block container.
2257
0
            if (display.is_block_outside() && display.is_flow_inside()) {
2258
0
                new_display = CSS::Display { CSS::DisplayOutside::Inline, CSS::DisplayInside::FlowRoot, display.list_item() };
2259
0
            }
2260
2261
0
            new_display = CSS::Display { CSS::DisplayOutside::Inline, display.inside(), display.list_item() };
2262
0
        }
2263
0
        break;
2264
0
    }
2265
2266
0
    if (new_display != display)
2267
0
        style.set_property(CSS::PropertyID::Display, DisplayStyleValue::create(new_display));
2268
0
}
2269
2270
NonnullRefPtr<StyleProperties> StyleComputer::create_document_style() const
2271
0
{
2272
0
    auto style = StyleProperties::create();
2273
0
    compute_math_depth(style, nullptr, {});
2274
0
    compute_font(style, nullptr, {});
2275
0
    compute_defaulted_values(style, nullptr, {});
2276
0
    absolutize_values(style);
2277
0
    style->set_property(CSS::PropertyID::Width, CSS::LengthStyleValue::create(CSS::Length::make_px(viewport_rect().width())));
2278
0
    style->set_property(CSS::PropertyID::Height, CSS::LengthStyleValue::create(CSS::Length::make_px(viewport_rect().height())));
2279
0
    style->set_property(CSS::PropertyID::Display, CSS::DisplayStyleValue::create(CSS::Display::from_short(CSS::Display::Short::Block)));
2280
0
    return style;
2281
0
}
2282
2283
NonnullRefPtr<StyleProperties> StyleComputer::compute_style(DOM::Element& element, Optional<CSS::Selector::PseudoElement::Type> pseudo_element) const
2284
0
{
2285
0
    return compute_style_impl(element, move(pseudo_element), ComputeStyleMode::Normal).release_nonnull();
2286
0
}
2287
2288
RefPtr<StyleProperties> StyleComputer::compute_pseudo_element_style_if_needed(DOM::Element& element, Optional<CSS::Selector::PseudoElement::Type> pseudo_element) const
2289
0
{
2290
0
    return compute_style_impl(element, move(pseudo_element), ComputeStyleMode::CreatePseudoElementStyleIfNeeded);
2291
0
}
2292
2293
RefPtr<StyleProperties> StyleComputer::compute_style_impl(DOM::Element& element, Optional<CSS::Selector::PseudoElement::Type> pseudo_element, ComputeStyleMode mode) const
2294
0
{
2295
0
    build_rule_cache_if_needed();
2296
2297
    // Special path for elements that use pseudo element as style selector
2298
0
    if (element.use_pseudo_element().has_value()) {
2299
0
        auto& parent_element = verify_cast<HTML::HTMLElement>(*element.root().parent_or_shadow_host());
2300
0
        auto style = compute_style(parent_element, *element.use_pseudo_element());
2301
2302
        // Merge back inline styles
2303
0
        if (auto inline_style = element.inline_style()) {
2304
0
            for (auto const& property : inline_style->properties())
2305
0
                style->set_property(property.property_id, property.value);
2306
0
        }
2307
0
        return style;
2308
0
    }
2309
2310
0
    ScopeGuard guard { [&element]() { element.set_needs_style_update(false); } };
2311
2312
0
    auto style = StyleProperties::create();
2313
    // 1. Perform the cascade. This produces the "specified style"
2314
0
    bool did_match_any_pseudo_element_rules = false;
2315
0
    compute_cascaded_values(style, element, pseudo_element, did_match_any_pseudo_element_rules, mode);
2316
2317
0
    if (mode == ComputeStyleMode::CreatePseudoElementStyleIfNeeded) {
2318
        // NOTE: If we're computing style for a pseudo-element, we look for a number of reasons to bail early.
2319
2320
        // Bail if no pseudo-element rules matched.
2321
0
        if (!did_match_any_pseudo_element_rules)
2322
0
            return nullptr;
2323
2324
        // Bail if no pseudo-element would be generated due to...
2325
        // - content: none
2326
        // - content: normal (for ::before and ::after)
2327
0
        bool content_is_normal = false;
2328
0
        if (auto content_value = style->maybe_null_property(CSS::PropertyID::Content)) {
2329
0
            if (content_value->is_keyword()) {
2330
0
                auto content = content_value->as_keyword().keyword();
2331
0
                if (content == CSS::Keyword::None)
2332
0
                    return nullptr;
2333
0
                content_is_normal = content == CSS::Keyword::Normal;
2334
0
            } else {
2335
0
                content_is_normal = false;
2336
0
            }
2337
0
        } else {
2338
            // NOTE: `normal` is the initial value, so the absence of a value is treated as `normal`.
2339
0
            content_is_normal = true;
2340
0
        }
2341
0
        if (content_is_normal && first_is_one_of(*pseudo_element, CSS::Selector::PseudoElement::Type::Before, CSS::Selector::PseudoElement::Type::After)) {
2342
0
            return nullptr;
2343
0
        }
2344
0
    }
2345
2346
    // 2. Compute the math-depth property, since that might affect the font-size
2347
0
    compute_math_depth(style, &element, pseudo_element);
2348
2349
    // 3. Compute the font, since that may be needed for font-relative CSS units
2350
0
    compute_font(style, &element, pseudo_element);
2351
2352
    // 4. Absolutize values, turning font/viewport relative lengths into absolute lengths
2353
0
    absolutize_values(style);
2354
2355
    // 5. Default the values, applying inheritance and 'initial' as needed
2356
0
    compute_defaulted_values(style, &element, pseudo_element);
2357
2358
    // 6. Run automatic box type transformations
2359
0
    transform_box_type_if_needed(style, element, pseudo_element);
2360
2361
    // 7. Resolve effective overflow values
2362
0
    resolve_effective_overflow_values(style);
2363
2364
    // 8. Let the element adjust computed style
2365
0
    element.adjust_computed_style(style);
2366
2367
    // 9. Transition declarations [css-transitions-1]
2368
    // Theoretically this should be part of the cascade, but it works with computed values, which we don't have until now.
2369
0
    compute_transitioned_properties(style, element, pseudo_element);
2370
0
    if (auto const* previous_style = element.computed_css_values()) {
2371
0
        start_needed_transitions(*previous_style, style, element, pseudo_element);
2372
0
    }
2373
2374
0
    return style;
2375
0
}
2376
2377
void StyleComputer::build_rule_cache_if_needed() const
2378
0
{
2379
0
    if (m_author_rule_cache && m_user_rule_cache && m_user_agent_rule_cache)
2380
0
        return;
2381
0
    const_cast<StyleComputer&>(*this).build_rule_cache();
2382
0
}
2383
2384
struct SimplifiedSelectorForBucketing {
2385
    CSS::Selector::SimpleSelector::Type type;
2386
    FlyString name;
2387
};
2388
2389
static Optional<SimplifiedSelectorForBucketing> is_roundabout_selector_bucketable_as_something_simpler(CSS::Selector::SimpleSelector const& simple_selector)
2390
0
{
2391
0
    if (simple_selector.type != CSS::Selector::SimpleSelector::Type::PseudoClass)
2392
0
        return {};
2393
2394
0
    if (simple_selector.pseudo_class().type != CSS::PseudoClass::Is
2395
0
        && simple_selector.pseudo_class().type != CSS::PseudoClass::Where)
2396
0
        return {};
2397
2398
0
    if (simple_selector.pseudo_class().argument_selector_list.size() != 1)
2399
0
        return {};
2400
2401
0
    auto const& argument_selector = *simple_selector.pseudo_class().argument_selector_list.first();
2402
2403
0
    auto const& compound_selector = argument_selector.compound_selectors().last();
2404
0
    if (compound_selector.simple_selectors.size() != 1)
2405
0
        return {};
2406
2407
0
    auto const& inner_simple_selector = compound_selector.simple_selectors.first();
2408
0
    if (inner_simple_selector.type == CSS::Selector::SimpleSelector::Type::Class
2409
0
        || inner_simple_selector.type == CSS::Selector::SimpleSelector::Type::Id) {
2410
0
        return SimplifiedSelectorForBucketing { inner_simple_selector.type, inner_simple_selector.name() };
2411
0
    }
2412
2413
0
    if (inner_simple_selector.type == CSS::Selector::SimpleSelector::Type::TagName) {
2414
0
        return SimplifiedSelectorForBucketing { inner_simple_selector.type, inner_simple_selector.qualified_name().name.lowercase_name };
2415
0
    }
2416
2417
0
    return {};
2418
0
}
2419
2420
NonnullOwnPtr<StyleComputer::RuleCache> StyleComputer::make_rule_cache_for_cascade_origin(CascadeOrigin cascade_origin)
2421
0
{
2422
0
    auto rule_cache = make<RuleCache>();
2423
2424
0
    size_t num_class_rules = 0;
2425
0
    size_t num_id_rules = 0;
2426
0
    size_t num_tag_name_rules = 0;
2427
0
    size_t num_pseudo_element_rules = 0;
2428
0
    size_t num_root_rules = 0;
2429
0
    size_t num_attribute_rules = 0;
2430
0
    size_t num_hover_rules = 0;
2431
2432
0
    Vector<MatchingRule> matching_rules;
2433
0
    size_t style_sheet_index = 0;
2434
0
    for_each_stylesheet(cascade_origin, [&](auto& sheet, JS::GCPtr<DOM::ShadowRoot> shadow_root) {
2435
0
        size_t rule_index = 0;
2436
0
        sheet.for_each_effective_style_producing_rule([&](auto const& rule) {
2437
0
            size_t selector_index = 0;
2438
0
            SelectorList const& absolutized_selectors = [&]() {
2439
0
                if (rule.type() == CSSRule::Type::Style)
2440
0
                    return static_cast<CSSStyleRule const&>(rule).absolutized_selectors();
2441
0
                if (rule.type() == CSSRule::Type::NestedDeclarations)
2442
0
                    return static_cast<CSSStyleRule const&>(*rule.parent_rule()).absolutized_selectors();
2443
0
                VERIFY_NOT_REACHED();
2444
0
            }();
2445
0
            for (CSS::Selector const& selector : absolutized_selectors) {
2446
0
                MatchingRule matching_rule {
2447
0
                    shadow_root,
2448
0
                    &rule,
2449
0
                    sheet,
2450
0
                    style_sheet_index,
2451
0
                    rule_index,
2452
0
                    selector_index,
2453
0
                    selector.specificity(),
2454
0
                    cascade_origin,
2455
0
                    false,
2456
0
                    SelectorEngine::can_use_fast_matches(selector),
2457
0
                    false,
2458
0
                };
2459
2460
0
                bool contains_root_pseudo_class = false;
2461
0
                Optional<CSS::Selector::PseudoElement::Type> pseudo_element;
2462
2463
0
                for (auto const& simple_selector : selector.compound_selectors().last().simple_selectors) {
2464
0
                    if (!rule_cache->has_has_selectors && simple_selector.type == CSS::Selector::SimpleSelector::Type::PseudoClass && simple_selector.pseudo_class().type == CSS::PseudoClass::Has)
2465
0
                        rule_cache->has_has_selectors = true;
2466
0
                    if (!matching_rule.contains_pseudo_element) {
2467
0
                        if (simple_selector.type == CSS::Selector::SimpleSelector::Type::PseudoElement) {
2468
0
                            matching_rule.contains_pseudo_element = true;
2469
0
                            pseudo_element = simple_selector.pseudo_element().type();
2470
0
                            ++num_pseudo_element_rules;
2471
0
                        }
2472
0
                    }
2473
0
                    if (!contains_root_pseudo_class) {
2474
0
                        if (simple_selector.type == CSS::Selector::SimpleSelector::Type::PseudoClass
2475
0
                            && simple_selector.pseudo_class().type == CSS::PseudoClass::Root) {
2476
0
                            contains_root_pseudo_class = true;
2477
0
                            ++num_root_rules;
2478
0
                        }
2479
0
                    }
2480
2481
0
                    if (!matching_rule.must_be_hovered) {
2482
0
                        if (simple_selector.type == CSS::Selector::SimpleSelector::Type::PseudoClass && simple_selector.pseudo_class().type == CSS::PseudoClass::Hover) {
2483
0
                            matching_rule.must_be_hovered = true;
2484
0
                            ++num_hover_rules;
2485
0
                        }
2486
0
                        if (simple_selector.type == CSS::Selector::SimpleSelector::Type::PseudoClass
2487
0
                            && (simple_selector.pseudo_class().type == CSS::PseudoClass::Is
2488
0
                                || simple_selector.pseudo_class().type == CSS::PseudoClass::Where)) {
2489
0
                            auto const& argument_selectors = simple_selector.pseudo_class().argument_selector_list;
2490
2491
0
                            if (argument_selectors.size() == 1) {
2492
0
                                auto const& simple_argument_selector = argument_selectors.first()->compound_selectors().last().simple_selectors.last();
2493
0
                                if (simple_argument_selector.type == CSS::Selector::SimpleSelector::Type::PseudoClass
2494
0
                                    && simple_argument_selector.pseudo_class().type == CSS::PseudoClass::Hover) {
2495
0
                                    matching_rule.must_be_hovered = true;
2496
0
                                    ++num_hover_rules;
2497
0
                                }
2498
0
                            }
2499
0
                        }
2500
0
                    }
2501
0
                }
2502
2503
                // NOTE: We traverse the simple selectors in reverse order to make sure that class/ID buckets are preferred over tag buckets
2504
                //       in the common case of div.foo or div#foo selectors.
2505
0
                bool added_to_bucket = false;
2506
2507
0
                auto add_to_id_bucket = [&](FlyString const& name) {
2508
0
                    rule_cache->rules_by_id.ensure(name).append(move(matching_rule));
2509
0
                    ++num_id_rules;
2510
0
                    added_to_bucket = true;
2511
0
                };
2512
2513
0
                auto add_to_class_bucket = [&](FlyString const& name) {
2514
0
                    rule_cache->rules_by_class.ensure(name).append(move(matching_rule));
2515
0
                    ++num_class_rules;
2516
0
                    added_to_bucket = true;
2517
0
                };
2518
2519
0
                auto add_to_tag_name_bucket = [&](FlyString const& name) {
2520
0
                    rule_cache->rules_by_tag_name.ensure(name).append(move(matching_rule));
2521
0
                    ++num_tag_name_rules;
2522
0
                    added_to_bucket = true;
2523
0
                };
2524
2525
0
                for (auto const& simple_selector : selector.compound_selectors().last().simple_selectors.in_reverse()) {
2526
0
                    if (simple_selector.type == CSS::Selector::SimpleSelector::Type::Id) {
2527
0
                        add_to_id_bucket(simple_selector.name());
2528
0
                        break;
2529
0
                    }
2530
0
                    if (simple_selector.type == CSS::Selector::SimpleSelector::Type::Class) {
2531
0
                        add_to_class_bucket(simple_selector.name());
2532
0
                        break;
2533
0
                    }
2534
0
                    if (simple_selector.type == CSS::Selector::SimpleSelector::Type::TagName) {
2535
0
                        add_to_tag_name_bucket(simple_selector.qualified_name().name.lowercase_name);
2536
0
                        break;
2537
0
                    }
2538
                    // NOTE: Selectors like `:is/where(.foo)` and `:is/where(.foo .bar)` are bucketed as class selectors for `foo` and `bar` respectively.
2539
0
                    if (auto simplified = is_roundabout_selector_bucketable_as_something_simpler(simple_selector); simplified.has_value()) {
2540
0
                        if (simplified->type == CSS::Selector::SimpleSelector::Type::TagName) {
2541
0
                            add_to_tag_name_bucket(simplified->name);
2542
0
                            break;
2543
0
                        }
2544
0
                        if (simplified->type == CSS::Selector::SimpleSelector::Type::Class) {
2545
0
                            add_to_class_bucket(simplified->name);
2546
0
                            break;
2547
0
                        }
2548
0
                        if (simplified->type == CSS::Selector::SimpleSelector::Type::Id) {
2549
0
                            add_to_id_bucket(simplified->name);
2550
0
                            break;
2551
0
                        }
2552
0
                    }
2553
0
                }
2554
0
                if (!added_to_bucket) {
2555
0
                    if (matching_rule.contains_pseudo_element) {
2556
0
                        if (CSS::Selector::PseudoElement::is_known_pseudo_element_type(pseudo_element.value())) {
2557
0
                            rule_cache->rules_by_pseudo_element[to_underlying(pseudo_element.value())].append(move(matching_rule));
2558
0
                        } else {
2559
                            // NOTE: We don't cache rules for unknown pseudo-elements. They can't match anything anyway.
2560
0
                        }
2561
0
                    } else if (contains_root_pseudo_class) {
2562
0
                        rule_cache->root_rules.append(move(matching_rule));
2563
0
                    } else {
2564
0
                        for (auto const& simple_selector : selector.compound_selectors().last().simple_selectors) {
2565
0
                            if (simple_selector.type == CSS::Selector::SimpleSelector::Type::Attribute) {
2566
0
                                rule_cache->rules_by_attribute_name.ensure(simple_selector.attribute().qualified_name.name.lowercase_name).append(move(matching_rule));
2567
0
                                ++num_attribute_rules;
2568
0
                                added_to_bucket = true;
2569
0
                                break;
2570
0
                            }
2571
0
                        }
2572
0
                        if (!added_to_bucket) {
2573
0
                            rule_cache->other_rules.append(move(matching_rule));
2574
0
                        }
2575
0
                    }
2576
0
                }
2577
2578
0
                ++selector_index;
2579
0
            }
2580
0
            ++rule_index;
2581
0
        });
2582
2583
        // Loosely based on https://drafts.csswg.org/css-animations-2/#keyframe-processing
2584
0
        sheet.for_each_effective_keyframes_at_rule([&](CSSKeyframesRule const& rule) {
2585
0
            auto keyframe_set = adopt_ref(*new Animations::KeyframeEffect::KeyFrameSet);
2586
0
            HashTable<PropertyID> animated_properties;
2587
2588
            // Forwards pass, resolve all the user-specified keyframe properties.
2589
0
            for (auto const& keyframe_rule : *rule.css_rules()) {
2590
0
                auto const& keyframe = verify_cast<CSSKeyframeRule>(*keyframe_rule);
2591
0
                Animations::KeyframeEffect::KeyFrameSet::ResolvedKeyFrame resolved_keyframe;
2592
2593
0
                auto key = static_cast<u64>(keyframe.key().value() * Animations::KeyframeEffect::AnimationKeyFrameKeyScaleFactor);
2594
0
                auto const& keyframe_style = *keyframe.style_as_property_owning_style_declaration();
2595
0
                for (auto const& it : keyframe_style.properties()) {
2596
                    // Unresolved properties will be resolved in collect_animation_into()
2597
0
                    for_each_property_expanding_shorthands(it.property_id, it.value, AllowUnresolved::Yes, [&](PropertyID shorthand_id, CSSStyleValue const& shorthand_value) {
2598
0
                        animated_properties.set(shorthand_id);
2599
0
                        resolved_keyframe.properties.set(shorthand_id, NonnullRefPtr<CSSStyleValue const> { shorthand_value });
2600
0
                    });
2601
0
                }
2602
2603
0
                keyframe_set->keyframes_by_key.insert(key, resolved_keyframe);
2604
0
            }
2605
2606
0
            Animations::KeyframeEffect::generate_initial_and_final_frames(keyframe_set, animated_properties);
2607
2608
            if constexpr (LIBWEB_CSS_DEBUG) {
2609
                dbgln("Resolved keyframe set '{}' into {} keyframes:", rule.name(), keyframe_set->keyframes_by_key.size());
2610
                for (auto it = keyframe_set->keyframes_by_key.begin(); it != keyframe_set->keyframes_by_key.end(); ++it)
2611
                    dbgln("    - keyframe {}: {} properties", it.key(), it->properties.size());
2612
            }
2613
2614
0
            rule_cache->rules_by_animation_keyframes.set(rule.name(), move(keyframe_set));
2615
0
        });
2616
0
        ++style_sheet_index;
2617
0
    });
2618
2619
0
    size_t total_rules = num_class_rules + num_id_rules + num_tag_name_rules + num_pseudo_element_rules + num_root_rules + num_attribute_rules + rule_cache->other_rules.size();
2620
    if constexpr (LIBWEB_CSS_DEBUG) {
2621
        dbgln("Built rule cache!");
2622
        dbgln("           ID: {}", num_id_rules);
2623
        dbgln("        Class: {}", num_class_rules);
2624
        dbgln("      TagName: {}", num_tag_name_rules);
2625
        dbgln("PseudoElement: {}", num_pseudo_element_rules);
2626
        dbgln("         Root: {}", num_root_rules);
2627
        dbgln("    Attribute: {}", num_attribute_rules);
2628
        dbgln("        Other: {}", rule_cache->other_rules.size());
2629
        dbgln("        Total: {}", total_rules);
2630
    }
2631
0
    return rule_cache;
2632
0
}
2633
2634
struct LayerNode {
2635
    OrderedHashMap<FlyString, LayerNode> children {};
2636
};
2637
2638
static void flatten_layer_names_tree(Vector<FlyString>& layer_names, StringView const& parent_qualified_name, FlyString const& name, LayerNode const& node)
2639
0
{
2640
0
    FlyString qualified_name = parent_qualified_name.is_empty() ? name : MUST(String::formatted("{}.{}", parent_qualified_name, name));
2641
2642
0
    for (auto const& item : node.children)
2643
0
        flatten_layer_names_tree(layer_names, qualified_name, item.key, item.value);
2644
2645
0
    layer_names.append(qualified_name);
2646
0
}
2647
2648
void StyleComputer::build_qualified_layer_names_cache()
2649
0
{
2650
0
    LayerNode root;
2651
2652
0
    auto insert_layer_name = [&](FlyString const& internal_qualified_name) {
2653
0
        auto* node = &root;
2654
0
        internal_qualified_name.bytes_as_string_view()
2655
0
            .for_each_split_view('.', SplitBehavior::Nothing, [&](StringView part) {
2656
0
                auto local_name = MUST(FlyString::from_utf8(part));
2657
0
                node = &node->children.ensure(local_name);
2658
0
            });
2659
0
    };
2660
2661
    // Walk all style sheets, identifying when we first see a @layer name, and add its qualified name to the list.
2662
    // TODO: Separate the light and shadow-dom layers.
2663
0
    for_each_stylesheet(CascadeOrigin::Author, [&](auto& sheet, JS::GCPtr<DOM::ShadowRoot>) {
2664
        // NOTE: Postorder so that a @layer block is iterated after its children,
2665
        // because we want those children to occur before it in the list.
2666
0
        sheet.for_each_effective_rule(TraversalOrder::Postorder, [&](auto& rule) {
2667
0
            switch (rule.type()) {
2668
0
            case CSSRule::Type::Import:
2669
                // TODO: Handle `layer(foo)` in import rules once we implement that.
2670
0
                break;
2671
0
            case CSSRule::Type::LayerBlock: {
2672
0
                auto& layer_block = static_cast<CSSLayerBlockRule const&>(rule);
2673
0
                insert_layer_name(layer_block.internal_qualified_name({}));
2674
0
                break;
2675
0
            }
2676
0
            case CSSRule::Type::LayerStatement: {
2677
0
                auto& layer_statement = static_cast<CSSLayerStatementRule const&>(rule);
2678
0
                auto qualified_names = layer_statement.internal_qualified_name_list({});
2679
0
                for (auto& name : qualified_names)
2680
0
                    insert_layer_name(name);
2681
0
                break;
2682
0
            }
2683
2684
                // Ignore everything else
2685
0
            case CSSRule::Type::Style:
2686
0
            case CSSRule::Type::Media:
2687
0
            case CSSRule::Type::FontFace:
2688
0
            case CSSRule::Type::Keyframes:
2689
0
            case CSSRule::Type::Keyframe:
2690
0
            case CSSRule::Type::Namespace:
2691
0
            case CSSRule::Type::NestedDeclarations:
2692
0
            case CSSRule::Type::Supports:
2693
0
                break;
2694
0
            }
2695
0
        });
2696
0
    });
2697
2698
    // Now, produce a flat list of qualified names to use later
2699
0
    m_qualified_layer_names_in_order.clear();
2700
0
    flatten_layer_names_tree(m_qualified_layer_names_in_order, ""sv, {}, root);
2701
0
}
2702
2703
void StyleComputer::build_rule_cache()
2704
0
{
2705
0
    if (auto user_style_source = document().page().user_style(); user_style_source.has_value()) {
2706
0
        m_user_style_sheet = JS::make_handle(parse_css_stylesheet(CSS::Parser::ParsingContext(document()), user_style_source.value()));
2707
0
    }
2708
2709
0
    build_qualified_layer_names_cache();
2710
2711
0
    m_author_rule_cache = make_rule_cache_for_cascade_origin(CascadeOrigin::Author);
2712
0
    m_user_rule_cache = make_rule_cache_for_cascade_origin(CascadeOrigin::User);
2713
0
    m_user_agent_rule_cache = make_rule_cache_for_cascade_origin(CascadeOrigin::UserAgent);
2714
2715
0
    m_has_has_selectors = m_author_rule_cache->has_has_selectors || m_user_rule_cache->has_has_selectors || m_user_agent_rule_cache->has_has_selectors;
2716
0
}
2717
2718
void StyleComputer::invalidate_rule_cache()
2719
0
{
2720
0
    m_author_rule_cache = nullptr;
2721
2722
    // NOTE: We could be smarter about keeping the user rule cache, and style sheet.
2723
    //       Currently we are re-parsing the user style sheet every time we build the caches,
2724
    //       as it may have changed.
2725
0
    m_user_rule_cache = nullptr;
2726
0
    m_user_style_sheet = nullptr;
2727
2728
    // NOTE: It might not be necessary to throw away the UA rule cache.
2729
    //       If we are sure that it's safe, we could keep it as an optimization.
2730
0
    m_user_agent_rule_cache = nullptr;
2731
0
}
2732
2733
void StyleComputer::did_load_font(FlyString const&)
2734
0
{
2735
0
    document().invalidate_style(DOM::StyleInvalidationReason::CSSFontLoaded);
2736
0
}
2737
2738
Optional<FontLoader&> StyleComputer::load_font_face(ParsedFontFace const& font_face, Function<void(FontLoader const&)> on_load, Function<void()> on_fail)
2739
0
{
2740
0
    if (font_face.sources().is_empty()) {
2741
0
        if (on_fail)
2742
0
            on_fail();
2743
0
        return {};
2744
0
    }
2745
2746
0
    FontFaceKey key {
2747
0
        .family_name = font_face.font_family(),
2748
0
        .weight = font_face.weight().value_or(0),
2749
0
        .slope = font_face.slope().value_or(0),
2750
0
    };
2751
2752
0
    Vector<URL::URL> urls;
2753
0
    for (auto const& source : font_face.sources()) {
2754
        // FIXME: These should be loaded relative to the stylesheet URL instead of the document URL.
2755
0
        if (source.local_or_url.has<URL::URL>())
2756
0
            urls.append(m_document->parse_url(MUST(source.local_or_url.get<URL::URL>().to_string())));
2757
        // FIXME: Handle local()
2758
0
    }
2759
2760
0
    if (urls.is_empty()) {
2761
0
        if (on_fail)
2762
0
            on_fail();
2763
0
        return {};
2764
0
    }
2765
2766
0
    auto loader = make<FontLoader>(const_cast<StyleComputer&>(*this), font_face.font_family(), font_face.unicode_ranges(), move(urls), move(on_load), move(on_fail));
2767
0
    auto& loader_ref = *loader;
2768
0
    auto maybe_font_loaders_list = const_cast<StyleComputer&>(*this).m_loaded_fonts.get(key);
2769
0
    if (maybe_font_loaders_list.has_value()) {
2770
0
        maybe_font_loaders_list->append(move(loader));
2771
0
    } else {
2772
0
        FontLoaderList loaders;
2773
0
        loaders.append(move(loader));
2774
0
        const_cast<StyleComputer&>(*this).m_loaded_fonts.set(key, move(loaders));
2775
0
    }
2776
    // Actual object owned by font loader list inside m_loaded_fonts, this isn't use-after-move/free
2777
0
    return loader_ref;
2778
0
}
2779
2780
void StyleComputer::load_fonts_from_sheet(CSSStyleSheet& sheet)
2781
0
{
2782
0
    for (auto const& rule : sheet.rules()) {
2783
0
        if (!is<CSSFontFaceRule>(*rule))
2784
0
            continue;
2785
0
        auto font_loader = load_font_face(static_cast<CSSFontFaceRule const&>(*rule).font_face());
2786
0
        if (font_loader.has_value()) {
2787
0
            sheet.add_associated_font_loader(font_loader.value());
2788
0
        }
2789
0
    }
2790
0
}
2791
2792
void StyleComputer::unload_fonts_from_sheet(CSSStyleSheet& sheet)
2793
0
{
2794
0
    for (auto& [_, font_loader_list] : m_loaded_fonts) {
2795
0
        font_loader_list.remove_all_matching([&](auto& font_loader) {
2796
0
            return sheet.has_associated_font_loader(*font_loader);
2797
0
        });
2798
0
    }
2799
0
}
2800
2801
void StyleComputer::compute_math_depth(StyleProperties& style, DOM::Element const* element, Optional<CSS::Selector::PseudoElement::Type> pseudo_element) const
2802
0
{
2803
    // https://w3c.github.io/mathml-core/#propdef-math-depth
2804
2805
    // First, ensure that the relevant CSS properties have been defaulted.
2806
    // FIXME: This should be more sophisticated.
2807
0
    compute_defaulted_property_value(style, element, CSS::PropertyID::MathDepth, pseudo_element);
2808
0
    compute_defaulted_property_value(style, element, CSS::PropertyID::MathStyle, pseudo_element);
2809
2810
0
    auto inherited_math_depth = [&]() {
2811
0
        if (!element || !element->parent_element())
2812
0
            return InitialValues::math_depth();
2813
0
        return element->parent_element()->computed_css_values()->math_depth();
2814
0
    };
2815
2816
0
    auto value = style.property(CSS::PropertyID::MathDepth);
2817
0
    if (!value->is_math_depth()) {
2818
0
        style.set_math_depth(inherited_math_depth());
2819
0
        return;
2820
0
    }
2821
0
    auto& math_depth = value->as_math_depth();
2822
2823
0
    auto resolve_integer = [&](CSSStyleValue const& integer_value) {
2824
0
        if (integer_value.is_integer())
2825
0
            return integer_value.as_integer().integer();
2826
0
        if (integer_value.is_math())
2827
0
            return integer_value.as_math().resolve_integer().value();
2828
0
        VERIFY_NOT_REACHED();
2829
0
    };
2830
2831
    // The computed value of the math-depth value is determined as follows:
2832
    // - If the specified value of math-depth is auto-add and the inherited value of math-style is compact
2833
    //   then the computed value of math-depth of the element is its inherited value plus one.
2834
0
    if (math_depth.is_auto_add() && style.property(CSS::PropertyID::MathStyle)->to_keyword() == Keyword::Compact) {
2835
0
        style.set_math_depth(inherited_math_depth() + 1);
2836
0
        return;
2837
0
    }
2838
    // - If the specified value of math-depth is of the form add(<integer>) then the computed value of
2839
    //   math-depth of the element is its inherited value plus the specified integer.
2840
0
    if (math_depth.is_add()) {
2841
0
        style.set_math_depth(inherited_math_depth() + resolve_integer(*math_depth.integer_value()));
2842
0
        return;
2843
0
    }
2844
    // - If the specified value of math-depth is of the form <integer> then the computed value of math-depth
2845
    //   of the element is the specified integer.
2846
0
    if (math_depth.is_integer()) {
2847
0
        style.set_math_depth(resolve_integer(*math_depth.integer_value()));
2848
0
        return;
2849
0
    }
2850
    // - Otherwise, the computed value of math-depth of the element is the inherited one.
2851
0
    style.set_math_depth(inherited_math_depth());
2852
0
}
2853
2854
static void for_each_element_hash(DOM::Element const& element, auto callback)
2855
0
{
2856
0
    callback(element.local_name().hash());
2857
0
    if (element.id().has_value())
2858
0
        callback(element.id().value().hash());
2859
0
    for (auto const& class_ : element.class_names())
2860
0
        callback(class_.hash());
2861
0
    element.for_each_attribute([&](auto& attribute) {
2862
0
        callback(attribute.local_name().hash());
2863
0
    });
Unexecuted instantiation: StyleComputer.cpp:auto Web::CSS::for_each_element_hash<Web::CSS::StyleComputer::push_ancestor(Web::DOM::Element const&)::$_0>(Web::DOM::Element const&, Web::CSS::StyleComputer::push_ancestor(Web::DOM::Element const&)::$_0)::{lambda(auto:1&)#1}::operator()<Web::DOM::Attr const>(Web::DOM::Attr const&) const
Unexecuted instantiation: StyleComputer.cpp:auto Web::CSS::for_each_element_hash<Web::CSS::StyleComputer::pop_ancestor(Web::DOM::Element const&)::$_0>(Web::DOM::Element const&, Web::CSS::StyleComputer::pop_ancestor(Web::DOM::Element const&)::$_0)::{lambda(auto:1&)#1}::operator()<Web::DOM::Attr const>(Web::DOM::Attr const&) const
2864
0
}
Unexecuted instantiation: StyleComputer.cpp:void Web::CSS::for_each_element_hash<Web::CSS::StyleComputer::push_ancestor(Web::DOM::Element const&)::$_0>(Web::DOM::Element const&, Web::CSS::StyleComputer::push_ancestor(Web::DOM::Element const&)::$_0)
Unexecuted instantiation: StyleComputer.cpp:void Web::CSS::for_each_element_hash<Web::CSS::StyleComputer::pop_ancestor(Web::DOM::Element const&)::$_0>(Web::DOM::Element const&, Web::CSS::StyleComputer::pop_ancestor(Web::DOM::Element const&)::$_0)
2865
2866
void StyleComputer::reset_ancestor_filter()
2867
0
{
2868
0
    m_ancestor_filter.clear();
2869
0
}
2870
2871
void StyleComputer::push_ancestor(DOM::Element const& element)
2872
0
{
2873
0
    for_each_element_hash(element, [&](u32 hash) {
2874
0
        m_ancestor_filter.increment(hash);
2875
0
    });
2876
0
}
2877
2878
void StyleComputer::pop_ancestor(DOM::Element const& element)
2879
0
{
2880
0
    for_each_element_hash(element, [&](u32 hash) {
2881
0
        m_ancestor_filter.decrement(hash);
2882
0
    });
2883
0
}
2884
2885
}