Skip to content

Commit 5785c14

Browse files
committed
support Blob type
1 parent b68675b commit 5785c14

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/browser/file/Blob.zig

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ mime: []const u8,
4141
const ConstructorOptions = struct {
4242
/// MIME type.
4343
type: []const u8 = "",
44-
/// How to handle newline (LF) characters.
44+
/// How to handle line endings (CR and LF).
4545
/// `transparent` means do nothing, `native` expects CRLF (\r\n) on Windows.
4646
endings: []const u8 = "transparent",
4747
};
@@ -53,22 +53,29 @@ pub fn constructor(
5353
page: *Page,
5454
) !Blob {
5555
const options: ConstructorOptions = maybe_options orelse .{};
56+
// Setup MIME; This can be any string according to my observations.
57+
const mime: []const u8 = blk: {
58+
const t = options.type;
59+
if (t.len == 0) {
60+
break :blk "";
61+
}
62+
63+
break :blk try page.arena.dupe(u8, t);
64+
};
5665

5766
if (maybe_blob_parts) |blob_parts| {
5867
var w: Writer.Allocating = .init(page.arena);
5968
const use_native_endings = std.mem.eql(u8, options.endings, "native");
6069
try writeBlobParts(&w.writer, blob_parts, use_native_endings);
6170

62-
const written = w.written();
63-
64-
return .{ .slice = written, .mime = options.type };
71+
return .{ .slice = w.written(), .mime = mime };
6572
}
6673

6774
// We don't have `blob_parts`, why would you want a Blob anyway then?
68-
return .{ .slice = "", .mime = options.type };
75+
return .{ .slice = "", .mime = mime };
6976
}
7077

71-
/// Writes blob parts to given `Writer` by desired encoding.
78+
/// Writes blob parts to given `Writer` with desired endings.
7279
fn writeBlobParts(
7380
writer: *Writer,
7481
blob_parts: []const []const u8,
@@ -168,6 +175,11 @@ pub fn get_size(self: *const Blob) usize {
168175
return self.slice.len;
169176
}
170177

178+
/// Returns the type of Blob; likely a MIME type, yet anything can be given.
179+
pub fn get_type(self: *const Blob) []const u8 {
180+
return self.mime;
181+
}
182+
171183
const testing = @import("../../testing.zig");
172184
test "Browser: File.Blob" {
173185
try testing.htmlRunner("file/blob.html");

0 commit comments

Comments
 (0)