0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-01-17 09:52:29 +01:00

update to latest jszip

commit 04fc93045334dc1b679aba5c2dce206da777a85c
2013-10-12T12:39:56-07:00
This commit is contained in:
Chris Rebert 2013-10-13 23:37:14 -07:00
parent 3abe86b7ed
commit 9416cd3738

View File

@ -58,8 +58,62 @@ JSZip.defaults = {
compression: null
};
/*
* List features that require a modern browser, and if the current browser support them.
*/
JSZip.support = {
// contains true if JSZip can read/generate ArrayBuffer, false otherwise.
arraybuffer : (function(){
return typeof ArrayBuffer !== "undefined" && typeof Uint8Array !== "undefined";
})(),
// contains true if JSZip can read/generate nodejs Buffer, false otherwise.
nodebuffer : (function(){
return typeof Buffer !== "undefined";
})(),
// contains true if JSZip can read/generate Uint8Array, false otherwise.
uint8array : (function(){
return typeof Uint8Array !== "undefined";
})(),
// contains true if JSZip can read/generate Blob, false otherwise.
blob : (function(){
// the spec started with BlobBuilder then replaced it with a construtor for Blob.
// Result : we have browsers that :
// * know the BlobBuilder (but with prefix)
// * know the Blob constructor
// * know about Blob but not about how to build them
// About the "=== 0" test : if given the wrong type, it may be converted to a string.
// Instead of an empty content, we will get "[object Uint8Array]" for example.
if (typeof ArrayBuffer === "undefined") {
return false;
}
var buffer = new ArrayBuffer(0);
try {
return new Blob([buffer], { type: "application/zip" }).size === 0;
}
catch(e) {}
try {
var builder = new (window.BlobBuilder || window.WebKitBlobBuilder ||
window.MozBlobBuilder || window.MSBlobBuilder)();
builder.append(buffer);
return builder.getBlob('application/zip').size === 0;
}
catch(e) {}
return false;
})()
};
JSZip.prototype = (function () {
var textEncoder, textDecoder;
if (
JSZip.support.uint8array &&
typeof TextEncoder === "function" &&
typeof TextDecoder === "function"
) {
textEncoder = new TextEncoder("utf-8");
textDecoder = new TextDecoder("utf-8");
}
/**
* Returns the raw data of a ZipObject, decompress the content if necessary.
@ -97,8 +151,8 @@ JSZip.prototype = (function () {
if (!file.options.binary) {
// unicode text !
// unicode string => binary string is a painful process, check if we can avoid it.
if (JSZip.support.uint8array && typeof TextEncoder === "function") {
return TextEncoder("utf-8").encode(result);
if (textEncoder) {
return textEncoder.encode(result);
}
if (JSZip.support.nodebuffer) {
return new Buffer(result, "utf-8");
@ -557,7 +611,7 @@ JSZip.prototype = (function () {
*/
file : function(name, data, o) {
if (arguments.length === 1) {
if (name instanceof RegExp) {
if (JSZip.utils.isRegExp(name)) {
var regexp = name;
return this.filter(function(relativePath, file) {
return !file.options.dir && regexp.test(relativePath);
@ -584,7 +638,7 @@ JSZip.prototype = (function () {
return this;
}
if (arg instanceof RegExp) {
if (JSZip.utils.isRegExp(arg)) {
return this.filter(function(relativePath, file) {
return file.options.dir && arg.test(relativePath);
});
@ -854,8 +908,8 @@ JSZip.prototype = (function () {
// TextEncoder + Uint8Array to binary string is faster than checking every bytes on long strings.
// http://jsperf.com/utf8encode-vs-textencoder
// On short strings (file names for example), the TextEncoder API is (currently) slower.
if (JSZip.support.uint8array && typeof TextEncoder === "function") {
var u8 = TextEncoder("utf-8").encode(string);
if (textEncoder) {
var u8 = textEncoder.encode(string);
return JSZip.utils.transformTo("string", u8);
}
if (JSZip.support.nodebuffer) {
@ -898,8 +952,8 @@ JSZip.prototype = (function () {
// check if we can use the TextDecoder API
// see http://encoding.spec.whatwg.org/#api
if (JSZip.support.uint8array && typeof TextDecoder === "function") {
return TextDecoder("utf-8").decode(
if (textDecoder) {
return textDecoder.decode(
JSZip.utils.transformTo("uint8array", input)
);
}
@ -960,52 +1014,6 @@ JSZip.compressions = {
}
};
/*
* List features that require a modern browser, and if the current browser support them.
*/
JSZip.support = {
// contains true if JSZip can read/generate ArrayBuffer, false otherwise.
arraybuffer : (function(){
return typeof ArrayBuffer !== "undefined" && typeof Uint8Array !== "undefined";
})(),
// contains true if JSZip can read/generate nodejs Buffer, false otherwise.
nodebuffer : (function(){
return typeof Buffer !== "undefined";
})(),
// contains true if JSZip can read/generate Uint8Array, false otherwise.
uint8array : (function(){
return typeof Uint8Array !== "undefined";
})(),
// contains true if JSZip can read/generate Blob, false otherwise.
blob : (function(){
// the spec started with BlobBuilder then replaced it with a construtor for Blob.
// Result : we have browsers that :
// * know the BlobBuilder (but with prefix)
// * know the Blob constructor
// * know about Blob but not about how to build them
// About the "=== 0" test : if given the wrong type, it may be converted to a string.
// Instead of an empty content, we will get "[object Uint8Array]" for example.
if (typeof ArrayBuffer === "undefined") {
return false;
}
var buffer = new ArrayBuffer(0);
try {
return new Blob([buffer], { type: "application/zip" }).size === 0;
}
catch(e) {}
try {
var builder = new (window.BlobBuilder || window.WebKitBlobBuilder ||
window.MozBlobBuilder || window.MSBlobBuilder)();
builder.append(buffer);
return builder.getBlob('application/zip').size === 0;
}
catch(e) {}
return false;
})()
};
(function () {
JSZip.utils = {
/**
@ -1120,12 +1128,36 @@ JSZip.support = {
var chunk = 65536;
var result = [], len = array.length, type = JSZip.utils.getTypeOf(array), k = 0;
var canUseApply = true;
try {
switch(type) {
case "uint8array":
String.fromCharCode.apply(null, new Uint8Array(0));
break;
case "nodebuffer":
String.fromCharCode.apply(null, new Buffer(0));
break;
}
} catch(e) {
canUseApply = false;
}
// no apply : slow and painful algorithm
// default browser on android 4.*
if (!canUseApply) {
var resultStr = "";
for(var i = 0; i < array.length;i++) {
resultStr += String.fromCharCode(array[i]);
}
return resultStr;
}
while (k < len && chunk > 1) {
try {
if (type === "array" || type === "nodebuffer") {
result.push(String.fromCharCode.apply(null, array.slice(k, Math.max(k + chunk, len))));
result.push(String.fromCharCode.apply(null, array.slice(k, Math.min(k + chunk, len))));
} else {
result.push(String.fromCharCode.apply(null, array.subarray(k, k + chunk)));
result.push(String.fromCharCode.apply(null, array.subarray(k, Math.min(k + chunk, len))));
}
k += chunk;
} catch (e) {
@ -1263,7 +1295,7 @@ JSZip.support = {
if (typeof input === "string") {
return "string";
}
if (input instanceof Array) {
if (Object.prototype.toString.call(input) === "[object Array]") {
return "array";
}
if (JSZip.support.nodebuffer && Buffer.isBuffer(input)) {
@ -1277,6 +1309,16 @@ JSZip.support = {
}
};
/**
* Cross-window, cross-Node-context regular expression detection
* @param {Object} object Anything
* @return {Boolean} true if the object is a regular expression,
* false otherwise
*/
JSZip.utils.isRegExp = function (object) {
return Object.prototype.toString.call(object) === "[object RegExp]";
};
/**
* Throw an exception if the type is not supported.
* @param {String} type the type to check.