Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/third_party/closure/goog/fs/blob.js
4079 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview Wrappers for the HTML5 File API. These wrappers closely mirror
9
* the underlying APIs, but use Closure-style events and Deferred return values.
10
* Their existence also makes it possible to mock the FileSystem API for testing
11
* in browsers that don't support it natively.
12
*
13
* When adding public functions to anything under this namespace, be sure to add
14
* its mock counterpart to goog.testing.fs.
15
*/
16
17
goog.provide('goog.fs.blob');
18
19
20
21
/**
22
* Concatenates one or more values together and converts them to a Blob.
23
*
24
* @param {...(string|!Blob|!ArrayBuffer)} var_args The values that will make up
25
* the resulting blob.
26
* @return {!Blob} The blob.
27
*/
28
goog.fs.blob.getBlob = function(var_args) {
29
'use strict';
30
const BlobBuilder = goog.global.BlobBuilder || goog.global.WebKitBlobBuilder;
31
32
if (BlobBuilder !== undefined) {
33
const bb = new BlobBuilder();
34
for (let i = 0; i < arguments.length; i++) {
35
bb.append(arguments[i]);
36
}
37
return bb.getBlob();
38
} else {
39
return goog.fs.blob.getBlobWithProperties(
40
Array.prototype.slice.call(arguments));
41
}
42
};
43
44
45
/**
46
* Creates a blob with the given properties.
47
* See https://developer.mozilla.org/en-US/docs/Web/API/Blob for more details.
48
*
49
* @param {!Array<string|!Blob|!ArrayBuffer>} parts The values that will make up
50
* the resulting blob (subset supported by both BlobBuilder.append() and
51
* Blob constructor).
52
* @param {string=} opt_type The MIME type of the Blob.
53
* @param {string=} opt_endings Specifies how strings containing newlines are to
54
* be written out.
55
* @return {!Blob} The blob.
56
*/
57
goog.fs.blob.getBlobWithProperties = function(parts, opt_type, opt_endings) {
58
'use strict';
59
const BlobBuilder = goog.global.BlobBuilder || goog.global.WebKitBlobBuilder;
60
61
if (BlobBuilder !== undefined) {
62
const bb = new BlobBuilder();
63
for (let i = 0; i < parts.length; i++) {
64
bb.append(parts[i], opt_endings);
65
}
66
return bb.getBlob(opt_type);
67
} else if (goog.global.Blob !== undefined) {
68
const properties = {};
69
if (opt_type) {
70
properties['type'] = opt_type;
71
}
72
if (opt_endings) {
73
properties['endings'] = opt_endings;
74
}
75
return new Blob(parts, properties);
76
} else {
77
throw new Error('This browser doesn\'t seem to support creating Blobs');
78
}
79
};
80
81