Repository: LadybirdBrowser/ladybird
File: Libraries/LibWeb/HTML/HTMLMediaElement.cpp (C++)

Code before:
            //           a media element task given the media element to run the dedicated media source failure steps with the result.
            // FIXME: 5. Wait for the task queued by the previous step to have executed.

            // 6. Return. The element won't attempt to load another resource until this algorithm is triggered again.
            return;

        // -> If mode is attribute
        case SelectMode::Attribute: {
            auto failed_with_attribute = [this](auto error_message) {
                // 6. Failed with attribute: Reaching this step indicates that the media resource failed to load or that the given URL could not be parsed. Take
                //    pending play promises and queue a media element task given the media element to run the dedicated media source failure steps with the result.
                queue_a_media_element_task([this, error_message = move(error_message)]() mutable {
                    auto promises = take_pending_play_promises();
                    handle_media_source_failure(promises, move(error_message));
                });

                // 7. Wait for the task queued by the previous step to have executed.
                // AD-HOC: All calls to the failure steps immediately return, so we do not actually need to wait here.
            };

            // 1. ⌛ If the src attribute's value is the empty string, then end the synchronous section, and jump down to the failed with attribute step below.
            auto source = get_attribute_value(HTML::AttributeNames::src);
            if (source.is_empty()) {
                failed_with_attribute("The 'src' attribute is empty"_string);
                return;
            }

            // 2. ⌛ Let urlRecord be the result of encoding-parsing a URL given the src attribute's value,
            //    relative to the media element's node document when the src attribute was last changed.
            auto url_record = document().encoding_parse_url(source);

            // 3. ⌛ If urlRecord is not failure, then set the currentSrc attribute to the result of applying the URL serializer to urlRecord.
            if (url_record.has_value())
                m_current_src = url_record->serialize();

            // 4. End the synchronous section, continuing the remaining steps in parallel.

            // 5. If urlRecord was obtained successfully, run the resource fetch algorithm with urlRecord. If that algorithm returns without aborting this one,
            //    then the load failed.
            queue_a_media_element_task([this, url_record = move(url_record), failed_with_attribute = move(failed_with_attribute)]() mutable {
                if (url_record.has_value()) {
                    fetch_resource(*url_record, move(failed_with_attribute));
                    return;
                }
            });

            // 8. Return. The element won't attempt to load another resource until this algorithm is triggered again.
            return;
        }

        // -> Otherwise (mode is children)

Review comment (bug):
From what I can gather, I think the point of the wait here is that running the failjre steps can invoke javascript which changes the state of future steps, like for example maybe the src attribute is changed by this point

Code after:
        switch (*mode) {
        // -> If mode is object
        case SelectMode::Object:
            // FIXME: 1. ⌛ Set the currentSrc attribute to the empty string.
            // FIXME: 2. End the synchronous section, continuing the remaining steps in parallel.
            // FIXME: 3. Run the resource fetch algorithm with the assigned media provider object. If that algorithm returns without aborting this one,
            //           then theload failed.
            // FIXME: 4. Failed with media provider: Reaching this step indicates that the media resource failed to load. Take pending play promises and queue
            //           a media element task given the media element to run the dedicated media source failure steps with the result.
            // FIXME: 5. Wait for the task queued by the previous step to have executed.

            // 6. Return. The element won't attempt to load another resource until this algorithm is triggered again.
            return;

        // -> If mode is attribute
        case SelectMode::Attribute: {
            auto failed_with_attribute = [this](auto error_message) {
                // 6. Failed with attribute: Reaching this step indicates that the media resource failed to load or that the given URL could not be parsed. Take
                //    pending play promises and queue a media element task given the media element to run the dedicated media source failure steps with the result.
                queue_a_media_element_task([this, error_message = move(error_message)]() mutable {
                    auto promises = take_pending_play_promises();
                    handle_media_source_failure(promises, move(error_message));
                });

                // 7. Wait for the task queued by the previous step to have executed.
                // AD-HOC: All calls to the failure steps immediately return, so we do not actually need to wait here.
            };

            // 1. ⌛ If the src attribute's value is the empty string, then end the synchronous section, and jump down to the failed with attribute step below.
            auto source = get_attribute_value(HTML::AttributeNames::src);
            if (source.is_empty()) {
                failed_with_attribute("The 'src' attribute is empty"_string);
                return;
            }

            // 2. ⌛ Let urlRecord be the result of encoding-parsing a URL given the src attribute's value,
            //    relative to the media element's node document when the src attribute was last changed.
            auto url_record = document().encoding_parse_url(source);

            // 3. ⌛ If urlRecord is not failure, then set the currentSrc attribute to the result of applying the URL serializer to urlRecord.
            if (url_record.has_value())
                m_current_src = url_record->serialize();

            // 4. End the synchronous section, continuing the remaining steps in parallel.

            // 5. If urlRecord was obtained successfully, run the resource fetch algorithm with urlRecord. If that algorithm returns without aborting this one,
            //    then the load failed.
            queue_a_media_element_task([this, url_record = move(url_record), failed_with_attribute = move(failed_with_attribute)]() mutable {
                if (url_record.has_value()) {
                    fetch_resource(*url_record, move(failed_with_attribute));
                    return;