summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/CSS/Supports.cpp
blob: 65ba825edb1f84554ae5d2f24c7a185981a3939c (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibWeb/CSS/Parser/Parser.h>
#include <LibWeb/CSS/Supports.h>

namespace Web::CSS {

Supports::Supports(NonnullOwnPtr<Condition>&& condition)
    : m_condition(move(condition))
{
    m_matches = m_condition->evaluate();
}

bool Supports::Condition::evaluate() const
{
    switch (type) {
    case Type::Not:
        return !children.first().evaluate();
    case Type::And:
        for (auto& child : children) {
            if (!child.evaluate())
                return false;
        }
        return true;
    case Type::Or:
        for (auto& child : children) {
            if (child.evaluate())
                return true;
        }
        return false;
    }
    VERIFY_NOT_REACHED();
}

bool Supports::InParens::evaluate() const
{
    return value.visit(
        [&](NonnullOwnPtr<Condition> const& condition) {
            return condition->evaluate();
        },
        [&](Feature const& feature) {
            return feature.evaluate();
        },
        [&](GeneralEnclosed const&) {
            return false;
        });
}

bool Supports::Declaration::evaluate() const
{
    auto style_property = parse_css_supports_condition({}, declaration);
    return style_property.has_value();
}

bool Supports::Selector::evaluate() const
{
    auto style_property = parse_selector({}, selector);
    return style_property.has_value();
}

bool Supports::Feature::evaluate() const
{
    return value.visit(
        [&](Declaration const& declaration) {
            return declaration.evaluate();
        },
        [&](Selector const& selector) {
            return selector.evaluate();
        });
}

ErrorOr<String> Supports::Declaration::to_string() const
{
    return String::formatted("({})", declaration);
}

ErrorOr<String> Supports::Selector::to_string() const
{
    return String::formatted("selector({})", selector);
}

ErrorOr<String> Supports::Feature::to_string() const
{
    return value.visit([](auto& it) { return it.to_string(); });
}

ErrorOr<String> Supports::InParens::to_string() const
{
    return value.visit(
        [](NonnullOwnPtr<Condition> const& condition) -> ErrorOr<String> { return String::formatted("({})", TRY(condition->to_string())); },
        [](Supports::Feature const& it) -> ErrorOr<String> { return it.to_string(); },
        [](GeneralEnclosed const& it) -> ErrorOr<String> { return it.to_string(); });
}

ErrorOr<String> Supports::Condition::to_string() const
{
    switch (type) {
    case Type::Not:
        return String::formatted("not {}", TRY(children.first().to_string()));
    case Type::And:
        return String::join(" and "sv, children);
    case Type::Or:
        return String::join(" or "sv, children);
    }
    VERIFY_NOT_REACHED();
}

ErrorOr<String> Supports::to_string() const
{
    return m_condition->to_string();
}

}