mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-12-14 23:38:57 +00:00
initial Blob support
This commit is contained in:
145
src/browser/file/Blob.zig
Normal file
145
src/browser/file/Blob.zig
Normal file
@@ -0,0 +1,145 @@
|
|||||||
|
// Copyright (C) 2023-2025 Lightpanda (Selecy SAS)
|
||||||
|
//
|
||||||
|
// Francis Bouvier <francis@lightpanda.io>
|
||||||
|
// Pierre Tachoire <pierre@lightpanda.io>
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Affero General Public License as
|
||||||
|
// published by the Free Software Foundation, either version 3 of the
|
||||||
|
// License, or (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Affero General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const Writer = std.Io.Writer;
|
||||||
|
|
||||||
|
const builtin = @import("builtin");
|
||||||
|
const is_windows = builtin.os.tag == .windows;
|
||||||
|
|
||||||
|
const Page = @import("../page.zig").Page;
|
||||||
|
const js = @import("../js/js.zig");
|
||||||
|
|
||||||
|
/// https://w3c.github.io/FileAPI/#blob-section
|
||||||
|
/// https://developer.mozilla.org/en-US/docs/Web/API/Blob
|
||||||
|
const Blob = @This();
|
||||||
|
|
||||||
|
/// Immutable slice of blob.
|
||||||
|
/// Note that another blob may hold a pointer/slice to this,
|
||||||
|
/// so its better to leave the deallocation of it to arena allocator.
|
||||||
|
slice: []const u8,
|
||||||
|
/// MIME attached to blob. Can be an empty string.
|
||||||
|
mime: []const u8,
|
||||||
|
|
||||||
|
const ConstructorOptions = struct {
|
||||||
|
/// MIME type.
|
||||||
|
type: []const u8 = "",
|
||||||
|
/// How to handle newline (LF) characters.
|
||||||
|
/// `transparent` means do nothing, `native` expects CRLF (\r\n) on Windows.
|
||||||
|
endings: []const u8 = "transparent",
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Creates a new Blob.
|
||||||
|
pub fn constructor(
|
||||||
|
maybe_blob_parts: ?[]const []const u8,
|
||||||
|
maybe_options: ?ConstructorOptions,
|
||||||
|
page: *Page,
|
||||||
|
) !Blob {
|
||||||
|
const options: ConstructorOptions = maybe_options orelse .{};
|
||||||
|
|
||||||
|
if (maybe_blob_parts) |blob_parts| {
|
||||||
|
var w: Writer.Allocating = .init(page.arena);
|
||||||
|
const use_native_endings = std.mem.eql(u8, options.endings, "native");
|
||||||
|
try writeBlobParts(&w.writer, blob_parts, use_native_endings);
|
||||||
|
|
||||||
|
const written = w.written();
|
||||||
|
|
||||||
|
return .{ .slice = written, .mime = options.type };
|
||||||
|
}
|
||||||
|
|
||||||
|
// We don't have `blob_parts`, why would you want a Blob anyway then?
|
||||||
|
return .{ .slice = "", .mime = options.type };
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Writes blob parts to given `Writer` by desired encoding.
|
||||||
|
fn writeBlobParts(
|
||||||
|
writer: *Writer,
|
||||||
|
blob_parts: []const []const u8,
|
||||||
|
use_native_endings: bool,
|
||||||
|
) !void {
|
||||||
|
// Transparent.
|
||||||
|
if (!use_native_endings) {
|
||||||
|
for (blob_parts) |part| {
|
||||||
|
try writer.writeAll(part);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Windows support.
|
||||||
|
// TODO: Vector search.
|
||||||
|
|
||||||
|
// Linux & Unix.
|
||||||
|
// Both Firefox and Chrome implement it as such:
|
||||||
|
// CRLF => LF
|
||||||
|
// CR => LF
|
||||||
|
// So even though CR is not followed by LF, it gets replaced.
|
||||||
|
//
|
||||||
|
// I believe this is because such scenario is possible:
|
||||||
|
// ```
|
||||||
|
// let parts = [ "the quick\r", "\nbrown fox" ];
|
||||||
|
// ```
|
||||||
|
// In the example, one should have to check the part before in order to
|
||||||
|
// understand that CRLF is being presented in the final buffer.
|
||||||
|
// So they took a simpler approach, here's what given blob parts produce:
|
||||||
|
// ```
|
||||||
|
// "the quick\n\nbrown fox"
|
||||||
|
// ```
|
||||||
|
scan_parts: for (blob_parts) |part| {
|
||||||
|
var end: usize = 0;
|
||||||
|
var start = end;
|
||||||
|
while (end < part.len) {
|
||||||
|
if (part[end] == '\r') {
|
||||||
|
try writer.writeAll(part[start..end]);
|
||||||
|
try writer.writeByte('\n');
|
||||||
|
|
||||||
|
// Part ends with CR. We can continue to next part.
|
||||||
|
if (end + 1 == part.len) {
|
||||||
|
continue :scan_parts;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If next char is LF, skip it too.
|
||||||
|
if (part[end + 1] == '\n') {
|
||||||
|
start = end + 2;
|
||||||
|
} else {
|
||||||
|
start = end + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
end += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write the remaining. We get this in such situations:
|
||||||
|
// `the quick brown\rfox`
|
||||||
|
// `the quick brown\r\nfox`
|
||||||
|
try writer.writeAll(part[start..end]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_size(self: *const Blob) usize {
|
||||||
|
return self.slice.len;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_str(self: *const Blob) []const u8 {
|
||||||
|
return self.slice;
|
||||||
|
}
|
||||||
|
|
||||||
|
const testing = @import("../../testing.zig");
|
||||||
|
test "Browser: File.Blob" {
|
||||||
|
try testing.htmlRunner("file/blob.html");
|
||||||
|
}
|
||||||
@@ -21,14 +21,12 @@ const std = @import("std");
|
|||||||
// https://w3c.github.io/FileAPI/#file-section
|
// https://w3c.github.io/FileAPI/#file-section
|
||||||
const File = @This();
|
const File = @This();
|
||||||
|
|
||||||
// Very incomplete. The prototype for this is Blob, which we don't have.
|
/// TODO: Implement File API.
|
||||||
// This minimum "implementation" is added because some JavaScript code just
|
|
||||||
// checks: if (x instanceof File) throw Error(...)
|
|
||||||
pub fn constructor() File {
|
pub fn constructor() File {
|
||||||
return .{};
|
return .{};
|
||||||
}
|
}
|
||||||
|
|
||||||
const testing = @import("../../testing.zig");
|
const testing = @import("../../testing.zig");
|
||||||
test "Browser: File" {
|
test "Browser: File.File" {
|
||||||
try testing.htmlRunner("xhr/file.html");
|
try testing.htmlRunner("file/file.html");
|
||||||
}
|
}
|
||||||
7
src/browser/file/root.zig
Normal file
7
src/browser/file/root.zig
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
//! File API.
|
||||||
|
//! https://developer.mozilla.org/en-US/docs/Web/API/File_API
|
||||||
|
|
||||||
|
pub const Interfaces = .{
|
||||||
|
@import("./Blob.zig"),
|
||||||
|
@import("./File.zig"),
|
||||||
|
};
|
||||||
@@ -17,8 +17,8 @@ const Interfaces = generate.Tuple(.{
|
|||||||
@import("../url/url.zig").Interfaces,
|
@import("../url/url.zig").Interfaces,
|
||||||
@import("../xhr/xhr.zig").Interfaces,
|
@import("../xhr/xhr.zig").Interfaces,
|
||||||
@import("../navigation/root.zig").Interfaces,
|
@import("../navigation/root.zig").Interfaces,
|
||||||
|
@import("../file/root.zig").Interfaces,
|
||||||
@import("../xhr/form_data.zig").Interfaces,
|
@import("../xhr/form_data.zig").Interfaces,
|
||||||
@import("../xhr/File.zig"),
|
|
||||||
@import("../xmlserializer/xmlserializer.zig").Interfaces,
|
@import("../xmlserializer/xmlserializer.zig").Interfaces,
|
||||||
@import("../fetch/fetch.zig").Interfaces,
|
@import("../fetch/fetch.zig").Interfaces,
|
||||||
@import("../streams/streams.zig").Interfaces,
|
@import("../streams/streams.zig").Interfaces,
|
||||||
|
|||||||
18
src/tests/file/blob.html
Normal file
18
src/tests/file/blob.html
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<script src="../testing.js"></script>
|
||||||
|
|
||||||
|
<script id=Blob>
|
||||||
|
const parts = ["\r\nthe quick brown\rfo\rx\r", "\njumps over\r\nthe\nlazy\r", "\ndog"];
|
||||||
|
|
||||||
|
// "transparent" ending should not modify the final buffer.
|
||||||
|
let blob1 = new Blob(parts);
|
||||||
|
let expected = parts.join("");
|
||||||
|
testing.expectEqual(expected.length, blob1.size);
|
||||||
|
testing.expectEqual(expected, blob1.str);
|
||||||
|
|
||||||
|
// "native" ending should modify the final buffer.
|
||||||
|
let blob2 = new Blob(parts, { endings: "native" });
|
||||||
|
expected = "\nthe quick brown\nfo\nx\n\njumps over\nthe\nlazy\n\ndog";
|
||||||
|
testing.expectEqual(expected.length, blob2.size);
|
||||||
|
testing.expectEqual(expected, blob2.str);
|
||||||
|
</script>
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<script src="../testing.js"></script>
|
<script src="../testing.js"></script>
|
||||||
|
|
||||||
<script id=file>
|
<script id=file>
|
||||||
let f = new File()
|
let f = new File();
|
||||||
testing.expectEqual(true, f instanceof File);
|
testing.expectEqual(true, f instanceof File);
|
||||||
</script>
|
</script>
|
||||||
Reference in New Issue
Block a user