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