/src/serenity/Userland/Libraries/LibWeb/CSS/Supports.h
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 | | #pragma once |
8 | | |
9 | | #include <AK/NonnullOwnPtr.h> |
10 | | #include <AK/RefCounted.h> |
11 | | #include <AK/String.h> |
12 | | #include <AK/Variant.h> |
13 | | #include <AK/Vector.h> |
14 | | #include <LibWeb/CSS/GeneralEnclosed.h> |
15 | | |
16 | | namespace Web::CSS { |
17 | | |
18 | | // https://www.w3.org/TR/css-conditional-3/#at-supports |
19 | | class Supports final : public RefCounted<Supports> { |
20 | | friend class Parser::Parser; |
21 | | |
22 | | public: |
23 | | struct Declaration { |
24 | | String declaration; |
25 | | [[nodiscard]] bool evaluate(JS::Realm&) const; |
26 | | String to_string() const; |
27 | | }; |
28 | | |
29 | | struct Selector { |
30 | | String selector; |
31 | | [[nodiscard]] bool evaluate(JS::Realm&) const; |
32 | | String to_string() const; |
33 | | }; |
34 | | |
35 | | struct Feature { |
36 | | Variant<Declaration, Selector> value; |
37 | | [[nodiscard]] bool evaluate(JS::Realm&) const; |
38 | | String to_string() const; |
39 | | }; |
40 | | |
41 | | struct Condition; |
42 | | struct InParens { |
43 | | Variant<NonnullOwnPtr<Condition>, Feature, GeneralEnclosed> value; |
44 | | |
45 | | [[nodiscard]] bool evaluate(JS::Realm&) const; |
46 | | String to_string() const; |
47 | | }; |
48 | | |
49 | | struct Condition { |
50 | | enum class Type { |
51 | | Not, |
52 | | And, |
53 | | Or, |
54 | | }; |
55 | | Type type; |
56 | | Vector<InParens> children; |
57 | | |
58 | | [[nodiscard]] bool evaluate(JS::Realm&) const; |
59 | | String to_string() const; |
60 | | }; |
61 | | |
62 | | static NonnullRefPtr<Supports> create(JS::Realm& realm, NonnullOwnPtr<Condition>&& condition) |
63 | 0 | { |
64 | 0 | return adopt_ref(*new Supports(realm, move(condition))); |
65 | 0 | } |
66 | | |
67 | 0 | bool matches() const { return m_matches; } |
68 | | String to_string() const; |
69 | | |
70 | | private: |
71 | | Supports(JS::Realm&, NonnullOwnPtr<Condition>&&); |
72 | | |
73 | | NonnullOwnPtr<Condition> m_condition; |
74 | | bool m_matches { false }; |
75 | | }; |
76 | | |
77 | | } |
78 | | |
79 | | template<> |
80 | | struct AK::Formatter<Web::CSS::Supports::InParens> : AK::Formatter<StringView> { |
81 | | ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Supports::InParens const& in_parens) |
82 | 0 | { |
83 | 0 | return Formatter<StringView>::format(builder, in_parens.to_string()); |
84 | 0 | } |
85 | | }; |