Repository: LadybirdBrowser/ladybird
File: Libraries/LibWeb/CSS/CSSStyleProperties.cpp (C++)

Code before:
        return to_underlying(last_longhand_property_id) - to_underlying(first_longhand_property_id) + 1;
    }

    return m_properties.size() + m_custom_properties.size();
}

String CSSStyleProperties::item(size_t index) const
{
    // The item(index) method must return the property name of the CSS declaration at position index.
    // If there is no indexth object in the collection, then the method must return the empty string.
    auto custom_properties_count = m_custom_properties.size();

    if (index >= length())
        return {};

    if (is_computed()) {
        auto property_id = static_cast<PropertyID>(index + to_underlying(first_longhand_property_id));
        return string_from_property_id(property_id).to_string();
    }

    if (index < custom_properties_count) {
        auto keys = m_custom_properties.keys();
        auto custom_property = m_custom_properties.get(keys[index]);
        if (custom_property.has_value()) {
            return CSS::string_from_property_id(custom_property.ptr()->property_id).to_string();
        }
    }

    return CSS::string_from_property_id(m_properties[index - custom_properties_count].property_id).to_string();
}

Optional<StyleProperty> CSSStyleProperties::property(PropertyID property_id) const
{
    if (is_computed()) {
        if (!owner_node().has_value())
            return {};

        auto& element = owner_node()->element();
        auto pseudo_element = owner_node()->pseudo_element();

        // https://www.w3.org/TR/cssom-1/#dom-window-getcomputedstyle
        // NB: This is a partial enforcement of step 5 ("If elt is connected, ...")
        if (!element.is_connected())
            return {};

        auto get_layout_node = [&]() {
            if (pseudo_element.has_value())
                return element.get_pseudo_element_node(pseudo_element.value());
            return element.layout_node();
        };


Review comment (bug):
This `if` shouldn't be needed: If the HashMap can't find a value for its own key, something has gone very wrong. Also in that case, we'd end up falling through to the return below this which would do a lookup with a wrong index.

Code after:
        return number_of_longhand_properties;
    }

    return m_properties.size() + m_custom_properties.size();
}

String CSSStyleProperties::item(size_t index) const
{
    // The item(index) method must return the property name of the CSS declaration at position index.
    // If there is no indexth object in the collection, then the method must return the empty string.
    auto custom_properties_count = m_custom_properties.size();

    if (index >= length())
        return {};

    if (is_computed()) {
        auto property_id = static_cast<PropertyID>(index + to_underlying(first_longhand_property_id));
        return string_from_property_id(property_id).to_string();
    }

    if (index < custom_properties_count) {
        auto keys = m_custom_properties.keys();
        auto custom_property = m_custom_properties.get(keys[index]);
        return custom_property.ptr()->custom_name.to_string();
    }

    return CSS::string_from_property_id(m_properties[index - custom_properties_count].property_id).to_string();
}

Optional<StyleProperty> CSSStyleProperties::property(PropertyID property_id) const
{
    if (is_computed()) {
        if (!owner_node().has_value())
            return {};

        auto abstract_element = *owner_node();

        // https://www.w3.org/TR/cssom-1/#dom-window-getcomputedstyle
        // NB: This is a partial enforcement of step 5 ("If elt is connected, ...")
        if (!abstract_element.element().is_connected())
            return {};

        Layout::NodeWithStyle* layout_node = abstract_element.layout_node();

        // FIXME: Be smarter about updating layout if there's no layout node.
        //        We may legitimately have no layout node if we're not visible, but this protects against situations
        //        where we're requesting the computed style before layout has happened.
        if (!layout_node || property_needs_layout_for_getcomputedstyle(property_id)) {
            abstract_element.document().update_layout(DOM::UpdateLayoutReason::ResolvedCSSStyleDeclarationProperty);
            layout_node = abstract_element.layout_node();
        } else {