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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
|
/*
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/CSS/MediaQuery.h>
#include <LibWeb/CSS/Serialize.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 MediaFeatureValue::to_string() const
{
return m_value.visit(
[](String const& ident) { return serialize_an_identifier(ident); },
[](Length const& length) { return length.to_string(); },
[](double number) { return String::number(number); });
}
bool MediaFeatureValue::is_same_type(MediaFeatureValue const& other) const
{
return m_value.visit(
[&](String const&) { return other.is_ident(); },
[&](Length const&) { return other.is_length(); },
[&](double) { return other.is_number(); });
}
bool MediaFeatureValue::equals(MediaFeatureValue const& other) const
{
if (!is_same_type(other))
return false;
if (is_ident() && other.is_ident())
return m_value.get<String>().equals_ignoring_case(other.m_value.get<String>());
if (is_length() && other.is_length()) {
// FIXME: Handle relative lengths. https://www.w3.org/TR/mediaqueries-4/#ref-for-relative-length
auto& my_length = m_value.get<Length>();
auto& other_length = other.m_value.get<Length>();
if (!my_length.is_absolute() || !other_length.is_absolute()) {
dbgln("TODO: Support relative lengths in media queries!");
return false;
}
return my_length.absolute_length_to_px() == other_length.absolute_length_to_px();
}
if (is_number() && other.is_number())
return m_value.get<double>() == other.m_value.get<double>();
VERIFY_NOT_REACHED();
}
String MediaFeature::to_string() const
{
auto comparison_string = [](Comparison comparison) -> StringView {
switch (comparison) {
case Comparison::Equal:
return "="sv;
case Comparison::LessThan:
return "<"sv;
case Comparison::LessThanOrEqual:
return "<="sv;
case Comparison::GreaterThan:
return ">"sv;
case Comparison::GreaterThanOrEqual:
return ">="sv;
}
VERIFY_NOT_REACHED();
};
switch (m_type) {
case Type::IsTrue:
return serialize_an_identifier(m_name);
case Type::ExactValue:
return String::formatted("{}:{}", serialize_an_identifier(m_name), m_value->to_string());
case Type::MinValue:
return String::formatted("min-{}:{}", serialize_an_identifier(m_name), m_value->to_string());
case Type::MaxValue:
return String::formatted("max-{}:{}", serialize_an_identifier(m_name), m_value->to_string());
case Type::Range:
if (!m_range->right_comparison.has_value())
return String::formatted("{} {} {}", m_range->left_value.to_string(), comparison_string(m_range->left_comparison), serialize_an_identifier(m_name));
return String::formatted("{} {} {} {} {}", m_range->left_value.to_string(), comparison_string(m_range->left_comparison), serialize_an_identifier(m_name), comparison_string(*m_range->right_comparison), m_range->right_value->to_string());
}
VERIFY_NOT_REACHED();
}
bool MediaFeature::evaluate(DOM::Window const& window) const
{
auto maybe_queried_value = window.query_media_feature(m_name);
if (!maybe_queried_value.has_value())
return false;
auto queried_value = maybe_queried_value.release_value();
switch (m_type) {
case Type::IsTrue:
if (queried_value.is_number())
return queried_value.number() != 0;
if (queried_value.is_length())
return queried_value.length().raw_value() != 0;
if (queried_value.is_ident())
return queried_value.ident() != "none";
return false;
case Type::ExactValue:
return compare(*m_value, Comparison::Equal, queried_value);
case Type::MinValue:
return compare(queried_value, Comparison::GreaterThanOrEqual, *m_value);
case Type::MaxValue:
return compare(queried_value, Comparison::LessThanOrEqual, *m_value);
case Type::Range:
if (!compare(m_range->left_value, m_range->left_comparison, queried_value))
return false;
if (m_range->right_comparison.has_value())
if (!compare(queried_value, *m_range->right_comparison, *m_range->right_value))
return false;
return true;
}
VERIFY_NOT_REACHED();
}
bool MediaFeature::compare(MediaFeatureValue left, Comparison comparison, MediaFeatureValue right)
{
if (!left.is_same_type(right))
return false;
if (left.is_ident()) {
if (comparison == Comparison::Equal)
return left.ident().equals_ignoring_case(right.ident());
return false;
}
if (left.is_number()) {
switch (comparison) {
case Comparison::Equal:
return left.number() == right.number();
case Comparison::LessThan:
return left.number() < right.number();
case Comparison::LessThanOrEqual:
return left.number() <= right.number();
case Comparison::GreaterThan:
return left.number() > right.number();
case Comparison::GreaterThanOrEqual:
return left.number() >= right.number();
}
VERIFY_NOT_REACHED();
}
if (left.is_length()) {
// FIXME: Handle relative lengths. https://www.w3.org/TR/mediaqueries-4/#ref-for-relative-length
if (!left.length().is_absolute() || !right.length().is_absolute()) {
dbgln("TODO: Support relative lengths in media queries!");
return false;
}
auto left_px = left.length().absolute_length_to_px();
auto right_px = right.length().absolute_length_to_px();
switch (comparison) {
case Comparison::Equal:
return left_px == right_px;
case Comparison::LessThan:
return left_px < right_px;
case Comparison::LessThanOrEqual:
return left_px <= right_px;
case Comparison::GreaterThan:
return left_px > right_px;
case Comparison::GreaterThanOrEqual:
return left_px >= right_px;
}
VERIFY_NOT_REACHED();
}
VERIFY_NOT_REACHED();
}
NonnullOwnPtr<MediaCondition> MediaCondition::from_general_enclosed(GeneralEnclosed&& general_enclosed)
{
auto result = new MediaCondition;
result->type = Type::GeneralEnclosed;
result->general_enclosed = move(general_enclosed);
return adopt_own(*result);
}
NonnullOwnPtr<MediaCondition> MediaCondition::from_feature(MediaFeature&& feature)
{
auto result = new MediaCondition;
result->type = Type::Single;
result->feature = move(feature);
return adopt_own(*result);
}
NonnullOwnPtr<MediaCondition> MediaCondition::from_not(NonnullOwnPtr<MediaCondition>&& condition)
{
auto result = new MediaCondition;
result->type = Type::Not;
result->conditions.append(move(condition));
return adopt_own(*result);
}
NonnullOwnPtr<MediaCondition> MediaCondition::from_and_list(NonnullOwnPtrVector<MediaCondition>&& conditions)
{
auto result = new MediaCondition;
result->type = Type::And;
result->conditions = move(conditions);
return adopt_own(*result);
}
NonnullOwnPtr<MediaCondition> MediaCondition::from_or_list(NonnullOwnPtrVector<MediaCondition>&& conditions)
{
auto result = new MediaCondition;
result->type = Type::Or;
result->conditions = move(conditions);
return adopt_own(*result);
}
String 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;
case Type::GeneralEnclosed:
builder.append(general_enclosed->to_string());
break;
}
builder.append(')');
return builder.to_string();
}
MatchResult MediaCondition::evaluate(DOM::Window const& window) const
{
switch (type) {
case Type::Single:
return as_match_result(feature->evaluate(window));
case Type::Not:
return negate(conditions.first().evaluate(window));
case Type::And:
return evaluate_and(conditions, [&](auto& child) { return child.evaluate(window); });
case Type::Or:
return evaluate_or(conditions, [&](auto& child) { return child.evaluate(window); });
case Type::GeneralEnclosed:
return general_enclosed->evaluate();
}
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) -> MatchResult {
switch (media) {
case MediaType::All:
return MatchResult::True;
case MediaType::Print:
// FIXME: Enable for printing, when we have printing!
return MatchResult::False;
case MediaType::Screen:
// FIXME: Disable for printing, when we have printing!
return MatchResult::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 MatchResult::False;
}
VERIFY_NOT_REACHED();
};
MatchResult result = matches_media(m_media_type);
if ((result == MatchResult::True) && m_media_condition)
result = m_media_condition->evaluate(window);
if (m_negated)
result = negate(result);
m_matches = result == MatchResult::True;
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();
}
bool is_media_feature_name(StringView name)
{
// MEDIAQUERIES-4 - https://www.w3.org/TR/mediaqueries-4/#media-descriptor-table
if (name.equals_ignoring_case("any-hover"sv))
return true;
if (name.equals_ignoring_case("any-pointer"sv))
return true;
if (name.equals_ignoring_case("aspect-ratio"sv))
return true;
if (name.equals_ignoring_case("color"sv))
return true;
if (name.equals_ignoring_case("color-gamut"sv))
return true;
if (name.equals_ignoring_case("color-index"sv))
return true;
if (name.equals_ignoring_case("device-aspect-ratio"sv))
return true;
if (name.equals_ignoring_case("device-height"sv))
return true;
if (name.equals_ignoring_case("device-width"sv))
return true;
if (name.equals_ignoring_case("grid"sv))
return true;
if (name.equals_ignoring_case("height"sv))
return true;
if (name.equals_ignoring_case("hover"sv))
return true;
if (name.equals_ignoring_case("monochrome"sv))
return true;
if (name.equals_ignoring_case("orientation"sv))
return true;
if (name.equals_ignoring_case("overflow-block"sv))
return true;
if (name.equals_ignoring_case("overflow-inline"sv))
return true;
if (name.equals_ignoring_case("pointer"sv))
return true;
if (name.equals_ignoring_case("resolution"sv))
return true;
if (name.equals_ignoring_case("scan"sv))
return true;
if (name.equals_ignoring_case("update"sv))
return true;
if (name.equals_ignoring_case("width"sv))
return true;
// MEDIAQUERIES-5 - https://www.w3.org/TR/mediaqueries-5/#media-descriptor-table
if (name.equals_ignoring_case("prefers-color-scheme"sv))
return true;
// FIXME: Add other level 5 feature names
return false;
}
}
|