/src/serenity/Userland/Libraries/LibWeb/CSS/CSSSupportsRule.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <LibWeb/Bindings/CSSSupportsRulePrototype.h> |
8 | | #include <LibWeb/Bindings/Intrinsics.h> |
9 | | #include <LibWeb/CSS/CSSSupportsRule.h> |
10 | | #include <LibWeb/CSS/Parser/Parser.h> |
11 | | |
12 | | namespace Web::CSS { |
13 | | |
14 | | JS_DEFINE_ALLOCATOR(CSSSupportsRule); |
15 | | |
16 | | JS::NonnullGCPtr<CSSSupportsRule> CSSSupportsRule::create(JS::Realm& realm, NonnullRefPtr<Supports>&& supports, CSSRuleList& rules) |
17 | 0 | { |
18 | 0 | return realm.heap().allocate<CSSSupportsRule>(realm, realm, move(supports), rules); |
19 | 0 | } |
20 | | |
21 | | CSSSupportsRule::CSSSupportsRule(JS::Realm& realm, NonnullRefPtr<Supports>&& supports, CSSRuleList& rules) |
22 | 0 | : CSSConditionRule(realm, rules) |
23 | 0 | , m_supports(move(supports)) |
24 | 0 | { |
25 | 0 | } |
26 | | |
27 | | void CSSSupportsRule::initialize(JS::Realm& realm) |
28 | 0 | { |
29 | 0 | Base::initialize(realm); |
30 | 0 | WEB_SET_PROTOTYPE_FOR_INTERFACE(CSSSupportsRule); |
31 | 0 | } |
32 | | |
33 | | String CSSSupportsRule::condition_text() const |
34 | 0 | { |
35 | 0 | return m_supports->to_string(); |
36 | 0 | } |
37 | | |
38 | | // https://www.w3.org/TR/cssom-1/#serialize-a-css-rule |
39 | | String CSSSupportsRule::serialized() const |
40 | 0 | { |
41 | | // Note: The spec doesn't cover this yet, so I'm roughly following the spec for the @media rule. |
42 | | // It should be pretty close! |
43 | |
|
44 | 0 | StringBuilder builder; |
45 | |
|
46 | 0 | builder.append("@supports "sv); |
47 | 0 | builder.append(condition_text()); |
48 | 0 | builder.append(" {\n"sv); |
49 | 0 | for (size_t i = 0; i < css_rules().length(); i++) { |
50 | 0 | auto rule = css_rules().item(i); |
51 | 0 | if (i != 0) |
52 | 0 | builder.append("\n"sv); |
53 | 0 | builder.append(" "sv); |
54 | 0 | builder.append(rule->css_text()); |
55 | 0 | } |
56 | 0 | builder.append("\n}"sv); |
57 | |
|
58 | 0 | return MUST(builder.to_string()); |
59 | 0 | } |
60 | | |
61 | | } |