Update select shadow tree when contents of selected option change (#36958)

The label that is displayed inside a `<select>` element is that of the
selected option, or it's text contents if the option does not have a
label. Therefore, we need to update the `<select>` shadow tree when the
contents of the selected option change.

Testing: Covered by existing web platform tests
Fixes https://github.com/servo/servo/issues/36926
Fixes https://github.com/servo/servo/issues/36925

---------

Signed-off-by: Simon Wülker <simon.wuelker@arcor.de>
This commit is contained in:
Simon Wülker 2025-05-11 18:21:56 +02:00 committed by GitHub
parent fec6778eaa
commit 41283220dd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 32 additions and 11 deletions

View file

@ -153,7 +153,7 @@ impl HTMLSelectElement {
n
}
// https://html.spec.whatwg.org/multipage/#concept-select-option-list
/// <https://html.spec.whatwg.org/multipage/#concept-select-option-list>
pub(crate) fn list_of_options(
&self,
) -> impl Iterator<Item = DomRoot<HTMLOptionElement>> + use<'_> {
@ -353,8 +353,10 @@ impl HTMLSelectElement {
.fire_bubbling_event(atom!("change"), can_gc);
}
fn selected_option(&self) -> Option<DomRoot<HTMLOptionElement>> {
self.list_of_options().find(|opt_elem| opt_elem.Selected())
pub(crate) fn selected_option(&self) -> Option<DomRoot<HTMLOptionElement>> {
self.list_of_options()
.find(|opt_elem| opt_elem.Selected())
.or_else(|| self.list_of_options().next())
}
pub(crate) fn show_menu(&self, can_gc: CanGc) -> Option<usize> {
@ -539,7 +541,8 @@ impl HTMLSelectElementMethods<crate::DomTypeHolder> for HTMLSelectElement {
/// <https://html.spec.whatwg.org/multipage/#dom-select-value>
fn Value(&self) -> DOMString {
self.selected_option()
self.list_of_options()
.find(|opt_elem| opt_elem.Selected())
.map(|opt_elem| opt_elem.Value())
.unwrap_or_default()
}