summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/CSS/MediaQuery.cpp
blob: a61b8259428a141a28260d3f7a3f87d40d201046 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
 * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibWeb/CSS/MediaQuery.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Window.h>

namespace Web::CSS {

NonnullRefPtr<MediaQuery> MediaQuery::create_not_all()
{
    auto media_query = new MediaQuery;
    media_query->m_negated = true;
    media_query->m_media_type = MediaType::All;

    return adopt_ref(*media_query);
}

String MediaQuery::MediaFeature::to_string() const
{
    switch (type) {
    case Type::IsTrue:
        return name;
    case Type::ExactValue:
        return String::formatted("{}:{}", name, value->to_string());
    case Type::MinValue:
        return String::formatted("min-{}:{}", name, value->to_string());
    case Type::MaxValue:
        return String::formatted("max-{}:{}", name, value->to_string());
    }

    VERIFY_NOT_REACHED();
}

bool MediaQuery::MediaFeature::evaluate(DOM::Window const& window) const
{
    auto queried_value = window.query_media_feature(name);
    if (!queried_value)
        return false;

    switch (type) {
    case Type::IsTrue:
        if (queried_value->has_number())
            return queried_value->to_number() != 0;
        if (queried_value->has_length())
            return queried_value->to_length().raw_value() != 0;
        if (queried_value->has_identifier())
            return queried_value->to_identifier() != ValueID::None;
        return false;

    case Type::ExactValue:
        return queried_value->equals(*value);

    case Type::MinValue:
        if (queried_value->has_number() && value->has_number())
            return queried_value->to_number() >= value->to_number();
        if (queried_value->has_length() && value->has_length()) {
            auto queried_length = queried_value->to_length();
            auto value_length = value->to_length();
            // FIXME: We should be checking that lengths are valid during parsing
            if (!value_length.is_absolute()) {
                dbgln("Media feature was given a non-absolute length, which is invalid! {}", value_length.to_string());
                return false;
            }
            return queried_length.absolute_length_to_px() >= value_length.absolute_length_to_px();
        }
        return false;

    case Type::MaxValue:
        if (queried_value->has_number() && value->has_number())
            return queried_value->to_number() <= value->to_number();
        if (queried_value->has_length() && value->has_length()) {
            auto queried_length = queried_value->to_length();
            auto value_length = value->to_length();
            // FIXME: We should be checking that lengths are valid during parsing
            if (!value_length.is_absolute()) {
                dbgln("Media feature was given a non-absolute length, which is invalid! {}", value_length.to_string());
                return false;
            }
            return queried_length.absolute_length_to_px() <= value_length.absolute_length_to_px();
        }
        return false;
    }

    VERIFY_NOT_REACHED();
}

String MediaQuery::MediaCondition::to_string() const
{
    StringBuilder builder;
    builder.append('(');
    switch (type) {
    case Type::Single:
        builder.append(feature.to_string());
        break;
    case Type::Not:
        builder.append("not ");
        builder.append(conditions.first().to_string());
        break;
    case Type::And:
        builder.join(" and ", conditions);
        break;
    case Type::Or:
        builder.join(" or ", conditions);
        break;
    }
    builder.append(')');
    return builder.to_string();
}

bool MediaQuery::MediaCondition::evaluate(DOM::Window const& window) const
{
    switch (type) {
    case Type::Single:
        return feature.evaluate(window);

    case Type::Not:
        return !conditions.first().evaluate(window);

    case Type::And:
        for (auto& child : conditions) {
            if (!child.evaluate(window))
                return false;
        }
        return true;

    case Type::Or:
        for (auto& child : conditions) {
            if (child.evaluate(window))
                return true;
        }
        return false;
    }

    VERIFY_NOT_REACHED();
}

String MediaQuery::to_string() const
{
    StringBuilder builder;

    if (m_negated)
        builder.append("not ");

    if (m_negated || m_media_type != MediaType::All || !m_media_condition) {
        switch (m_media_type) {
        case MediaType::All:
            builder.append("all");
            break;
        case MediaType::Aural:
            builder.append("aural");
            break;
        case MediaType::Braille:
            builder.append("braille");
            break;
        case MediaType::Embossed:
            builder.append("embossed");
            break;
        case MediaType::Handheld:
            builder.append("handheld");
            break;
        case MediaType::Print:
            builder.append("print");
            break;
        case MediaType::Projection:
            builder.append("projection");
            break;
        case MediaType::Screen:
            builder.append("screen");
            break;
        case MediaType::Speech:
            builder.append("speech");
            break;
        case MediaType::TTY:
            builder.append("tty");
            break;
        case MediaType::TV:
            builder.append("tv");
            break;
        }
        if (m_media_condition)
            builder.append(" and ");
    }

    if (m_media_condition) {
        builder.append(m_media_condition->to_string());
    }

    return builder.to_string();
}

bool MediaQuery::evaluate(DOM::Window const& window)
{
    auto matches_media = [](MediaType media) -> bool {
        switch (media) {
        case MediaType::All:
            return true;
        case MediaType::Print:
            // FIXME: Enable for printing, when we have printing!
            return false;
        case MediaType::Screen:
            // FIXME: Disable for printing, when we have printing!
            return true;
        // Deprecated, must never match:
        case MediaType::TTY:
        case MediaType::TV:
        case MediaType::Projection:
        case MediaType::Handheld:
        case MediaType::Braille:
        case MediaType::Embossed:
        case MediaType::Aural:
        case MediaType::Speech:
            return false;
        }
        VERIFY_NOT_REACHED();
    };

    bool result = matches_media(m_media_type);

    if (result && m_media_condition)
        result = m_media_condition->evaluate(window);

    m_matches = m_negated ? !result : result;
    return m_matches;
}

// https://www.w3.org/TR/cssom-1/#serialize-a-media-query-list
String serialize_a_media_query_list(NonnullRefPtrVector<MediaQuery> const& media_queries)
{
    // 1. If the media query list is empty, then return the empty string.
    if (media_queries.is_empty())
        return "";

    // 2. Serialize each media query in the list of media queries, in the same order as they
    // appear in the media query list, and then serialize the list.
    StringBuilder builder;
    builder.join(", ", media_queries);
    return builder.to_string();
}

}