summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/HTML
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2022-03-20 16:13:23 +0100
committerAndreas Kling <kling@serenityos.org>2022-03-20 16:19:47 +0100
commita6063455766a0cf642b806e4c9e6edbed1d290bd (patch)
tree7792525af992a347ed9e6c0c1fa467c6d75ac167 /Userland/Libraries/LibWeb/HTML
parent5dc085548947984fde41a48046ddc805162c0a27 (diff)
downloadserenity-a6063455766a0cf642b806e4c9e6edbed1d290bd.zip
LibWeb: Implement HTMLSelectElement.selectedIndex
Diffstat (limited to 'Userland/Libraries/LibWeb/HTML')
-rw-r--r--Userland/Libraries/LibWeb/HTML/HTMLOptionElement.h8
-rw-r--r--Userland/Libraries/LibWeb/HTML/HTMLSelectElement.cpp55
-rw-r--r--Userland/Libraries/LibWeb/HTML/HTMLSelectElement.h7
-rw-r--r--Userland/Libraries/LibWeb/HTML/HTMLSelectElement.idl2
4 files changed, 70 insertions, 2 deletions
diff --git a/Userland/Libraries/LibWeb/HTML/HTMLOptionElement.h b/Userland/Libraries/LibWeb/HTML/HTMLOptionElement.h
index f98e174a31..b043b0dc49 100644
--- a/Userland/Libraries/LibWeb/HTML/HTMLOptionElement.h
+++ b/Userland/Libraries/LibWeb/HTML/HTMLOptionElement.h
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
+ * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@@ -16,6 +17,13 @@ public:
HTMLOptionElement(DOM::Document&, DOM::QualifiedName);
virtual ~HTMLOptionElement() override;
+
+private:
+ // https://html.spec.whatwg.org/multipage/form-elements.html#concept-option-selectedness
+ bool m_selected { false };
+
+ // https://html.spec.whatwg.org/multipage/form-elements.html#concept-option-dirtiness
+ bool m_dirty { false };
};
}
diff --git a/Userland/Libraries/LibWeb/HTML/HTMLSelectElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLSelectElement.cpp
index 84329b7610..9853f60e90 100644
--- a/Userland/Libraries/LibWeb/HTML/HTMLSelectElement.cpp
+++ b/Userland/Libraries/LibWeb/HTML/HTMLSelectElement.cpp
@@ -1,11 +1,12 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
- * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
+ * Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/HTML/HTMLFormElement.h>
+#include <LibWeb/HTML/HTMLOptGroupElement.h>
#include <LibWeb/HTML/HTMLOptionElement.h>
#include <LibWeb/HTML/HTMLSelectElement.h>
@@ -33,4 +34,56 @@ RefPtr<HTMLOptionsCollection> const& HTMLSelectElement::options()
return m_options;
}
+// https://html.spec.whatwg.org/multipage/form-elements.html#concept-select-option-list
+NonnullRefPtrVector<HTMLOptionElement> HTMLSelectElement::list_of_options() const
+{
+ // 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.
+ NonnullRefPtrVector<HTMLOptionElement> list;
+
+ for_each_child_of_type<HTMLOptionElement>([&](HTMLOptionElement const& option_element) {
+ list.append(option_element);
+ });
+
+ for_each_child_of_type<HTMLOptGroupElement>([&](HTMLOptGroupElement const& optgroup_element) {
+ optgroup_element.for_each_child_of_type<HTMLOptionElement>([&](HTMLOptionElement const& option_element) {
+ list.append(option_element);
+ });
+ });
+
+ 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()) {
+ if (option_element.selected())
+ 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)
+ option.m_selected = false;
+
+ if (index < 0 || index >= static_cast<int>(options.size()))
+ return;
+
+ auto& selected_option = options[index];
+ selected_option.m_selected = true;
+ selected_option.m_dirty = true;
+}
+
}
diff --git a/Userland/Libraries/LibWeb/HTML/HTMLSelectElement.h b/Userland/Libraries/LibWeb/HTML/HTMLSelectElement.h
index b59ef5c2d6..1d0f3964dc 100644
--- a/Userland/Libraries/LibWeb/HTML/HTMLSelectElement.h
+++ b/Userland/Libraries/LibWeb/HTML/HTMLSelectElement.h
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
- * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
+ * Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
@@ -23,6 +23,11 @@ public:
RefPtr<HTMLOptionsCollection> const& options();
+ int selected_index() const;
+ void set_selected_index(int);
+
+ NonnullRefPtrVector<HTMLOptionElement> list_of_options() const;
+
// ^FormAssociatedElement
// https://html.spec.whatwg.org/multipage/forms.html#category-listed
virtual bool is_listed() const override { return true; }
diff --git a/Userland/Libraries/LibWeb/HTML/HTMLSelectElement.idl b/Userland/Libraries/LibWeb/HTML/HTMLSelectElement.idl
index 6a2a348242..e473f31070 100644
--- a/Userland/Libraries/LibWeb/HTML/HTMLSelectElement.idl
+++ b/Userland/Libraries/LibWeb/HTML/HTMLSelectElement.idl
@@ -8,4 +8,6 @@ interface HTMLSelectElement : HTMLElement {
[Reflect] attribute boolean required;
[SameObject] readonly attribute HTMLOptionsCollection options;
+ attribute long selectedIndex;
+
};