From f02b9566c546ce98dfb51ac523befedb78d295f6 Mon Sep 17 00:00:00 2001 From: Karl Seguin Date: Fri, 27 Jun 2025 18:36:29 +0800 Subject: [PATCH] add atob and btoa --- src/browser/html/window.zig | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/browser/html/window.zig b/src/browser/html/window.zig index 7d97a555..66c4dc0d 100644 --- a/src/browser/html/window.zig +++ b/src/browser/html/window.zig @@ -217,6 +217,21 @@ pub const Window = struct { }; } + pub fn _btoa(_: *const Window, value: []const u8, page: *Page) ![]const u8 { + const Encoder = std.base64.standard.Encoder; + const out = try page.call_arena.alloc(u8, Encoder.calcSize(value.len)); + return Encoder.encode(out, value); + } + + pub fn _atob(_: *const Window, value: []const u8, page: *Page) ![]const u8 { + const Decoder = std.base64.standard.Decoder; + const size = Decoder.calcSizeForSlice(value) catch return error.InvalidCharacterError; + + const out = try page.call_arena.alloc(u8, size); + Decoder.decode(out, value) catch return error.InvalidCharacterError; + return out; + } + const CreateTimeoutOpts = struct { repeat: bool = false, animation_frame: bool = false, @@ -414,4 +429,12 @@ test "Browser.HTML.Window" { "true", }, }, .{}); + + try runner.testCases(&.{ + .{ "const b64 = btoa('https://ziglang.org/documentation/master/std/#std.base64.Base64Decoder')", "undefined" }, + .{ "b64", "aHR0cHM6Ly96aWdsYW5nLm9yZy9kb2N1bWVudGF0aW9uL21hc3Rlci9zdGQvI3N0ZC5iYXNlNjQuQmFzZTY0RGVjb2Rlcg==" }, + .{ "const str = atob(b64)", "undefined" }, + .{ "str", "https://ziglang.org/documentation/master/std/#std.base64.Base64Decoder" }, + .{ "try { atob('b') } catch (e) { e } ", "Error: InvalidCharacterError" }, + }, .{}); }