Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: add select.length setter

Karl Seguinf2f0effab28e
diff --git a/src/browser/tests/element/html/select.html b/src/browser/tests/element/html/select.html
index f96ff2df6..89e2ebccf 100644
--- a/src/browser/tests/element/html/select.html
+++ b/src/browser/tests/element/html/select.html
@@ -1070,3 +1070,51 @@
testing.expectEqual(true, s.validity.valueMissing);
}
</script>
+
+<script id="length_setter">
+{
+ const s = document.createElement('select');
+ const reset = () => {
+ s.innerHTML = '<option>a</option><optgroup><option>b</option><option>c</option></optgroup><option>d</option>';
+ };
+
+ // Shrinking removes the options past the new length, inside an optgroup too.
+ reset();
+ s.options.length = 2;
+ testing.expectEqual('<option>a</option><optgroup><option>b</option></optgroup>', s.innerHTML);
+
+ // select.length is the same setter.
+ reset();
+ s.length = 1;
+ testing.expectEqual('<option>a</option><optgroup></optgroup>', s.innerHTML);
+
+ // Growing appends blank options to the select, one mutation record each (Chrome).
+ reset();
+ const mo = new MutationObserver(() => {});
+ mo.observe(s, { childList: true, subtree: true });
+ s.options.length = 6;
+ testing.expectEqual(2, mo.takeRecords().length);
+ mo.disconnect();
+ testing.expectEqual(6, s.length);
+ testing.expectEqual('<option>d</option><option></option><option></option>', s.innerHTML.slice(s.innerHTML.indexOf('<option>d')));
+
+ // Removing the selected option resets the selection.
+ reset();
+ s.options[1].selected = true;
+ s.options.length = 1;
+ testing.expectEqual(0, s.selectedIndex);
+
+ // unsigned long conversion; past the cap (and -1 -> 4294967295) is ignored.
+ reset();
+ s.options.length = -1;
+ testing.expectEqual(4, s.length);
+ s.options.length = 100001;
+ testing.expectEqual(4, s.length);
+ s.options.length = 2.9;
+ testing.expectEqual(2, s.length);
+ s.options.length = '3';
+ testing.expectEqual(3, s.length);
+ s.options.length = NaN;
+ testing.expectEqual(0, s.length);
+}
+</script>
diff --git a/src/browser/webapi/collections/HTMLOptionsCollection.zig b/src/browser/webapi/collections/HTMLOptionsCollection.zig
index 3d0a38849..7374d9869 100644
--- a/src/browser/webapi/collections/HTMLOptionsCollection.zig
+++ b/src/browser/webapi/collections/HTMLOptionsCollection.zig
@@ -21,8 +21,11 @@ const lp = @import("lightpanda");
const js = @import("../../js/js.zig");
const Page = @import("../../Page.zig");
const Frame = @import("../../Frame.zig");
+
const Node = @import("../Node.zig");
const Element = @import("../Element.zig");
+const Select = @import("../element/html/Select.zig");
+
const HTMLCollection = @import("HTMLCollection.zig");
const HTMLOptionsCollection = @This();
@@ -30,7 +33,7 @@ const HTMLOptionsCollection = @This();
pub const Proto = HTMLCollection;
_proto: *HTMLCollection,
-_select: *@import("../element/html/Select.zig"),
+_select: *Select,
// The refcount lives on the proto, but the finalizer anchors here so deinit
// destroys the whole {HTMLCollection, self} chain from its leaf.
@@ -51,6 +54,10 @@ pub fn length(self: *HTMLOptionsCollection, frame: *Frame) u32 {
return self._proto.length(frame);
}
+fn setLength(self: *HTMLOptionsCollection, value: u32, frame: *Frame) !void {
+ return self._select.setLength(value, frame);
+}
+
// Forward indexed access to HTMLCollection
pub fn getAtIndex(self: *HTMLOptionsCollection, index: usize, frame: *Frame) ?*Element {
return self._proto.getAtIndex(index, frame);
@@ -106,11 +113,6 @@ pub fn remove(self: *HTMLOptionsCollection, index: i32, frame: *Frame) void {
}
}
-// Chrome's cap (kMaxListItems): padding up to a huge index would otherwise
-// create that many options. Past it, Chrome ignores the set (with a console
-// warning); Firefox has no cap.
-const max_list_items = 100_000;
-
// The indexed setter: null removes the option at index; an index past the
// end pads with blank options and then appends; otherwise the option at
// index is replaced.
@@ -129,8 +131,8 @@ fn setAtIndex(self: *HTMLOptionsCollection, index: u32, option_: ?*Option, frame
return;
}
- if (index >= max_list_items) {
- lp.log.debug(.js, "select overflow", .{ .max_list_items = max_list_items, .request = index });
+ if (index >= Select.max_list_items) {
+ lp.log.debug(.js, "select overflow", .{ .max_list_items = Select.max_list_items, .request = index });
return;
}
@@ -142,8 +144,8 @@ fn setAtIndex(self: *HTMLOptionsCollection, index: u32, option_: ?*Option, frame
const doc = select_node.ownerDocument(frame).?;
const fragment = (try Node.DocumentFragment.init(doc, frame)).asNode();
for (len..index) |_| {
- const blank = try doc.createElementNS("http://www.w3.org/1999/xhtml", "option", frame);
- _ = try fragment.appendChild(blank.asNode(), frame);
+ const blank = try Frame.node_factory.createElementNS(doc, .html, "option", null);
+ _ = try fragment.appendChild(blank, frame);
}
_ = try select_node.appendChild(fragment, frame);
}
@@ -160,7 +162,7 @@ pub const JsApi = struct {
pub const manage = false;
};
- pub const length = bridge.accessor(HTMLOptionsCollection.length, null, .{});
+ pub const length = bridge.accessor(HTMLOptionsCollection.length, setLength, .{ .ce_reactions = true });
// Indexed access
pub const @"[int]" = bridge.indexedReadWrite(HTMLOptionsCollection.getAtIndex, setAtIndex, null, null, null, .{ .null_as_undefined = true, .ce_reactions = true });
diff --git a/src/browser/webapi/element/html/Select.zig b/src/browser/webapi/element/html/Select.zig
index eac20bb38..4e0f722b0 100644
--- a/src/browser/webapi/element/html/Select.zig
+++ b/src/browser/webapi/element/html/Select.zig
@@ -341,6 +341,39 @@ pub fn getLength(self: *Select) u32 {
return i;
}
+pub const max_list_items = 100_000;
+pub fn setLength(self: *Select, value: u32, frame: *Frame) !void {
+ // value > options.len means add blank options
+ // value < options.len means remove extra options
+
+ // we'll collect the length AND trim at the same time
+
+ var len: u32 = 0;
+ var it = OptionIterator.init(self);
+ while (it.next()) |option| : (len += 1) {
+ if (len >= value) {
+ // we have to be in trim mode
+ option.asElement().remove(frame);
+ }
+ }
+ if (len >= value) {
+ return;
+ }
+
+ // growing, we append blank options
+ if (value > max_list_items) {
+ lp.log.debug(.js, "select overflow", .{ .max_list_items = max_list_items, .request = value });
+ return;
+ }
+ const self_node = self.asNode();
+ const doc = self_node.ownerDocument(frame).?;
+ for (len..value) |_| {
+ // yes, one at time, for the correct mutation records
+ const blank = try Frame.node_factory.createElementNS(doc, .html, "option", null);
+ _ = try self_node.appendChild(blank, frame);
+ }
+}
+
// remove() is ChildNode's; remove(index) removes the option at index.
fn remove(self: *Select, index_: ?js.Value, frame: *Frame) !void {
const value = index_ orelse return self.asElement().remove(frame);
@@ -512,7 +545,7 @@ pub const JsApi = struct {
pub const selectedOptions = bridge.accessor(Select.getSelectedOptions, null, .{});
pub const form = bridge.accessor(Select.getForm, null, .{});
pub const size = reflect.unsignedLong("size", .{});
- pub const length = bridge.accessor(Select.getLength, null, .{});
+ pub const length = bridge.accessor(Select.getLength, Select.setLength, .{ .ce_reactions = true });
pub const labels = bridge.accessor(Select.getLabels, null, .{});
pub const willValidate = bridge.accessor(Select.getWillValidate, null, .{});
pub const validity = bridge.accessor(Select.getValidity, null, .{});