/src/serenity/Userland/Libraries/LibWeb/CSS/CSSRule.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2021, the SerenityOS developers. |
3 | | * Copyright (c) 2022-2024, Sam Atkins <sam@ladybird.org> |
4 | | * Copyright (c) 2022, Andreas Kling <kling@serenityos.org> |
5 | | * |
6 | | * SPDX-License-Identifier: BSD-2-Clause |
7 | | */ |
8 | | |
9 | | #include <LibWeb/Bindings/CSSRulePrototype.h> |
10 | | #include <LibWeb/CSS/CSSLayerBlockRule.h> |
11 | | #include <LibWeb/CSS/CSSRule.h> |
12 | | #include <LibWeb/CSS/CSSStyleSheet.h> |
13 | | |
14 | | namespace Web::CSS { |
15 | | |
16 | | CSSRule::CSSRule(JS::Realm& realm) |
17 | 0 | : PlatformObject(realm) |
18 | 0 | { |
19 | 0 | } |
20 | | |
21 | | void CSSRule::visit_edges(Cell::Visitor& visitor) |
22 | 0 | { |
23 | 0 | Base::visit_edges(visitor); |
24 | 0 | visitor.visit(m_parent_style_sheet); |
25 | 0 | visitor.visit(m_parent_rule); |
26 | 0 | } |
27 | | |
28 | | // https://www.w3.org/TR/cssom/#dom-cssrule-csstext |
29 | | String CSSRule::css_text() const |
30 | 0 | { |
31 | | // The cssText attribute must return a serialization of the CSS rule. |
32 | 0 | return serialized(); |
33 | 0 | } |
34 | | |
35 | | // https://www.w3.org/TR/cssom/#dom-cssrule-csstext |
36 | | void CSSRule::set_css_text(StringView) |
37 | 0 | { |
38 | | // On setting the cssText attribute must do nothing. |
39 | 0 | } |
40 | | |
41 | | void CSSRule::set_parent_rule(CSSRule* parent_rule) |
42 | 0 | { |
43 | 0 | m_parent_rule = parent_rule; |
44 | 0 | clear_caches(); |
45 | 0 | } |
46 | | |
47 | | void CSSRule::set_parent_style_sheet(CSSStyleSheet* parent_style_sheet) |
48 | 0 | { |
49 | 0 | m_parent_style_sheet = parent_style_sheet; |
50 | 0 | clear_caches(); |
51 | 0 | } |
52 | | |
53 | | void CSSRule::clear_caches() |
54 | 0 | { |
55 | 0 | m_cached_layer_name.clear(); |
56 | 0 | } |
57 | | |
58 | | FlyString const& CSSRule::parent_layer_internal_qualified_name_slow_case() const |
59 | 0 | { |
60 | 0 | Vector<FlyString> layer_names; |
61 | 0 | for (auto* rule = parent_rule(); rule; rule = rule->parent_rule()) { |
62 | 0 | switch (rule->type()) { |
63 | 0 | case Type::Import: |
64 | | // TODO: Handle `layer(foo)` in import rules once we implement that. |
65 | 0 | break; |
66 | | |
67 | 0 | case Type::LayerBlock: { |
68 | 0 | auto& layer_block = static_cast<CSSLayerBlockRule const&>(*rule); |
69 | 0 | layer_names.append(layer_block.internal_name()); |
70 | 0 | break; |
71 | 0 | } |
72 | | |
73 | | // Ignore everything else |
74 | | // Note that LayerStatement cannot have child rules so we still ignore it here. |
75 | 0 | case Type::LayerStatement: |
76 | 0 | case Type::Style: |
77 | 0 | case Type::Media: |
78 | 0 | case Type::FontFace: |
79 | 0 | case Type::Keyframes: |
80 | 0 | case Type::Keyframe: |
81 | 0 | case Type::Namespace: |
82 | 0 | case Type::Supports: |
83 | 0 | case Type::NestedDeclarations: |
84 | 0 | break; |
85 | 0 | } |
86 | 0 | } |
87 | | |
88 | 0 | m_cached_layer_name = MUST(String::join("."sv, layer_names.in_reverse())); |
89 | 0 | return m_cached_layer_name.value(); |
90 | 0 | } |
91 | | |
92 | | } |