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

Code before:
        // 6. If child has a media attribute, and its value does not match the environment, continue to the next child.
        if (child->has_attribute(HTML::AttributeNames::media)) {
            auto media_query = parse_media_query(CSS::Parser::ParsingParams { element.document() },
                child->get_attribute_value(HTML::AttributeNames::media));
            if (!media_query || !media_query->evaluate(element.document())) {
                continue;
            }
        }

        // 7. Parse child's sizes attribute with img, and let source set's source size be the returned value.
        source_set.m_source_size = parse_a_sizes_attribute(element, child->get_attribute_value(HTML::AttributeNames::sizes), img);

        // 8. If child has a type attribute, and its value is an unknown or unsupported MIME type, continue to the next child.
        if (child->has_attribute(HTML::AttributeNames::type)) {
            auto mime_type = child->get_attribute_value(HTML::AttributeNames::type);
            if (is<HTMLImageElement>(element)) {
                if (!is_supported_image_type(mime_type))
                    continue;
            }

            // FIXME: Implement this step for link elements
        }

        // 9. If child has width or height attributes, set el's dimension attribute source to child.
        //    Otherwise, set el's dimension attribute source to el.
        if (auto* image_element = as_if<HTMLImageElement>(element)) {
            if (child->has_attribute(HTML::AttributeNames::width) || child->has_attribute(HTML::AttributeNames::height))
                image_element->set_dimension_attribute_source(child);
            else
                image_element->set_dimension_attribute_source(nullptr);
        }

        // 10. Normalize the source densities of source set.
        source_set.normalize_source_densities(element);

        // 11. Set el's source set to source set.
        if (auto* image_element = as_if<HTMLImageElement>(element))
            image_element->set_source_set(move(source_set));
        else if (is<HTMLLinkElement>(element))
            TODO();

        // 12. Return.
        return;
    }
}

// https://html.spec.whatwg.org/multipage/images.html#select-an-image-source
Optional<ImageSourceAndPixelDensity> HTMLImageElement::select_an_image_source()
{
    // 1. Update the source set for el.
    update_the_source_set(*this);

Review comment (suggestion):
From the spec note on step 5.1.11, `el` should always be an img element at this point. (The same is true for the check in step 8, and the FIXME below that.)

Code after:
                child->get_attribute_value(HTML::AttributeNames::media));
            if (!media_query || !media_query->evaluate(element.document())) {
                continue;
            }
        }

        // 7. Parse child's sizes attribute with img, and let source set's source size be the returned value.
        source_set.m_source_size = parse_a_sizes_attribute(element, child->get_attribute_value(HTML::AttributeNames::sizes), img);

        // 8. If child has a type attribute, and its value is an unknown or unsupported MIME type, continue to the next child.
        if (child->has_attribute(HTML::AttributeNames::type)) {
            auto mime_type = child->get_attribute_value(HTML::AttributeNames::type);
            if (!is_supported_image_type(mime_type))
                continue;
        }

        // 9. If child has width or height attributes, set el's dimension attribute source to child.
        //    Otherwise, set el's dimension attribute source to el.
        if (child->has_attribute(HTML::AttributeNames::width) || child->has_attribute(HTML::AttributeNames::height))
            img->set_dimension_attribute_source(child);
        else
            img->set_dimension_attribute_source(nullptr);

        // 10. Normalize the source densities of source set.
        source_set.normalize_source_densities(element);

        // 11. Set el's source set to source set.
        img->set_source_set(move(source_set));

        // 12. Return.
        return;
    }
}

// https://html.spec.whatwg.org/multipage/images.html#select-an-image-source
Optional<ImageSourceAndPixelDensity> HTMLImageElement::select_an_image_source()
{
    // 1. Update the source set for el.
    update_the_source_set(*this);

    // 2. If el's source set is empty, return null as the URL and undefined as the pixel density.
    if (m_source_set.is_empty())
        return {};

    // 3. Return the result of selecting an image from el's source set.
    return m_source_set.select_an_image_source();
}

void HTMLImageElement::set_source_set(SourceSet source_set)
{
    m_source_set = move(source_set);