Simplify encodeUTF8 function; ensure base 64 decoding also uses new decodeUTF8

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@2879 eee81c28-f429-11dd-99c0-75d572ba1ddd
master
Brett Zamir 2014-06-12 23:42:27 +00:00
parent 6f7651299c
commit d84b1d267f
1 changed files with 7 additions and 58 deletions

View File

@ -124,7 +124,7 @@ svgedit.utilities.encode64 = function(input) {
// Converts a string from base64
svgedit.utilities.decode64 = function(input) {
if(window.atob) {
return window.atob(input);
return svgedit.utilities.decodeUTF8(window.atob(input));
}
var output = '';
var chr1, chr2, chr3 = '';
@ -157,67 +157,16 @@ svgedit.utilities.decode64 = function(input) {
enc1 = enc2 = enc3 = enc4 = '';
} while (i < input.length);
return unescape(output);
return svgedit.utilities.decodeUTF8(output);
};
svgedit.utilities.decodeUTF8 = function (argString) {
return decodeURIComponent(escape(argString));
};
// based on http://phpjs.org/functions/utf8_encode
// codedread:does not seem to work with webkit-based browsers on OSX // Brettz9: please test again as function upgraded
svgedit.utilities.encodeUTF8 = function (argString) {
//return unescape(encodeURIComponent(input)); //may or may not work
if (argString === null || argString === undef) {
return '';
}
// .replace(/\r\n/g, "\n").replace(/\r/g, "\n");
var string = String(argString);
var utftext = '',
start, end, stringl = 0;
start = end = 0;
stringl = string.length;
var n;
for (n = 0; n < stringl; n++) {
var c1 = string.charCodeAt(n);
var enc = null;
if (c1 < 128) {
end++;
} else if (c1 > 127 && c1 < 2048) {
enc = String.fromCharCode(
(c1 >> 6) | 192, (c1 & 63) | 128
);
} else if ((c1 & 0xF800) != 0xD800) {
enc = String.fromCharCode(
(c1 >> 12) | 224, ((c1 >> 6) & 63) | 128, (c1 & 63) | 128
);
} else {
// surrogate pairs
if ((c1 & 0xFC00) != 0xD800) {
throw new RangeError('Unmatched trail surrogate at ' + n);
}
var c2 = string.charCodeAt(++n);
if ((c2 & 0xFC00) != 0xDC00) {
throw new RangeError('Unmatched lead surrogate at ' + (n - 1));
}
c1 = ((c1 & 0x3FF) << 10) + (c2 & 0x3FF) + 0x10000;
enc = String.fromCharCode(
(c1 >> 18) | 240, ((c1 >> 12) & 63) | 128, ((c1 >> 6) & 63) | 128, (c1 & 63) | 128
);
}
if (enc !== null) {
if (end > start) {
utftext += string.slice(start, end);
}
utftext += enc;
start = end = n + 1;
}
}
if (end > start) {
utftext += string.slice(start, stringl);
}
return utftext;
return unescape(encodeURIComponent(argString));
};
// Function: svgedit.utilities.convertToXMLReferences