summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/CSS/Supports.cpp
blob: bb96088e1026b5f80fa7f64ea8c19d0845ecc9a5 (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
/*
 * Copyright (c) 2021, 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))
{
    auto result = m_condition->evaluate();
    if (result == MatchResult::Unknown) {
        dbgln("!!! Evaluation of CSS Supports returned 'Unknown'!");
    }
    m_matches = result == MatchResult::True;
}

Supports::MatchResult Supports::Condition::evaluate() const
{
    switch (type) {
    case Type::Not:
        return negate(children.first().evaluate());
    case Type::And: {
        size_t true_results = 0;
        for (auto& child : children) {
            auto child_match = child.evaluate();
            if (child_match == MatchResult::False)
                return MatchResult::False;
            if (child_match == MatchResult::True)
                true_results++;
        }
        if (true_results == children.size())
            return MatchResult::True;
        return MatchResult::Unknown;
    }
    case Type::Or: {
        size_t false_results = 0;
        for (auto& child : children) {
            auto child_match = child.evaluate();
            if (child_match == MatchResult::True)
                return MatchResult::True;
            if (child_match == MatchResult::False)
                false_results++;
        }
        if (false_results == children.size())
            return MatchResult::False;
        return MatchResult::Unknown;
    }
    }
    VERIFY_NOT_REACHED();
}

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

Supports::MatchResult Supports::Feature::evaluate() const
{
    auto style_property = Parser({}, "").convert_to_style_property(declaration);
    if (style_property.has_value())
        return MatchResult::True;
    return MatchResult::False;
}

}