feature(zip) add unescape pollyfill

This commit is contained in:
coderaiser 2014-03-31 03:55:51 -04:00
parent d257a8c00c
commit f08d79ea82

View file

@ -3,7 +3,11 @@ var Util, Zip, pako;
(function () {
'use strict';
Zip = new ZipProto();
var UTF8 = {
unescape: typeof unescape === 'function' ? unescape : decode
};
Zip = new ZipProto();
function ZipProto() {
this.pack = function(str, callback) {
@ -29,7 +33,7 @@ var Util, Zip, pako;
function utf8AbFromStr(str) {
var i,
strUtf8 = unescape(encodeURIComponent(str)),
strUtf8 = UTF8.unescape(encodeURIComponent(str)),
n = strUtf8.length,
arr = new Uint8Array(n);
@ -39,4 +43,46 @@ var Util, Zip, pako;
return arr;
}
}
/* unescape polyfill
* http://unixpapa.com/js/querystring.html
*/
function decode(s) {
s = s.replace(/%([EF][0-9A-F])%([89AB][0-9A-F])%([89AB][0-9A-F])/gi,
function(code,hex1,hex2,hex3) {
var n, n3,
n1 = parseInt(hex1,16)-0xE0;
n2 = parseInt(hex2,16)-0x80;
if (n1 === 0 && n2 < 32)
return code;
n3 = parseInt(hex3,16)-0x80;
n = (n1<<12) + (n2<<6) + n3;
if (n > 0xFFFF)
return code;
return String.fromCharCode(n);
});
s = s.replace(/%([CD][0-9A-F])%([89AB][0-9A-F])/gi,
function(code,hex1,hex2) {
var n1, n2;
n1 = parseInt(hex1,16) - 0xC0;
if (n1 < 2)
return code;
n2 = parseInt(hex2,16) - 0x80;
return String.fromCharCode((n1<<6) + n2);
});
s = s.replace(/%([0-7][0-9A-F])/gi,
function(code, hex) {
return String.fromCharCode(parseInt(hex,16));
});
return s;
}
})();