| 
									
										
										
										
											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> | 
					
						
							| 
									
										
										
										
											2023-02-20 18:56:08 +01:00
										 |  |  |  * Copyright (c) 2022-2023, Andreas Kling <kling@serenityos.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>
 | 
					
						
							| 
									
										
										
										
											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 { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-19 19:47:52 +01:00
										 |  |  | JS_DEFINE_ALLOCATOR(MediaList); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-13 13:05:26 +02:00
										 |  |  | JS::NonnullGCPtr<MediaList> MediaList::create(JS::Realm& realm, Vector<NonnullRefPtr<MediaQuery>>&& media) | 
					
						
							| 
									
										
										
										
											2022-08-08 15:32:27 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-08-13 13:05:26 +02:00
										 |  |  |     return realm.heap().allocate<MediaList>(realm, 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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-08-07 08:41:28 +02:00
										 |  |  |     Base::initialize(realm); | 
					
						
							| 
									
										
										
										
											2023-11-22 12:55:21 +13:00
										 |  |  |     set_prototype(&Bindings::ensure_web_prototype<Bindings::MediaListPrototype>(realm, "MediaList"_fly_string)); | 
					
						
							| 
									
										
										
										
											2023-01-10 06:56:59 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | { | 
					
						
							|  |  |  |     m_media.clear(); | 
					
						
							|  |  |  |     if (text.is_empty()) | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2023-02-28 16:54:25 +00:00
										 |  |  |     m_media = parse_media_query_list(Parser::ParsingContext { realm() }, text); | 
					
						
							| 
									
										
										
										
											2021-09-29 12:08:09 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-22 14:10:59 +01:00
										 |  |  | bool MediaList::is_supported_property_index(u32 index) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return index < length(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-04-22 14:10:59 +01:00
										 |  |  |     if (!is_supported_property_index(index)) | 
					
						
							| 
									
										
										
										
											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.
 | 
					
						
							| 
									
										
										
										
											2023-02-28 16:54:25 +00:00
										 |  |  |     auto m = parse_media_query(Parser::ParsingContext { 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()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-02-28 16:54:25 +00:00
										 |  |  |     auto m = parse_media_query(Parser::ParsingContext { realm() }, medium); | 
					
						
							| 
									
										
										
										
											2021-09-29 12:08:09 +01:00
										 |  |  |     if (!m) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     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
										 |  |  |     }); | 
					
						
							|  |  |  |     // FIXME: If nothing was removed, then throw a NotFoundError exception.
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-07 23:08:26 +01:00
										 |  |  | bool MediaList::evaluate(HTML::Window const& window) | 
					
						
							| 
									
										
										
										
											2021-10-03 19:39:48 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     for (auto& media : m_media) | 
					
						
							| 
									
										
										
										
											2023-03-06 14:17:01 +01:00
										 |  |  |         media->evaluate(window); | 
					
						
							| 
									
										
										
										
											2021-10-03 19:39:48 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return matches(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool MediaList::matches() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-10-23 21:05:34 +03:00
										 |  |  |     if (m_media.is_empty()) { | 
					
						
							|  |  |  |         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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-28 00:05:39 +00:00
										 |  |  | WebIDL::ExceptionOr<JS::Value> MediaList::item_value(size_t index) const | 
					
						
							| 
									
										
										
										
											2022-08-08 15:32:27 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (index >= m_media.size()) | 
					
						
							|  |  |  |         return JS::js_undefined(); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } |