Skip to content

Commit

Permalink
Merge pull request #59 from BBboy01/fix/btoa-boundary
Browse files Browse the repository at this point in the history
fix: update btoa boundary
  • Loading branch information
davidchambers authored Sep 18, 2023
2 parents 0048721 + 6239d38 commit 8bec771
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion base64.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}

Expand Down
10 changes: 9 additions & 1 deletion test/base64.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
});

});

0 comments on commit 8bec771

Please sign in to comment.