Skip to content

Lightpanda: the headless browser designed for AI and automation

select: implement HTMLSelectElement.remove(index)

Scott Taylor960e8edd5ef6
diff --git a/src/browser/tests/element/html/select.html b/src/browser/tests/element/html/select.html
index c6f378158..f96ff2df6 100644
--- a/src/browser/tests/element/html/select.html
+++ b/src/browser/tests/element/html/select.html
@@ -256,6 +256,46 @@
}
</script>
+<script id="select_remove_index">
+ {
+ const parent = document.createElement('div')
+ document.body.appendChild(parent)
+ parent.innerHTML = '<select><option>a</option><optgroup><option>b</option></optgroup><option>c</option></select>'
+ const sel = parent.firstChild
+
+ // remove(index) removes the option at index, like options.remove(index)
+ sel.remove(1)
+ testing.expectEqual(true, sel.isConnected)
+ testing.expectEqual('a,c', Array.from(sel.options, (o) => o.text).join(','))
+
+ // out of range is a no-op
+ sel.remove(5)
+ sel.remove(-1)
+ testing.expectEqual(2, sel.options.length)
+
+ let iterations = 0
+ while (sel.options.length > 0 && iterations < 10) {
+ sel.remove(0)
+ iterations++
+ }
+ testing.expectEqual(2, iterations)
+ testing.expectEqual(true, sel.isConnected)
+
+ // the index converts like a WebIDL long
+ parent.innerHTML = '<select><option>a</option><option>b</option><option>c</option><option>d</option></select>'
+ const sel2 = parent.firstChild
+ sel2.remove(undefined)
+ sel2.remove(null)
+ sel2.remove('1')
+ testing.expectEqual('c', Array.from(sel2.options, (o) => o.text).join(','))
+ testing.expectEqual(true, sel2.isConnected)
+
+ // remove() without an index removes the select itself
+ sel.remove()
+ testing.expectEqual(false, sel.isConnected)
+ }
+</script>
+
<script id="options_add_errors">
{
const sel = document.createElement('select')
diff --git a/src/browser/webapi/element/html/Select.zig b/src/browser/webapi/element/html/Select.zig
index da9b2fd5e..e9699230d 100644
--- a/src/browser/webapi/element/html/Select.zig
+++ b/src/browser/webapi/element/html/Select.zig
@@ -341,6 +341,21 @@ pub fn getLength(self: *Select) u32 {
return i;
}
+// remove() is ChildNode's; remove(index) removes the option at index.
+pub fn remove(self: *Select, index_: ?js.Value, frame: *Frame) !void {
+ const index = try (index_ orelse return self.asElement().remove(frame)).toI32();
+ if (index < 0) {
+ return;
+ }
+ var i: u32 = 0;
+ var it = OptionIterator.init(self);
+ while (it.next()) |option| : (i += 1) {
+ if (i == index) {
+ return option.asElement().remove(frame);
+ }
+ }
+}
+
const AddBeforeOption = union(enum) {
option: *Option,
index: u32,
@@ -501,6 +516,7 @@ pub const JsApi = struct {
pub const validity = bridge.accessor(Select.getValidity, null, .{});
pub const validationMessage = bridge.accessor(Select.getValidationMessage, null, .{});
pub const add = bridge.function(Select.add, .{ .ce_reactions = true });
+ pub const remove = bridge.function(Select.remove, .{ .ce_reactions = true });
pub const checkValidity = bridge.function(Select.checkValidity, .{});
pub const reportValidity = bridge.function(Select.reportValidity, .{});
pub const setCustomValidity = bridge.function(Select.setCustomValidity, .{});