Skip to content

Lightpanda: the headless browser designed for AI and automation

form: skip inputs with empty name

Pierre Tachoired5b58511aa9a
diff --git a/src/browser/tests/net/form_data.html b/src/browser/tests/net/form_data.html
index 413c7725a..91090db2c 100644
--- a/src/browser/tests/net/form_data.html
+++ b/src/browser/tests/net/form_data.html
@@ -458,6 +458,40 @@
}
</script>
+<script id=emptyNameSkipped>
+{
+ // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#constructing-the-form-data-set
+ // A control whose name attribute is the empty string is skipped, the same
+ // as one without a name attribute.
+ const form = document.createElement('form');
+
+ const named = document.createElement('input');
+ named.name = 'q';
+ named.value = 'lightpanda';
+ form.appendChild(named);
+
+ const hidden = document.createElement('input');
+ hidden.type = 'hidden';
+ hidden.setAttribute('name', '');
+ hidden.value = 'nope-hidden';
+ form.appendChild(hidden);
+
+ const text = document.createElement('input');
+ text.setAttribute('name', '');
+ text.value = 'nope-text';
+ form.appendChild(text);
+
+ const ta = document.createElement('textarea');
+ ta.setAttribute('name', '');
+ ta.value = 'nope-textarea';
+ form.appendChild(ta);
+
+ const fd = new FormData(form);
+ testing.expectEqual([['q', 'lightpanda']], Array.from(fd.entries()));
+ testing.expectEqual(false, fd.has(''));
+}
+</script>
+
<script id=multipleImagesWithSameName>
{
// Test that when multiple images share a name, only submitter's coordinates are included
diff --git a/src/browser/webapi/net/FormData.zig b/src/browser/webapi/net/FormData.zig
index e3bf56136..b53752556 100644
--- a/src/browser/webapi/net/FormData.zig
+++ b/src/browser/webapi/net/FormData.zig
@@ -819,7 +819,11 @@ fn collectForm(arena: Allocator, form_: ?*Form, submitter_: ?*Element, charset:
}
}
+ // A missing or empty name excludes the control from the entry list.
const name = element.getName() orelse continue;
+ if (name.len == 0) {
+ continue;
+ }
const value = blk: {
if (element.is(Form.Input)) |input| {
const input_type = input._input_type;