Skip to content

Lightpanda: the headless browser designed for AI and automation

Correctly serialize File in URLSearchParams

Karl Seguin8b9c066ea960
diff --git a/src/browser/tests/net/url_search_params.html b/src/browser/tests/net/url_search_params.html
index db51296c5..08b196225 100644
--- a/src/browser/tests/net/url_search_params.html
+++ b/src/browser/tests/net/url_search_params.html
@@ -381,6 +381,37 @@
}
</script>
+<script id=formDataWithFiles>
+{
+ // A FormData init is converted as sequence<sequence<USVString>> by
+ // iterating it, so a File value stringifies to "[object File]", not to
+ // its name.
+ const form = document.createElement('form');
+ const input = document.createElement('input');
+ input.type = 'file';
+ input.name = 'empty';
+ form.appendChild(input);
+
+ const fd = new FormData(form);
+ fd.append('s', 'text');
+ fd.append('f', new File(['abc'], 'a.txt'));
+ fd.append('b', new Blob(['xyz']));
+ fd.append('r', new File(['abc'], 'a.txt'), 'renamed.txt');
+
+ const usp = new URLSearchParams(fd);
+ testing.expectEqual(5, usp.size);
+ testing.expectEqual('[object File]', usp.get('empty'));
+ testing.expectEqual('text', usp.get('s'));
+ testing.expectEqual('[object File]', usp.get('f'));
+ testing.expectEqual('[object File]', usp.get('b'));
+ testing.expectEqual('[object File]', usp.get('r'));
+ testing.expectEqual('empty=%5Bobject+File%5D&s=text&f=%5Bobject+File%5D&b=%5Bobject+File%5D&r=%5Bobject+File%5D', usp.toString());
+
+ // Same result as passing the iterated entries explicitly.
+ testing.expectEqual(new URLSearchParams([...fd]).toString(), usp.toString());
+}
+</script>
+
<script id=arrayOfArrays>
{
const usp = new URLSearchParams([["a", "1"], ["b", "2"], ["a", "3"]]);
diff --git a/src/browser/webapi/net/FormData.zig b/src/browser/webapi/net/FormData.zig
index 19cd83f0a..185341f7f 100644
--- a/src/browser/webapi/net/FormData.zig
+++ b/src/browser/webapi/net/FormData.zig
@@ -769,12 +769,16 @@ fn decodeMultipartName(arena: Allocator, raw: []const u8) ![]const u8 {
return out.items;
}
-// Used by URLSearchParams to ingest a FormData; file entries collapse via Value.asString.
+// Used by URLSearchParams to ingest a FormData.
pub fn toKeyValueList(self: *const FormData, arena: Allocator) !KeyValueList {
var list: KeyValueList = .empty;
try list.ensureTotalCapacity(arena, self._entries.items.len);
for (self._entries.items) |*entry| {
- try list.appendAssumeCapacity(arena, entry.name.str(), entry.value.asString());
+ const value = switch (entry.value) {
+ .string => |*s| s.str(),
+ .file, .no_file => "[object File]",
+ };
+ try list.appendAssumeCapacity(arena, entry.name.str(), value);
}
return list;
}