Coverage Report

Created: 2025-03-04 07:22

/src/serenity/Userland/Libraries/LibWeb/CSS/CSSMediaRule.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
3
 * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
4
 *
5
 * SPDX-License-Identifier: BSD-2-Clause
6
 */
7
8
#include <LibJS/Runtime/Realm.h>
9
#include <LibWeb/Bindings/CSSMediaRulePrototype.h>
10
#include <LibWeb/Bindings/Intrinsics.h>
11
#include <LibWeb/CSS/CSSMediaRule.h>
12
13
namespace Web::CSS {
14
15
JS_DEFINE_ALLOCATOR(CSSMediaRule);
16
17
JS::NonnullGCPtr<CSSMediaRule> CSSMediaRule::create(JS::Realm& realm, MediaList& media_queries, CSSRuleList& rules)
18
0
{
19
0
    return realm.heap().allocate<CSSMediaRule>(realm, realm, media_queries, rules);
20
0
}
21
22
CSSMediaRule::CSSMediaRule(JS::Realm& realm, MediaList& media, CSSRuleList& rules)
23
0
    : CSSConditionRule(realm, rules)
24
0
    , m_media(media)
25
0
{
26
0
}
27
28
void CSSMediaRule::initialize(JS::Realm& realm)
29
0
{
30
0
    Base::initialize(realm);
31
0
    WEB_SET_PROTOTYPE_FOR_INTERFACE(CSSMediaRule);
32
0
}
33
34
void CSSMediaRule::visit_edges(Cell::Visitor& visitor)
35
0
{
36
0
    Base::visit_edges(visitor);
37
0
    visitor.visit(m_media);
38
0
}
39
40
String CSSMediaRule::condition_text() const
41
0
{
42
0
    return m_media->media_text();
43
0
}
44
45
// https://www.w3.org/TR/cssom-1/#serialize-a-css-rule
46
String CSSMediaRule::serialized() const
47
0
{
48
    // The result of concatenating the following:
49
0
    StringBuilder builder;
50
51
    // 1. The string "@media", followed by a single SPACE (U+0020).
52
0
    builder.append("@media "sv);
53
    // 2. The result of performing serialize a media query list on rule’s media query list.
54
0
    builder.append(condition_text());
55
    // 3. A single SPACE (U+0020), followed by the string "{", i.e., LEFT CURLY BRACKET (U+007B), followed by a newline.
56
0
    builder.append(" {\n"sv);
57
    // 4. The result of performing serialize a CSS rule on each rule in the rule’s cssRules list,
58
    //    filtering out empty strings, indenting each item with two spaces, all joined with newline.
59
0
    for (size_t i = 0; i < css_rules().length(); i++) {
60
0
        auto rule = css_rules().item(i);
61
0
        auto result = rule->css_text();
62
63
0
        if (result.is_empty())
64
0
            continue;
65
66
0
        builder.append("  "sv);
67
0
        builder.append(result);
68
0
        builder.append('\n');
69
0
    }
70
    // 5. A newline, followed by the string "}", i.e., RIGHT CURLY BRACKET (U+007D)
71
    // AD-HOC: All modern browsers omit the ending newline if there are no CSS rules, so let's do the same.
72
    //         If there are rules, the required newline will be appended in the for-loop above.
73
0
    builder.append('}');
74
75
0
    return MUST(builder.to_string());
76
0
}
77
78
}