1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-09 17:44:56 +09:00

LibWeb/HTML: Iterate over select options in tree order

This also saves us from iterating twice over the children, and
instead do it in a single pass.
This commit is contained in:
Shannon Booth 2025-01-26 14:34:12 +13:00 committed by Tim Ledbetter
parent 4f80c7a5f3
commit 802529bafc
Notes: github-actions[bot] 2025-01-27 00:11:13 +00:00
3 changed files with 29 additions and 11 deletions

View file

@ -203,18 +203,19 @@ Vector<GC::Root<HTMLOptionElement>> HTMLSelectElement::list_of_options() const
// and all the option element children of all the optgroup element children of the select element, in tree order. // and all the option element children of all the optgroup element children of the select element, in tree order.
Vector<GC::Root<HTMLOptionElement>> list; Vector<GC::Root<HTMLOptionElement>> list;
for_each_child_of_type<HTMLOptionElement>([&](HTMLOptionElement& option_element) { for (auto* node = first_child(); node; node = node->next_sibling()) {
list.append(GC::make_root(option_element)); if (auto* maybe_option = as_if<HTMLOptionElement>(*node)) {
return IterationDecision::Continue; list.append(GC::make_root(const_cast<HTMLOptionElement&>(*maybe_option)));
}); continue;
}
for_each_child_of_type<HTMLOptGroupElement>([&](HTMLOptGroupElement const& optgroup_element) { if (auto* maybe_opt_group = as_if<HTMLOptGroupElement>(node)) {
optgroup_element.for_each_child_of_type<HTMLOptionElement>([&](HTMLOptionElement& option_element) { maybe_opt_group->for_each_child_of_type<HTMLOptionElement>([&](HTMLOptionElement& option_element) {
list.append(GC::make_root(option_element)); list.append(GC::make_root(option_element));
return IterationDecision::Continue; return IterationDecision::Continue;
}); });
return IterationDecision::Continue; }
}); }
return list; return list;
} }

View file

@ -0,0 +1,16 @@
<select id="test-select">
<option value="0" selected>Option 0</option>
<optgroup label="Group 1">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</optgroup>
<option value="3" selected>Option 3</option>
<option value="4">Option 4</option>
</select>
<script src="../include.js"></script>
<script>
test(() => {
const selectElement = document.getElementById('test-select');
println(selectElement.selectedIndex);
});
</script>