LibWeb: Set the first added <option> element under <select> as selected

This commit is contained in:
Timur Sultanov 2024-07-24 22:32:05 +04:00 committed by Andreas Kling
parent d1a7caa35d
commit afb2e063b7
Notes: github-actions[bot] 2024-07-25 14:59:20 +00:00

View file

@ -133,7 +133,16 @@ HTMLOptionElement* HTMLSelectElement::named_item(FlyString const& name)
WebIDL::ExceptionOr<void> HTMLSelectElement::add(HTMLOptionOrOptGroupElement element, Optional<HTMLElementOrElementIndex> before)
{
// Similarly, the add(element, before) method must act like its namesake method on that same options collection.
return const_cast<HTMLOptionsCollection&>(*options()).add(move(element), move(before));
TRY(const_cast<HTMLOptionsCollection&>(*options()).add(move(element), move(before)));
// If the inserted element is the first and only element, mark it as selected
auto options = list_of_options();
if (options.size() == 1) {
options.at(0)->set_selected(true);
update_inner_text_element();
}
return {};
}
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-remove