Repository: LadybirdBrowser/ladybird
File: Libraries/LibWeb/SVG/SVGUseElement.cpp (C++)

Code before:
    // NOTE: We need to check the ancestor because attribute_changed of a child doesn't call children_changed on the parent(s)
    if (to_clone == &svg_element || to_clone->is_ancestor_of(svg_element)) {
        clone_element_tree_as_our_shadow_tree(to_clone);
    }
}

void SVGUseElement::svg_element_removed(SVGElement& svg_element)
{
    if (!m_href.has_value() || !m_href->fragment().has_value() || !is_referenced_element_same_document()) {
        return;
    }

    if (AK::StringUtils::matches(svg_element.get_attribute_value("id"_fly_string), m_href->fragment().value())) {
        shadow_root()->remove_all_children();
    }
}

// https://svgwg.org/svg2-draft/linking.html#processingURL-target
GC::Ptr<DOM::Element> SVGUseElement::referenced_element()
{
    if (!m_href.has_value())
        return nullptr;

    if (!m_href->fragment().has_value())
        return nullptr;

    if (is_referenced_element_same_document()) {
        auto id = String::from_utf8_with_replacement_character(URL::percent_decode(*m_href->fragment()), String::WithBOMHandling::No);
        return document().get_element_by_id(id);
    }

    if (!m_resource_request)
        return nullptr;

    auto data = m_resource_request->image_data();
    if (!data || !is<SVG::SVGDecodedImageData>(*data))
        return nullptr;

    return as<SVG::SVGDecodedImageData>(*data).svg_document().get_element_by_id(*m_href->fragment());
}

// https://svgwg.org/svg2-draft/linking.html#processingURL-fetch
void SVGUseElement::fetch_the_document(URL::URL const& url)
{
    m_load_event_delayer.emplace(document());
    m_resource_request = HTML::SharedResourceRequest::get_or_create(realm(), document().page(), url);
    m_resource_request->add_callbacks(
        [this] {
            clone_element_tree_as_our_shadow_tree(referenced_element());
            m_load_event_delayer.clear();
        },

Review comment (suggestion):
Ah, just caught as I was going to approve one more case we are doing it here - https://github.com/LadybirdBrowser/ladybird/blob/b317461d50d1e7a31bdbbdf6027f9334f30e865f/Libraries/LibWeb/SVG/SVGUseElement.cpp#L128

Code after:
    // NOTE: We need to check the ancestor because attribute_changed of a child doesn't call children_changed on the parent(s)
    if (to_clone == &svg_element || to_clone->is_ancestor_of(svg_element)) {
        clone_element_tree_as_our_shadow_tree(to_clone);
    }
}

void SVGUseElement::svg_element_removed(SVGElement& svg_element)
{
    if (!m_href.has_value() || !m_href->fragment().has_value() || !is_referenced_element_same_document()) {
        return;
    }

    auto id = String::from_utf8_with_replacement_character(URL::percent_decode(*m_href->fragment()), String::WithBOMHandling::No);
    if (AK::StringUtils::matches(svg_element.get_attribute_value("id"_fly_string), id)) {
        shadow_root()->remove_all_children();
    }
}

// https://svgwg.org/svg2-draft/linking.html#processingURL-target
GC::Ptr<DOM::Element> SVGUseElement::referenced_element()
{
    if (!m_href.has_value())
        return nullptr;

    if (!m_href->fragment().has_value())
        return nullptr;

    if (is_referenced_element_same_document()) {
        auto id = String::from_utf8_with_replacement_character(URL::percent_decode(*m_href->fragment()), String::WithBOMHandling::No);
        return document().get_element_by_id(id);
    }

    if (!m_resource_request)
        return nullptr;

    auto data = m_resource_request->image_data();
    if (!data || !is<SVG::SVGDecodedImageData>(*data))
        return nullptr;

    return as<SVG::SVGDecodedImageData>(*data).svg_document().get_element_by_id(*m_href->fragment());
}

// https://svgwg.org/svg2-draft/linking.html#processingURL-fetch
void SVGUseElement::fetch_the_document(URL::URL const& url)
{
    m_load_event_delayer.emplace(document());
    m_resource_request = HTML::SharedResourceRequest::get_or_create(realm(), document().page(), url);
    m_resource_request->add_callbacks(
        [this] {
            clone_element_tree_as_our_shadow_tree(referenced_element());
            m_load_event_delayer.clear();