From f08d79ea82012c8e118770e524e483288a70e198 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Mon, 31 Mar 2014 03:55:51 -0400 Subject: [PATCH] feature(zip) add unescape pollyfill --- lib/client/zip.js | 50 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/lib/client/zip.js b/lib/client/zip.js index 0d757e69..c551f476 100644 --- a/lib/client/zip.js +++ b/lib/client/zip.js @@ -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; + } })();