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

Code before:

void ListOfActiveFormattingElements::visit_edges(JS::Cell::Visitor& visitor)
{
    for (auto& entry : m_entries)
        visitor.visit(entry.element);
}

// If there are already three elements in the list of active formatting elements after the last marker,
// if any, or anywhere in the list if there are no markers, that have the same tag name, namespace, and attributes as element,
// then remove the earliest such element from the list of active formatting elements.
// For these purposes, the attributes must be compared as they were when the elements were created by the parser;
// two elements have the same attributes if all their parsed attributes can be paired such that the two attributes
// in each pair have identical names, namespaces, and values (the order of the attributes does not matter).
void ListOfActiveFormattingElements::ensure_noahs_ark_clause(DOM::Element& element)
{
    Vector<Entry> possible_matches;
    for (size_t i = m_entries.size(); i > 0;) {
        i--;
        auto& entry = m_entries[i];
        if (entry.is_marker())
            break;
        if (entry.element->local_name() == element.local_name()
            && entry.element->namespace_uri() == element.namespace_uri()
            && entry.element->attribute_list_size() == element.attribute_list_size())
            possible_matches.append(entry);
    }

    if (possible_matches.size() < NoahsArkCapacity)
        return;

    element.for_each_attribute([&](auto& name, auto& value) {
        possible_matches.remove_all_matching([&](auto& entry) {
            auto attr = entry.element->get_attribute(name);
            return !attr.has_value() || attr != value;
        });
    });

    if (possible_matches.size() < NoahsArkCapacity)
        return;

    remove(*possible_matches.last().element);
}

// https://html.spec.whatwg.org/multipage/parsing.html#push-onto-the-list-of-active-formatting-elements
void ListOfActiveFormattingElements::add(DOM::Element& element)
{
    ensure_noahs_ark_clause(element);
    m_entries.append({ element });
}

void ListOfActiveFormattingElements::add_marker()

Review comment (suggestion):
~~This only ensures that the possible matches have all the attributes that element has.~~
~~Should they also be rejected if they have any extra attributes that element doesn't have?~~

EDIT: Nevermind, that gets checked earlier.

Code after:

void ListOfActiveFormattingElements::visit_edges(JS::Cell::Visitor& visitor)
{
    for (auto& entry : m_entries)
        visitor.visit(entry.element);
}

void ListOfActiveFormattingElements::ensure_noahs_ark_clause(DOM::Element& element)
{
    Vector<Entry> possible_matches;
    for (size_t i = m_entries.size(); i > 0;) {
        i--;
        auto& entry = m_entries[i];
        if (entry.is_marker())
            break;
        if (entry.element->local_name() == element.local_name()
            && entry.element->namespace_uri() == element.namespace_uri()
            && entry.element->attribute_list_size() == element.attribute_list_size())
            possible_matches.append(entry);
    }

    if (possible_matches.size() < NoahsArkCapacity)
        return;

    // FIXME: the attributes should be compared as they where created by the parser
    element.for_each_attribute([&](auto& name, auto& value) {
        possible_matches.remove_all_matching([&](auto& entry) {
            auto attr = entry.element->get_attribute(name);
            return !attr.has_value() || attr != value;
        });
    });

    if (possible_matches.size() < NoahsArkCapacity)
        return;

    remove(*possible_matches.last().element);
}

// https://html.spec.whatwg.org/multipage/parsing.html#push-onto-the-list-of-active-formatting-elements
void ListOfActiveFormattingElements::add(DOM::Element& element)
{
    // 1. If there are already three elements in the list of active formatting elements after the last marker, if any, or anywhere in the list if there are no markers,
    //    that have the same tag name, namespace, and attributes as element, then remove the earliest such element from the list of active formatting elements.
    //    For these purposes, the attributes must be compared as they were when the elements were created by the parser; two elements have the same attributes if all their parsed attributes
    //    can be paired such that the two attributes in each pair have identical names, namespaces, and values (the order of the attributes does not matter).
    ensure_noahs_ark_clause(element);
    // 2. Add element to the list of active formatting elements.
    m_entries.append({ element });
}

void ListOfActiveFormattingElements::add_marker()