GUAC-876: Update Blob implementation from upstream.

This commit is contained in:
Michael Jumper
2014-10-08 15:30:52 -07:00
parent 8d1bee18aa
commit a792e6f271
2 changed files with 54 additions and 40 deletions

View File

@@ -1,9 +1,4 @@
This software is licensed under the MIT/X11 license. Copyright © 2014 [Eli Grey][1].
MIT/X11 license
---------------
Copyright © 2011 [Eli Grey][1].
Permission is hereby granted, free of charge, to any person Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation obtaining a copy of this software and associated documentation
@@ -27,4 +22,4 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE. OTHER DEALINGS IN THE SOFTWARE.
[1]: http://eligrey.com [1]: http://eligrey.com

View File

@@ -1,11 +1,11 @@
/* Blob.js /* Blob.js
* A Blob implementation. * A Blob implementation.
* 2013-06-20 * 2014-07-24
* *
* By Eli Grey, http://eligrey.com * By Eli Grey, http://eligrey.com
* By Devin Samarin, https://github.com/eboyjr * By Devin Samarin, https://github.com/dsamarin
* License: X11/MIT * License: X11/MIT
* See LICENSE.md * See https://github.com/eligrey/Blob.js/blob/master/LICENSE.md
*/ */
/*global self, unescape */ /*global self, unescape */
@@ -14,12 +14,21 @@
/*! @source http://purl.eligrey.com/github/Blob.js/blob/master/Blob.js */ /*! @source http://purl.eligrey.com/github/Blob.js/blob/master/Blob.js */
if (typeof Blob !== "function" || typeof URL === "undefined") (function (view) {
if (typeof Blob === "function" && typeof webkitURL !== "undefined") var URL = webkitURL;
else var Blob = (function (view) {
"use strict"; "use strict";
var BlobBuilder = view.BlobBuilder || view.WebKitBlobBuilder || view.MozBlobBuilder || view.MSBlobBuilder || (function(view) { view.URL = view.URL || view.webkitURL;
if (view.Blob && view.URL) {
try {
new Blob;
return;
} catch (e) {}
}
// Internally we use a BlobBuilder implementation to base Blob off of
// in order to support older browsers that only have BlobBuilder
var BlobBuilder = view.BlobBuilder || view.WebKitBlobBuilder || view.MozBlobBuilder || (function(view) {
var var
get_class = function(object) { get_class = function(object) {
return Object.prototype.toString.call(object).match(/^\[object\s(.*)\]$/)[1]; return Object.prototype.toString.call(object).match(/^\[object\s(.*)\]$/)[1];
@@ -50,25 +59,34 @@ else var Blob = (function (view) {
, URL = real_URL , URL = real_URL
, btoa = view.btoa , btoa = view.btoa
, atob = view.atob , atob = view.atob
, can_apply_typed_arrays = false
, can_apply_typed_arrays_test = function(pass) {
can_apply_typed_arrays = !pass;
}
, ArrayBuffer = view.ArrayBuffer , ArrayBuffer = view.ArrayBuffer
, Uint8Array = view.Uint8Array , Uint8Array = view.Uint8Array
, origin = /^[\w-]+:\/*\[?[\w\.:-]+\]?(?::[0-9]+)?/
; ;
FakeBlob.fake = FB_proto.fake = true; FakeBlob.fake = FB_proto.fake = true;
while (file_ex_code--) { while (file_ex_code--) {
FileException.prototype[file_ex_codes[file_ex_code]] = file_ex_code + 1; FileException.prototype[file_ex_codes[file_ex_code]] = file_ex_code + 1;
} }
try { // Polyfill URL
if (Uint8Array) {
can_apply_typed_arrays_test.apply(0, new Uint8Array(1));
}
} catch (ex) {}
if (!real_URL.createObjectURL) { if (!real_URL.createObjectURL) {
URL = view.URL = {}; URL = view.URL = function(uri) {
var
uri_info = document.createElementNS("http://www.w3.org/1999/xhtml", "a")
, uri_origin
;
uri_info.href = uri;
if (!("origin" in uri_info)) {
if (uri_info.protocol.toLowerCase() === "data:") {
uri_info.origin = null;
} else {
uri_origin = uri.match(origin);
uri_info.origin = uri_origin && uri_origin[1];
}
}
return uri_info;
};
} }
URL.createObjectURL = function(blob) { URL.createObjectURL = function(blob) {
var var
@@ -102,19 +120,16 @@ else var Blob = (function (view) {
var bb = this.data; var bb = this.data;
// decode data to a binary string // decode data to a binary string
if (Uint8Array && (data instanceof ArrayBuffer || data instanceof Uint8Array)) { if (Uint8Array && (data instanceof ArrayBuffer || data instanceof Uint8Array)) {
if (can_apply_typed_arrays) { var
bb.push(String.fromCharCode.apply(String, new Uint8Array(data))); str = ""
} else { , buf = new Uint8Array(data)
var , i = 0
str = "" , buf_len = buf.length
, buf = new Uint8Array(data) ;
, i = 0 for (; i < buf_len; i++) {
, buf_len = buf.length str += String.fromCharCode(buf[i]);
;
for (; i < buf_len; i++) {
str += String.fromCharCode(buf[i]);
}
} }
bb.push(str);
} else if (get_class(data) === "Blob" || get_class(data) === "File") { } else if (get_class(data) === "Blob" || get_class(data) === "File") {
if (FileReaderSync) { if (FileReaderSync) {
var fr = new FileReaderSync; var fr = new FileReaderSync;
@@ -162,10 +177,14 @@ else var Blob = (function (view) {
FB_proto.toString = function() { FB_proto.toString = function() {
return "[object Blob]"; return "[object Blob]";
}; };
FB_proto.close = function() {
this.size = 0;
delete this.data;
};
return FakeBlobBuilder; return FakeBlobBuilder;
}(view)); }(view));
return function Blob(blobParts, options) { view.Blob = function(blobParts, options) {
var type = options ? (options.type || "") : ""; var type = options ? (options.type || "") : "";
var builder = new BlobBuilder(); var builder = new BlobBuilder();
if (blobParts) { if (blobParts) {
@@ -175,4 +194,4 @@ else var Blob = (function (view) {
} }
return builder.getBlob(type); return builder.getBlob(type);
}; };
}(self)); }(typeof self !== "undefined" && self || typeof window !== "undefined" && window || this.content || this));