Skip to content

Lightpanda: the headless browser designed for AI and automation

Don't create (leak) HTMLFormControlsCollection

Karl Seguin07326db23eac
diff --git a/src/browser/tests/element/html/form.html b/src/browser/tests/element/html/form.html
index 8532ad266..7f85034aa 100644
--- a/src/browser/tests/element/html/form.html
+++ b/src/browser/tests/element/html/form.html
@@ -52,8 +52,18 @@
testing.expectEqual(form.elements.user, form.user);
testing.expectEqual(form.elements.pass, form.pass);
- // real properties still win
- testing.expectEqual('function', typeof form.submit);
+ // more than one match: a live RadioNodeList
+ testing.expectTrue(form.choice instanceof RadioNodeList);
+ testing.expectEqual(2, form.choice.length);
+ testing.expectEqual('', form.choice.value);
+ form.choice[1].checked = true;
+ testing.expectEqual('2', form.choice.value);
+ form.choice[1].checked = false;
+
+ testing.expectEqual(undefined, form.nope);
+ testing.expectTrue('user' in form);
+ testing.expectFalse('nope' in form);
+ testing.expectFalse(Object.keys(form).includes('user'));
}
</script>
diff --git a/src/browser/webapi/collections/HTMLFormControlsCollection.zig b/src/browser/webapi/collections/HTMLFormControlsCollection.zig
index 8e3300085..a951318ff 100644
--- a/src/browser/webapi/collections/HTMLFormControlsCollection.zig
+++ b/src/browser/webapi/collections/HTMLFormControlsCollection.zig
@@ -60,7 +60,7 @@ pub const NamedItemResult = union(enum) {
radio_node_list: *RadioNodeList,
};
-pub fn namedItem(self: *HTMLFormControlsCollection, name: []const u8, frame: *Frame) !?NamedItemResult {
+fn namedItem(self: *HTMLFormControlsCollection, name: []const u8, frame: *Frame) !?NamedItemResult {
if (name.len == 0) {
return null;
}
@@ -81,21 +81,7 @@ pub fn namedItem(self: *HTMLFormControlsCollection, name: []const u8, frame: *Fr
count += 1;
if (count == 2) {
- const radio_node_list = try frame._factory.chained(.{
- NodeList{ ._data = undefined },
- RadioNodeList{
- ._proto = undefined,
- ._form_collection = self,
- ._name = try frame.dupeString(name),
- },
- });
- radio_node_list._proto._data = .{ .radio_node_list = radio_node_list };
-
- // The RadioNodeList outlives this call; its NodeList releases
- // the ref in deinit.
- self.acquireRef();
-
- return .{ .radio_node_list = radio_node_list };
+ return .{ .radio_node_list = try self.radioNodeList(name, frame) };
}
}
}
@@ -112,7 +98,26 @@ pub fn namedItem(self: *HTMLFormControlsCollection, name: []const u8, frame: *Fr
return .{ .element = first_element.? };
}
-fn matchesName(element: *Element, name: []const u8) bool {
+// The live list of every control named "name".
+pub fn radioNodeList(self: *HTMLFormControlsCollection, name: []const u8, frame: *Frame) !*RadioNodeList {
+ const radio_node_list = try frame._factory.chained(.{
+ NodeList{ ._data = undefined },
+ RadioNodeList{
+ ._proto = undefined,
+ ._form_collection = self,
+ ._name = try frame.dupeString(name),
+ },
+ });
+ radio_node_list._proto._data = .{ .radio_node_list = radio_node_list };
+
+ // The RadioNodeList outlives this call; its NodeList releases
+ // the ref in deinit.
+ self.acquireRef();
+
+ return radio_node_list;
+}
+
+pub fn matchesName(element: *Element, name: []const u8) bool {
if (element.getId()) |id| {
if (std.mem.eql(u8, id, name)) {
return true;
diff --git a/src/browser/webapi/element/html/Form.zig b/src/browser/webapi/element/html/Form.zig
index f5407dda0..5dac1f506 100644
--- a/src/browser/webapi/element/html/Form.zig
+++ b/src/browser/webapi/element/html/Form.zig
@@ -33,6 +33,9 @@ pub const Button = @import("Button.zig");
pub const Select = @import("Select.zig");
pub const TextArea = @import("TextArea.zig");
+const NodeLive = collections.NodeLive;
+const HTMLFormControlsCollection = collections.HTMLFormControlsCollection;
+
const Form = @This();
pub const Proto = HtmlElement;
@@ -88,24 +91,24 @@ pub fn setMethod(self: *Form, method: []const u8, frame: *Frame) !void {
try self.asElement().setAttributeSafe(comptime .wrap("method"), .wrap(method), frame);
}
-pub fn getElements(self: *Form, frame: *Frame) !*collections.HTMLFormControlsCollection {
+pub fn getElements(self: *Form, frame: *Frame) !*HTMLFormControlsCollection {
const node_live = self.iterator(frame);
const elements = try frame._factory.chained(.{
node_live.htmlCollectionValue(),
- collections.HTMLFormControlsCollection{ ._proto = undefined },
+ HTMLFormControlsCollection{ ._proto = undefined },
});
elements._proto._chained = .form_controls;
return elements;
}
-pub fn iterator(self: *Form, frame: *Frame) collections.NodeLive(.form) {
+pub fn iterator(self: *Form, frame: *Frame) NodeLive(.form) {
const form_id = self.asElement().getId();
const root = if (form_id != null)
self.asNode().getRootNode(.{}) // Has ID: walk entire document to find form=ID controls
else
self.asNode(); // No ID: walk only form subtree (no external controls possible)
- return collections.NodeLive(.form).init(root, .{ .form = self, .form_id = form_id }, frame);
+ return NodeLive(.form).init(root, .{ .form = self, .form_id = form_id }, frame);
}
fn getAction(self: *Form, frame: *Frame) ![]const u8 {
@@ -188,23 +191,30 @@ fn getFormOwner(element: *Element, frame: *Frame) ?*Form {
return null;
}
-fn matchesName(element: *Element, name: []const u8) bool {
- if (element.getId()) |id| {
- if (std.mem.eql(u8, id, name)) {
- return true;
- }
+// https://html.spec.whatwg.org/multipage/forms.html#dom-form-nameditem
+// One matching control is returned as is; more than one as a live RadioNodeList.
+fn namedItem(self: *Form, name: []const u8, frame: *Frame) !?HTMLFormControlsCollection.NamedItemResult {
+ if (name.len == 0) {
+ return null;
}
- if (element.getName()) |elem_name| {
- if (std.mem.eql(u8, elem_name, name)) {
- return true;
+
+ var first: ?*Element = null;
+ var it = self.iterator(frame);
+ while (it.next()) |element| {
+ if (HTMLFormControlsCollection.matchesName(element, name) == false) {
+ continue;
+ }
+ if (first == null) {
+ first = element;
+ continue;
}
+ // Only the RadioNodeList needs a collection; it holds a ref to it.
+ const elements = try self.getElements(frame);
+ return .{ .radio_node_list = try elements.radioNodeList(name, frame) };
}
- return false;
-}
-fn namedItem(self: *Form, name: []const u8, frame: *Frame) !?collections.HTMLFormControlsCollection.NamedItemResult {
- const elements = try self.getElements(frame);
- return elements.namedItem(name, frame);
+ const element = first orelse return null;
+ return .{ .element = element };
}
/// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-form-checkvalidity
@@ -248,9 +258,15 @@ pub const JsApi = struct {
pub const @"[str]" = bridge.namedIndexed(Form.namedItem, null, null, null, struct {
fn wrap(self: *Form, field_name: []const u8, frame: *Frame) !u32 {
- if (try hasNamed(self, field_name, frame)) {
- // Named properties are [LegacyUnenumerableNamedProperties]
- return js.v8.DontEnum;
+ if (field_name.len == 0) {
+ return error.NotHandled;
+ }
+
+ var it = self.iterator(frame);
+ while (it.next()) |element| {
+ if (HTMLFormControlsCollection.matchesName(element, field_name)) {
+ return js.v8.DontEnum;
+ }
}
return error.NotHandled;
}
@@ -273,21 +289,6 @@ pub const JsApi = struct {
pub const requestSubmit = bridge.function(Form.requestSubmit, .{});
pub const checkValidity = bridge.function(Form.checkValidity, .{});
pub const reportValidity = bridge.function(Form.reportValidity, .{});
-
- // Presence only, `namedItem` is relativel expensive / RC'd
- fn hasNamed(self: *Form, field_name: []const u8, frame: *Frame) !bool {
- if (field_name.len == 0) {
- return false;
- }
-
- var it = self.iterator(frame);
- while (it.next()) |element| {
- if (matchesName(element, field_name)) {
- return true;
- }
- }
- return false;
- }
};
const testing = @import("../../../../testing.zig");