/src/serenity/Userland/Libraries/LibWeb/CSS/CSSLayerBlockRule.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2024, Sam Atkins <sam@ladybird.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include "CSSLayerBlockRule.h" |
8 | | #include <LibWeb/Bindings/CSSLayerBlockRulePrototype.h> |
9 | | #include <LibWeb/Bindings/Intrinsics.h> |
10 | | #include <LibWeb/CSS/Serialize.h> |
11 | | |
12 | | namespace Web::CSS { |
13 | | |
14 | | JS_DEFINE_ALLOCATOR(CSSLayerBlockRule); |
15 | | |
16 | | JS::NonnullGCPtr<CSSLayerBlockRule> CSSLayerBlockRule::create(JS::Realm& realm, FlyString name, CSSRuleList& rules) |
17 | 0 | { |
18 | 0 | return realm.heap().allocate<CSSLayerBlockRule>(realm, realm, move(name), rules); |
19 | 0 | } |
20 | | |
21 | | FlyString CSSLayerBlockRule::next_unique_anonymous_layer_name() |
22 | 0 | { |
23 | 0 | static u64 s_anonymous_layer_id = 0; |
24 | 0 | return MUST(String::formatted("#{}", ++s_anonymous_layer_id)); |
25 | 0 | } |
26 | | |
27 | | CSSLayerBlockRule::CSSLayerBlockRule(JS::Realm& realm, FlyString name, CSSRuleList& rules) |
28 | 0 | : CSSGroupingRule(realm, rules) |
29 | 0 | , m_name(move(name)) |
30 | 0 | { |
31 | 0 | if (m_name.is_empty()) { |
32 | 0 | m_name_internal = next_unique_anonymous_layer_name(); |
33 | 0 | } else { |
34 | 0 | m_name_internal = m_name; |
35 | 0 | } |
36 | 0 | } |
37 | | |
38 | | void CSSLayerBlockRule::initialize(JS::Realm& realm) |
39 | 0 | { |
40 | 0 | Base::initialize(realm); |
41 | 0 | WEB_SET_PROTOTYPE_FOR_INTERFACE(CSSLayerBlockRule); |
42 | 0 | } |
43 | | |
44 | | String CSSLayerBlockRule::serialized() const |
45 | 0 | { |
46 | | // AD-HOC: No spec yet, so this is based on the @media serialization algorithm. |
47 | 0 | StringBuilder builder; |
48 | 0 | builder.append("@layer"sv); |
49 | 0 | if (!m_name.is_empty()) |
50 | 0 | builder.appendff(" {}", m_name); |
51 | |
|
52 | 0 | builder.append(" {\n"sv); |
53 | | // AD-HOC: All modern browsers omit the ending newline if there are no CSS rules, so let's do the same. |
54 | 0 | if (css_rules().length() == 0) { |
55 | 0 | builder.append('}'); |
56 | 0 | return builder.to_string_without_validation(); |
57 | 0 | } |
58 | | |
59 | 0 | for (size_t i = 0; i < css_rules().length(); i++) { |
60 | 0 | auto rule = css_rules().item(i); |
61 | 0 | if (i != 0) |
62 | 0 | builder.append("\n"sv); |
63 | 0 | builder.append(" "sv); |
64 | 0 | builder.append(rule->css_text()); |
65 | 0 | } |
66 | |
|
67 | 0 | builder.append("\n}"sv); |
68 | |
|
69 | 0 | return builder.to_string_without_validation(); |
70 | 0 | } |
71 | | |
72 | | FlyString CSSLayerBlockRule::internal_qualified_name(Badge<StyleComputer>) const |
73 | 0 | { |
74 | 0 | auto const& parent_name = parent_layer_internal_qualified_name(); |
75 | 0 | if (parent_name.is_empty()) |
76 | 0 | return m_name_internal; |
77 | 0 | return MUST(String::formatted("{}.{}", parent_name, m_name_internal)); |
78 | 0 | } |
79 | | |
80 | | } |