forked from LadybirdBrowser/ladybird
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSSRule.cpp
105 lines (90 loc) · 2.74 KB
/
CSSRule.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* Copyright (c) 2021, the SerenityOS developers.
* Copyright (c) 2022-2024, Sam Atkins <[email protected]>
* Copyright (c) 2022, Andreas Kling <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Bindings/CSSRulePrototype.h>
#include <LibWeb/CSS/CSSLayerBlockRule.h>
#include <LibWeb/CSS/CSSRule.h>
#include <LibWeb/CSS/CSSStyleSheet.h>
namespace Web::CSS {
CSSRule::CSSRule(JS::Realm& realm, Type type)
: PlatformObject(realm)
, m_type(type)
{
}
void CSSRule::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_parent_style_sheet);
visitor.visit(m_parent_rule);
}
// https://www.w3.org/TR/cssom/#dom-cssrule-type
WebIDL::UnsignedShort CSSRule::type_for_bindings() const
{
// NOTE: Types that aren't defined in the spec must return 0.
// To do this, we arbitrarily make non-spec ones start at 100.
auto type = to_underlying(m_type);
if (type >= 100)
return 0;
return type;
}
// https://www.w3.org/TR/cssom/#dom-cssrule-csstext
String CSSRule::css_text() const
{
// The cssText attribute must return a serialization of the CSS rule.
return serialized();
}
// https://www.w3.org/TR/cssom/#dom-cssrule-csstext
void CSSRule::set_css_text(StringView)
{
// On setting the cssText attribute must do nothing.
}
void CSSRule::set_parent_rule(CSSRule* parent_rule)
{
m_parent_rule = parent_rule;
clear_caches();
}
void CSSRule::set_parent_style_sheet(CSSStyleSheet* parent_style_sheet)
{
m_parent_style_sheet = parent_style_sheet;
clear_caches();
}
void CSSRule::clear_caches()
{
m_cached_layer_name.clear();
}
FlyString const& CSSRule::parent_layer_internal_qualified_name_slow_case() const
{
Vector<FlyString> layer_names;
for (auto* rule = parent_rule(); rule; rule = rule->parent_rule()) {
switch (rule->type()) {
case Type::Import:
// TODO: Handle `layer(foo)` in import rules once we implement that.
break;
case Type::LayerBlock: {
auto& layer_block = static_cast<CSSLayerBlockRule const&>(*rule);
layer_names.append(layer_block.internal_name());
break;
}
// Ignore everything else
// Note that LayerStatement cannot have child rules so we still ignore it here.
case Type::LayerStatement:
case Type::Style:
case Type::Media:
case Type::FontFace:
case Type::Keyframes:
case Type::Keyframe:
case Type::Namespace:
case Type::Supports:
case Type::NestedDeclarations:
case Type::Property:
break;
}
}
m_cached_layer_name = MUST(String::join("."sv, layer_names.in_reverse()));
return m_cached_layer_name.value();
}
}