Repository: LadybirdBrowser/ladybird
File: Libraries/LibWeb/DOM/Node.cpp (C++)

Code before:
        return false;

    // Each child of A equals the child of B at the identical index.
    auto* this_child = first_child();
    auto* other_child = other_node->first_child();
    while (this_child) {
        VERIFY(other_child);
        if (!this_child->is_equal_node(other_child))
            return false;

        this_child = this_child->next_sibling();
        other_child = other_child->next_sibling();
    }

    return true;
}

Vector<String> Node::get_in_scope_prefixes() const
{
    // https://html.spec.whatwg.org/multipage/xhtml.html#parsing-xhtml-fragments
    // "A namespace prefix is in scope if the DOM lookupNamespaceURI() method on the element would return a non-null value for that prefix."

    // NB: This implementation omits the empty prefix because it is handled specially and the "xmlns" prefix because it is illegal to declare.

    Vector<String> prefixes;
    HashTable<String> seen_prefixes;

    prefixes.append("xml"_string);
    seen_prefixes.set("xml"_string);

    Element const* current = nullptr;

    if (is<Element>(*this)) {
        current = static_cast<Element const*>(this);
    } else if (is<Document>(*this)) {
        current = static_cast<Document const*>(this)->document_element();
    } else if (is<Attr>(*this)) {
        current = static_cast<Attr const*>(this)->owner_element();
    } else {
        current = parent_element();
    }

    while (current) {
        if (current->prefix().has_value() && current->namespace_uri().has_value()) {
            auto prefix = current->prefix().value().to_string();
            if (!seen_prefixes.contains(prefix)) {
                prefixes.append(prefix);
                seen_prefixes.set(prefix);
            }
        }


Review comment (refactor):
This feels like it should be using FlyString over String here especially since what we are adding is already a FlyString.

Also maybe it makes sense to have a lambda helper or something for adding to seen_prefixes/prefixes together  alongside the uniqueness check?

Code after:
        return false;

    // Each child of A equals the child of B at the identical index.
    auto* this_child = first_child();
    auto* other_child = other_node->first_child();
    while (this_child) {
        VERIFY(other_child);
        if (!this_child->is_equal_node(other_child))
            return false;

        this_child = this_child->next_sibling();
        other_child = other_child->next_sibling();
    }

    return true;
}

Vector<FlyString> Node::get_in_scope_prefixes() const
{
    // https://html.spec.whatwg.org/multipage/xhtml.html#parsing-xhtml-fragments
    // "A namespace prefix is in scope if the DOM lookupNamespaceURI() method on the element would return a non-null value for that prefix."

    Vector<FlyString> prefixes;
    HashTable<FlyString> seen_prefixes;

    auto add_prefix = [&](FlyString const& prefix) {
        if (!seen_prefixes.contains(prefix)) {
            prefixes.append(prefix);
            seen_prefixes.set(prefix);
            VERIFY(lookup_namespace_uri(prefix.to_string()).has_value());
        }
    };

    add_prefix("xml"_fly_string);
    add_prefix("xmlns"_fly_string);

    Element const* current = nullptr;

    if (is<Element>(*this)) {
        current = static_cast<Element const*>(this);
    } else if (is<Document>(*this)) {
        current = static_cast<Document const*>(this)->document_element();
    } else if (is<Attr>(*this)) {
        current = static_cast<Attr const*>(this)->owner_element();
    } else {
        current = parent_element();
    }

    while (current) {
        if (current->namespace_uri().has_value()) {
            auto prefix = current->prefix().value_or(""_fly_string);