Skip to content

Truly independent web browser

LibWeb: Implement TextTrack's list of cues

Shannon Boothe6c15d8df266
diff --git a/Libraries/LibWeb/HTML/HTMLMediaElement.cpp b/Libraries/LibWeb/HTML/HTMLMediaElement.cpp
index 3b1806c83b1..7392ee3e54d 100644
--- a/Libraries/LibWeb/HTML/HTMLMediaElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLMediaElement.cpp
@@ -796,12 +796,12 @@ GC::Ref<TextTrack> HTMLMediaElement::add_text_track(Bindings::TextTrackKind kind
text_track->set_language(language);
text_track->set_readiness_state(TextTrack::ReadinessState::Loaded);
text_track->set_mode(TextTrackMode::Hidden);
- // FIXME: set text track list of cues to an empty list
// FIXME: 3. Initially, the text track list of cues is not associated with any rules for updating the text track rendering.
// When a text track cue is added to it, the text track list of cues has its rules permanently set accordingly.
- // FIXME: 4. Add the new text track to the media element's list of text tracks.
+ // 4. Add the new text track to the media element's list of text tracks.
+ m_text_tracks->add_track(text_track);
// 5. Queue a media element task given the media element to fire an event named addtrack at the media element's
// textTracks attribute's TextTrackList object, using TrackEvent, with the track attribute initialized to the new
diff --git a/Libraries/LibWeb/HTML/TextTrack.cpp b/Libraries/LibWeb/HTML/TextTrack.cpp
index 40891063ea8..89064ad09bd 100644
--- a/Libraries/LibWeb/HTML/TextTrack.cpp
+++ b/Libraries/LibWeb/HTML/TextTrack.cpp
@@ -4,10 +4,15 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/BinarySearch.h>
+#include <AK/QuickSort.h>
#include <LibGC/Heap.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/TextTrack.h>
+#include <LibWeb/HTML/TextTrackCue.h>
+#include <LibWeb/HTML/TextTrackCueList.h>
#include <LibWeb/HTML/TextTrackObserver.h>
+#include <LibWeb/WebIDL/DOMException.h>
namespace Web::HTML {
@@ -29,6 +34,96 @@ void TextTrack::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_observers);
+ visitor.visit(m_text_track_list_of_cues);
+ visitor.visit(m_cues);
+ visitor.visit(m_active_cues);
+}
+
+// https://html.spec.whatwg.org/multipage/media.html#dom-texttrack-cues
+GC::Ptr<TextTrackCueList> TextTrack::cues()
+{
+ // 1. If this's mode is the text track disabled mode, then return null.
+ if (m_mode == TextTrackMode::Disabled)
+ return nullptr;
+
+ // 2. Return a live TextTrackCueList object that represents the subset of this's text track list of cues whose end
+ // times occur at or after the earliest possible position when the script started, in text track cue order.
+ // For each TextTrack object, when an object is returned, the same TextTrackCueList object must be returned each time.
+ if (!m_cues)
+ m_cues = TextTrackCueList::create(*this, TextTrackCueList::Subset::All);
+ return m_cues;
+}
+
+// https://html.spec.whatwg.org/multipage/media.html#dom-texttrack-activecues
+GC::Ptr<TextTrackCueList> TextTrack::active_cues()
+{
+ // 1. If this's mode is the text track disabled mode, then return null.
+ if (m_mode == TextTrackMode::Disabled)
+ return nullptr;
+
+ // 2. Return a live TextTrackCueList object that represents the subset of this's text track list of cues whose active
+ // flag was set when the script started, in text track cue order.
+ // For each TextTrack object, when an object is returned, the same TextTrackCueList object must be returned each time.
+ if (!m_active_cues)
+ m_active_cues = TextTrackCueList::create(*this, TextTrackCueList::Subset::Active);
+ return m_active_cues;
+}
+
+// https://html.spec.whatwg.org/multipage/media.html#text-track-cue-order
+static bool cue_order_precedes(TextTrackCue const& a, TextTrackCue const& b)
+{
+ // Within each group, cues must be sorted by their start time, earliest first; then, any cues with the same start
+ // time must be sorted by their end time, latest first; and finally, any cues with identical end times must be
+ // sorted in the order they were last added to their respective text track list of cues, oldest first.
+ if (a.start_time() != b.start_time())
+ return a.start_time() < b.start_time();
+ if (a.end_time() != b.end_time())
+ return a.end_time() > b.end_time();
+ return a.order_added() < b.order_added();
+}
+
+// https://html.spec.whatwg.org/multipage/media.html#dom-texttrack-addcue
+void TextTrack::add_cue(GC::Ref<TextTrackCue> cue)
+{
+ // 1. Let list be this's text track list of cues.
+ // FIXME: 2. If list does not yet have any associated rules for updating the text track rendering, then associate list
+ // with the rules for updating the text track rendering appropriate to cue.
+ // FIXME: 3. If list's associated rules for updating the text track rendering are not the same rules for updating the
+ // text track rendering as appropriate for cue, then throw an "InvalidStateError" DOMException.
+
+ // 4. If the given cue is in a text track list of cues, then remove cue from that text track list of cues.
+ if (auto track = cue->track())
+ track->m_text_track_list_of_cues.remove_first_matching([&](auto const& entry) { return entry == cue; });
+
+ // 5. Add cue to list.
+ cue->set_track(this);
+ cue->set_order_added(m_next_cue_order_added++);
+ auto index = lower_bound_index(m_text_track_list_of_cues, cue, [](auto const& entry, auto const& new_cue) {
+ return cue_order_precedes(*entry, *new_cue) ? -1 : 1;
+ });
+ m_text_track_list_of_cues.insert(index, cue);
+}
+
+// https://html.spec.whatwg.org/multipage/media.html#text-track-cue-order
+void TextTrack::sort_text_track_list_of_cues()
+{
+ quick_sort(m_text_track_list_of_cues, [](auto const& a, auto const& b) {
+ return cue_order_precedes(*a, *b);
+ });
+}
+
+// https://html.spec.whatwg.org/multipage/media.html#dom-texttrack-removecue
+WebIDL::ExceptionOr<void> TextTrack::remove_cue(GC::Ref<TextTrackCue> cue)
+{
+ // 1. If the given cue is not in this's text track list of cues, then throw a "NotFoundError" DOMException.
+ auto index = m_text_track_list_of_cues.find_first_index(cue);
+ if (!index.has_value())
+ return WebIDL::NotFoundError::create("Cue is not in this text track's list of cues"_utf16);
+
+ // 2. Remove cue from this's text track list of cues.
+ m_text_track_list_of_cues.remove(*index);
+ cue->set_track(nullptr);
+ return {};
}
// https://html.spec.whatwg.org/multipage/media.html#dom-texttrack-kind
diff --git a/Libraries/LibWeb/HTML/TextTrack.h b/Libraries/LibWeb/HTML/TextTrack.h
index 9c3dd50110c..e2f20facc42 100644
--- a/Libraries/LibWeb/HTML/TextTrack.h
+++ b/Libraries/LibWeb/HTML/TextTrack.h
@@ -50,6 +50,15 @@ public:
TextTrackMode mode();
void set_mode(TextTrackMode);
+ GC::Ptr<TextTrackCueList> cues();
+ GC::Ptr<TextTrackCueList> active_cues();
+
+ void add_cue(GC::Ref<TextTrackCue>);
+ WebIDL::ExceptionOr<void> remove_cue(GC::Ref<TextTrackCue>);
+
+ ReadonlySpan<GC::Ref<TextTrackCue>> text_track_list_of_cues() const { return m_text_track_list_of_cues; }
+ void sort_text_track_list_of_cues();
+
void set_oncuechange(WebIDL::CallbackType*);
WebIDL::CallbackType* oncuechange();
@@ -74,6 +83,13 @@ private:
ReadinessState m_readiness_state { ReadinessState::NotLoaded };
+ // https://html.spec.whatwg.org/multipage/media.html#text-track-list-of-cues
+ Vector<GC::Ref<TextTrackCue>> m_text_track_list_of_cues;
+ u64 m_next_cue_order_added { 0 };
+
+ GC::Ptr<TextTrackCueList> m_cues;
+ GC::Ptr<TextTrackCueList> m_active_cues;
+
HashTable<GC::Ref<TextTrackObserver>> m_observers;
};
diff --git a/Libraries/LibWeb/HTML/TextTrack.idl b/Libraries/LibWeb/HTML/TextTrack.idl
index da1288dc0cf..8ca7edc3dff 100644
--- a/Libraries/LibWeb/HTML/TextTrack.idl
+++ b/Libraries/LibWeb/HTML/TextTrack.idl
@@ -16,11 +16,11 @@ interface TextTrack : EventTarget {
attribute TextTrackMode mode;
- [FIXME] readonly attribute TextTrackCueList? cues;
- [FIXME] readonly attribute TextTrackCueList? activeCues;
+ readonly attribute TextTrackCueList? cues;
+ readonly attribute TextTrackCueList? activeCues;
- [FIXME] undefined addCue(TextTrackCue cue);
- [FIXME] undefined removeCue(TextTrackCue cue);
+ undefined addCue(TextTrackCue cue);
+ undefined removeCue(TextTrackCue cue);
attribute EventHandler oncuechange;
};
diff --git a/Libraries/LibWeb/HTML/TextTrackCue.cpp b/Libraries/LibWeb/HTML/TextTrackCue.cpp
index 253ac19aca9..c5fc9fb8e95 100644
--- a/Libraries/LibWeb/HTML/TextTrackCue.cpp
+++ b/Libraries/LibWeb/HTML/TextTrackCue.cpp
@@ -31,6 +31,8 @@ void TextTrackCue::set_start_time(double start_time)
{
// On setting, the text track cue start time must be set to the new value, interpreted in seconds;
m_start_time = start_time;
+ if (m_track)
+ m_track->sort_text_track_list_of_cues();
// FIXME: then, if the TextTrackCue object's text track cue is in a text track's list of cues, and that text track is in a media
// element's list of text tracks, and the media element's show poster flag is not set, then run the time marches on steps
@@ -46,6 +48,8 @@ WebIDL::ExceptionOr<void> TextTrackCue::set_end_time(double end_time)
// Otherwise, the text track cue end time must be set to the new value.
m_end_time = end_time;
+ if (m_track)
+ m_track->sort_text_track_list_of_cues();
// FIXME: Then, if the TextTrackCue object's text track cue is in a text track's list of cues, and that text track is in a media
// element's list of text tracks, and the media element's show poster flag is not set, then run the time marches on steps
diff --git a/Libraries/LibWeb/HTML/TextTrackCue.h b/Libraries/LibWeb/HTML/TextTrackCue.h
index e34cdb92f01..0e20f684aae 100644
--- a/Libraries/LibWeb/HTML/TextTrackCue.h
+++ b/Libraries/LibWeb/HTML/TextTrackCue.h
@@ -23,6 +23,7 @@ public:
virtual ~TextTrackCue() override;
GC::Ptr<TextTrack> track() { return m_track; }
+ void set_track(GC::Ptr<TextTrack> track) { m_track = track; }
static constexpr size_t track_offset() { return offsetof(TextTrackCue, m_track); }
Utf16String const& id() const { return m_identifier; }
@@ -43,6 +44,12 @@ public:
WebIDL::CallbackType* onexit();
void set_onexit(WebIDL::CallbackType*);
+ bool is_active() const { return m_active; }
+ void set_active(bool active) { m_active = active; }
+
+ u64 order_added() const { return m_order_added; }
+ void set_order_added(u64 order_added) { m_order_added = order_added; }
+
protected:
TextTrackCue(GC::Ptr<TextTrack>);
virtual void visit_edges(Visitor&) override;
@@ -60,6 +67,11 @@ protected:
// https://html.spec.whatwg.org/multipage/media.html#text-track-cue-pause-on-exit-flag
bool m_pause_on_exit;
+
+ // https://html.spec.whatwg.org/multipage/media.html#text-track-cue-active-flag
+ bool m_active { false };
+
+ u64 m_order_added { 0 };
};
}
diff --git a/Libraries/LibWeb/HTML/TextTrackCueList.cpp b/Libraries/LibWeb/HTML/TextTrackCueList.cpp
index f5400c9e2da..efae472dac9 100644
--- a/Libraries/LibWeb/HTML/TextTrackCueList.cpp
+++ b/Libraries/LibWeb/HTML/TextTrackCueList.cpp
@@ -4,15 +4,24 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <LibGC/Heap.h>
#include <LibWeb/HTML/EventNames.h>
+#include <LibWeb/HTML/TextTrack.h>
#include <LibWeb/HTML/TextTrackCueList.h>
namespace Web::HTML {
GC_DEFINE_ALLOCATOR(TextTrackCueList);
-TextTrackCueList::TextTrackCueList()
+GC::Ref<TextTrackCueList> TextTrackCueList::create(GC::Ref<TextTrack> track, Subset subset)
+{
+ return GC::Heap::the().allocate<TextTrackCueList>(track, subset);
+}
+
+TextTrackCueList::TextTrackCueList(GC::Ref<TextTrack> track, Subset subset)
: DOM::EventTarget()
+ , m_track(track)
+ , m_subset(subset)
{
}
@@ -21,24 +30,47 @@ TextTrackCueList::~TextTrackCueList() = default;
void TextTrackCueList::visit_edges(JS::Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
- visitor.visit(m_cues);
+ visitor.visit(m_track);
+}
+
+// FIXME: The cues list should only contain cues whose end times occur at or after the earliest possible position when
+// the script started, and the active cues list should use the active flags as they were when the script started.
+template<typename Callback>
+void TextTrackCueList::for_each_cue(Callback callback) const
+{
+ for (auto const& cue : m_track->text_track_list_of_cues()) {
+ if (m_subset == Subset::Active && !cue->is_active())
+ continue;
+ if (callback(cue) == IterationDecision::Break)
+ return;
+ }
}
GC::Ptr<TextTrackCue> TextTrackCueList::item(size_t index) 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 (index >= m_cues.size())
- return nullptr;
-
- return m_cues.at(index);
+ GC::Ptr<TextTrackCue> result;
+ for_each_cue([&](auto const& cue) {
+ if (index-- == 0) {
+ result = cue;
+ return IterationDecision::Break;
+ }
+ return IterationDecision::Continue;
+ });
+ return result;
}
// 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();
+ size_t length = 0;
+ for_each_cue([&](auto const&) {
+ ++length;
+ return IterationDecision::Continue;
+ });
+ return length;
}
// https://html.spec.whatwg.org/multipage/media.html#dom-texttrackcuelist-getcuebyid
@@ -50,14 +82,15 @@ GC::Ptr<TextTrackCue> TextTrackCueList::get_cue_by_id(Utf16View id) const
if (id.is_empty())
return nullptr;
- auto it = m_cues.find_if([&](auto const& cue) {
- return cue->id() == id;
+ GC::Ptr<TextTrackCue> result;
+ for_each_cue([&](auto const& cue) {
+ if (cue->id() == id) {
+ result = cue;
+ return IterationDecision::Break;
+ }
+ return IterationDecision::Continue;
});
-
- if (it == m_cues.end())
- return nullptr;
-
- return *it;
+ return result;
}
}
diff --git a/Libraries/LibWeb/HTML/TextTrackCueList.h b/Libraries/LibWeb/HTML/TextTrackCueList.h
index 4229a7eb309..2e0dff4fea3 100644
--- a/Libraries/LibWeb/HTML/TextTrackCueList.h
+++ b/Libraries/LibWeb/HTML/TextTrackCueList.h
@@ -19,6 +19,12 @@ class TextTrackCueList final : public DOM::EventTarget {
GC_DECLARE_ALLOCATOR(TextTrackCueList);
public:
+ enum class Subset {
+ All,
+ Active,
+ };
+
+ static GC::Ref<TextTrackCueList> create(GC::Ref<TextTrack>, Subset);
virtual ~TextTrackCueList() override;
size_t length() const;
@@ -27,10 +33,14 @@ public:
GC::Ptr<TextTrackCue> get_cue_by_id(Utf16View id) const;
private:
- TextTrackCueList();
+ TextTrackCueList(GC::Ref<TextTrack>, Subset);
virtual void visit_edges(Visitor&) override;
- Vector<GC::Ref<TextTrackCue>> m_cues;
+ template<typename Callback>
+ void for_each_cue(Callback) const;
+
+ GC::Ref<TextTrack> m_track;
+ Subset m_subset;
};
}
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrack/addCue.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrack/addCue.txt
new file mode 100644
index 00000000000..bcca498d0f4
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrack/addCue.txt
@@ -0,0 +1,11 @@
+Harness status: OK
+
+Found 5 tests
+
+4 Pass
+1 Fail
+Pass TextTrack.addCue(), adding a cue to two different tracks
+Pass TextTrack.addCue(), adding a cue to a track twice
+Pass TextTrack.addCue(), adding a removed cue to a different track
+Pass TextTrack.addCue(), adding an associated but removed cue to the same track
+Fail TextTrack.addCue(), adding a cue associated with a track element to other track
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrack/cues.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrack/cues.txt
new file mode 100644
index 00000000000..f13c3e599db
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrack/cues.txt
@@ -0,0 +1,11 @@
+Harness status: OK
+
+Found 5 tests
+
+4 Pass
+1 Fail
+Pass TextTrack.cues, empty list
+Pass TextTrack.cues, after addCue()
+Pass TextTrack.cues, different modes
+Pass TextTrack.cues, changing order
+Fail TextTrack.cues, default attribute
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrack/oncuechange.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrack/oncuechange.txt
new file mode 100644
index 00000000000..da85befc33e
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrack/oncuechange.txt
@@ -0,0 +1,7 @@
+Harness status: OK
+
+Found 2 tests
+
+2 Pass
+Pass TextTrack.oncuechange
+Pass TextTrack.addEventListener/removeEventListener
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrack/removeCue.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrack/removeCue.txt
new file mode 100644
index 00000000000..8473d583fcc
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrack/removeCue.txt
@@ -0,0 +1,8 @@
+Harness status: OK
+
+Found 2 tests
+
+1 Pass
+1 Fail
+Pass TextTrack.removeCue(), two elementless tracks
+Fail TextTrack.removeCue(), cue from track element
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrackCueList/getCueById.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrackCueList/getCueById.txt
new file mode 100644
index 00000000000..886d9b1936c
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrackCueList/getCueById.txt
@@ -0,0 +1,9 @@
+Harness status: OK
+
+Found 4 tests
+
+4 Pass
+Pass TextTrackCueList.getCueById, no id
+Pass TextTrackCueList.getCueById, id foo
+Pass TextTrackCueList.getCueById, no 1
+Pass TextTrackCueList.getCueById, id a\u0000b
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrackCueList/getter.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrackCueList/getter.txt
new file mode 100644
index 00000000000..6aafb94369c
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrackCueList/getter.txt
@@ -0,0 +1,8 @@
+Harness status: OK
+
+Found 3 tests
+
+3 Pass
+Pass TextTrackCueList getter
+Pass TextTrackCueList getter, no indexed set/create
+Pass TextTrackCueList getter, no indexed set/create (strict)
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrackCueList/length.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrackCueList/length.txt
new file mode 100644
index 00000000000..84c0dabfeb3
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrackCueList/length.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass TextTrackCueList.length
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrack/addCue.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrack/addCue.html
new file mode 100644
index 00000000000..33958bc686b
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrack/addCue.html
@@ -0,0 +1,68 @@
+<!doctype html>
+<title>TextTrack.addCue()</title>
+<script src=../../../../../../resources/testharness.js></script>
+<script src=../../../../../../resources/testharnessreport.js></script>
+<div id=log></div>
+<script>
+setup(function(){
+ window.video = document.createElement('video');
+ document.body.appendChild(video);
+});
+test(function() {
+ var t1 = video.addTextTrack('subtitles');
+ var t2 = video.addTextTrack('subtitles');
+ var c1 = new VTTCue(0, 1, 'text1');
+ t1.addCue(c1);
+ t2.addCue(c1);
+ assert_equals(c1.track, t2);
+}, document.title+', adding a cue to two different tracks');
+test(function() {
+ var t1 = video.addTextTrack('subtitles');
+ var c1 = new VTTCue(0, 1, 'text1');
+ t1.addCue(c1);
+ assert_equals(c1.track, t1);
+ t1.addCue(c1);
+ assert_equals(c1.track, t1);
+}, document.title+', adding a cue to a track twice');
+test(function() {
+ var t1 = video.addTextTrack('subtitles');
+ var t2 = video.addTextTrack('subtitles');
+ var c1 = new VTTCue(0, 1, 'text1');
+ t1.addCue(c1);
+ assert_equals(c1.track, t1);
+ t1.removeCue(c1);
+ assert_equals(c1.track, null);
+ t2.addCue(c1);
+ assert_equals(c1.track, t2);
+}, document.title+', adding a removed cue to a different track');
+test(function() {
+ var t1 = video.addTextTrack('subtitles');
+ var c1 = new VTTCue(0, 1, 'text1');
+ t1.addCue(c1);
+ assert_equals(t1.cues.length, 1, 't1.cues.length after first addition');
+ t1.removeCue(c1);
+ assert_equals(t1.cues.length, 0, 't1.cues.length after removal');
+ t1.addCue(c1);
+ assert_equals(t1.cues.length, 1, 't1.cues.length after second addition');
+}, document.title+', adding an associated but removed cue to the same track');
+
+var t = async_test(document.title+', adding a cue associated with a track element to other track');
+t.step(function(){
+ var t1 = video.addTextTrack('subtitles');
+ var track = document.createElement('track');
+ track.onload = t.step_func(function(){
+ var cue = track.track.cues[0];
+ track.track.removeCue(cue);
+ t1.addCue(cue);
+ assert_equals(cue.track, t1);
+ t.done();
+ });
+ track.onerror = t.step_func(function() {
+ assert_unreached('got error event');
+ });
+ track.src= 'data:text/vtt,'+encodeURIComponent('WEBVTT\n\n00:00:00.000 --> 00:00:01.000\ntest\n');
+ track.kind = 'subtitles';
+ track.track.mode = 'hidden';
+ video.appendChild(track);
+});
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrack/cues.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrack/cues.html
new file mode 100644
index 00000000000..cb298a0057b
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrack/cues.html
@@ -0,0 +1,100 @@
+<!doctype html>
+<title>TextTrack.cues</title>
+<script src=../../../../../../resources/testharness.js></script>
+<script src=../../../../../../resources/testharnessreport.js></script>
+<div id=log></div>
+<script>
+test(function(){
+ var video = document.createElement('video');
+ var t1 = video.addTextTrack('subtitles');
+ assert_equals(t1.cues, t1.cues, 't1.cues should return same object');
+ assert_not_equals(t1.cues, null, 't1.cues should not be null');
+ assert_true(t1.cues instanceof TextTrackCueList, 't1.cues instanceof TextTrackCueList');
+ assert_equals(t1.cues.length, 0, 't1.cues.length');
+}, document.title+', empty list');
+
+function addCue(texttrack, start, end, text, id) {
+ var c = new VTTCue(start, end, text);
+ c.id = id;
+ texttrack.addCue(c);
+ return c;
+}
+
+test(function(){
+ var video = document.createElement('video');
+ var t1 = video.addTextTrack('subtitles');
+ var t1_cues = t1.cues;
+ var c = addCue(t1, 0, 1, 'text', 'id');
+ assert_equals(t1.cues, t1_cues, "t1.cues should return same object");
+ assert_equals(t1.cues.length, 1, "t1.cues.length");
+ var c2 = addCue(t1, 1, 2, 'text2', 'id2');
+ assert_equals(t1.cues, t1_cues, "t1.cues should return the same object after adding a second cue");
+ assert_equals(t1.cues.length, 2, "t1.cues.length after adding a second cue");
+ assert_equals(t1.cues[0].id, "id");
+ assert_equals(t1.cues[1].id, "id2");
+}, document.title+', after addCue()');
+
+test(function(){
+ var video = document.createElement('video');
+ var t1 = video.addTextTrack('subtitles');
+ var t1_cues = t1.cues;
+ var c = addCue(t1, 0, 1, 'text', 'id');
+ var c2 = addCue(t1, 1, 2, 'text2', 'id2');
+ t1.mode = 'showing';
+ assert_equals(t1.cues, t1_cues, "t1.cues should return the same object after setting mode to 'showing'");
+ t1.mode = 'hidden';
+ assert_equals(t1.cues, t1_cues, "t1.cues should return the same object after setting mode to 'hidden'");
+ t1.mode = 'disabled';
+ assert_equals(t1.cues, null, "t1.cues should be null when mode is 'disabled'");
+ assert_equals(t1_cues.length, 2, "t1_cues should still be intact after setting mode to 'disabled'");
+ assert_equals(t1_cues[0].id, "id", "t1_cues first cue should still be intact after setting mode to 'disabled'");
+ assert_equals(t1_cues[1].id, "id2", "t1_cues second cue should still be intact after setting mode to 'disabled'");
+ t1.mode = 'hidden';
+ assert_equals(t1.cues, t1_cues, "t1.cues should return the same object after setting mode to 'disabled' and then 'hidden'");
+ t1.mode = 'disabled';
+ assert_equals(t1.cues, null, "t1.cues should be null when mode is set to 'disabled' again");
+ assert_equals(t1_cues.length, 2, "t1_cues should still be intact after setting mode to 'disabled' again");
+ assert_equals(t1_cues[0].id, "id", "t1_cues first cue should still be intact after setting mode to 'disabled' again");
+ assert_equals(t1_cues[1].id, "id2", "t1_cues second cue should still be intact after setting mode to 'disabled' again");
+ t1.mode = 'showing';
+ assert_equals(t1.cues, t1_cues, "t1.cues should return the same object after setting mode to 'disabled' and then 'showing'");
+}, document.title+', different modes');
+
+test(function(){
+ var video = document.createElement('video');
+ var t1 = video.addTextTrack('subtitles');
+ var t1_cues = t1.cues;
+ var c = addCue(t1, 0, 1, 'text', 'id');
+ var c2 = addCue(t1, 1, 2, 'text2', 'id2');
+ t1.mode = 'showing';
+ t1.cues[1].startTime = 0; // this should change the text track cue order
+ assert_equals(t1.cues[0].id, 'id2');
+ assert_equals(t1.cues[1].id, 'id');
+ t1.cues[0].startTime = 0.5; // this should change it back
+ assert_equals(t1.cues[0].id, 'id');
+ assert_equals(t1.cues[1].id, 'id2');
+}, document.title+', changing order');
+
+async_test(function(){
+ var video = document.createElement('video');
+ var t1 = video.addTextTrack('subtitles');
+ var t1_cues = t1.cues;
+ t1.mode = 'hidden';
+ var track = document.createElement('track');
+ track['default'] = true;
+ video.appendChild(track); // queues a task to "honor user preferences...", media element event task source
+ var t2 = track.track;
+ assert_equals(t2.cues, null, 't2.cues should be null');
+ // We need to wait until the "honor user preferences..." steps have run so we invoke play()
+ // which queues an event with the same task source.
+ video.onplay = this.step_func(function(){
+ assert_equals(t2.cues, t2.cues, 't2.cues should return same object');
+ assert_not_equals(t1.cues, t2.cues, 't1.cues and t2.cues should be different objects');
+ assert_not_equals(t2.cues, null, 't2.cues should not be null');
+ assert_true(t2.cues instanceof TextTrackCueList, 't2.cues instanceof TextTrackCueList');
+ assert_equals(t2.cues.length, 0, 't2.cues should have length 0');
+ this.done();
+ });
+ video.play(); // queues a task to fire 'play', media element event task source
+}, document.title+', default attribute');
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrack/oncuechange.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrack/oncuechange.html
new file mode 100644
index 00000000000..43295d04909
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrack/oncuechange.html
@@ -0,0 +1,33 @@
+<!doctype html>
+<title>TextTrack.oncuechange</title>
+<script src=../../../../../../resources/testharness.js></script>
+<script src=../../../../../../resources/testharnessreport.js></script>
+<div id=log></div>
+<script>
+setup(function(){
+ window.video = document.createElement('video');
+ window.t1 = video.addTextTrack('subtitles');
+ window.ev = new Event('cuechange');
+ window.ran = false;
+ window.cb = function() { ran = true; };
+});
+test(function(){
+ assert_equals(t1.oncuechange, null);
+ t1.oncuechange = cb;
+ t1.dispatchEvent(ev);
+ assert_true(ran);
+ t1.oncuechange = null;
+ ran = false;
+ t1.dispatchEvent(ev);
+ assert_false(ran);
+});
+test(function(){
+ t1.addEventListener('cuechange', cb, false);
+ t1.dispatchEvent(ev);
+ assert_true(ran);
+ t1.removeEventListener('cuechange', cb, false);
+ ran = false;
+ t1.dispatchEvent(ev);
+ assert_false(ran);
+}, 'TextTrack.addEventListener/removeEventListener');
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrack/removeCue.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrack/removeCue.html
new file mode 100644
index 00000000000..09a8733ed65
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrack/removeCue.html
@@ -0,0 +1,50 @@
+<!doctype html>
+<title>TextTrack.removeCue()</title>
+<script src=../../../../../../resources/testharness.js></script>
+<script src=../../../../../../resources/testharnessreport.js></script>
+<div id=log></div>
+<script>
+setup(function(){
+ window.video = document.createElement('video');
+ document.body.appendChild(video);
+});
+test(function() {
+ var t1 = video.addTextTrack('subtitles');
+ var t2 = video.addTextTrack('subtitles');
+ var c1 = new VTTCue(0, 1, 'text1');
+ assert_throws_dom("NOT_FOUND_ERR", function() {
+ t1.removeCue(c1);
+ }, 'standalone');
+ t1.addCue(c1);
+ assert_throws_dom("NOT_FOUND_ERR", function() {
+ t2.removeCue(c1);
+ }, 'listed in t1, remove from t2');
+ t1.removeCue(c1);
+ assert_throws_dom("NOT_FOUND_ERR", function() {
+ t1.removeCue(c1);
+ }, 'standalone, remove from t1');
+ assert_throws_dom("NOT_FOUND_ERR", function() {
+ t2.removeCue(c1);
+ }, 'standalone, remove from t2');
+}, document.title+', two elementless tracks');
+var t = async_test(document.title+', cue from track element');
+t.step(function(){
+ var t1 = video.addTextTrack('subtitles');
+ var track = document.createElement('track');
+ track.onload = t.step_func(function(){
+ var cue = track.track.cues[0];
+ assert_throws_dom('NOT_FOUND_ERR', function() { t1.removeCue(cue); }, 'listed in track.track, remove from t1');
+ track.track.removeCue(cue);
+ assert_throws_dom('NOT_FOUND_ERR', function() { track.track.removeCue(cue); }, 'standalone, remove from track.track');
+ assert_throws_dom('NOT_FOUND_ERR', function() { t1.removeCue(cue); }, 'standalone, remove from t1');
+ t.done();
+ });
+ track.onerror = t.step_func(function() {
+ assert_unreached('got error event');
+ });
+ track.src= 'data:text/vtt,'+encodeURIComponent('WEBVTT\n\n00:00:00.000 --> 00:00:01.000\ntest\n');
+ track.kind = 'subtitles';
+ track.track.mode = 'hidden';
+ video.appendChild(track);
+});
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrackCueList/getCueById.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrackCueList/getCueById.html
new file mode 100644
index 00000000000..a667317af9c
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrackCueList/getCueById.html
@@ -0,0 +1,53 @@
+<!doctype html>
+<title>TextTrackCueList.getCueById</title>
+<script src=../../../../../../resources/testharness.js></script>
+<script src=../../../../../../resources/testharnessreport.js></script>
+<div id=log></div>
+<script>
+test(function(){
+ var video = document.createElement('video');
+ var t = video.addTextTrack('subtitles');
+ document.body.appendChild(video);
+ var cues = t.cues;
+ var c = new VTTCue(0, 1, 'text1');
+ t.addCue(c);
+ assert_equals(cues.getCueById(""), null, '""');
+ assert_equals(cues.getCueById(null), null, 'null');
+ assert_equals(cues.getCueById(undefined), null, 'undefined');
+}, document.title+ ', no id');
+test(function(){
+ var video = document.createElement('video');
+ var t = video.addTextTrack('subtitles');
+ document.body.appendChild(video);
+ var cues = t.cues;
+ var c = new VTTCue(0, 1, 'text1');
+ c.id = 'foo';
+ t.addCue(c);
+ assert_equals(cues.getCueById(""), null, '""');
+ assert_equals(cues.getCueById("foo"), c, '"foo"');
+ assert_equals(cues.getCueById({toString:function(){return "foo"}}), c, 'object');
+}, document.title+ ', id foo');
+test(function(){
+ var video = document.createElement('video');
+ var t = video.addTextTrack('subtitles');
+ document.body.appendChild(video);
+ var cues = t.cues;
+ var c = new VTTCue(0, 1, 'text1');
+ c.id = '1';
+ t.addCue(c);
+ assert_equals(cues.getCueById(""), null, '""');
+ assert_equals(cues.getCueById("1"), c, '"1"');
+ assert_equals(cues.getCueById(1), c, '1');
+}, document.title+ ', no 1');
+test(function(){
+ var video = document.createElement('video');
+ var t = video.addTextTrack('subtitles');
+ document.body.appendChild(video);
+ var cues = t.cues;
+ var c = new VTTCue(0, 1, 'text1');
+ c.id = 'a\u0000b';
+ t.addCue(c);
+ assert_equals(cues.getCueById("a\u0000b"), c, '"a\\u0000b"');
+ assert_equals(cues.getCueById("a"), null, '"a"');
+}, document.title+ ', id a\\u0000b');
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrackCueList/getter.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrackCueList/getter.html
new file mode 100644
index 00000000000..6079e2b2e5a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrackCueList/getter.html
@@ -0,0 +1,49 @@
+<!doctype html>
+<title>TextTrackCueList getter</title>
+<script src=../../../../../../resources/testharness.js></script>
+<script src=../../../../../../resources/testharnessreport.js></script>
+<div id=log></div>
+<script>
+setup(function(){
+ window.video = document.createElement('video');
+ window.t1 = video.addTextTrack('subtitles');
+ document.body.appendChild(video);
+});
+test(function(){
+ var cues = t1.cues;
+ assert_equals(cues[0], undefined, 'cues[0] before');
+ var c1 = new VTTCue(0, 1, 'text1');
+ t1.addCue(c1);
+ assert_equals(cues[0], c1, 'cues[0]');
+ assert_equals(cues[1], undefined, 'cues[1]');
+ assert_equals(cues[-1], undefined, 'cues[-1]');
+ t1.removeCue(c1);
+ assert_equals(cues[0], undefined, 'cues[0] after');
+});
+test(function(){
+ var cues = t1.cues;
+ assert_equals(cues[0], undefined);
+ cues[0] = 'foo';
+ assert_equals(cues[0], undefined);
+ var c1 = new VTTCue(0, 1, 'text1');
+ t1.addCue(c1);
+ assert_equals(cues[0], c1);
+ cues[0] = 'foo';
+ assert_equals(cues[0], c1);
+ t1.removeCue(c1);
+}, document.title+', no indexed set/create');
+test(function(){
+ 'use strict';
+ var cues = t1.cues;
+ assert_equals(cues[0], undefined);
+ assert_throws_js(TypeError, function() { cues[0] = 'foo'; });
+ assert_equals(cues[0], undefined);
+ var c1 = new VTTCue(0, 1, 'text1');
+ t1.addCue(c1);
+ assert_equals(cues[0], c1);
+ assert_throws_js(TypeError, function() { cues[0] = 'foo'; });
+ assert_equals(cues[0], c1);
+ t1.removeCue(c1);
+}, document.title+', no indexed set/create (strict)');
+
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrackCueList/length.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrackCueList/length.html
new file mode 100644
index 00000000000..7c5cc3681df
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrackCueList/length.html
@@ -0,0 +1,22 @@
+<!doctype html>
+<title>TextTrackCueList.length</title>
+<script src=../../../../../../resources/testharness.js></script>
+<script src=../../../../../../resources/testharnessreport.js></script>
+<div id=log></div>
+<script>
+setup(function(){
+ window.video = document.createElement('video');
+ window.t1 = video.addTextTrack('subtitles');
+ document.body.appendChild(video);
+});
+test(function(){
+ var cues = t1.cues;
+ assert_equals(cues.length, 0);
+ var c1 = new VTTCue(0, 1, 'text1');
+ t1.addCue(c1);
+ assert_equals(cues.length, 1);
+ t1.removeCue(c1);
+ assert_equals(cues.length, 0);
+});
+
+</script>