summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/CSS/Supports.h
blob: 6fe96351ce9c2aa2cb850e3c157c9d07277fbbdf (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
/*
 * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/NonnullOwnPtr.h>
#include <AK/RefCounted.h>
#include <AK/String.h>
#include <AK/Variant.h>
#include <AK/Vector.h>
#include <LibWeb/CSS/Parser/StyleDeclarationRule.h>

namespace Web::CSS {

class Supports final : public RefCounted<Supports> {
    friend class Parser;

private:
    enum class MatchResult {
        False,
        True,
        Unknown,
    };

    static MatchResult negate(MatchResult value)
    {
        switch (value) {
        case MatchResult::False:
            return MatchResult::True;
        case MatchResult::True:
            return MatchResult::False;
        case MatchResult::Unknown:
            return MatchResult::Unknown;
        }
        VERIFY_NOT_REACHED();
    }

public:
    struct GeneralEnclosed {
    };

    struct Feature {
        // FIXME: Using this internal parser class is a bit of a hack.
        StyleDeclarationRule declaration;
        MatchResult evaluate() const;
    };

    struct Condition;
    struct InParens {
        Variant<NonnullOwnPtr<Condition>, Feature, GeneralEnclosed> value;

        MatchResult evaluate() const;
    };

    struct Condition {
        enum class Type {
            Not,
            And,
            Or,
        };
        Type type;
        Vector<InParens> children;

        MatchResult evaluate() const;
    };

    static NonnullRefPtr<Supports> create(NonnullOwnPtr<Condition>&& condition)
    {
        return adopt_ref(*new Supports(move(condition)));
    }

    bool matches() const { return m_matches; }

private:
    Supports(NonnullOwnPtr<Condition>&&);

    NonnullOwnPtr<Condition> m_condition;
    bool m_matches { false };
};

}