LibWeb/HTML: Implement TextTrackList IDL interface

(cherry picked from commit ecad28657a0941c99870bdb72f3a6f09b6db6ee3)
This commit is contained in:
Jamie Mansfield 2024-06-09 11:07:41 +01:00 committed by Nico Weber
parent 894567cb44
commit a09b52e40f
10 changed files with 177 additions and 0 deletions

View file

@ -86,6 +86,7 @@ static bool is_platform_object(Type const& type)
"Table"sv,
"Text"sv,
"TextMetrics"sv,
"TextTrack"sv,
"URLSearchParams"sv,
"VideoTrack"sv,
"VideoTrackList"sv,

View file

@ -164,6 +164,7 @@ source_set("HTML") {
"TagNames.cpp",
"TextMetrics.cpp",
"TextTrack.cpp",
"TextTrackList.cpp",
"TimeRanges.cpp",
"Timer.cpp",
"ToggleEvent.cpp",

View file

@ -225,6 +225,7 @@ standard_idl_files = [
"//Userland/Libraries/LibWeb/HTML/SubmitEvent.idl",
"//Userland/Libraries/LibWeb/HTML/TextMetrics.idl",
"//Userland/Libraries/LibWeb/HTML/TextTrack.idl",
"//Userland/Libraries/LibWeb/HTML/TextTrackList.idl",
"//Userland/Libraries/LibWeb/HTML/TimeRanges.idl",
"//Userland/Libraries/LibWeb/HTML/ToggleEvent.idl",
"//Userland/Libraries/LibWeb/HTML/TrackEvent.idl",

View file

@ -324,6 +324,7 @@ TextDecoder
TextEncoder
TextMetrics
TextTrack
TextTrackList
TimeRanges
ToggleEvent
TrackEvent

View file

@ -431,6 +431,7 @@ set(SOURCES
HTML/TagNames.cpp
HTML/TextMetrics.cpp
HTML/TextTrack.cpp
HTML/TextTrackList.cpp
HTML/Timer.cpp
HTML/TimeRanges.cpp
HTML/ToggleEvent.cpp

View file

@ -466,6 +466,7 @@ class Storage;
class SubmitEvent;
class TextMetrics;
class TextTrack;
class TextTrackList;
class Timer;
class TimeRanges;
class ToggleEvent;

View file

@ -0,0 +1,111 @@
/*
* 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 {
JS_DEFINE_ALLOCATOR(TextTrackList);
TextTrackList::TextTrackList(JS::Realm& realm)
: DOM::EventTarget(realm, MayInterfereWithIndexedPropertyAccess::Yes)
{
}
TextTrackList::~TextTrackList() = default;
void TextTrackList::initialize(JS::Realm& realm)
{
Base::initialize(realm);
WEB_SET_PROTOTYPE_FOR_INTERFACE(TextTrackList);
}
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
JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> TextTrackList::internal_get_own_property(const JS::PropertyKey& property_name) const
{
// 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
JS::GCPtr<TextTrack> TextTrackList::get_track_by_id(StringView id) const
{
// 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);
}
}

View file

@ -0,0 +1,46 @@
/*
* 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/TextTrack.h>
namespace Web::HTML {
class TextTrackList final : public DOM::EventTarget {
WEB_PLATFORM_OBJECT(TextTrackList, DOM::EventTarget);
JS_DECLARE_ALLOCATOR(TextTrackList);
public:
virtual ~TextTrackList() override;
size_t length() const;
JS::GCPtr<TextTrack> get_track_by_id(StringView id) const;
void set_onchange(WebIDL::CallbackType*);
WebIDL::CallbackType* onchange();
void set_onaddtrack(WebIDL::CallbackType*);
WebIDL::CallbackType* onaddtrack();
void set_onremovetrack(WebIDL::CallbackType*);
WebIDL::CallbackType* onremovetrack();
private:
TextTrackList(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<TextTrack>> m_text_tracks;
};
}

View file

@ -0,0 +1,13 @@
#import <DOM/EventTarget.idl>
// https://html.spec.whatwg.org/multipage/media.html#texttracklist
[Exposed=Window]
interface TextTrackList : EventTarget {
readonly attribute unsigned long length;
getter TextTrack (unsigned long index);
TextTrack? getTrackById(DOMString id);
attribute EventHandler onchange;
attribute EventHandler onaddtrack;
attribute EventHandler onremovetrack;
};

View file

@ -210,6 +210,7 @@ libweb_js_bindings(HTML/Storage)
libweb_js_bindings(HTML/SubmitEvent)
libweb_js_bindings(HTML/TextMetrics)
libweb_js_bindings(HTML/TextTrack)
libweb_js_bindings(HTML/TextTrackList)
libweb_js_bindings(HTML/TimeRanges)
libweb_js_bindings(HTML/ToggleEvent)
libweb_js_bindings(HTML/TrackEvent)