2021-09-29 12:08:09 +01:00
|
|
|
/*
|
2023-08-22 12:40:18 +01:00
|
|
|
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2022-2023, Andreas Kling <andreas@ladybird.org>
|
2021-09-29 12:08:09 +01:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2022-09-24 16:34:04 -06:00
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
|
|
|
#include <LibWeb/Bindings/MediaListPrototype.h>
|
2025-03-04 14:50:11 +01:00
|
|
|
#include <LibWeb/CSS/CSSStyleSheet.h>
|
2021-09-29 12:08:09 +01:00
|
|
|
#include <LibWeb/CSS/MediaList.h>
|
|
|
|
#include <LibWeb/CSS/Parser/Parser.h>
|
2023-02-13 10:50:45 +01:00
|
|
|
#include <LibWeb/WebIDL/ExceptionOr.h>
|
2021-09-29 12:08:09 +01:00
|
|
|
|
|
|
|
namespace Web::CSS {
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DEFINE_ALLOCATOR(MediaList);
|
2023-11-19 19:47:52 +01:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<MediaList> MediaList::create(JS::Realm& realm, Vector<NonnullRefPtr<MediaQuery>>&& media)
|
2022-08-08 15:32:27 +02:00
|
|
|
{
|
2024-11-14 05:50:17 +13:00
|
|
|
return realm.create<MediaList>(realm, move(media));
|
2022-08-08 15:32:27 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 14:17:01 +01:00
|
|
|
MediaList::MediaList(JS::Realm& realm, Vector<NonnullRefPtr<MediaQuery>>&& media)
|
2024-01-09 16:05:03 -07:00
|
|
|
: Bindings::PlatformObject(realm)
|
2022-08-08 15:32:27 +02:00
|
|
|
, m_media(move(media))
|
2021-09-29 12:08:09 +01:00
|
|
|
{
|
2024-01-09 16:05:03 -07:00
|
|
|
m_legacy_platform_object_flags = LegacyPlatformObjectFlags { .supports_indexed_properties = true };
|
2021-09-29 12:08:09 +01:00
|
|
|
}
|
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
void MediaList::initialize(JS::Realm& realm)
|
2023-01-10 06:56:59 -05:00
|
|
|
{
|
2024-03-16 13:13:08 +01:00
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(MediaList);
|
2025-04-20 16:22:57 +02:00
|
|
|
Base::initialize(realm);
|
2023-01-10 06:56:59 -05:00
|
|
|
}
|
|
|
|
|
2025-03-04 14:50:11 +01:00
|
|
|
void MediaList::visit_edges(Visitor& visitor)
|
|
|
|
{
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
visitor.visit(m_associated_style_sheet);
|
|
|
|
}
|
|
|
|
|
2021-09-29 12:08:09 +01:00
|
|
|
// https://www.w3.org/TR/cssom-1/#dom-medialist-mediatext
|
2023-08-26 17:24:11 +12:00
|
|
|
String MediaList::media_text() const
|
2021-09-29 12:08:09 +01:00
|
|
|
{
|
2023-08-26 17:24:11 +12:00
|
|
|
return serialize_a_media_query_list(m_media);
|
2021-09-29 12:08:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/cssom-1/#dom-medialist-mediatext
|
2023-08-26 17:24:11 +12:00
|
|
|
void MediaList::set_media_text(StringView text)
|
2021-09-29 12:08:09 +01:00
|
|
|
{
|
2025-03-04 14:50:11 +01:00
|
|
|
ScopeGuard guard = [&] {
|
|
|
|
if (m_associated_style_sheet)
|
|
|
|
as<CSS::CSSStyleSheet>(*m_associated_style_sheet).invalidate_owners(DOM::StyleInvalidationReason::MediaListSetMediaText);
|
|
|
|
};
|
|
|
|
|
2021-09-29 12:08:09 +01:00
|
|
|
m_media.clear();
|
|
|
|
if (text.is_empty())
|
|
|
|
return;
|
2025-02-05 12:08:27 +00:00
|
|
|
m_media = parse_media_query_list(Parser::ParsingParams { realm() }, text);
|
2021-09-29 12:08:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/cssom-1/#dom-medialist-item
|
2023-08-26 17:24:11 +12:00
|
|
|
Optional<String> MediaList::item(u32 index) const
|
2021-09-29 12:08:09 +01:00
|
|
|
{
|
2024-07-25 18:30:29 +12:00
|
|
|
if (index >= m_media.size())
|
2021-09-29 12:08:09 +01:00
|
|
|
return {};
|
|
|
|
|
2023-08-26 17:24:11 +12:00
|
|
|
return m_media[index]->to_string();
|
2021-09-29 12:08:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/cssom-1/#dom-medialist-appendmedium
|
2023-08-26 17:24:11 +12:00
|
|
|
void MediaList::append_medium(StringView medium)
|
2021-09-29 12:08:09 +01:00
|
|
|
{
|
2023-02-20 18:56:08 +01:00
|
|
|
// 1. Let m be the result of parsing the given value.
|
2025-02-05 12:08:27 +00:00
|
|
|
auto m = parse_media_query(Parser::ParsingParams { realm() }, medium);
|
2023-02-20 18:56:08 +01:00
|
|
|
|
|
|
|
// 2. If m is null, then return.
|
2021-09-29 12:08:09 +01:00
|
|
|
if (!m)
|
|
|
|
return;
|
2023-02-20 18:56:08 +01:00
|
|
|
|
|
|
|
// 3. If comparing m with any of the media queries in the collection of media queries returns true, then return.
|
2023-08-22 12:40:18 +01:00
|
|
|
auto serialized = m->to_string();
|
2023-02-20 18:56:08 +01:00
|
|
|
for (auto& existing_medium : m_media) {
|
2023-08-22 12:40:18 +01:00
|
|
|
if (existing_medium->to_string() == serialized)
|
2023-02-20 18:56:08 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 4. Append m to the collection of media queries.
|
2021-09-29 12:08:09 +01:00
|
|
|
m_media.append(m.release_nonnull());
|
2025-03-04 14:50:11 +01:00
|
|
|
|
|
|
|
if (m_associated_style_sheet)
|
|
|
|
as<CSS::CSSStyleSheet>(*m_associated_style_sheet).invalidate_owners(DOM::StyleInvalidationReason::MediaListAppendMedium);
|
2021-09-29 12:08:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/cssom-1/#dom-medialist-deletemedium
|
2023-08-26 17:24:11 +12:00
|
|
|
void MediaList::delete_medium(StringView medium)
|
2021-09-29 12:08:09 +01:00
|
|
|
{
|
2025-02-05 12:08:27 +00:00
|
|
|
auto m = parse_media_query(Parser::ParsingParams { realm() }, medium);
|
2021-09-29 12:08:09 +01:00
|
|
|
if (!m)
|
|
|
|
return;
|
2025-03-04 14:50:11 +01:00
|
|
|
bool was_removed = m_media.remove_all_matching([&](auto& existing) -> bool {
|
2023-08-22 12:40:18 +01:00
|
|
|
return m->to_string() == existing->to_string();
|
2021-09-29 12:08:09 +01:00
|
|
|
});
|
2025-03-04 14:50:11 +01:00
|
|
|
if (was_removed) {
|
|
|
|
if (m_associated_style_sheet)
|
|
|
|
as<CSS::CSSStyleSheet>(*m_associated_style_sheet).invalidate_owners(DOM::StyleInvalidationReason::MediaListDeleteMedium);
|
|
|
|
}
|
2021-09-29 12:08:09 +01:00
|
|
|
// FIXME: If nothing was removed, then throw a NotFoundError exception.
|
|
|
|
}
|
|
|
|
|
2025-10-07 00:54:19 +13:00
|
|
|
bool MediaList::evaluate(DOM::Document const& document)
|
2021-10-03 19:39:48 +01:00
|
|
|
{
|
|
|
|
for (auto& media : m_media)
|
2025-10-07 00:54:19 +13:00
|
|
|
media->evaluate(document);
|
2021-10-03 19:39:48 +01:00
|
|
|
|
|
|
|
return matches();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MediaList::matches() const
|
|
|
|
{
|
2024-10-03 16:00:48 +10:00
|
|
|
if (m_media.is_empty())
|
2022-10-23 21:05:34 +03:00
|
|
|
return true;
|
|
|
|
|
2021-10-03 19:39:48 +01:00
|
|
|
for (auto& media : m_media) {
|
2023-03-06 14:17:01 +01:00
|
|
|
if (media->matches())
|
2021-10-03 19:39:48 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-07-25 18:15:51 +12:00
|
|
|
Optional<JS::Value> MediaList::item_value(size_t index) const
|
2022-08-08 15:32:27 +02:00
|
|
|
{
|
|
|
|
if (index >= m_media.size())
|
2024-07-25 18:15:51 +12:00
|
|
|
return {};
|
2023-12-01 16:45:31 +00:00
|
|
|
return JS::PrimitiveString::create(vm(), m_media[index]->to_string());
|
2022-08-08 15:32:27 +02:00
|
|
|
}
|
|
|
|
|
2021-09-29 12:08:09 +01:00
|
|
|
}
|