Truly independent web browser
LibWeb: Recheck a source selection after a source’s error event
sideshowbarker779abad5b43f
diff --git a/Libraries/LibWeb/HTML/HTMLMediaElement.cpp b/Libraries/LibWeb/HTML/HTMLMediaElement.cppindex f91486e2db5..704c563b3a9 100644--- a/Libraries/LibWeb/HTML/HTMLMediaElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLMediaElement.cpp@@ -1204,19 +1204,39 @@ private:if (selector.m_candidate)selector.m_candidate->dispatch_event(DOM::Event::create(HTML::relevant_global_object(*selector.m_candidate), HTML::EventNames::error));- // 10. Await a stable state. The synchronous section consists of all the remaining steps of this algorithm until- // the algorithm says the synchronous section has ended. (Steps in synchronous sections are marked with ⌛.)- // FIXME: Also run the next steps within this task, instead of waiting for a stable state.- // This seems to match the intent in https://github.com/whatwg/html/issues/2882#issuecomment-1108531815.- // Update this if the spec is clarified in regard to this step.+ // NB: An error listener that invoked the load algorithm (by setting src, e.g.) aborted this selection.+ if (!selector.is_current())+ return;- // 11. ⌛ Forget the media element's media-resource-specific tracks.- selector.m_media_element->forget_media_resource_specific_tracks();+ // NB: An error listener can also adopt the element into a document that isn't fully active, where nothing can+ // be fetched. So go on from a media element task instead, which waits until the element is back in a+ // fully active document.+ if (!selector.m_media_element->document().is_fully_active()) {+ selector.m_media_element->queue_a_media_element_task(GC::create_function(GC::Heap::the(), GC::captures(selector), [](auto& selector, HTMLMediaElement&) {+ if (selector.is_current())+ selector.continue_after_failure();+ }));+ return;+ }- selector.find_next_candidate();+ selector.continue_after_failure();}));}+ void continue_after_failure()+ {+ // 10. Await a stable state. The synchronous section consists of all the remaining steps of this algorithm until+ // the algorithm says the synchronous section has ended. (Steps in synchronous sections are marked with ⌛.)+ // FIXME: Also run the next steps within this task, instead of waiting for a stable state.+ // This seems to match the intent in https://github.com/whatwg/html/issues/2882#issuecomment-1108531815.+ // Update this if the spec is clarified in regard to this step.++ // 11. ⌛ Forget the media element's media-resource-specific tracks.+ m_media_element->forget_media_resource_specific_tracks();++ find_next_candidate();+ }+GC::Ptr<DOM::Node> node_after(GC::Ptr<DOM::Node> node_before_pointer) const{return node_before_pointer ? node_before_pointer->next_sibling() : m_media_element->first_child();diff --git a/Tests/LibWeb/Text/expected/HTML/media-source-error-listener-adopts-into-inactive-document.txt b/Tests/LibWeb/Text/expected/HTML/media-source-error-listener-adopts-into-inactive-document.txtnew file mode 100644index 00000000000..b0e5b78a684--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/media-source-error-listener-adopts-into-inactive-document.txt@@ -0,0 +1,2 @@+adopted back from the next task: loaded metadata: h264-reordered-frames.mp4+adopted back after 100ms: loaded metadata: h264-reordered-frames.mp4diff --git a/Tests/LibWeb/Text/expected/HTML/media-source-error-listener-starts-new-load.txt b/Tests/LibWeb/Text/expected/HTML/media-source-error-listener-starts-new-load.txtnew file mode 100644index 00000000000..2c2a8aa5ee1--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/media-source-error-listener-starts-new-load.txt@@ -0,0 +1 @@+loaded metadata: h264-reordered-frames.mp4diff --git a/Tests/LibWeb/Text/input/HTML/media-source-error-listener-adopts-into-inactive-document.html b/Tests/LibWeb/Text/input/HTML/media-source-error-listener-adopts-into-inactive-document.htmlnew file mode 100644index 00000000000..d6bc7db6f90--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/media-source-error-listener-adopts-into-inactive-document.html@@ -0,0 +1,36 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ // The first source has no src, and its error listener adopts the video into a document that isn't fully active.+ // The selection waits there, and once the video is adopted back, it goes on to the next source, which loads. The+ // next source's URL is absolute, since one relative to the other document's about:blank URL would fail to parse.+ async function run(label, delay) {+ const inactive = document.implementation.createHTMLDocument();+ const video = document.createElement("video");+ const first = document.createElement("source");+ const next = document.createElement("source");+ next.src = new URL("../../../Assets/h264-reordered-frames.mp4", location.href).href;+ video.append(first, next);+ const firstFailed = new Promise(resolve => first.addEventListener("error", () => {+ inactive.body.appendChild(video);+ resolve();+ }, { once: true }));+ next.addEventListener("error", () => println(`${label}: error at the next source`));+ document.body.appendChild(video);+ await firstFailed;++ // The task that fired the error goes on once the listener returns, so adopt the video back from a later task.+ await new Promise(resolve => setTimeout(resolve, delay));+ const loaded = new Promise(resolve => video.addEventListener("loadedmetadata", resolve));+ document.body.appendChild(video);+ await loaded;+ println(`${label}: loaded metadata: ${video.currentSrc.split("/").pop()}`);+ video.remove();+ }++ asyncTest(async done => {+ await run("adopted back from the next task", 0);+ await run("adopted back after 100ms", 100);+ done();+ });+</script>diff --git a/Tests/LibWeb/Text/input/HTML/media-source-error-listener-starts-new-load.html b/Tests/LibWeb/Text/input/HTML/media-source-error-listener-starts-new-load.htmlnew file mode 100644index 00000000000..a496e927cd0--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/media-source-error-listener-starts-new-load.html@@ -0,0 +1,25 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ // The first source has no src, and its error listener sets src on the video. That load replaces the selection+ // among the source elements, so the next source is never tried.+ asyncTest(done => {+ const video = document.createElement("video");+ const first = document.createElement("source");+ const next = document.createElement("source");+ next.src = "../../../Assets/solid-blue-160x120.webm";+ video.append(first, next);+ first.addEventListener("error", () => {+ video.src = "../../../Assets/h264-reordered-frames.mp4";+ });+ const loaded = [];+ video.addEventListener("loadedmetadata", () => {+ loaded.push(video.currentSrc.split("/").pop());+ setTimeout(() => {+ println(`loaded metadata: ${loaded.join(", ")}`);+ done();+ }, 500);+ });+ document.body.appendChild(video);+ });+</script>