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

@ -135,8 +135,8 @@ impl VirtualMethods for HTMLOptGroupElement {
}
fn bind_to_tree(&self, context: &BindContext, can_gc: CanGc) {
if let Some(s) = self.super_type() {
s.bind_to_tree(context, can_gc);
if let Some(super_type) = self.super_type() {
super_type.bind_to_tree(context, can_gc);
}
self.update_select_validity(can_gc);

View file

@ -29,7 +29,7 @@ use crate::dom::htmlformelement::HTMLFormElement;
use crate::dom::htmloptgroupelement::HTMLOptGroupElement;
use crate::dom::htmlscriptelement::HTMLScriptElement;
use crate::dom::htmlselectelement::HTMLSelectElement;
use crate::dom::node::{BindContext, Node, ShadowIncluding, UnbindContext};
use crate::dom::node::{BindContext, ChildrenMutation, Node, ShadowIncluding, UnbindContext};
use crate::dom::text::Text;
use crate::dom::validation::Validatable;
use crate::dom::validitystate::ValidationFlags;
@ -380,4 +380,26 @@ impl VirtualMethods for HTMLOptionElement {
el.check_disabled_attribute();
}
}
fn children_changed(&self, mutation: &ChildrenMutation) {
if let Some(super_type) = self.super_type() {
super_type.children_changed(mutation);
}
// Changing the descendants of a selected option can change it's displayed label
// if it does not have a label attribute
if !self
.upcast::<Element>()
.has_attribute(&local_name!("label"))
{
if let Some(owner_select) = self.owner_select_element() {
if owner_select
.selected_option()
.is_some_and(|selected_option| self == &*selected_option)
{
owner_select.update_shadow_tree(CanGc::note());
}
}
}
}
}

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()
}

View file

@ -1,2 +0,0 @@
[select-1-block-size.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[select-multiple-re-add-option-via-document-fragment.html]
expected: FAIL