LibWeb/HTML: Implement TextTrackCueList idl interface

This commit is contained in:
Jamie Mansfield 2024-09-20 23:06:51 +01:00 committed by Tim Ledbetter
commit cfec88feb3
Notes: github-actions[bot] 2024-09-24 22:50:04 +00:00
7 changed files with 130 additions and 0 deletions

View file

@ -345,6 +345,7 @@ TextEncoder
TextMetrics
TextTrack
TextTrackCue
TextTrackCueList
TextTrackList
TimeRanges
ToggleEvent

View file

@ -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

View file

@ -497,6 +497,7 @@ class SubmitEvent;
class TextMetrics;
class TextTrack;
class TextTrackCue;
class TextTrackCueList;
class TextTrackList;
class Timer;
class TimeRanges;

View file

@ -0,0 +1,79 @@
/*
* 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/TextTrackCueListPrototype.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/TextTrackCueList.h>
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<Optional<JS::PropertyDescriptor>> 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<TextTrackCue> 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;
}
}

View file

@ -0,0 +1,38 @@
/*
* Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Heap/MarkedVector.h>
#include <LibWeb/DOM/EventTarget.h>
#include <LibWeb/HTML/TextTrackCue.h>
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<TextTrackCue> 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<Optional<JS::PropertyDescriptor>> internal_get_own_property(JS::PropertyKey const& property_name) const override;
Vector<JS::NonnullGCPtr<TextTrackCue>> m_cues;
};
}

View file

@ -0,0 +1,9 @@
#import <HTML/TextTrackCue.idl>
// 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);
};

View file

@ -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)