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

Code before:
                set_selected_internal(true);
        }
    } else if (name == HTML::AttributeNames::label) {
        update_selection_label();
    }
}

// https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-selected
void HTMLOptionElement::set_selected(bool selected)
{
    // On setting, it must set the element's selectedness to the new value, set its dirtiness to true, and then cause the element to ask for a reset.
    set_selected_internal(selected);
    m_dirty = true;
    ask_for_a_reset();
}

void HTMLOptionElement::set_selected_internal(bool selected)
{
    if (m_selected != selected)
        invalidate_style(DOM::StyleInvalidationReason::HTMLOptionElementSelectedChange);

    m_selected = selected;
    if (selected)
        m_selectedness_update_index = m_next_selectedness_update_index++;

    // AD-HOC this is here to break the cache on the HTMLCollection in HTMLSelectElement::selected_options
    document().bump_dom_tree_version();
}

// https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-value
Utf16String HTMLOptionElement::value() const
{
    // The value of an option element is the value of the value content attribute, if there is one.
    // ...or, if there is not, the value of the element's text IDL attribute.
    if (auto value = attribute(HTML::AttributeNames::value); value.has_value())
        return Utf16String::from_utf8(*value);
    return text();
}

// https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-value
WebIDL::ExceptionOr<void> HTMLOptionElement::set_value(Utf16String const& value)
{
    return set_attribute(HTML::AttributeNames::value, value);
}

static void concatenate_descendants_text_content(DOM::Node const* node, StringBuilder& builder)
{
    if (is<HTMLScriptElement>(node) || is<SVG::SVGScriptElement>(node))
        return;
    if (is<DOM::Text>(node))
        builder.append(as<DOM::Text>(node)->data());

Review comment (suggestion):
You don't need to tag your comments with `AD-HOC` if the implementation isn't following any particular specification.

Code after:
        if (!value.has_value()) {
            // Whenever an option element's selected attribute is removed, if its dirtiness is false, its selectedness must be set to false.
            if (!m_dirty)
                set_selected_internal(false);
        } else {
            // Except where otherwise specified, when the element is created, its selectedness must be set to true
            // if the element has a selected attribute. Whenever an option element's selected attribute is added,
            // if its dirtiness is false, its selectedness must be set to true.
            if (!m_dirty)
                set_selected_internal(true);
        }
    } else if (name == HTML::AttributeNames::label) {
        update_selection_label();
    }
}

// https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-selected
void HTMLOptionElement::set_selected(bool selected)
{
    // On setting, it must set the element's selectedness to the new value, set its dirtiness to true, and then cause the element to ask for a reset.
    set_selected_internal(selected);
    m_dirty = true;
    ask_for_a_reset();
}

void HTMLOptionElement::set_selected_internal(bool selected)
{
    if (m_selected != selected)
        invalidate_style(DOM::StyleInvalidationReason::HTMLOptionElementSelectedChange);

    m_selected = selected;
    if (selected)
        m_selectedness_update_index = m_next_selectedness_update_index++;

    // this is here to invalidate the cache on the HTMLCollection in HTMLSelectElement::selected_options
    document().bump_dom_tree_version();
}

// https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-value
Utf16String HTMLOptionElement::value() const
{
    // The value of an option element is the value of the value content attribute, if there is one.
    // ...or, if there is not, the value of the element's text IDL attribute.
    if (auto value = attribute(HTML::AttributeNames::value); value.has_value())
        return Utf16String::from_utf8(*value);
    return text();
}

// https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-value
void HTMLOptionElement::set_value(Utf16String const& value)
{