[svg-edit] r2879 committed - Simplify encodeUTF8 function; ensure base 64 decoding also uses new de...

2 views
Skip to first unread message

svg-...@googlecode.com

unread,
Jun 12, 2014, 7:42:41 PM6/12/14
to svg-edi...@googlegroups.com
Revision: 2879
Author: brettz9
Date: Thu Jun 12 23:42:27 2014 UTC
Log: Simplify encodeUTF8 function; ensure base 64 decoding also uses
new decodeUTF8
http://code.google.com/p/svg-edit/source/detail?r=2879

Modified:
/trunk/editor/svgutils.js

=======================================
--- /trunk/editor/svgutils.js Thu May 22 10:21:29 2014 UTC
+++ /trunk/editor/svgutils.js Thu Jun 12 23:42:27 2014 UTC
@@ -124,7 +124,7 @@
// 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 @@
enc1 = enc2 = enc3 = enc4 = '';

} while (i < input.length);
- return unescape(output);
+ return svgedit.utilities.decodeUTF8(output);
};

-// based on http://phpjs.org/functions/utf8_encode
+svgedit.utilities.decodeUTF8 = function (argString) {
+ return decodeURIComponent(escape(argString));
+};
+
// 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
Reply all
Reply to author
Forward
0 new messages