LibWeb: Add HTMLOptionsCollection selected index property

This commit is contained in:
Bastiaan van der Plaat 2024-04-08 21:53:22 +02:00 committed by Andreas Kling
parent 5c277144d8
commit 5decf4b33c
5 changed files with 29 additions and 1 deletions

View file

@ -11,3 +11,4 @@
11. 999
12. 10
13. 10
14. "5 5"

View file

@ -135,5 +135,16 @@
select.length = -10;
return select.length;
});
// 14. OptionsCollection selectedIndex is the same as select selectedIndex
testPart(() => {
const select = document.createElement('select');
for (let i = 0; i < 10; i++) {
const option = document.createElement('option');
if (i == 5) option.selected = true;
select.appendChild(option);
}
return `${select.options.selectedIndex} ${select.selectedIndex}`;
});
});
</script>

View file

@ -111,4 +111,17 @@ WebIDL::ExceptionOr<void> HTMLOptionsCollection::add(HTMLOptionOrOptGroupElement
return {};
}
// https://html.spec.whatwg.org/#dom-htmloptionscollection-selectedindex
WebIDL::Long HTMLOptionsCollection::selected_index() const
{
// The selectedIndex IDL attribute must act like the identically named attribute
// on the select element on which the HTMLOptionsCollection is rooted.
return verify_cast<HTMLSelectElement>(*root()).selected_index();
}
void HTMLOptionsCollection::set_selected_index(WebIDL::Long index)
{
verify_cast<HTMLSelectElement>(*root()).set_selected_index(index);
}
}

View file

@ -28,6 +28,9 @@ public:
WebIDL::ExceptionOr<void> add(HTMLOptionOrOptGroupElement element, Optional<HTMLElementOrElementIndex> before = {});
WebIDL::Long selected_index() const;
void set_selected_index(WebIDL::Long);
private:
HTMLOptionsCollection(DOM::ParentNode& root, Function<bool(DOM::Element const&)> filter);

View file

@ -9,5 +9,5 @@ interface HTMLOptionsCollection : HTMLCollection {
// [CEReactions] setter undefined (unsigned long index, HTMLOptionElement? option);
[CEReactions] undefined add((HTMLOptionElement or HTMLOptGroupElement) element, optional (HTMLElement or long)? before = null);
// [CEReactions] undefined remove(long index);
// attribute long selectedIndex;
attribute long selectedIndex;
};