LibWeb: Update the select label when option's children are changed

This commit is contained in:
Gingeh 2025-01-10 19:35:03 +11:00 committed by Andreas Kling
parent c87f80454b
commit 58c78cb003
Notes: github-actions[bot] 2025-01-19 18:23:33 +00:00
2 changed files with 23 additions and 7 deletions

View file

@ -38,6 +38,16 @@ void HTMLOptionElement::initialize(JS::Realm& realm)
WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLOptionElement);
}
// FIXME: This needs to be called any time a descendant's text is modified.
void HTMLOptionElement::update_selection_label()
{
if (selected()) {
if (auto* select_element = first_ancestor_of_type<HTMLSelectElement>()) {
select_element->update_inner_text_element({});
}
}
}
void HTMLOptionElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value, Optional<FlyString> const& namespace_)
{
Base::attribute_changed(name, old_value, value, namespace_);
@ -54,6 +64,8 @@ void HTMLOptionElement::attribute_changed(FlyString const& name, Optional<String
if (!m_dirty)
set_selected_internal(true);
}
} else if (name == HTML::AttributeNames::label) {
update_selection_label();
}
}
@ -117,13 +129,7 @@ String HTMLOptionElement::label() const
void HTMLOptionElement::set_label(String const& label)
{
MUST(set_attribute(HTML::AttributeNames::label, label));
// NOTE: This option's select element may need to show different contents now.
if (selected()) {
if (auto select_element = first_ancestor_of_type<HTMLSelectElement>()) {
select_element->update_inner_text_element({});
}
}
// Note: this causes attribute_changed() to be called, which will update the <select>'s label
}
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-text
@ -147,6 +153,7 @@ String HTMLOptionElement::text() const
void HTMLOptionElement::set_text(String const& text)
{
string_replace_all(text);
// Note: this causes children_changed() to be called, which will update the <select>'s label
}
// https://html.spec.whatwg.org/multipage/form-elements.html#concept-option-index
@ -240,4 +247,11 @@ void HTMLOptionElement::removed_from(Node* old_parent)
}
}
void HTMLOptionElement::children_changed()
{
Base::children_changed();
update_selection_label();
}
}