Lightpanda: the headless browser designed for AI and automation
webapi: option.selected = true deselects its siblings in a single select
Adrià Arrufat68e11c57f20d
diff --git a/src/browser/tests/element/html/select.html b/src/browser/tests/element/html/select.htmlindex b10728f0b..4556d3540 100644--- a/src/browser/tests/element/html/select.html+++ b/src/browser/tests/element/html/select.html@@ -491,3 +491,34 @@testing.expectEqual('d2', select.options[1].value)}</script>++<select id="single">+ <option id="s1" value="1" selected>1</option>+ <option id="s2" value="2">2</option>+ <optgroup><option id="s3" value="3">3</option></optgroup>+</select>+<select id="multi" multiple>+ <option id="m1" selected>1</option>+ <option id="m2">2</option>+</select>++<script id="option_selected_deselects_siblings">+{+ $('#s2').selected = true;+ testing.expectEqual(true, $('#s2').selected);+ testing.expectEqual(false, $('#s1').selected);+ testing.expectEqual('2', $('#single').value);++ $('#s3').selected = true;+ testing.expectEqual(false, $('#s2').selected);+ testing.expectEqual('3', $('#single').value);++ $('#m2').selected = true;+ testing.expectEqual(true, $('#m1').selected);+ testing.expectEqual(true, $('#m2').selected);++ const orphan = document.createElement('option');+ orphan.selected = true;+ testing.expectEqual(true, orphan.selected);+}+</script>diff --git a/src/browser/webapi/element/html/Option.zig b/src/browser/webapi/element/html/Option.zigindex 513b7c9ed..243d717fb 100644--- a/src/browser/webapi/element/html/Option.zig+++ b/src/browser/webapi/element/html/Option.zig@@ -26,6 +26,7 @@ const Frame = @import("../../../Frame.zig");const Node = @import("../../Node.zig");const Element = @import("../../Element.zig");const HtmlElement = @import("../Html.zig");+const Select = @import("Select.zig");const String = lp.String;@@ -79,12 +80,27 @@ pub fn getSelected(self: *const Option) bool {}pub fn setSelected(self: *Option, selected: bool, frame: *Frame) !void {- // TODO: When setting selected=true, may need to unselect other options- // in the parent <select> if it doesn't have multiple attributeself._selected = selected;+ if (selected) {+ if (self.ownerSelect()) |select| {+ if (!select.asConstElement().hasAttributeSafe(comptime .wrap("multiple"))) {+ select.deselectOthers(self);+ }+ }+ }frame.domChanged();}+/// The <select> this option belongs to, directly or through an <optgroup>.+fn ownerSelect(self: *Option) ?*Select {+ var node = self.asNode().parentNode();+ while (node) |n| : (node = n.parentNode()) {+ if (n.is(Select)) |select| return select;+ if (n.is(Element.Html.OptGroup) == null) return null;+ }+ return null;+}+pub fn getDefaultSelected(self: *const Option) bool {return self.asConstElement().hasAttributeSafe(comptime .wrap("selected"));}diff --git a/src/browser/webapi/element/html/Select.zig b/src/browser/webapi/element/html/Select.zigindex a60937227..fbf219d31 100644--- a/src/browser/webapi/element/html/Select.zig+++ b/src/browser/webapi/element/html/Select.zig@@ -91,6 +91,13 @@ const OptionIterator = struct {}};+pub fn deselectOthers(self: *const Select, keep: *const Option) void {+ var it = OptionIterator.init(self);+ while (it.next()) |option| {+ if (option != keep) option._selected = false;+ }+}+// Resolves the option whose selectedness contributes to the select's value// per HTML §form-elements§selectedness-setting-algorithm: an explicitly// selected non-disabled option, falling back to the first non-disabled