diff --git a/Tests/LibWeb/Text/expected/all-window-properties.txt b/Tests/LibWeb/Text/expected/all-window-properties.txt index 533ecd18bc3..8a93afca788 100644 --- a/Tests/LibWeb/Text/expected/all-window-properties.txt +++ b/Tests/LibWeb/Text/expected/all-window-properties.txt @@ -345,6 +345,7 @@ TextEncoder TextMetrics TextTrack TextTrackCue +TextTrackCueList TextTrackList TimeRanges ToggleEvent diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index 55fe2abe624..603f4bbfe51 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -463,6 +463,7 @@ set(SOURCES HTML/TextMetrics.cpp HTML/TextTrack.cpp HTML/TextTrackCue.cpp + HTML/TextTrackCueList.cpp HTML/TextTrackList.cpp HTML/Timer.cpp HTML/TimeRanges.cpp diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index 634ff8fc811..e5fd8b4e227 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -497,6 +497,7 @@ class SubmitEvent; class TextMetrics; class TextTrack; class TextTrackCue; +class TextTrackCueList; class TextTrackList; class Timer; class TimeRanges; diff --git a/Userland/Libraries/LibWeb/HTML/TextTrackCueList.cpp b/Userland/Libraries/LibWeb/HTML/TextTrackCueList.cpp new file mode 100644 index 00000000000..b39c63f2b5a --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/TextTrackCueList.cpp @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2024, Jamie Mansfield + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include +#include + +namespace Web::HTML { + +JS_DEFINE_ALLOCATOR(TextTrackCueList); + +TextTrackCueList::TextTrackCueList(JS::Realm& realm) + : DOM::EventTarget(realm, MayInterfereWithIndexedPropertyAccess::Yes) +{ +} + +TextTrackCueList::~TextTrackCueList() = default; + +void TextTrackCueList::initialize(JS::Realm& realm) +{ + Base::initialize(realm); + WEB_SET_PROTOTYPE_FOR_INTERFACE(TextTrackCueList); +} + +void TextTrackCueList::visit_edges(JS::Cell::Visitor& visitor) +{ + Base::visit_edges(visitor); + visitor.visit(m_cues); +} + +// https://html.spec.whatwg.org/multipage/media.html#dom-texttrackcuelist-item +JS::ThrowCompletionOr> TextTrackCueList::internal_get_own_property(JS::PropertyKey const& property_name) const +{ + // To determine the value of an indexed property for a given index index, the user agent must return the indexth text track cue in the list + // represented by the TextTrackCueList object. + if (property_name.is_number()) { + if (auto index = property_name.as_number(); index < m_cues.size()) { + JS::PropertyDescriptor descriptor; + descriptor.value = m_cues.at(index); + + return descriptor; + } + } + + return Base::internal_get_own_property(property_name); +} + +// https://html.spec.whatwg.org/multipage/media.html#dom-texttrackcuelist-length +size_t TextTrackCueList::length() const +{ + // The length attribute must return the number of cues in the list represented by the TextTrackCueList object. + return m_cues.size(); +} + +// https://html.spec.whatwg.org/multipage/media.html#dom-texttrackcuelist-getcuebyid +JS::GCPtr TextTrackCueList::get_cue_by_id(StringView id) const +{ + // The getCueById(id) method, when called with an argument other than the empty string, must return the first text track cue in the list + // represented by the TextTrackCueList object whose text track cue identifier is id, if any, or null otherwise. If the argument is the + // empty string, then the method must return null. + if (id.is_empty()) + return nullptr; + + auto it = m_cues.find_if([&](auto const& cue) { + return cue->id() == id; + }); + + if (it == m_cues.end()) + return nullptr; + + return *it; +} + +} diff --git a/Userland/Libraries/LibWeb/HTML/TextTrackCueList.h b/Userland/Libraries/LibWeb/HTML/TextTrackCueList.h new file mode 100644 index 00000000000..5ee286c59a4 --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/TextTrackCueList.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2024, Jamie Mansfield + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include + +namespace Web::HTML { + +// https://html.spec.whatwg.org/multipage/media.html#texttrackcuelist +class TextTrackCueList final : public DOM::EventTarget { + WEB_PLATFORM_OBJECT(TextTrackCueList, DOM::EventTarget); + JS_DECLARE_ALLOCATOR(TextTrackCueList); + +public: + virtual ~TextTrackCueList() override; + + size_t length() const; + + JS::GCPtr get_cue_by_id(StringView id) const; + +private: + TextTrackCueList(JS::Realm&); + + virtual void initialize(JS::Realm&) override; + virtual void visit_edges(Visitor&) override; + + virtual JS::ThrowCompletionOr> internal_get_own_property(JS::PropertyKey const& property_name) const override; + + Vector> m_cues; +}; + +} diff --git a/Userland/Libraries/LibWeb/HTML/TextTrackCueList.idl b/Userland/Libraries/LibWeb/HTML/TextTrackCueList.idl new file mode 100644 index 00000000000..ba08b5f6200 --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/TextTrackCueList.idl @@ -0,0 +1,9 @@ +#import + +// https://html.spec.whatwg.org/multipage/media.html#texttrackcuelist +[Exposed=Window] +interface TextTrackCueList { + readonly attribute unsigned long length; + getter TextTrackCue (unsigned long index); + TextTrackCue? getCueById(DOMString id); +}; diff --git a/Userland/Libraries/LibWeb/idl_files.cmake b/Userland/Libraries/LibWeb/idl_files.cmake index 301e045067e..f7f564122e0 100644 --- a/Userland/Libraries/LibWeb/idl_files.cmake +++ b/Userland/Libraries/LibWeb/idl_files.cmake @@ -223,6 +223,7 @@ libweb_js_bindings(HTML/SubmitEvent) libweb_js_bindings(HTML/TextMetrics) libweb_js_bindings(HTML/TextTrack) libweb_js_bindings(HTML/TextTrackCue) +libweb_js_bindings(HTML/TextTrackCueList) libweb_js_bindings(HTML/TextTrackList) libweb_js_bindings(HTML/TimeRanges) libweb_js_bindings(HTML/ToggleEvent)