Browse Source

Landed fix for base64 test from https://bugs.webkit.org/attachment.cgi?id=18354&action=view

John Resig 16 years ago
parent
commit
70c06a801b
1 changed files with 12 additions and 10 deletions
  1. 12 10
      tests/dromaeo-string-base64.js

+ 12 - 10
tests/dromaeo-string-base64.js

@@ -48,22 +48,22 @@ function toBase64(data) {
     var i;
     // Convert every three bytes to 4 ascii characters.
     for (i = 0; i < (length - 2); i += 3) {
-        result += toBase64Table[data[i] >> 2];
-        result += toBase64Table[((data[i] & 0x03) << 4) + (data[i+1] >> 4)];
-        result += toBase64Table[((data[i+1] & 0x0f) << 2) + (data[i+2] >> 6)];
-        result += toBase64Table[data[i+2] & 0x3f];
+        result += toBase64Table[data.charCodeAt(i) >> 2];
+        result += toBase64Table[((data.charCodeAt(i) & 0x03) << 4) + (data.charCodeAt(i+1) >> 4)];
+        result += toBase64Table[((data.charCodeAt(i+1) & 0x0f) << 2) + (data.charCodeAt(i+2) >> 6)];
+        result += toBase64Table[data.charCodeAt(i+2) & 0x3f];
     }
 
     // Convert the remaining 1 or 2 bytes, pad out to 4 characters.
     if (length%3) {
         i = length - (length%3);
-        result += toBase64Table[data[i] >> 2];
+        result += toBase64Table[data.charCodeAt(i) >> 2];
         if ((length%3) == 2) {
-            result += toBase64Table[((data[i] & 0x03) << 4) + (data[i+1] >> 4)];
-            result += toBase64Table[(data[i+1] & 0x0f) << 2];
+            result += toBase64Table[((data.charCodeAt(i) & 0x03) << 4) + (data.chartCodeAt(i+1) >> 4)];
+            result += toBase64Table[(data.charCodeAt(i+1) & 0x0f) << 2];
             result += base64Pad;
         } else {
-            result += toBase64Table[(data[i] & 0x03) << 4];
+            result += toBase64Table[(data.charCodeAt(i) & 0x03) << 4];
             result += base64Pad + base64Pad;
         }
     }
@@ -91,7 +91,7 @@ function base64ToString(data) {
     // Convert one by one.
     for (var i = 0; i < data.length; i++) {
         var c = toBinaryTable[data.charCodeAt(i) & 0x7f];
-        var padding = (data[i] == base64Pad);
+        var padding = (data.charCodeAt(i) == base64Pad.charCodeAt(0));
         // Skip illegal characters and whitespace
         if (c == -1) continue;
         
@@ -139,7 +139,9 @@ prep(function(){
 });
 
 test( "Convert Base 64 to String", function(){
-	base64ToString(base64);
+	if ( str !== base64ToString(base64) ) {
+		throw "String conversion mis-match.";
+	}
 });
 
 endTest();