2020-08-01 03:07:00 +01:00
|
|
|
|
/*
|
2021-04-28 22:46:44 +02:00
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2022-03-20 16:13:23 +01:00
|
|
|
|
* Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
|
2020-08-01 03:07:00 +01:00
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-08-01 03:07:00 +01:00
|
|
|
|
*/
|
|
|
|
|
|
2022-09-30 17:16:16 -06:00
|
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
2021-04-20 22:52:55 +02:00
|
|
|
|
#include <LibWeb/HTML/HTMLFormElement.h>
|
2022-03-20 16:13:23 +01:00
|
|
|
|
#include <LibWeb/HTML/HTMLOptGroupElement.h>
|
2022-03-16 13:08:12 +01:00
|
|
|
|
#include <LibWeb/HTML/HTMLOptionElement.h>
|
2020-08-01 03:07:00 +01:00
|
|
|
|
#include <LibWeb/HTML/HTMLSelectElement.h>
|
|
|
|
|
|
|
|
|
|
namespace Web::HTML {
|
|
|
|
|
|
2022-02-18 21:00:52 +01:00
|
|
|
|
HTMLSelectElement::HTMLSelectElement(DOM::Document& document, DOM::QualifiedName qualified_name)
|
2022-03-23 18:55:54 -04:00
|
|
|
|
: HTMLElement(document, move(qualified_name))
|
2020-08-01 03:07:00 +01:00
|
|
|
|
{
|
2022-09-25 16:38:21 -06:00
|
|
|
|
set_prototype(&Bindings::cached_web_prototype(realm(), "HTMLSelectElement"));
|
2020-08-01 03:07:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-14 13:21:51 -06:00
|
|
|
|
HTMLSelectElement::~HTMLSelectElement() = default;
|
2020-08-01 03:07:00 +01:00
|
|
|
|
|
2022-09-01 20:50:16 +02:00
|
|
|
|
void HTMLSelectElement::visit_edges(Cell::Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
|
visitor.visit(m_options.ptr());
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-16 13:08:12 +01:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-options
|
2022-09-01 20:50:16 +02:00
|
|
|
|
JS::GCPtr<HTMLOptionsCollection> const& HTMLSelectElement::options()
|
2022-03-16 13:08:12 +01:00
|
|
|
|
{
|
|
|
|
|
if (!m_options) {
|
|
|
|
|
m_options = HTMLOptionsCollection::create(*this, [](DOM::Element const& element) {
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/form-elements.html#concept-select-option-list
|
|
|
|
|
// The list of options for a select element consists of all the option element children of
|
|
|
|
|
// the select element, and all the option element children of all the optgroup element children
|
|
|
|
|
// of the select element, in tree order.
|
|
|
|
|
return is<HTMLOptionElement>(element);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return m_options;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-21 20:03:37 -04:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-add
|
2022-09-25 17:03:42 +01:00
|
|
|
|
WebIDL::ExceptionOr<void> HTMLSelectElement::add(HTMLOptionOrOptGroupElement element, Optional<HTMLElementOrElementIndex> before)
|
2022-03-21 20:03:37 -04:00
|
|
|
|
{
|
|
|
|
|
// Similarly, the add(element, before) method must act like its namesake method on that same options collection.
|
2022-09-01 20:50:16 +02:00
|
|
|
|
return const_cast<HTMLOptionsCollection&>(*options()).add(move(element), move(before));
|
2022-03-21 20:03:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-20 16:13:23 +01:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/form-elements.html#concept-select-option-list
|
2022-08-28 13:42:07 +02:00
|
|
|
|
Vector<JS::Handle<HTMLOptionElement>> HTMLSelectElement::list_of_options() const
|
2022-03-20 16:13:23 +01:00
|
|
|
|
{
|
|
|
|
|
// The list of options for a select element consists of all the option element children of the select element,
|
|
|
|
|
// and all the option element children of all the optgroup element children of the select element, in tree order.
|
2022-08-28 13:42:07 +02:00
|
|
|
|
Vector<JS::Handle<HTMLOptionElement>> list;
|
2022-03-20 16:13:23 +01:00
|
|
|
|
|
|
|
|
|
for_each_child_of_type<HTMLOptionElement>([&](HTMLOptionElement const& option_element) {
|
2022-08-28 13:42:07 +02:00
|
|
|
|
list.append(JS::make_handle(const_cast<HTMLOptionElement&>(option_element)));
|
2022-03-20 16:13:23 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for_each_child_of_type<HTMLOptGroupElement>([&](HTMLOptGroupElement const& optgroup_element) {
|
|
|
|
|
optgroup_element.for_each_child_of_type<HTMLOptionElement>([&](HTMLOptionElement const& option_element) {
|
2022-08-28 13:42:07 +02:00
|
|
|
|
list.append(JS::make_handle(const_cast<HTMLOptionElement&>(option_element)));
|
2022-03-20 16:13:23 +01:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-selectedindex
|
|
|
|
|
int HTMLSelectElement::selected_index() const
|
|
|
|
|
{
|
|
|
|
|
// The selectedIndex IDL attribute, on getting, must return the index of the first option element in the list of options
|
|
|
|
|
// in tree order that has its selectedness set to true, if any. If there isn't one, then it must return −1.
|
|
|
|
|
|
|
|
|
|
int index = 0;
|
|
|
|
|
for (auto const& option_element : list_of_options()) {
|
2022-08-28 13:42:07 +02:00
|
|
|
|
if (option_element->selected())
|
2022-03-20 16:13:23 +01:00
|
|
|
|
return index;
|
|
|
|
|
++index;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HTMLSelectElement::set_selected_index(int index)
|
|
|
|
|
{
|
|
|
|
|
// On setting, the selectedIndex attribute must set the selectedness of all the option elements in the list of options to false,
|
|
|
|
|
// and then the option element in the list of options whose index is the given new value,
|
|
|
|
|
// if any, must have its selectedness set to true and its dirtiness set to true.
|
|
|
|
|
auto options = list_of_options();
|
|
|
|
|
for (auto& option : options)
|
2022-08-28 13:42:07 +02:00
|
|
|
|
option->m_selected = false;
|
2022-03-20 16:13:23 +01:00
|
|
|
|
|
|
|
|
|
if (index < 0 || index >= static_cast<int>(options.size()))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
auto& selected_option = options[index];
|
2022-08-28 13:42:07 +02:00
|
|
|
|
selected_option->m_selected = true;
|
|
|
|
|
selected_option->m_dirty = true;
|
2022-03-20 16:13:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-01 03:07:00 +01:00
|
|
|
|
}
|