Skip to content

Commit 3b2e436

Browse files
author
Nicholas C. Zakas
committed
Fixed IE bug
1 parent 2547b7e commit 3b2e436

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

encodings/base64/base64.htm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ <h1>Base64 Encoding/Decoding Tests</h1>
3333

3434
name : "Base 64 Encoding Tests",
3535

36+
_should: {
37+
error: {
38+
testInvalidChar: new Error("Can't base64 encode non-ASCII characters.")
39+
}
40+
},
41+
3642
//---------------------------------------------------------------------
3743
// Tests
3844
//---------------------------------------------------------------------
@@ -51,6 +57,10 @@ <h1>Base64 Encoding/Decoding Tests</h1>
5157
var expected = "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=";
5258
var result = base64Encode("Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.");
5359
assert.areEqual(expected, result);
60+
},
61+
62+
testInvalidChar: function(){
63+
base64Encode(String.fromCharCode(256) + "Hello!");
5464
}
5565
}));
5666

encodings/base64/base64.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ function base64Encode(text){
6565
quantaCount,
6666
bits = [],
6767
result = [];
68+
69+
//verify that there are no characters out of range
70+
if (/([^\u0000-\u00ff])/.test(text)){
71+
throw new Error("Can't base64 encode non-ASCII characters.");
72+
}
6873

6974
//create an array of binary digits representing the text
7075
while(i < text.length){
@@ -75,7 +80,7 @@ function base64Encode(text){
7580

7681
//figure out how many 24-bit quanta are in the array
7782
quantaCount = Math.floor(bits.length / 24);
78-
83+
7984
//encode all bits
8085
encodeBits: while(true){
8186

@@ -84,7 +89,7 @@ function base64Encode(text){
8489
for (j=0; j < 4; j++){
8590
part = bits.splice(0, 6).join("");
8691
index = parseInt(part,2);
87-
result.push(digits[index]);
92+
result.push(digits.charAt(index));
8893
}
8994
}
9095

0 commit comments

Comments
 (0)