Lightpanda: the headless browser designed for AI and automation
Simplify OptionIterator and node_live option matcher
Karl Seguin7e6bec8673e4
diff --git a/src/browser/tests/element/html/select-optgroup.html b/src/browser/tests/element/html/select-optgroup.htmlindex 3fcfe0f2f..646150edb 100644--- a/src/browser/tests/element/html/select-optgroup.html+++ b/src/browser/tests/element/html/select-optgroup.html@@ -168,3 +168,41 @@testing.expectEqual('direct', select.options[0].value)}</script>++<script id="options_stop_at_one_optgroup">+ {+ // The parser never nests optgroups, so build the tree by hand: an optgroup+ // inside an optgroup is not walked into, its options are out of the list.+ const select = document.createElement('select')+ const outer = document.createElement('optgroup')+ const inner = document.createElement('optgroup')++ const option = (value) => {+ const o = document.createElement('option')+ o.value = value+ return o+ }++ inner.appendChild(option('buried'))+ outer.appendChild(option('grouped'))+ outer.appendChild(inner)+ outer.appendChild(option('after'))+ select.appendChild(option('before'))+ select.appendChild(outer)+ select.appendChild(option('last'))+ document.body.appendChild(select)++ testing.expectEqual(4, select.length)+ testing.expectEqual(4, select.options.length)+ testing.expectEqual('before', select.options[0].value)+ testing.expectEqual('grouped', select.options[1].value)+ testing.expectEqual('after', select.options[2].value)+ testing.expectEqual('last', select.options[3].value)++ // The iterator resumes after the optgroup, so indexes stay in tree order.+ select.selectedIndex = 3+ testing.expectEqual('last', select.value)+ select.value = 'after'+ testing.expectEqual(2, select.selectedIndex)+ }+</script>diff --git a/src/browser/webapi/collections/node_live.zig b/src/browser/webapi/collections/node_live.zigindex 36c919732..77d5ac2eb 100644--- a/src/browser/webapi/collections/node_live.zig+++ b/src/browser/webapi/collections/node_live.zig@@ -305,19 +305,22 @@ pub fn NodeLive(comptime mode: Mode) type {return el.is(Element.Html.TableCell) != null;},.select_options, .selected_options => {- // The select's list of options is its option children plus- // the option children of its optgroup children — NOT every- // option descendant.- // https://html.spec.whatwg.org/multipage/form-elements.html#the-select-element- const el = node.is(Element) orelse return false;- const Option = Element.Html.Option;- const opt = el.is(Option) orelse return false;+ const opt = node.is(Element.Html.Option) orelse return false;++ // we have an option, but it's only a match IF+ // 1 - this is a direct child of the root (i.e. the <select>)+ // OR+ // 2 - this is a direct child of an <optgroup> which, itself+ // is a direct child of the root (i.e. the <select>)const parent = node.parentNode() orelse return false;- if (parent != self._tw._root) {- const group = parent.is(Element) orelse return false;- if (group.getTag() != .optgroup) return false;- if (parent.parentNode() != self._tw._root) return false;+ if (parent == self._tw._root) {+ // case 1: it _is_ a direct child of the root+ } else {+ if (parent.is(Element.Html.OptGroup) == null or parent.parentNode() != self._tw._root) {+ return false;+ }+ // the parent is an optgroup and its parent is the root}return if (comptime mode == .selected_options) opt.getSelected() else true;diff --git a/src/browser/webapi/element/html/Select.zig b/src/browser/webapi/element/html/Select.zigindex 62eb68329..efe8cffa9 100644--- a/src/browser/webapi/element/html/Select.zig+++ b/src/browser/webapi/element/html/Select.zig@@ -52,36 +52,38 @@ pub fn asConstNode(self: *const Select) *const Node {// the option children of its optgroup children, per HTML// §the-select-element. Options nested any deeper are not in the list.const OptionIterator = struct {- // Cursor over the select's children.- _child: ?*Node,- // Cursor over the current optgroup's children, while inside one.- _in_group: ?*Node = null,+ _node: ?*Node,+ _in_group: bool = false,fn init(select: *const Select) OptionIterator {- return .{ ._child = select.asConstNode().firstChild() };+ return .{ ._node = select.asConstNode().firstChild() };}fn next(self: *OptionIterator) ?*Option {- while (true) {- if (self._in_group) |node| {- self._in_group = node.nextSibling();- if (node.is(Option)) |option| {- return option;+ while (self._node) |node| {+ if (self._in_group == false and node.is(Element.Html.OptGroup) != null) {+ if (node.firstChild()) |first_child| {+ self._in_group = true;+ self._node = first_child;+ continue;}- continue;}- const node = self._child orelse return null;- self._child = node.nextSibling();+ self._node = node.nextSibling() orelse blk: {+ if (self._in_group == false) {+ break :blk null;+ }+ // Exhausted the optgroup, resume after it.+ self._in_group = false;+ const group = node.parentNode() orelse break :blk null;+ break :blk group.nextSibling();+ };if (node.is(Option)) |option| {return option;}- const element = node.is(Element) orelse continue;- if (element.getTag() == .optgroup) {- self._in_group = node.firstChild();- }}+ return null;}};@@ -98,8 +100,12 @@ pub fn effectiveOption(self: *const Select) ?*Option {// Element.isDisabled, not Option.getDisabled: an option is also// disabled when its parent is an <optgroup disabled>// (HTML "concept-option-disabled").- if (option.asElement().isDisabled()) continue;- if (option.getSelected()) return option;+ if (option.asElement().isDisabled()) {+ continue;+ }+ if (option.getSelected()) {+ return option;+ }if (first_option == null) first_option = option;}return first_option;