script: Implement HTMLSelectElement.selectedOptions (#43017)

This introduces a `CollectionSource` trait as an alternative to
`CollectionFilter` in `HTMLCollection`, allowing collections to provide
elements directly via a custom iterator rather than filtering a full
subtree traversal, which would otherwise be rather inefficient for
smaller iterable sequences of options that can be determined without
traversing the whole subtree again.

The newly implemented `selectedOptions` attribute on the `<select>`
element uses this to iterate only the element's list of options that are
marked as selected.

Testing: 14 assertions in the existing WPT went from failing to passing.

Fixes #15522.

---------

Signed-off-by: Jacob Adam <software@jacobadam.net>
This commit is contained in:
Jacob Adam W.
2026-03-07 06:16:33 +00:00
committed by GitHub
parent 42d228dcc7
commit d6fdd30c19
10 changed files with 250 additions and 141 deletions

View File

@@ -89,6 +89,9 @@ impl HTMLOptionElement {
pub(crate) fn set_selectedness(&self, selected: bool) {
self.selectedness.set(selected);
// Bump the tree version so that any live HTMLCollection (e.g. selectedOptions)
// rooted at an ancestor invalidates its cached length and cursor.
self.upcast::<Node>().rev_version();
}
pub(crate) fn set_dirtiness(&self, dirtiness: bool) {
@@ -373,7 +376,7 @@ impl HTMLOptionElementMethods<crate::DomTypeHolder> for HTMLOptionElement {
/// <https://html.spec.whatwg.org/multipage/#dom-option-selected>
fn SetSelected(&self, selected: bool, can_gc: CanGc) {
self.dirtiness.set(true);
self.selectedness.set(selected);
self.set_selectedness(selected);
self.pick_if_selected_and_reset();
self.update_select_validity(can_gc);
}