Skip to content

Truly independent web browser

LibWeb: Load WebVTT cues for track elements

Shannon Boothe4b08b736631
diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt
index 61e3a66e837..8714c155d6e 100644
--- a/Libraries/LibWeb/CMakeLists.txt
+++ b/Libraries/LibWeb/CMakeLists.txt
@@ -1076,6 +1076,7 @@ set(SOURCES
WebIDL/Promise.cpp
WebIDL/QuotaExceededError.cpp
WebSockets/WebSocket.cpp
+ WebVTT/Parser.cpp
WebVTT/VTTCue.cpp
WebVTT/VTTRegion.cpp
WebXR/XRLayer.cpp
diff --git a/Libraries/LibWeb/HTML/HTMLTrackElement.cpp b/Libraries/LibWeb/HTML/HTMLTrackElement.cpp
index 14eee22a2fb..3638eba5270 100644
--- a/Libraries/LibWeb/HTML/HTMLTrackElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLTrackElement.cpp
@@ -24,6 +24,8 @@
#include <LibWeb/HTML/TrackEvent.h>
#include <LibWeb/HighResolutionTime/TimeOrigin.h>
#include <LibWeb/Platform/EventLoopPlugin.h>
+#include <LibWeb/WebVTT/Parser.h>
+#include <LibWeb/WebVTT/VTTCue.h>
#include <LibWebCommon/Fetch/Infrastructure/HTTP/Statuses.h>
namespace Web::HTML {
@@ -67,8 +69,10 @@ void HTMLTrackElement::attribute_changed(Utf16FlyString const& name, Optional<Ut
m_track->set_language(value_or_empty);
} else if (name.equals_ignoring_ascii_case(HTML::AttributeNames::src)) {
// https://html.spec.whatwg.org/multipage/media.html#sourcing-out-of-band-text-tracks:attr-track-src
- // FIXME: Whenever a track element has its src attribute set, changed, or removed, the user agent must immediately empty the element's text track's text track list of cues.
- // (This also causes the algorithm above to stop adding cues from the resource being obtained using the previously given URL, if any.)
+ // Whenever a track element has its src attribute set, changed, or removed, the user agent must immediately empty the element's text track's text track list of cues.
+ // (This also causes the algorithm above to stop adding cues from the resource being obtained using the previously given URL, if any.)
+ m_track->empty_text_track_list_of_cues();
+ ++m_src_generation;
if (!value.has_value())
return;
@@ -265,7 +269,7 @@ void HTMLTrackElement::start_the_track_processing_model_parallel_steps()
request->set_initiator_type(Fetch::Infrastructure::Request::InitiatorType::Track);
Fetch::Infrastructure::FetchAlgorithms::Input fetch_algorithms_input {};
- fetch_algorithms_input.process_response_consume_body = [this](auto response, auto body_bytes) {
+ fetch_algorithms_input.process_response_consume_body = [this, src_generation = m_src_generation](auto response, auto body_bytes) {
m_loading = false;
// If fetching fails for any reason (network error, the server returns an error code, CORS fails, etc.),
@@ -280,19 +284,28 @@ void HTMLTrackElement::start_the_track_processing_model_parallel_steps()
// (e.g., the format in question is an XML format and the file contained a well-formedness error that XML requires be detected and reported to the application),
// then the task that is queued on the networking task source in which the aforementioned problem is found must change the text track readiness state to failed to
// load and fire an event named error at the track element.
- // FIXME: Currently we always fail here, since we don't support loading any track formats.
- track_failed_to_load();
+ // FIXME: Support text track formats other than WebVTT, and parse the file incrementally as it is received.
+ auto cues = WebVTT::parse_webvtt_file(body_bytes.template get<Core::ImmutableBytes>().bytes());
+ if (!cues.has_value()) {
+ track_failed_to_load();
+ return;
+ }
// If fetching does not fail, and the file was successfully processed, then the final task that is queued by the networking task source,
// after it has finished parsing the data, must change the text track readiness state to loaded, and fire an event named load at the track element.
- // FIXME: Enable this once we support processing track files
- if (false) {
- queue_an_element_task(Task::Source::Networking, [this]() {
- m_track->set_readiness_state(TextTrack::ReadinessState::Loaded);
- dispatch_event(DOM::Event::create(HTML::EventNames::load,
- HighResolutionTime::current_high_resolution_time(HTML::relevant_global_object(*this))));
- });
- }
+ queue_an_element_task(Task::Source::Networking, [this, cues = cues.release_value(), src_generation]() {
+ if (src_generation == m_src_generation) {
+ for (auto const& parsed_cue : cues) {
+ auto cue = MUST(WebVTT::VTTCue::create(parsed_cue.start_time, parsed_cue.end_time, parsed_cue.text));
+ cue->set_id(parsed_cue.identifier);
+ m_track->add_cue(cue);
+ }
+ }
+
+ m_track->set_readiness_state(TextTrack::ReadinessState::Loaded);
+ dispatch_event(DOM::Event::create(HTML::EventNames::load,
+ HighResolutionTime::current_high_resolution_time(HTML::relevant_global_object(*this))));
+ });
};
// 4. Fetch request.
diff --git a/Libraries/LibWeb/HTML/HTMLTrackElement.h b/Libraries/LibWeb/HTML/HTMLTrackElement.h
index 9fd7555b146..8e5d7477862 100644
--- a/Libraries/LibWeb/HTML/HTMLTrackElement.h
+++ b/Libraries/LibWeb/HTML/HTMLTrackElement.h
@@ -59,6 +59,9 @@ private:
bool m_loading { false };
bool m_awaiting_track_url_change { false };
+
+ // Incremented whenever the src attribute changes, so cues from a previously given URL are not added.
+ u64 m_src_generation { 0 };
};
}
diff --git a/Libraries/LibWeb/HTML/TextTrack.cpp b/Libraries/LibWeb/HTML/TextTrack.cpp
index 89064ad09bd..9cff8ada721 100644
--- a/Libraries/LibWeb/HTML/TextTrack.cpp
+++ b/Libraries/LibWeb/HTML/TextTrack.cpp
@@ -126,6 +126,16 @@ WebIDL::ExceptionOr<void> TextTrack::remove_cue(GC::Ref<TextTrackCue> cue)
return {};
}
+void TextTrack::empty_text_track_list_of_cues()
+{
+ if (m_text_track_list_of_cues.is_empty())
+ return;
+
+ for (auto const& cue : m_text_track_list_of_cues)
+ cue->set_track(nullptr);
+ m_text_track_list_of_cues.clear();
+}
+
// https://html.spec.whatwg.org/multipage/media.html#dom-texttrack-kind
TextTrackKind TextTrack::kind()
{
diff --git a/Libraries/LibWeb/HTML/TextTrack.h b/Libraries/LibWeb/HTML/TextTrack.h
index e2f20facc42..78ebedefdc9 100644
--- a/Libraries/LibWeb/HTML/TextTrack.h
+++ b/Libraries/LibWeb/HTML/TextTrack.h
@@ -55,6 +55,7 @@ public:
void add_cue(GC::Ref<TextTrackCue>);
WebIDL::ExceptionOr<void> remove_cue(GC::Ref<TextTrackCue>);
+ void empty_text_track_list_of_cues();
ReadonlySpan<GC::Ref<TextTrackCue>> text_track_list_of_cues() const { return m_text_track_list_of_cues; }
void sort_text_track_list_of_cues();
diff --git a/Libraries/LibWeb/WebVTT/Parser.cpp b/Libraries/LibWeb/WebVTT/Parser.cpp
new file mode 100644
index 00000000000..3141230b9a4
--- /dev/null
+++ b/Libraries/LibWeb/WebVTT/Parser.cpp
@@ -0,0 +1,498 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/CharacterTypes.h>
+#include <AK/GenericShorthands.h>
+#include <AK/Utf16StringBuilder.h>
+#include <LibWeb/WebVTT/Parser.h>
+#include <LibWebCommon/Infra/CharacterTypes.h>
+
+namespace Web::WebVTT {
+
+namespace {
+
+class Parser {
+public:
+ explicit Parser(Utf16View input)
+ : m_input(input)
+ {
+ }
+
+ Optional<Vector<ParsedCue>> parse();
+
+private:
+ bool is_past_end() const { return m_position >= m_input.length_in_code_units(); }
+ char16_t current() const { return m_input.code_unit_at(m_position); }
+
+ Utf16View collect_a_sequence_of_code_points_that_are_not_line_feeds();
+ void collect_a_sequence_of_line_feeds();
+ Optional<ParsedCue> collect_a_webvtt_block(bool in_header);
+
+ Utf16View m_input;
+ size_t m_position { 0 };
+ bool m_seen_cue { false };
+};
+
+Utf16View Parser::collect_a_sequence_of_code_points_that_are_not_line_feeds()
+{
+ auto start = m_position;
+ while (!is_past_end() && current() != '\n')
+ ++m_position;
+ return m_input.substring_view(start, m_position - start);
+}
+
+void Parser::collect_a_sequence_of_line_feeds()
+{
+ while (!is_past_end() && current() == '\n')
+ ++m_position;
+}
+
+class StringScanner {
+public:
+ explicit StringScanner(Utf16View input)
+ : m_input(input)
+ {
+ }
+
+ bool is_past_end() const { return m_position >= m_input.length_in_code_units(); }
+ char16_t current() const { return m_input.code_unit_at(m_position); }
+ void advance() { ++m_position; }
+
+ void skip_whitespace()
+ {
+ while (!is_past_end() && Infra::is_ascii_whitespace(current()))
+ ++m_position;
+ }
+
+ Utf16View collect_ascii_digits()
+ {
+ auto start = m_position;
+ while (!is_past_end() && is_ascii_digit(current()))
+ ++m_position;
+ return m_input.substring_view(start, m_position - start);
+ }
+
+ bool consume(char16_t code_unit)
+ {
+ if (is_past_end() || current() != code_unit)
+ return false;
+ ++m_position;
+ return true;
+ }
+
+private:
+ Utf16View m_input;
+ size_t m_position { 0 };
+};
+
+u64 interpret_as_base_ten_integer(Utf16View digits)
+{
+ u64 value = 0;
+ for (size_t i = 0; i < digits.length_in_code_units(); ++i)
+ value = (value * 10) + (digits.code_unit_at(i) - '0');
+ return value;
+}
+
+// https://w3c.github.io/webvtt/#collect-a-webvtt-timestamp
+Optional<double> collect_a_webvtt_timestamp(StringScanner& scanner)
+{
+ // 1. Let input and position be the same variables as those of the same name in the algorithm that invoked these
+ // steps.
+
+ // 2. Let most significant units be minutes.
+ bool most_significant_units_are_hours = false;
+
+ // 3. If position is past the end of input, return an error and abort these steps.
+ if (scanner.is_past_end())
+ return {};
+
+ // 4. If the character indicated by position is not an ASCII digit, then return an error and abort these steps.
+ if (!is_ascii_digit(scanner.current()))
+ return {};
+
+ // 5. Collect a sequence of code points that are ASCII digits, and let string be the collected substring.
+ auto string = scanner.collect_ascii_digits();
+
+ // 6. Interpret string as a base-ten integer. Let value1 be that integer.
+ auto value1 = interpret_as_base_ten_integer(string);
+
+ // 7. If string is not exactly two characters in length, or if value1 is greater than 59, let most significant units
+ // be hours.
+ if (string.length_in_code_units() != 2 || value1 > 59)
+ most_significant_units_are_hours = true;
+
+ // 8. If position is beyond the end of input or if the character at position is not a U+003A COLON character (:),
+ // then return an error and abort these steps. Otherwise, move position forwards one character.
+ if (!scanner.consume(':'))
+ return {};
+
+ // 9. Collect a sequence of code points that are ASCII digits, and let string be the collected substring.
+ string = scanner.collect_ascii_digits();
+
+ // 10. If string is not exactly two characters in length, return an error and abort these steps.
+ if (string.length_in_code_units() != 2)
+ return {};
+
+ // 11. Interpret string as a base-ten integer. Let value2 be that integer.
+ auto value2 = interpret_as_base_ten_integer(string);
+ u64 value3 = 0;
+
+ // 12. If most significant units is hours, or if position is not beyond the end of input and the character at
+ // position is a U+003A COLON character (:), run these substeps:
+ if (most_significant_units_are_hours || (!scanner.is_past_end() && scanner.current() == ':')) {
+ // 1. If position is beyond the end of input or if the character at position is not a U+003A COLON character
+ // (:), then return an error and abort these steps. Otherwise, move position forwards one character.
+ if (!scanner.consume(':'))
+ return {};
+
+ // 2. Collect a sequence of code points that are ASCII digits, and let string be the collected substring.
+ string = scanner.collect_ascii_digits();
+
+ // 3. If string is not exactly two characters in length, return an error and abort these steps.
+ if (string.length_in_code_units() != 2)
+ return {};
+
+ // 4. Interpret string as a base-ten integer. Let value3 be that integer.
+ value3 = interpret_as_base_ten_integer(string);
+ }
+ // Otherwise (if most significant units is not hours, and either position is beyond the end of input, or the
+ // character at position is not a U+003A COLON character (:)), let value3 have the value of value2, then value2 have
+ // the value of value1, then let value1 equal zero.
+ else {
+ value3 = value2;
+ value2 = value1;
+ value1 = 0;
+ }
+
+ // 13. If position is beyond the end of input or if the character at position is not a U+002E FULL STOP character
+ // (.), then return an error and abort these steps. Otherwise, move position forwards one character.
+ if (!scanner.consume('.'))
+ return {};
+
+ // 14. Collect a sequence of code points that are ASCII digits, and let string be the collected substring.
+ string = scanner.collect_ascii_digits();
+
+ // 15. If string is not exactly three characters in length, return an error and abort these steps.
+ if (string.length_in_code_units() != 3)
+ return {};
+
+ // 16. Interpret string as a base-ten integer. Let value4 be that integer.
+ auto value4 = interpret_as_base_ten_integer(string);
+
+ // 17. If value2 is greater than 59 or if value3 is greater than 59, return an error and abort these steps.
+ if (value2 > 59 || value3 > 59)
+ return {};
+
+ // 18. Let result be value1×60×60 + value2×60 + value3 + value4∕1000.
+ auto result = (static_cast<double>(value1) * 60 * 60) + (static_cast<double>(value2) * 60) + static_cast<double>(value3) + (static_cast<double>(value4) / 1000);
+
+ // 19. Return result.
+ return result;
+}
+
+// https://w3c.github.io/webvtt/#collect-webvtt-cue-timings-and-settings
+bool collect_webvtt_cue_timings_and_settings(Utf16View input, ParsedCue& cue)
+{
+ // 1. Let input be the string being parsed.
+ // 2. Let position be a pointer into input, initially pointing at the start of the string.
+ StringScanner scanner { input };
+
+ // 3. Skip whitespace.
+ scanner.skip_whitespace();
+
+ // 4. Collect a WebVTT timestamp. If that algorithm fails, then abort these steps and return failure. Otherwise, let
+ // cue's text track cue start time be the collected time.
+ auto start_time = collect_a_webvtt_timestamp(scanner);
+ if (!start_time.has_value())
+ return false;
+ cue.start_time = *start_time;
+
+ // 5. Skip whitespace.
+ scanner.skip_whitespace();
+
+ // 6. If the character at position is not a U+002D HYPHEN-MINUS character (-) then abort these steps and return
+ // failure. Otherwise, move position forwards one character.
+ // 7. If the character at position is not a U+002D HYPHEN-MINUS character (-) then abort these steps and return
+ // failure. Otherwise, move position forwards one character.
+ // 8. If the character at position is not a U+003E GREATER-THAN SIGN character (>) then abort these steps and return
+ // failure. Otherwise, move position forwards one character.
+ if (!scanner.consume('-') || !scanner.consume('-') || !scanner.consume('>'))
+ return false;
+
+ // 9. Skip whitespace.
+ scanner.skip_whitespace();
+
+ // 10. Collect a WebVTT timestamp. If that algorithm fails, then abort these steps and return failure. Otherwise, let
+ // cue's text track cue end time be the collected time.
+ auto end_time = collect_a_webvtt_timestamp(scanner);
+ if (!end_time.has_value())
+ return false;
+ cue.end_time = *end_time;
+
+ // FIXME: 11. Let remainder be the trailing substring of input starting at position.
+ // FIXME: 12. Parse the WebVTT cue settings from remainder using regions for cue.
+ return true;
+}
+
+bool is_block_keyword(Utf16View buffer, Utf16View keyword)
+{
+ if (!buffer.starts_with(keyword))
+ return false;
+ auto remainder = buffer.substring_view(keyword.length_in_code_units());
+ for (size_t i = 0; i < remainder.length_in_code_units(); ++i) {
+ if (!Infra::is_ascii_whitespace(remainder.code_unit_at(i)))
+ return false;
+ }
+ return true;
+}
+
+// https://w3c.github.io/webvtt/#collect-a-webvtt-block
+Optional<ParsedCue> Parser::collect_a_webvtt_block(bool in_header)
+{
+ // 1. Let input, position, seen cue and regions be the same variables as those of the same name in the algorithm
+ // that invoked these steps.
+
+ // 2. Let line count be zero.
+ size_t line_count = 0;
+
+ // 3. Let previous position be position.
+ auto previous_position = m_position;
+
+ // 4. Let line be the empty string.
+ Utf16View line;
+
+ // 5. Let buffer be the empty string.
+ Utf16StringBuilder buffer;
+
+ // 6. Let seen EOF be false.
+ bool seen_eof = false;
+
+ // 7. Let seen arrow be false.
+ bool seen_arrow = false;
+
+ // 8. Let cue be null.
+ Optional<ParsedCue> cue;
+
+ // FIXME: 9. Let stylesheet be null.
+ // FIXME: 10. Let region be null.
+
+ // 11. Loop: Run these substeps in a loop:
+ while (true) {
+ // 1. Collect a sequence of code points that are not U+000A LINE FEED (LF) characters. Let line be those
+ // characters, if any.
+ line = collect_a_sequence_of_code_points_that_are_not_line_feeds();
+
+ // 2. Increment line count by 1.
+ ++line_count;
+
+ // 3. If position is past the end of input, let seen EOF be true. Otherwise, the character indicated by position
+ // is a U+000A LINE FEED (LF) character; advance position to the next character in input.
+ if (is_past_end())
+ seen_eof = true;
+ else
+ ++m_position;
+
+ // 4. If line contains the three-character substring "-->" (U+002D HYPHEN-MINUS, U+002D HYPHEN-MINUS, U+003E
+ // GREATER-THAN SIGN), then run these substeps:
+ if (line.contains(u"-->"sv)) {
+ // 1. If in header is not set and at least one of the following conditions are true:
+ // - line count is 1
+ // - line count is 2 and seen arrow is false
+ // ...then run these substeps:
+ if (!in_header && (line_count == 1 || (line_count == 2 && !seen_arrow))) {
+ // 1. Let seen arrow be true.
+ seen_arrow = true;
+
+ // 2. Let previous position be position.
+ previous_position = m_position;
+
+ // 3. Cue creation: Let cue be a new WebVTT cue and initialize it as follows:
+ // Let cue's text track cue identifier be buffer.
+ // FIXME: Initialize the remaining cue fields once cue settings are supported.
+ cue = ParsedCue {};
+ cue->identifier = buffer.to_string();
+
+ // 4. Collect WebVTT cue timings and settings from line using regions for cue. If that fails, let cue be
+ // null. Otherwise, let buffer be the empty string and let seen cue be true.
+ if (!collect_webvtt_cue_timings_and_settings(line, *cue)) {
+ cue.clear();
+ } else {
+ buffer.clear();
+ m_seen_cue = true;
+ }
+ }
+ // Otherwise, let position be previous position and break out of loop.
+ else {
+ m_position = previous_position;
+ break;
+ }
+ }
+ // 5. Otherwise, if line is the empty string, break out of loop.
+ else if (line.is_empty()) {
+ break;
+ }
+ // 6. Otherwise, run these substeps:
+ else {
+ // 1. If in header is not set and line count is 2, run these substeps:
+ if (!in_header && line_count == 2) {
+ // 1. If seen cue is false and buffer starts with the substring "STYLE" (U+0053 LATIN CAPITAL LETTER S,
+ // U+0054 LATIN CAPITAL LETTER T, U+0059 LATIN CAPITAL LETTER Y, U+004C LATIN CAPITAL LETTER L, U+0045
+ // LATIN CAPITAL LETTER E), and the remaining characters in buffer (if any) are all ASCII whitespace,
+ // then run these substeps:
+ // 2. Otherwise, if seen cue is false and buffer starts with the substring "REGION" (U+0052 LATIN CAPITAL
+ // LETTER R, U+0045 LATIN CAPITAL LETTER E, U+0047 LATIN CAPITAL LETTER G, U+0049 LATIN CAPITAL LETTER
+ // I, U+004F LATIN CAPITAL LETTER O, U+004E LATIN CAPITAL LETTER N), and the remaining characters in
+ // buffer (if any) are all ASCII whitespace, then run these substeps:
+ if (!m_seen_cue && (is_block_keyword(buffer.view(), u"STYLE"sv) || is_block_keyword(buffer.view(), u"REGION"sv))) {
+ // FIXME: Create the CSS style sheet or WebVTT region.
+ // Let buffer be the empty string.
+ buffer.clear();
+ }
+ }
+
+ // 2. If buffer is not the empty string, append a U+000A LINE FEED (LF) character to buffer.
+ if (!buffer.is_empty())
+ buffer.append_code_unit('\n');
+
+ // 3. Append line to buffer.
+ buffer.append(line);
+
+ // 4. Let previous position be position.
+ previous_position = m_position;
+ }
+
+ // 7. If seen EOF is true, break out of loop.
+ if (seen_eof)
+ break;
+ }
+
+ // 12. If cue is not null, let the cue text of cue be buffer, and return cue.
+ if (cue.has_value()) {
+ cue->text = buffer.to_string();
+ return cue;
+ }
+
+ // FIXME: 13. Otherwise, if stylesheet is not null, then Parse a stylesheet from buffer. If it returned a list of
+ // rules, assign the list as stylesheet's CSS rules; otherwise, set stylesheet's CSS rules to an empty
+ // list. Finally, return stylesheet.
+ // FIXME: 14. Otherwise, if region is not null, then collect WebVTT region settings from buffer using region for the
+ // results. Construct a WebVTT Region Object from region, and return it.
+
+ // 15. Otherwise, return null.
+ return {};
+}
+
+// https://w3c.github.io/webvtt/#webvtt-parser-algorithm
+Optional<Vector<ParsedCue>> Parser::parse()
+{
+ Vector<ParsedCue> output;
+
+ // 2. Let position be a pointer into input, initially pointing at the start of the string.
+ // 3. Let seen cue be false.
+
+ // 4. If input is less than six characters long, then abort these steps. The file does not start with the correct
+ // WebVTT file signature and was therefore not successfully processed.
+ if (m_input.length_in_code_units() < 6)
+ return {};
+
+ // 5. If input is exactly six characters long but does not exactly equal "WEBVTT", then abort these steps. The file
+ // does not start with the correct WebVTT file signature and was therefore not successfully processed.
+ // 6. If input is more than six characters long but the first six characters do not exactly equal "WEBVTT", or the
+ // seventh character is not a U+0020 SPACE character, a U+0009 CHARACTER TABULATION (tab) character, or a U+000A
+ // LINE FEED (LF) character, then abort these steps. The file does not start with the correct WebVTT file
+ // signature and was therefore not successfully processed.
+ if (!m_input.starts_with(u"WEBVTT"sv))
+ return {};
+ if (m_input.length_in_code_units() > 6 && !first_is_one_of(m_input.code_unit_at(6), ' ', '\t', '\n'))
+ return {};
+
+ // 7. Collect a sequence of code points that are not U+000A LINE FEED (LF) characters.
+ (void)collect_a_sequence_of_code_points_that_are_not_line_feeds();
+
+ // 8. If position is past the end of input, then abort these steps. The file was successfully processed, but it
+ // contains no useful data and so no WebVTT cues were added to output.
+ if (is_past_end())
+ return output;
+
+ // 9. The character indicated by position is a U+000A LINE FEED (LF) character. Advance position to the next
+ // character in input.
+ ++m_position;
+
+ // 10. If position is past the end of input, then abort these steps. The file was successfully processed, but it
+ // contains no useful data and so no WebVTT cues were added to output.
+ if (is_past_end())
+ return output;
+
+ // 11. Header: If the character indicated by position is not a U+000A LINE FEED (LF) character, then collect a WebVTT
+ // block with the in header flag set. Otherwise, advance position to the next character in input.
+ if (current() != '\n')
+ (void)collect_a_webvtt_block(true);
+ else
+ ++m_position;
+
+ // 12. Collect a sequence of code points that are U+000A LINE FEED (LF) characters.
+ collect_a_sequence_of_line_feeds();
+
+ // FIXME: 13. Let regions be an empty text track list of regions.
+
+ // 14. Block loop: While position doesn't point past the end of input:
+ while (!is_past_end()) {
+ // 1. Collect a WebVTT block, and let block be the returned value.
+ auto block = collect_a_webvtt_block(false);
+
+ // 2. If block is a WebVTT cue, add block to the text track list of cues output.
+ if (block.has_value())
+ output.append(block.release_value());
+
+ // FIXME: 3. Otherwise, if block is a CSS style sheet, add block to stylesheets.
+ // FIXME: 4. Otherwise, if block is a WebVTT region object, add block to regions.
+
+ // 5. Collect a sequence of code points that are U+000A LINE FEED (LF) characters.
+ collect_a_sequence_of_line_feeds();
+ }
+
+ // 15. End: The file has ended. Abort these steps. The WebVTT parser has finished. The file was successfully
+ // processed.
+ return output;
+}
+
+}
+
+// https://w3c.github.io/webvtt/#webvtt-parser
+Optional<Vector<ParsedCue>> parse_webvtt_file(ReadonlyBytes bytes)
+{
+ // A WebVTT parser, given an input byte stream, a text track list of cues output, and a collection of CSS style
+ // sheets stylesheets, must decode the byte stream using the UTF-8 decode algorithm, and then must parse the resulting
+ // string according to the WebVTT parser algorithm below.
+ auto decoded = Utf16String::from_utf8_with_replacement_character(StringView { bytes }, Utf16String::WithBOMHandling::Yes);
+
+ // 1. Let input be the string being parsed, after conversion to Unicode, and with the following transformations
+ // applied:
+ // - Replace all U+0000 NULL characters by U+FFFD REPLACEMENT CHARACTERs.
+ // - Replace each U+000D CARRIAGE RETURN U+000A LINE FEED (CRLF) character pair by a single U+000A LINE FEED (LF)
+ // character.
+ // - Replace all remaining U+000D CARRIAGE RETURN characters by U+000A LINE FEED (LF) characters.
+ auto view = decoded.utf16_view();
+ Utf16StringBuilder builder;
+ for (size_t i = 0; i < view.length_in_code_units(); ++i) {
+ auto code_unit = view.code_unit_at(i);
+ if (code_unit == 0) {
+ builder.append_code_unit(0xFFFD);
+ } else if (code_unit == '\r') {
+ if (i + 1 < view.length_in_code_units() && view.code_unit_at(i + 1) == '\n')
+ ++i;
+ builder.append_code_unit('\n');
+ } else {
+ builder.append_code_unit(code_unit);
+ }
+ }
+ auto input = builder.to_string();
+
+ Parser parser { input.utf16_view() };
+ return parser.parse();
+}
+
+}
diff --git a/Libraries/LibWeb/WebVTT/Parser.h b/Libraries/LibWeb/WebVTT/Parser.h
new file mode 100644
index 00000000000..cd0c5f43265
--- /dev/null
+++ b/Libraries/LibWeb/WebVTT/Parser.h
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Optional.h>
+#include <AK/Utf16String.h>
+#include <AK/Vector.h>
+
+namespace Web::WebVTT {
+
+struct ParsedCue {
+ Utf16String identifier;
+ double start_time { 0 };
+ double end_time { 0 };
+ Utf16String text;
+};
+
+// Returns null if the file does not start with the WebVTT file signature.
+Optional<Vector<ParsedCue>> parse_webvtt_file(ReadonlyBytes);
+
+}
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index 6acdb22075e..5bdb2c4a24b 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -587,6 +587,9 @@ Text/input/wpt-import/resource-timing/initiator-type/img-srcset.html
Text/input/wpt-import/resource-timing/initiator-type/picture.html
Text/input/wpt-import/resource-timing/object-not-found-adds-entry.html
Text/input/wpt-import/resource-timing/status-codes-create-entry.html
+Text/input/wpt-import/webvtt/parsing/file-parsing/tests/ids.html
+Text/input/wpt-import/webvtt/parsing/file-parsing/tests/timings-too-short.html
+Text/input/wpt-import/webvtt/parsing/file-parsing/tests/whitespace-chars.html
[Skipped]
; Needs vertical-rl inline geometry fidelity beyond the binary transpose
diff --git a/Tests/LibWeb/Text/expected/HTML/HTMLTrackElement-src-change-empties-cues.txt b/Tests/LibWeb/Text/expected/HTML/HTMLTrackElement-src-change-empties-cues.txt
new file mode 100644
index 00000000000..36f610da4a9
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/HTMLTrackElement-src-change-empties-cues.txt
@@ -0,0 +1,3 @@
+first src: first
+after setting src: 0 cues
+second src: second one, second two
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
index bcca498d0f4..03f20e435cd 100644
--- 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
@@ -2,10 +2,9 @@ Harness status: OK
Found 5 tests
-4 Pass
-1 Fail
+5 Pass
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
+Pass 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/removeCue.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/media-elements/interfaces/TextTrack/removeCue.txt
index 8473d583fcc..4376ab93e84 100644
--- 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
@@ -2,7 +2,6 @@ Harness status: OK
Found 2 tests
-1 Pass
-1 Fail
+2 Pass
Pass TextTrack.removeCue(), two elementless tracks
-Fail TextTrack.removeCue(), cue from track element
\ No newline at end of file
+Pass TextTrack.removeCue(), cue from track element
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/webvtt/parsing/file-parsing/tests/ids.txt b/Tests/LibWeb/Text/expected/wpt-import/webvtt/parsing/file-parsing/tests/ids.txt
new file mode 100644
index 00000000000..fd76f5afbac
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/webvtt/parsing/file-parsing/tests/ids.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass ids
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/webvtt/parsing/file-parsing/tests/timings-too-short.txt b/Tests/LibWeb/Text/expected/wpt-import/webvtt/parsing/file-parsing/tests/timings-too-short.txt
new file mode 100644
index 00000000000..dac3c42a34c
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/webvtt/parsing/file-parsing/tests/timings-too-short.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass timings, too short
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/webvtt/parsing/file-parsing/tests/whitespace-chars.txt b/Tests/LibWeb/Text/expected/wpt-import/webvtt/parsing/file-parsing/tests/whitespace-chars.txt
new file mode 100644
index 00000000000..d23f3149753
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/webvtt/parsing/file-parsing/tests/whitespace-chars.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass whitespace chars
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/HTML/HTMLTrackElement-src-change-empties-cues.html b/Tests/LibWeb/Text/input/HTML/HTMLTrackElement-src-change-empties-cues.html
new file mode 100644
index 00000000000..664b3210bcd
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/HTMLTrackElement-src-change-empties-cues.html
@@ -0,0 +1,22 @@
+<!DOCTYPE html>
+<video><track src="data:text/vtt,WEBVTT%0A%0A00:00.000 --> 00:01.000%0Afirst"></video>
+<script src="../include.js"></script>
+<script>
+ asyncTest(async done => {
+ const trackElement = document.querySelector("track");
+ const track = trackElement.track;
+ const cueTexts = () => Array.from(track.cues, cue => cue.text).join(", ");
+
+ if (trackElement.readyState !== HTMLTrackElement.LOADED)
+ await new Promise(resolve => trackElement.addEventListener("load", resolve, { once: true }));
+ println(`first src: ${cueTexts()}`);
+
+ trackElement.src = "data:text/vtt,WEBVTT%0A%0A00:00.000 --> 00:01.000%0Asecond one%0A%0A00:01.000 --> 00:02.000%0Asecond two";
+ println(`after setting src: ${track.cues.length} cues`);
+
+ await new Promise(resolve => trackElement.addEventListener("load", resolve, { once: true }));
+ println(`second src: ${cueTexts()}`);
+
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/webvtt/parsing/file-parsing/tests/ids.html b/Tests/LibWeb/Text/input/wpt-import/webvtt/parsing/file-parsing/tests/ids.html
new file mode 100644
index 00000000000..1ae3cd474ab
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/webvtt/parsing/file-parsing/tests/ids.html
@@ -0,0 +1,44 @@
+<!doctype html>
+<!-- DO NOT EDIT! This file and support/ids.vtt are generated. -->
+<!-- See /webvtt/parsing/file-parsing/README.md -->
+<meta charset=utf-8>
+<title>WebVTT parser test: ids</title>
+<link rel="help" href="https://w3c.github.io/webvtt/#collect-a-webvtt-block">
+<script src="../../../../resources/testharness.js"></script>
+<script src="../../../../resources/testharnessreport.js"></script>
+<div id=log></div>
+<script>
+var t = async_test('ids');
+t.step(function(){
+ var video = document.createElement('video');
+ var track = document.createElement('track');
+ assert_true('src' in track, 'track element not supported');
+ track.src = 'support/ids.vtt';
+ track['default'] = true;
+ track.kind = 'subtitles';
+ track.onload = this.step_func(trackLoaded);
+ track.onerror = this.step_func(trackError);
+ video.appendChild(track);
+ document.body.appendChild(video);
+});
+
+function trackLoaded(event) {
+ var track = event.target;
+ var video = track.parentNode;
+ var cues = video.textTracks[0].cues;
+ {
+assert_equals(cues.length, 5);
+assert_equals(cues[0].id, " leading space");
+assert_equals(cues[1].id, "trailing space ");
+assert_equals(cues[2].id, "-- >");
+assert_equals(cues[3].id, "->");
+assert_equals(cues[4].id, " ");
+
+ }
+ this.done();
+}
+
+function trackError(e) {
+ assert_unreached('got unexpected error event');
+}
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/webvtt/parsing/file-parsing/tests/support/ids.vtt b/Tests/LibWeb/Text/input/wpt-import/webvtt/parsing/file-parsing/tests/support/ids.vtt
new file mode 100644
index 00000000000..5f3e91f8a4b
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/webvtt/parsing/file-parsing/tests/support/ids.vtt
@@ -0,0 +1,21 @@
+WEBVTT
+
+ leading space
+00:00:00.000 --> 00:00:01.000
+text0
+
+trailing space
+00:00:00.000 --> 00:00:01.000
+text1
+
+-- >
+00:00:00.000 --> 00:00:01.000
+text2
+
+->
+00:00:00.000 --> 00:00:01.000
+text3
+
+
+00:00:00.000 --> 00:00:01.000
+text4
diff --git a/Tests/LibWeb/Text/input/wpt-import/webvtt/parsing/file-parsing/tests/support/timings-too-short.vtt b/Tests/LibWeb/Text/input/wpt-import/webvtt/parsing/file-parsing/tests/support/timings-too-short.vtt
new file mode 100644
index 00000000000..78d190a7bca
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/webvtt/parsing/file-parsing/tests/support/timings-too-short.vtt
@@ -0,0 +1,97 @@
+WEBVTT
+
+0:00:00.000 --> 00:00:01.000
+text0
+
+0000:00.000 --> 00:00:01.000
+invalid
+
+00:0:00.000 --> 00:00:01.000
+invalid
+
+00:0000.000 --> 00:00:01.000
+invalid
+
+00:00:0.000 --> 00:00:01.000
+invalid
+
+00:00:00000 --> 00:00:01.000
+invalid
+
+00:00:00.00 --> 00:00:01.000
+invalid
+
+00:00:00.0 --> 00:00:01.000
+invalid
+
+00:00:00. --> 00:00:01.000
+invalid
+
+00:00:00 --> 00:00:01.000
+invalid
+
+00:00:0 --> 00:00:01.000
+invalid
+
+00:00: --> 00:00:01.000
+invalid
+
+00:00 --> 00:00:01.000
+invalid
+
+00:0 --> 00:00:01.000
+invalid
+
+00: --> 00:00:01.000
+invalid
+
+00 --> 00:00:01.000
+invalid
+
+0 --> 00:00:01.000
+invalid
+
+ --> 00:00:01.000
+invalid
+
+0:00.000 --> 00:01.000
+invalid
+
+0000.000 --> 00:01.000
+invalid
+
+00:0.000 --> 00:01.000
+invalid
+
+00:00000 --> 00:01.000
+invalid
+
+00:00.00 --> 00:01.000
+invalid
+
+00:00.0 --> 00:01.000
+invalid
+
+00:00. --> 00:01.000
+invalid
+
+0:00. --> 00:01.000
+invalid
+
+:00. --> 00:01.000
+invalid
+
+00. --> 00:01.000
+invalid
+
+0. --> 00:01.000
+invalid
+
+. --> 00:01.000
+invalid
+
+00:00.000 --> 0:01.000
+invalid
+
+00:00:00.000 --> 0:00:01.000
+text1
diff --git a/Tests/LibWeb/Text/input/wpt-import/webvtt/parsing/file-parsing/tests/support/whitespace-chars.vtt b/Tests/LibWeb/Text/input/wpt-import/webvtt/parsing/file-parsing/tests/support/whitespace-chars.vtt
new file mode 100644
index 00000000000..b26c93cd198
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/webvtt/parsing/file-parsing/tests/support/whitespace-chars.vtt
@@ -0,0 +1,17 @@
+WEBVTT
+
+spaces
+ 00:00:00.000 --> 00:00:01.000
+ text0
+
+tabs
+ 00:00:00.000 --> 00:00:01.000
+text1
+
+form feed
+ 00:00:00.000 --> 00:00:01.000
+text2
+
+vertical tab
+ 00:00:00.000 --> 00:00:01.000
+invalid
diff --git a/Tests/LibWeb/Text/input/wpt-import/webvtt/parsing/file-parsing/tests/timings-too-short.html b/Tests/LibWeb/Text/input/wpt-import/webvtt/parsing/file-parsing/tests/timings-too-short.html
new file mode 100644
index 00000000000..ee31bb9fb44
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/webvtt/parsing/file-parsing/tests/timings-too-short.html
@@ -0,0 +1,47 @@
+<!doctype html>
+<!-- DO NOT EDIT! This file and support/timings-too-short.vtt are generated. -->
+<!-- See /webvtt/parsing/file-parsing/README.md -->
+<meta charset=utf-8>
+<title>WebVTT parser test: timings, too short</title>
+<link rel="help" href="https://w3c.github.io/webvtt/#collect-a-webvtt-timestamp">
+<script src="../../../../resources/testharness.js"></script>
+<script src="../../../../resources/testharnessreport.js"></script>
+<div id=log></div>
+<script>
+var t = async_test('timings, too short');
+t.step(function(){
+ var video = document.createElement('video');
+ var track = document.createElement('track');
+ assert_true('src' in track, 'track element not supported');
+ track.src = 'support/timings-too-short.vtt';
+ track['default'] = true;
+ track.kind = 'subtitles';
+ track.onload = this.step_func(trackLoaded);
+ track.onerror = this.step_func(trackError);
+ video.appendChild(track);
+ document.body.appendChild(video);
+});
+
+function trackLoaded(event) {
+ var track = event.target;
+ var video = track.parentNode;
+ var cues = video.textTracks[0].cues;
+ {
+assert_equals(cues.length, 2);
+
+assert_equals(cues[0].text, 'text0');
+assert_equals(cues[0].startTime, 0);
+assert_equals(cues[0].endTime, 1);
+
+assert_equals(cues[1].text, 'text1');
+assert_equals(cues[1].startTime, 0);
+assert_equals(cues[1].endTime, 1);
+
+ }
+ this.done();
+}
+
+function trackError(e) {
+ assert_unreached('got unexpected error event');
+}
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/webvtt/parsing/file-parsing/tests/whitespace-chars.html b/Tests/LibWeb/Text/input/wpt-import/webvtt/parsing/file-parsing/tests/whitespace-chars.html
new file mode 100644
index 00000000000..2fb7d93b415
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/webvtt/parsing/file-parsing/tests/whitespace-chars.html
@@ -0,0 +1,48 @@
+<!doctype html>
+<!-- DO NOT EDIT! This file and support/whitespace-chars.vtt are generated. -->
+<!-- See /webvtt/parsing/file-parsing/README.md -->
+<meta charset=utf-8>
+<title>WebVTT parser test: whitespace chars</title>
+<link rel="help" href="https://w3c.github.io/webvtt/#collect-a-webvtt-block">
+<script src="../../../../resources/testharness.js"></script>
+<script src="../../../../resources/testharnessreport.js"></script>
+<div id=log></div>
+<script>
+var t = async_test('whitespace chars');
+t.step(function(){
+ var video = document.createElement('video');
+ var track = document.createElement('track');
+ assert_true('src' in track, 'track element not supported');
+ track.src = 'support/whitespace-chars.vtt';
+ track['default'] = true;
+ track.kind = 'subtitles';
+ track.onload = this.step_func(trackLoaded);
+ track.onerror = this.step_func(trackError);
+ video.appendChild(track);
+ document.body.appendChild(video);
+});
+
+function trackLoaded(event) {
+ var track = event.target;
+ var video = track.parentNode;
+ var cues = video.textTracks[0].cues;
+ {
+assert_equals(cues.length, 3);
+
+assert_equals(cues[0].id, 'spaces');
+assert_equals(cues[0].text, ' text0');
+
+assert_equals(cues[1].id, 'tabs');
+assert_equals(cues[1].text, 'text1');
+
+assert_equals(cues[2].id, 'form feed');
+assert_equals(cues[2].text, 'text2');
+
+ }
+ this.done();
+}
+
+function trackError(e) {
+ assert_unreached('got unexpected error event');
+}
+</script>