LibWeb: Mirror options collection on the select element

This commit is contained in:
Lorenz A 2025-11-06 17:51:35 +01:00 committed by Shannon Booth
parent e4fb4d7c1a
commit 3e296a1b92
Notes: github-actions[bot] 2026-01-06 12:05:11 +00:00
5 changed files with 37 additions and 13 deletions

View file

@ -39,6 +39,11 @@ GC_DEFINE_ALLOCATOR(HTMLSelectElement);
HTMLSelectElement::HTMLSelectElement(DOM::Document& document, DOM::QualifiedName qualified_name)
: HTMLElement(document, move(qualified_name))
{
m_legacy_platform_object_flags = LegacyPlatformObjectFlags {
.supports_indexed_properties = true,
.has_indexed_property_setter = true,
.indexed_property_setter_has_identifier = true,
};
}
HTMLSelectElement::~HTMLSelectElement() = default;
@ -120,12 +125,12 @@ void HTMLSelectElement::set_size(WebIDL::UnsignedLong size)
}
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-options
GC::Ptr<HTMLOptionsCollection> const& HTMLSelectElement::options()
GC::Ptr<HTMLOptionsCollection> const& HTMLSelectElement::options() const
{
// The options IDL attribute must return an HTMLOptionsCollection rooted at the select node,
// whose filter matches the elements in the list of options.
if (!m_options) {
m_options = HTMLOptionsCollection::create(*this, [this](DOM::Element const& element) {
m_options = HTMLOptionsCollection::create(const_cast<HTMLSelectElement&>(*this), [this](DOM::Element const& element) {
auto const* maybe_option = as_if<HTML::HTMLOptionElement>(element);
return maybe_option && maybe_option->nearest_select_element() == this;
});
@ -153,6 +158,14 @@ HTMLOptionElement* HTMLSelectElement::item(WebIDL::UnsignedLong index)
return as<HTMLOptionElement>(const_cast<HTMLOptionsCollection&>(*options()).item(index));
}
// https://html.spec.whatwg.org/multipage/form-elements.html#the-select-element:htmlselectelement
Optional<JS::Value> HTMLSelectElement::item_value(size_t index) const
{
// The options collection is also mirrored on the HTMLSelectElement object. The supported property indices at any
// instant are the indices supported by the object returned by the options attribute at that instant.
return (const_cast<HTMLOptionsCollection&>(*options()).item_value(index));
}
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-nameditem
HTMLOptionElement* HTMLSelectElement::named_item(FlyString const& name)
{
@ -171,6 +184,16 @@ WebIDL::ExceptionOr<void> HTMLSelectElement::add(HTMLOptionOrOptGroupElement ele
return {};
}
// https://html.spec.whatwg.org/multipage/form-elements.html#the-select-element:set-the-value-of-a-new-indexed-property
WebIDL::ExceptionOr<void> HTMLSelectElement::set_value_of_indexed_property(u32 n, JS::Value new_value)
{
// When the user agent is to set the value of a new indexed property or set the value of an existing indexed property
// for a select element, it must instead run the corresponding algorithm on the select element's options collection.
TRY(const_cast<HTMLOptionsCollection&>(*options()).set_value_of_indexed_property(n, new_value));
return {};
}
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-remove
void HTMLSelectElement::remove()
{