LibWeb: Fire error event if HTMLTrackElement src is empty on load

Previously, we would hang while waiting for the track to load.
This commit is contained in:
Tim Ledbetter 2025-03-01 07:12:27 +00:00 committed by Tim Flynn
commit 6aeb3e8839
Notes: github-actions[bot] 2025-03-01 13:26:09 +00:00
3 changed files with 39 additions and 5 deletions

View file

@ -180,6 +180,13 @@ void HTMLTrackElement::start_the_track_processing_model_parallel_steps(JS::Realm
// 9. End the synchronous section, continuing the remaining steps in parallel.
auto fire_error_event = [&]() {
queue_an_element_task(Task::Source::DOMManipulation, [this, &realm]() {
m_track->set_readiness_state(TextTrack::ReadinessState::FailedToLoad);
dispatch_event(DOM::Event::create(realm, HTML::EventNames::error));
});
};
// 10. If URL is not the empty string, then:
if (!url.is_empty()) {
// 1. Let request be the result of creating a potential-CORS request given URL, "track", and corsAttributeState,
@ -193,17 +200,14 @@ void HTMLTrackElement::start_the_track_processing_model_parallel_steps(JS::Realm
request->set_initiator_type(Fetch::Infrastructure::Request::InitiatorType::Track);
Fetch::Infrastructure::FetchAlgorithms::Input fetch_algorithms_input {};
fetch_algorithms_input.process_response_consume_body = [this, &realm](auto response, auto body_bytes) {
fetch_algorithms_input.process_response_consume_body = [this, &realm, &fire_error_event](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.),
// or if URL is the empty string, then queue an element task on the DOM manipulation task source given the media element
// to first change the text track readiness state to failed to load and then fire an event named error at the track element.
if (!response->url().has_value() || body_bytes.template has<Empty>() || body_bytes.template has<Fetch::Infrastructure::FetchAlgorithms::ConsumeBodyFailureTag>() || !Fetch::Infrastructure::is_ok_status(response->status()) || response->is_network_error()) {
queue_an_element_task(Task::Source::DOMManipulation, [this, &realm]() {
m_track->set_readiness_state(TextTrack::ReadinessState::FailedToLoad);
dispatch_event(DOM::Event::create(realm, HTML::EventNames::error));
});
fire_error_event();
return;
}
@ -232,6 +236,9 @@ void HTMLTrackElement::start_the_track_processing_model_parallel_steps(JS::Realm
m_fetch_algorithms = Fetch::Infrastructure::FetchAlgorithms::create(vm(), move(fetch_algorithms_input));
m_fetch_controller = MUST(Fetch::Fetching::fetch(realm, request, *m_fetch_algorithms));
return;
} else {
fire_error_event();
return;
}
// 11. Wait until the text track readiness state is no longer set to loading.

View file

@ -0,0 +1,6 @@
Harness status: OK
Found 1 tests
1 Pass
Pass Setting HTMLTrackElement.src to the empty string fires 'error' and sets readyState to ERROR

View file

@ -0,0 +1,21 @@
<!DOCTYPE html>
<title>Setting HTMLTrackElement.src to the empty string fires 'error' and sets readyState to ERROR</title>
<link rel="help" href="https://html.spec.whatwg.org/multipage/media.html#sourcing-out-of-band-text-tracks">
<script src="../../../../../../resources/testharness.js"></script>
<script src="../../../../../../resources/testharnessreport.js"></script>
<video></video>
<script>
async_test(t => {
let track = document.createElement("track");
track.src = '';
track.default = true;
track.onerror = t.step_func_done(() => {
assert_equals(track.readyState, HTMLTrackElement.ERROR);
});
track.onload = t.unreached_func('fired load');
assert_equals(track.readyState, HTMLTrackElement.NONE);
document.querySelector('video').appendChild(track);
});
</script>