From 6239d3824f897f79d0ad55224ea648bd553ea2f0 Mon Sep 17 00:00:00 2001 From: HyBer <59885141+BBboy01@users.noreply.github.com> Date: Fri, 8 Sep 2023 16:32:05 +0800 Subject: [PATCH] fix: update btoa boundary --- base64.js | 2 +- test/base64.js | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/base64.js b/base64.js index d517861..63dcb1d 100644 --- a/base64.js +++ b/base64.js @@ -37,7 +37,7 @@ o2 = data.charCodeAt (i++); o3 = data.charCodeAt (i++); - if (o1 > 128 || o2 > 128 || o3 > 128) { + if (o1 > 255 || o2 > 255 || o3 > 255) { throw new InvalidCharacterError ("'btoa' failed: The string to be encoded contains characters outside of the Latin1 range."); } diff --git a/test/base64.js b/test/base64.js index 5e25a7e..78db04b 100644 --- a/test/base64.js +++ b/test/base64.js @@ -29,7 +29,8 @@ describe ('Base64.js', function() { assert.strictEqual (btoa ('qrstuvwxyz{|}~'), 'cXJzdHV2d3h5ent8fX4='); }); - it ('cannot encode non-ASCII input', function() { + it ('cannot encode characters beyond U+00FF', function() { + assert.strictEqual (String.fromCharCode (0x2708), '✈'); assert.throws ( function() { btoa ('✈'); }, function(err) { @@ -83,4 +84,11 @@ describe ('Base64.js', function() { assert.strictEqual (atob (null), atob ('null')); }); + it ('can encode every character in [U+0000, U+00FF]', function() { + for (var code = 0x0; code <= 0xFF; code += 0x1) { + var char = String.fromCharCode (code); + assert.strictEqual (atob (btoa (char)), char); + } + }); + });