| 
									
										
										
										
											2024-06-09 11:07:41 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/Realm.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Bindings/Intrinsics.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Bindings/TextTrackListPrototype.h>
 | 
					
						
							|  |  |  | #include <LibWeb/HTML/EventNames.h>
 | 
					
						
							|  |  |  | #include <LibWeb/HTML/TextTrackList.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web::HTML { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | GC_DEFINE_ALLOCATOR(TextTrackList); | 
					
						
							| 
									
										
										
										
											2024-06-09 11:07:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | TextTrackList::TextTrackList(JS::Realm& realm) | 
					
						
							|  |  |  |     : DOM::EventTarget(realm, MayInterfereWithIndexedPropertyAccess::Yes) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TextTrackList::~TextTrackList() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextTrackList::initialize(JS::Realm& realm) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     WEB_SET_PROTOTYPE_FOR_INTERFACE(TextTrackList); | 
					
						
							| 
									
										
										
										
											2025-04-20 16:22:57 +02:00
										 |  |  |     Base::initialize(realm); | 
					
						
							| 
									
										
										
										
											2024-06-09 11:07:41 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextTrackList::visit_edges(JS::Cell::Visitor& visitor) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Base::visit_edges(visitor); | 
					
						
							|  |  |  |     visitor.visit(m_text_tracks); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/media.html#dom-texttracklist-item
 | 
					
						
							| 
									
										
										
										
											2025-08-29 13:02:52 +01:00
										 |  |  | JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> TextTrackList::internal_get_own_property(JS::PropertyKey const& property_name) const | 
					
						
							| 
									
										
										
										
											2024-06-09 11:07:41 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     // To determine the value of an indexed property of a TextTrackList object for a given index index, the user
 | 
					
						
							|  |  |  |     // agent must return the indexth text track in the list represented by the TextTrackList object.
 | 
					
						
							|  |  |  |     if (property_name.is_number()) { | 
					
						
							|  |  |  |         if (auto index = property_name.as_number(); index < m_text_tracks.size()) { | 
					
						
							|  |  |  |             JS::PropertyDescriptor descriptor; | 
					
						
							|  |  |  |             descriptor.value = m_text_tracks.at(index); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return descriptor; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return Base::internal_get_own_property(property_name); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/media.html#dom-texttracklist-length
 | 
					
						
							|  |  |  | size_t TextTrackList::length() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return m_text_tracks.size(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/media.html#dom-texttracklist-gettrackbyid
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | GC::Ptr<TextTrack> TextTrackList::get_track_by_id(StringView id) const | 
					
						
							| 
									
										
										
										
											2024-06-09 11:07:41 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     // The getTrackById(id) method must return the first TextTrack in the TextTrackList object whose id
 | 
					
						
							|  |  |  |     // IDL attribute would return a value equal to the value of the id argument.
 | 
					
						
							|  |  |  |     auto it = m_text_tracks.find_if([&](auto const& text_track) { | 
					
						
							|  |  |  |         return text_track->id() == id; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // When no tracks match the given argument, the method must return null.
 | 
					
						
							|  |  |  |     if (it == m_text_tracks.end()) | 
					
						
							|  |  |  |         return nullptr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return *it; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/media.html#handler-texttracklist-onchange
 | 
					
						
							|  |  |  | void TextTrackList::set_onchange(WebIDL::CallbackType* event_handler) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     set_event_handler_attribute(HTML::EventNames::change, event_handler); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/media.html#handler-texttracklist-onchange
 | 
					
						
							|  |  |  | WebIDL::CallbackType* TextTrackList::onchange() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return event_handler_attribute(HTML::EventNames::change); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/media.html#handler-texttracklist-onaddtrack
 | 
					
						
							|  |  |  | void TextTrackList::set_onaddtrack(WebIDL::CallbackType* event_handler) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     set_event_handler_attribute(HTML::EventNames::addtrack, event_handler); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/media.html#handler-texttracklist-onaddtrack
 | 
					
						
							|  |  |  | WebIDL::CallbackType* TextTrackList::onaddtrack() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return event_handler_attribute(HTML::EventNames::addtrack); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/media.html#handler-texttracklist-onremovetrack
 | 
					
						
							|  |  |  | void TextTrackList::set_onremovetrack(WebIDL::CallbackType* event_handler) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     set_event_handler_attribute(HTML::EventNames::removetrack, event_handler); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/media.html#handler-texttracklist-onremovetrack
 | 
					
						
							|  |  |  | WebIDL::CallbackType* TextTrackList::onremovetrack() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return event_handler_attribute(HTML::EventNames::removetrack); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |