From dbc7e6bd2ffb36d7038d6717fe47af9653907210 Mon Sep 17 00:00:00 2001 From: Psychpsyo Date: Sat, 16 Nov 2024 22:14:42 +0100 Subject: [PATCH] LibWeb: Make select element use option's label, not text content --- Libraries/LibWeb/HTML/HTMLOptionElement.cpp | 7 +++++++ Libraries/LibWeb/HTML/HTMLSelectElement.cpp | 7 ++++--- Libraries/LibWeb/HTML/HTMLSelectElement.h | 2 ++ Tests/LibWeb/Ref/expected/select-option-use-label.html | 4 ++++ Tests/LibWeb/Ref/input/select-option-use-label.html | 5 +++++ 5 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 Tests/LibWeb/Ref/expected/select-option-use-label.html create mode 100644 Tests/LibWeb/Ref/input/select-option-use-label.html diff --git a/Libraries/LibWeb/HTML/HTMLOptionElement.cpp b/Libraries/LibWeb/HTML/HTMLOptionElement.cpp index 87995bb31df..e6393715cdb 100644 --- a/Libraries/LibWeb/HTML/HTMLOptionElement.cpp +++ b/Libraries/LibWeb/HTML/HTMLOptionElement.cpp @@ -114,6 +114,13 @@ 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()) { + select_element->update_inner_text_element(); + } + } } // https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-text diff --git a/Libraries/LibWeb/HTML/HTMLSelectElement.cpp b/Libraries/LibWeb/HTML/HTMLSelectElement.cpp index 82e2490d2f2..07b67e84ebb 100644 --- a/Libraries/LibWeb/HTML/HTMLSelectElement.cpp +++ b/Libraries/LibWeb/HTML/HTMLSelectElement.cpp @@ -399,7 +399,7 @@ void HTMLSelectElement::show_the_picker_if_applicable() for (auto const& child : opt_group_element.children_as_vector()) { if (is(*child)) { auto& option_element = verify_cast(*child); - option_group_items.append(SelectItemOption { id_counter++, option_element.selected(), option_element.disabled(), option_element, strip_newlines(option_element.text_content()), option_element.value() }); + option_group_items.append(SelectItemOption { id_counter++, option_element.selected(), option_element.disabled(), option_element, strip_newlines(option_element.label()), option_element.value() }); } } m_select_items.append(SelectItemOptionGroup { opt_group_element.get_attribute(AttributeNames::label).value_or(String {}), option_group_items }); @@ -407,7 +407,7 @@ void HTMLSelectElement::show_the_picker_if_applicable() if (is(*child)) { auto& option_element = verify_cast(*child); - m_select_items.append(SelectItemOption { id_counter++, option_element.selected(), option_element.disabled(), option_element, strip_newlines(option_element.text_content()), option_element.value() }); + m_select_items.append(SelectItemOption { id_counter++, option_element.selected(), option_element.disabled(), option_element, strip_newlines(option_element.label()), option_element.value() }); } if (is(*child)) @@ -553,6 +553,7 @@ void HTMLSelectElement::create_shadow_tree_if_needed() update_inner_text_element(); } +// FIXME: This needs to be called any time the selected option's children are modified. void HTMLSelectElement::update_inner_text_element() { if (!m_inner_text_element) @@ -561,7 +562,7 @@ void HTMLSelectElement::update_inner_text_element() // Update inner text element to text content of selected option for (auto const& option_element : list_of_options()) { if (option_element->selected()) { - m_inner_text_element->set_text_content(strip_newlines(option_element->text_content())); + m_inner_text_element->set_text_content(strip_newlines(option_element->label())); return; } } diff --git a/Libraries/LibWeb/HTML/HTMLSelectElement.h b/Libraries/LibWeb/HTML/HTMLSelectElement.h index c9656fea7b7..d3359bac066 100644 --- a/Libraries/LibWeb/HTML/HTMLSelectElement.h +++ b/Libraries/LibWeb/HTML/HTMLSelectElement.h @@ -97,6 +97,8 @@ public: void update_selectedness(); private: + friend class HTMLOptionElement; + HTMLSelectElement(DOM::Document&, DOM::QualifiedName); virtual void initialize(JS::Realm&) override; diff --git a/Tests/LibWeb/Ref/expected/select-option-use-label.html b/Tests/LibWeb/Ref/expected/select-option-use-label.html new file mode 100644 index 00000000000..aad9e583640 --- /dev/null +++ b/Tests/LibWeb/Ref/expected/select-option-use-label.html @@ -0,0 +1,4 @@ + + diff --git a/Tests/LibWeb/Ref/input/select-option-use-label.html b/Tests/LibWeb/Ref/input/select-option-use-label.html new file mode 100644 index 00000000000..c26b6d1e2a9 --- /dev/null +++ b/Tests/LibWeb/Ref/input/select-option-use-label.html @@ -0,0 +1,5 @@ + + +