diff --git a/Libraries/LibWeb/HTML/HTMLSelectElement.cpp b/Libraries/LibWeb/HTML/HTMLSelectElement.cpp
index 424fe131041..a99d4da9fb1 100644
--- a/Libraries/LibWeb/HTML/HTMLSelectElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLSelectElement.cpp
@@ -203,18 +203,19 @@ Vector> HTMLSelectElement::list_of_options() const
// and all the option element children of all the optgroup element children of the select element, in tree order.
Vector> list;
- for_each_child_of_type([&](HTMLOptionElement& option_element) {
- list.append(GC::make_root(option_element));
- return IterationDecision::Continue;
- });
+ for (auto* node = first_child(); node; node = node->next_sibling()) {
+ if (auto* maybe_option = as_if(*node)) {
+ list.append(GC::make_root(const_cast(*maybe_option)));
+ continue;
+ }
- for_each_child_of_type([&](HTMLOptGroupElement const& optgroup_element) {
- optgroup_element.for_each_child_of_type([&](HTMLOptionElement& option_element) {
- list.append(GC::make_root(option_element));
- return IterationDecision::Continue;
- });
- return IterationDecision::Continue;
- });
+ if (auto* maybe_opt_group = as_if(node)) {
+ maybe_opt_group->for_each_child_of_type([&](HTMLOptionElement& option_element) {
+ list.append(GC::make_root(option_element));
+ return IterationDecision::Continue;
+ });
+ }
+ }
return list;
}
diff --git a/Tests/LibWeb/Text/expected/HTML/select-index-is-in-tree-order.txt b/Tests/LibWeb/Text/expected/HTML/select-index-is-in-tree-order.txt
new file mode 100644
index 00000000000..00750edc07d
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/select-index-is-in-tree-order.txt
@@ -0,0 +1 @@
+3
diff --git a/Tests/LibWeb/Text/input/HTML/select-index-is-in-tree-order.html b/Tests/LibWeb/Text/input/HTML/select-index-is-in-tree-order.html
new file mode 100644
index 00000000000..88188698ec3
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/select-index-is-in-tree-order.html
@@ -0,0 +1,16 @@
+
+
+