/src/serenity/Userland/Libraries/LibWeb/CSS/Supports.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <LibJS/Runtime/Realm.h> |
8 | | #include <LibWeb/CSS/Parser/Parser.h> |
9 | | #include <LibWeb/CSS/Supports.h> |
10 | | |
11 | | namespace Web::CSS { |
12 | | |
13 | | Supports::Supports(JS::Realm& realm, NonnullOwnPtr<Condition>&& condition) |
14 | 0 | : m_condition(move(condition)) |
15 | 0 | { |
16 | 0 | m_matches = m_condition->evaluate(realm); |
17 | 0 | } |
18 | | |
19 | | bool Supports::Condition::evaluate(JS::Realm& realm) const |
20 | 0 | { |
21 | 0 | switch (type) { |
22 | 0 | case Type::Not: |
23 | 0 | return !children.first().evaluate(realm); |
24 | 0 | case Type::And: |
25 | 0 | for (auto& child : children) { |
26 | 0 | if (!child.evaluate(realm)) |
27 | 0 | return false; |
28 | 0 | } |
29 | 0 | return true; |
30 | 0 | case Type::Or: |
31 | 0 | for (auto& child : children) { |
32 | 0 | if (child.evaluate(realm)) |
33 | 0 | return true; |
34 | 0 | } |
35 | 0 | return false; |
36 | 0 | } |
37 | 0 | VERIFY_NOT_REACHED(); |
38 | 0 | } |
39 | | |
40 | | bool Supports::InParens::evaluate(JS::Realm& realm) const |
41 | 0 | { |
42 | 0 | return value.visit( |
43 | 0 | [&](NonnullOwnPtr<Condition> const& condition) { |
44 | 0 | return condition->evaluate(realm); |
45 | 0 | }, |
46 | 0 | [&](Feature const& feature) { |
47 | 0 | return feature.evaluate(realm); |
48 | 0 | }, |
49 | 0 | [&](GeneralEnclosed const&) { |
50 | 0 | return false; |
51 | 0 | }); |
52 | 0 | } |
53 | | |
54 | | bool Supports::Declaration::evaluate(JS::Realm& realm) const |
55 | 0 | { |
56 | 0 | auto style_property = parse_css_supports_condition(Parser::ParsingContext { realm }, declaration); |
57 | 0 | return style_property.has_value(); |
58 | 0 | } |
59 | | |
60 | | bool Supports::Selector::evaluate(JS::Realm& realm) const |
61 | 0 | { |
62 | 0 | auto style_property = parse_selector(Parser::ParsingContext { realm }, selector); |
63 | 0 | return style_property.has_value(); |
64 | 0 | } |
65 | | |
66 | | bool Supports::Feature::evaluate(JS::Realm& realm) const |
67 | 0 | { |
68 | 0 | return value.visit( |
69 | 0 | [&](Declaration const& declaration) { |
70 | 0 | return declaration.evaluate(realm); |
71 | 0 | }, |
72 | 0 | [&](Selector const& selector) { |
73 | 0 | return selector.evaluate(realm); |
74 | 0 | }); |
75 | 0 | } |
76 | | |
77 | | String Supports::Declaration::to_string() const |
78 | 0 | { |
79 | 0 | return MUST(String::formatted("({})", declaration)); |
80 | 0 | } |
81 | | |
82 | | String Supports::Selector::to_string() const |
83 | 0 | { |
84 | 0 | return MUST(String::formatted("selector({})", selector)); |
85 | 0 | } |
86 | | |
87 | | String Supports::Feature::to_string() const |
88 | 0 | { |
89 | 0 | return value.visit([](auto& it) { return it.to_string(); }); Unexecuted instantiation: Supports.cpp:auto Web::CSS::Supports::Feature::to_string() const::$_0::operator()<Web::CSS::Supports::Declaration const>(Web::CSS::Supports::Declaration const&) const Unexecuted instantiation: Supports.cpp:auto Web::CSS::Supports::Feature::to_string() const::$_0::operator()<Web::CSS::Supports::Selector const>(Web::CSS::Supports::Selector const&) const Unexecuted instantiation: Supports.cpp:auto Web::CSS::Supports::Feature::to_string() const::$_0::operator()<Web::CSS::Supports::Declaration>(Web::CSS::Supports::Declaration&) const Unexecuted instantiation: Supports.cpp:auto Web::CSS::Supports::Feature::to_string() const::$_0::operator()<Web::CSS::Supports::Selector>(Web::CSS::Supports::Selector&) const |
90 | 0 | } |
91 | | |
92 | | String Supports::InParens::to_string() const |
93 | 0 | { |
94 | 0 | return value.visit( |
95 | 0 | [](NonnullOwnPtr<Condition> const& condition) { return MUST(String::formatted("({})", condition->to_string())); }, |
96 | 0 | [](Supports::Feature const& it) { return it.to_string(); }, |
97 | 0 | [](GeneralEnclosed const& it) { return it.to_string(); }); |
98 | 0 | } |
99 | | |
100 | | String Supports::Condition::to_string() const |
101 | 0 | { |
102 | 0 | switch (type) { |
103 | 0 | case Type::Not: |
104 | 0 | return MUST(String::formatted("not {}", children.first().to_string())); |
105 | 0 | case Type::And: |
106 | 0 | return MUST(String::join(" and "sv, children)); |
107 | 0 | case Type::Or: |
108 | 0 | return MUST(String::join(" or "sv, children)); |
109 | 0 | } |
110 | 0 | VERIFY_NOT_REACHED(); |
111 | 0 | } |
112 | | |
113 | | String Supports::to_string() const |
114 | 0 | { |
115 | 0 | return m_condition->to_string(); |
116 | 0 | } |
117 | | |
118 | | } |