summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/CSS/MediaQuery.h
blob: 845918cedf4deaa3bf8e26c128795df23f0dfe7e (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
118
119
120
121
122
123
124
125
126
/*
 * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/FlyString.h>
#include <AK/NonnullOwnPtrVector.h>
#include <AK/NonnullRefPtr.h>
#include <AK/Optional.h>
#include <AK/OwnPtr.h>
#include <AK/RefCounted.h>
#include <LibWeb/CSS/StyleValue.h>

namespace Web::CSS {

class MediaQuery : public RefCounted<MediaQuery> {
    friend class Parser;

public:
    ~MediaQuery() = default;

    // https://www.w3.org/TR/mediaqueries-4/#media-types
    enum class MediaType {
        All,
        Print,
        Screen,

        // Deprecated, must never match:
        TTY,
        TV,
        Projection,
        Handheld,
        Braille,
        Embossed,
        Aural,
        Speech,
    };

    // https://www.w3.org/TR/mediaqueries-4/#mq-features
    struct MediaFeature {
        // FIXME: Implement range syntax: https://www.w3.org/TR/mediaqueries-4/#mq-ranges
        enum class Type {
            IsTrue,
            ExactValue,
            MinValue,
            MaxValue,
        };

        Type type;
        FlyString name;
        RefPtr<StyleValue> value { nullptr };

        bool evaluate(DOM::Window const&) const;
        String to_string() const;
    };

    // https://www.w3.org/TR/mediaqueries-4/#media-conditions
    struct MediaCondition {
        enum class Type {
            Single,
            And,
            Or,
            Not,
        };

        Type type;
        MediaFeature feature;
        NonnullOwnPtrVector<MediaCondition> conditions;

        bool evaluate(DOM::Window const&) const;
        String to_string() const;
    };

    static NonnullRefPtr<MediaQuery> create_not_all();
    static NonnullRefPtr<MediaQuery> create() { return adopt_ref(*new MediaQuery); }

    bool matches() const { return m_matches; }
    bool evaluate(DOM::Window const&);
    String to_string() const;

private:
    MediaQuery() = default;

    // https://www.w3.org/TR/mediaqueries-4/#mq-not
    bool m_negated { false };
    MediaType m_media_type { MediaType::All };
    OwnPtr<MediaCondition> m_media_condition { nullptr };

    // Cached value, updated by evaluate()
    bool m_matches { false };
};

String serialize_a_media_query_list(NonnullRefPtrVector<MediaQuery> const&);

}

namespace AK {

template<>
struct Formatter<Web::CSS::MediaQuery::MediaFeature> : Formatter<StringView> {
    ErrorOr<void> format(FormatBuilder& builder, Web::CSS::MediaQuery::MediaFeature const& media_feature)
    {
        return Formatter<StringView>::format(builder, media_feature.to_string());
    }
};

template<>
struct Formatter<Web::CSS::MediaQuery::MediaCondition> : Formatter<StringView> {
    ErrorOr<void> format(FormatBuilder& builder, Web::CSS::MediaQuery::MediaCondition const& media_condition)
    {
        return Formatter<StringView>::format(builder, media_condition.to_string());
    }
};

template<>
struct Formatter<Web::CSS::MediaQuery> : Formatter<StringView> {
    ErrorOr<void> format(FormatBuilder& builder, Web::CSS::MediaQuery const& media_query)
    {
        return Formatter<StringView>::format(builder, media_query.to_string());
    }
};

}