window.atob must trim input

This commit is contained in:
Pierre Tachoire
2026-02-09 17:08:32 +01:00
parent 4138180f43
commit d5c2aaeea3
2 changed files with 11 additions and 2 deletions

View File

@@ -52,6 +52,7 @@
<script id=btoa>
testing.expectEqual('SGVsbG8gV29ybGQh', btoa('Hello World!'));
testing.expectEqual('', btoa(''));
testing.expectEqual('IA==', btoa(' '));
testing.expectEqual('YQ==', btoa('a'));
testing.expectEqual('YWI=', btoa('ab'));
testing.expectEqual('YWJj', btoa('abc'));
@@ -62,6 +63,13 @@
<script id=atob>
testing.expectEqual('Hello World!', atob('SGVsbG8gV29ybGQh'));
testing.expectEqual('', atob(''));
// atob must trim input
testing.expectEqual('', atob(' '));
testing.expectEqual(' ', atob('IA=='));
testing.expectEqual(' ', atob(' IA=='));
testing.expectEqual(' ', atob('IA== '));
testing.expectEqual('a', atob('YQ=='));
testing.expectEqual('ab', atob('YWI='));
testing.expectEqual('abc', atob('YWJj'));

View File

@@ -373,9 +373,10 @@ pub fn btoa(_: *const Window, input: []const u8, page: *Page) ![]const u8 {
}
pub fn atob(_: *const Window, input: []const u8, page: *Page) ![]const u8 {
const decoded_len = std.base64.standard.Decoder.calcSizeForSlice(input) catch return error.InvalidCharacterError;
const trimmed = std.mem.trim(u8, input, &std.ascii.whitespace);
const decoded_len = std.base64.standard.Decoder.calcSizeForSlice(trimmed) catch return error.InvalidCharacterError;
const decoded = try page.call_arena.alloc(u8, decoded_len);
std.base64.standard.Decoder.decode(decoded, input) catch return error.InvalidCharacterError;
std.base64.standard.Decoder.decode(decoded, trimmed) catch return error.InvalidCharacterError;
return decoded;
}