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/CSSImportRule.cpp
Line
Count
Source
1
/*
2
 * Copyright (c) 2021, the SerenityOS developers.
3
 * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
4
 * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 */
8
9
#include <AK/Debug.h>
10
#include <LibURL/URL.h>
11
#include <LibWeb/Bindings/CSSImportRulePrototype.h>
12
#include <LibWeb/Bindings/Intrinsics.h>
13
#include <LibWeb/CSS/CSSImportRule.h>
14
#include <LibWeb/CSS/Parser/Parser.h>
15
#include <LibWeb/CSS/StyleComputer.h>
16
#include <LibWeb/DOM/Document.h>
17
#include <LibWeb/HTML/Window.h>
18
#include <LibWeb/Loader/ResourceLoader.h>
19
20
namespace Web::CSS {
21
22
JS_DEFINE_ALLOCATOR(CSSImportRule);
23
24
JS::NonnullGCPtr<CSSImportRule> CSSImportRule::create(URL::URL url, DOM::Document& document)
25
0
{
26
0
    auto& realm = document.realm();
27
0
    return realm.heap().allocate<CSSImportRule>(realm, move(url), document);
28
0
}
29
30
CSSImportRule::CSSImportRule(URL::URL url, DOM::Document& document)
31
0
    : CSSRule(document.realm())
32
0
    , m_url(move(url))
33
0
    , m_document(document)
34
0
{
35
0
    dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Loading import URL: {}", m_url);
36
0
    auto request = LoadRequest::create_for_url_on_page(m_url, &document.page());
37
38
    // NOTE: Mark this rule as delaying the document load event *before* calling set_resource()
39
    //       as it may trigger a synchronous resource_did_load() callback.
40
0
    m_document_load_event_delayer.emplace(document);
41
42
0
    set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, request));
43
0
}
44
45
void CSSImportRule::initialize(JS::Realm& realm)
46
0
{
47
0
    Base::initialize(realm);
48
0
    WEB_SET_PROTOTYPE_FOR_INTERFACE(CSSImportRule);
49
0
}
50
51
void CSSImportRule::visit_edges(Cell::Visitor& visitor)
52
0
{
53
0
    Base::visit_edges(visitor);
54
0
    visitor.visit(m_document);
55
0
    visitor.visit(m_style_sheet);
56
0
}
57
58
// https://www.w3.org/TR/cssom/#serialize-a-css-rule
59
String CSSImportRule::serialized() const
60
0
{
61
0
    StringBuilder builder;
62
    // The result of concatenating the following:
63
64
    // 1. The string "@import" followed by a single SPACE (U+0020).
65
0
    builder.append("@import "sv);
66
67
    // 2. The result of performing serialize a URL on the rule’s location.
68
0
    serialize_a_url(builder, MUST(m_url.to_string()));
69
70
    // FIXME: 3. If the rule’s associated media list is not empty, a single SPACE (U+0020) followed by the result of performing serialize a media query list on the media list.
71
72
    // 4. The string ";", i.e., SEMICOLON (U+003B).
73
0
    builder.append(';');
74
75
0
    return MUST(builder.to_string());
76
0
}
77
78
void CSSImportRule::resource_did_fail()
79
0
{
80
0
    dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Resource did fail. URL: {}", resource()->url());
81
82
0
    m_document_load_event_delayer.clear();
83
0
}
84
85
void CSSImportRule::resource_did_load()
86
0
{
87
0
    VERIFY(resource());
88
89
0
    if (!m_document)
90
0
        return;
91
92
0
    m_document_load_event_delayer.clear();
93
94
0
    if (!resource()->has_encoded_data()) {
95
0
        dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Resource did load, no encoded data. URL: {}", resource()->url());
96
0
    } else {
97
0
        dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Resource did load, has encoded data. URL: {}", resource()->url());
98
0
    }
99
100
0
    auto* sheet = parse_css_stylesheet(CSS::Parser::ParsingContext(*m_document, resource()->url()), resource()->encoded_data(), resource()->url());
101
0
    if (!sheet) {
102
0
        dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Failed to parse stylesheet: {}", resource()->url());
103
0
        return;
104
0
    }
105
106
0
    m_style_sheet = sheet;
107
0
    m_style_sheet->set_owner_css_rule(this);
108
109
0
    m_document->style_computer().invalidate_rule_cache();
110
0
    m_document->style_computer().load_fonts_from_sheet(*m_style_sheet);
111
0
    m_document->invalidate_style(DOM::StyleInvalidationReason::CSSImportRule);
112
0
}
113
114
}