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
|
/*
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/MediaListPrototype.h>
#include <LibWeb/CSS/MediaList.h>
#include <LibWeb/CSS/Parser/Parser.h>
namespace Web::CSS {
MediaList* MediaList::create(JS::Realm& realm, NonnullRefPtrVector<MediaQuery>&& media)
{
return realm.heap().allocate<MediaList>(realm, realm, move(media));
}
MediaList::MediaList(JS::Realm& realm, NonnullRefPtrVector<MediaQuery>&& media)
: Bindings::LegacyPlatformObject(realm)
, m_media(move(media))
{
}
void MediaList::initialize(JS::Realm& realm)
{
Base::initialize(realm);
set_prototype(&Bindings::ensure_web_prototype<Bindings::MediaListPrototype>(realm, "MediaList"));
}
// https://www.w3.org/TR/cssom-1/#dom-medialist-mediatext
DeprecatedString MediaList::media_text() const
{
return serialize_a_media_query_list(m_media);
}
// https://www.w3.org/TR/cssom-1/#dom-medialist-mediatext
void MediaList::set_media_text(DeprecatedString const& text)
{
m_media.clear();
if (text.is_empty())
return;
m_media = parse_media_query_list({}, text);
}
bool MediaList::is_supported_property_index(u32 index) const
{
return index < length();
}
// https://www.w3.org/TR/cssom-1/#dom-medialist-item
DeprecatedString MediaList::item(u32 index) const
{
if (!is_supported_property_index(index))
return {};
return m_media[index].to_string().release_value_but_fixme_should_propagate_errors().to_deprecated_string();
}
// https://www.w3.org/TR/cssom-1/#dom-medialist-appendmedium
void MediaList::append_medium(DeprecatedString medium)
{
auto m = parse_media_query({}, medium);
if (!m)
return;
if (m_media.contains_slow(*m))
return;
m_media.append(m.release_nonnull());
}
// https://www.w3.org/TR/cssom-1/#dom-medialist-deletemedium
void MediaList::delete_medium(DeprecatedString medium)
{
auto m = parse_media_query({}, medium);
if (!m)
return;
m_media.remove_all_matching([&](auto& existing) -> bool {
return m->to_string().release_value_but_fixme_should_propagate_errors() == existing->to_string().release_value_but_fixme_should_propagate_errors();
});
// FIXME: If nothing was removed, then throw a NotFoundError exception.
}
bool MediaList::evaluate(HTML::Window const& window)
{
for (auto& media : m_media)
media.evaluate(window);
return matches();
}
bool MediaList::matches() const
{
if (m_media.is_empty()) {
return true;
}
for (auto& media : m_media) {
if (media.matches())
return true;
}
return false;
}
JS::Value MediaList::item_value(size_t index) const
{
if (index >= m_media.size())
return JS::js_undefined();
return JS::PrimitiveString::create(vm(), m_media[index].to_string().release_value_but_fixme_should_propagate_errors().to_deprecated_string());
}
}
|