|
14 | 14 |
|
15 | 15 | /*global window, atob, Blob, ArrayBuffer, Uint8Array, define, module */ |
16 | 16 |
|
17 | | -(function (window) { |
18 | | - 'use strict'; |
19 | | - var CanvasPrototype = window.HTMLCanvasElement && |
20 | | - window.HTMLCanvasElement.prototype, |
21 | | - hasBlobConstructor = window.Blob && (function () { |
22 | | - try { |
23 | | - return Boolean(new Blob()); |
24 | | - } catch (e) { |
25 | | - return false; |
26 | | - } |
27 | | - }()), |
28 | | - hasArrayBufferViewSupport = hasBlobConstructor && window.Uint8Array && |
29 | | - (function () { |
30 | | - try { |
31 | | - return new Blob([new Uint8Array(100)]).size === 100; |
32 | | - } catch (e) { |
33 | | - return false; |
34 | | - } |
35 | | - }()), |
36 | | - BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || |
37 | | - window.MozBlobBuilder || window.MSBlobBuilder, |
38 | | - dataURIPattern = /^data:((.*?)(;charset=.*?)?)(;base64)?,/, |
39 | | - dataURLtoBlob = (hasBlobConstructor || BlobBuilder) && window.atob && |
40 | | - window.ArrayBuffer && window.Uint8Array && function (dataURI) { |
41 | | - var matches, |
42 | | - mediaType, |
43 | | - isBase64, |
44 | | - dataString, |
45 | | - byteString, |
46 | | - arrayBuffer, |
47 | | - intArray, |
48 | | - i, |
49 | | - bb; |
50 | | - // Parse the dataURI components as per RFC 2397 |
51 | | - matches = dataURI.match(dataURIPattern); |
52 | | - if (!matches) { |
53 | | - throw new Error('invalid data URI'); |
54 | | - } |
55 | | - // Default to text/plain;charset=US-ASCII |
56 | | - mediaType = matches[2] ? |
57 | | - matches[1] : |
58 | | - 'text/plain' + (matches[3] || ';charset=US-ASCII'); |
59 | | - isBase64 = !!matches[4]; |
60 | | - dataString = dataURI.slice(matches[0].length); |
61 | | - if (isBase64) { |
62 | | - // Convert base64 to raw binary data held in a string: |
63 | | - byteString = atob(dataString); |
64 | | - } else { |
65 | | - // Convert base64/URLEncoded data component to raw binary: |
66 | | - byteString = decodeURIComponent(dataString); |
67 | | - } |
68 | | - // Write the bytes of the string to an ArrayBuffer: |
69 | | - arrayBuffer = new ArrayBuffer(byteString.length); |
70 | | - intArray = new Uint8Array(arrayBuffer); |
71 | | - for (i = 0; i < byteString.length; i += 1) { |
72 | | - intArray[i] = byteString.charCodeAt(i); |
73 | | - } |
74 | | - // Write the ArrayBuffer (or ArrayBufferView) to a blob: |
75 | | - if (hasBlobConstructor) { |
76 | | - return new Blob( |
77 | | - [hasArrayBufferViewSupport ? intArray : arrayBuffer], |
78 | | - {type: mediaType} |
79 | | - ); |
80 | | - } |
81 | | - bb = new BlobBuilder(); |
82 | | - bb.append(arrayBuffer); |
83 | | - return bb.getBlob(mediaType); |
84 | | - }; |
85 | | - if (window.HTMLCanvasElement && !CanvasPrototype.toBlob) { |
86 | | - if (CanvasPrototype.mozGetAsFile) { |
87 | | - CanvasPrototype.toBlob = function (callback, type, quality) { |
88 | | - if (quality && CanvasPrototype.toDataURL && dataURLtoBlob) { |
89 | | - callback(dataURLtoBlob(this.toDataURL(type, quality))); |
90 | | - } else { |
91 | | - callback(this.mozGetAsFile('blob', type)); |
92 | | - } |
93 | | - }; |
94 | | - } else if (CanvasPrototype.toDataURL && dataURLtoBlob) { |
95 | | - CanvasPrototype.toBlob = function (callback, type, quality) { |
96 | | - callback(dataURLtoBlob(this.toDataURL(type, quality))); |
97 | | - }; |
98 | | - } |
| 17 | +;(function (window) { |
| 18 | + 'use strict' |
| 19 | + |
| 20 | + var CanvasPrototype = window.HTMLCanvasElement && |
| 21 | + window.HTMLCanvasElement.prototype |
| 22 | + var hasBlobConstructor = window.Blob && (function () { |
| 23 | + try { |
| 24 | + return Boolean(new Blob()) |
| 25 | + } catch (e) { |
| 26 | + return false |
99 | 27 | } |
100 | | - if (typeof define === 'function' && define.amd) { |
101 | | - define(function () { |
102 | | - return dataURLtoBlob; |
103 | | - }); |
104 | | - } else if (typeof module === 'object' && module.exports) { |
105 | | - module.exports = dataURLtoBlob; |
106 | | - } else { |
107 | | - window.dataURLtoBlob = dataURLtoBlob; |
| 28 | + }()) |
| 29 | + var hasArrayBufferViewSupport = hasBlobConstructor && window.Uint8Array && |
| 30 | + (function () { |
| 31 | + try { |
| 32 | + return new Blob([new Uint8Array(100)]).size === 100 |
| 33 | + } catch (e) { |
| 34 | + return false |
| 35 | + } |
| 36 | + }()) |
| 37 | + var BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || |
| 38 | + window.MozBlobBuilder || window.MSBlobBuilder |
| 39 | + var dataURIPattern = /^data:((.*?)(;charset=.*?)?)(;base64)?,/ |
| 40 | + var dataURLtoBlob = (hasBlobConstructor || BlobBuilder) && window.atob && |
| 41 | + window.ArrayBuffer && window.Uint8Array && |
| 42 | + function (dataURI) { |
| 43 | + var matches, |
| 44 | + mediaType, |
| 45 | + isBase64, |
| 46 | + dataString, |
| 47 | + byteString, |
| 48 | + arrayBuffer, |
| 49 | + intArray, |
| 50 | + i, |
| 51 | + bb |
| 52 | + // Parse the dataURI components as per RFC 2397 |
| 53 | + matches = dataURI.match(dataURIPattern) |
| 54 | + if (!matches) { |
| 55 | + throw new Error('invalid data URI') |
| 56 | + } |
| 57 | + // Default to text/plain;charset=US-ASCII |
| 58 | + mediaType = matches[2] |
| 59 | + ? matches[1] |
| 60 | + : 'text/plain' + (matches[3] || ';charset=US-ASCII') |
| 61 | + isBase64 = !!matches[4] |
| 62 | + dataString = dataURI.slice(matches[0].length) |
| 63 | + if (isBase64) { |
| 64 | + // Convert base64 to raw binary data held in a string: |
| 65 | + byteString = atob(dataString) |
| 66 | + } else { |
| 67 | + // Convert base64/URLEncoded data component to raw binary: |
| 68 | + byteString = decodeURIComponent(dataString) |
| 69 | + } |
| 70 | + // Write the bytes of the string to an ArrayBuffer: |
| 71 | + arrayBuffer = new ArrayBuffer(byteString.length) |
| 72 | + intArray = new Uint8Array(arrayBuffer) |
| 73 | + for (i = 0; i < byteString.length; i += 1) { |
| 74 | + intArray[i] = byteString.charCodeAt(i) |
| 75 | + } |
| 76 | + // Write the ArrayBuffer (or ArrayBufferView) to a blob: |
| 77 | + if (hasBlobConstructor) { |
| 78 | + return new Blob( |
| 79 | + [hasArrayBufferViewSupport ? intArray : arrayBuffer], |
| 80 | + {type: mediaType} |
| 81 | + ) |
| 82 | + } |
| 83 | + bb = new BlobBuilder() |
| 84 | + bb.append(arrayBuffer) |
| 85 | + return bb.getBlob(mediaType) |
| 86 | + } |
| 87 | + if (window.HTMLCanvasElement && !CanvasPrototype.toBlob) { |
| 88 | + if (CanvasPrototype.mozGetAsFile) { |
| 89 | + CanvasPrototype.toBlob = function (callback, type, quality) { |
| 90 | + if (quality && CanvasPrototype.toDataURL && dataURLtoBlob) { |
| 91 | + callback(dataURLtoBlob(this.toDataURL(type, quality))) |
| 92 | + } else { |
| 93 | + callback(this.mozGetAsFile('blob', type)) |
| 94 | + } |
| 95 | + } |
| 96 | + } else if (CanvasPrototype.toDataURL && dataURLtoBlob) { |
| 97 | + CanvasPrototype.toBlob = function (callback, type, quality) { |
| 98 | + callback(dataURLtoBlob(this.toDataURL(type, quality))) |
| 99 | + } |
108 | 100 | } |
109 | | -}(window)); |
| 101 | + } |
| 102 | + if (typeof define === 'function' && define.amd) { |
| 103 | + define(function () { |
| 104 | + return dataURLtoBlob |
| 105 | + }) |
| 106 | + } else if (typeof module === 'object' && module.exports) { |
| 107 | + module.exports = dataURLtoBlob |
| 108 | + } else { |
| 109 | + window.dataURLtoBlob = dataURLtoBlob |
| 110 | + } |
| 111 | +}(window)) |
0 commit comments