Skip to content

Commit 9641706

Browse files
FileAPI: Blob 'endings' option is now standardized.
The 'endings' option for the Blob (and File) constructor has now been standardized in the spec[1]. Remove TODOs and align the IDL with the spec. No behavior changes. [1] https://w3c.github.io/FileAPI Bug: 605710, 509793 Change-Id: I26a1694eb56e415b6a50cc90148d10393b88b8d2 Reviewed-on: https://chromium-review.googlesource.com/802621 Commit-Queue: Joshua Bell <[email protected]> Reviewed-by: Marijn Kruisselbrink <[email protected]> Cr-Commit-Position: refs/heads/master@{#521537}
1 parent 0e48503 commit 9641706

File tree

2 files changed

+128
-15
lines changed

2 files changed

+128
-15
lines changed

FileAPI/blob/Blob-constructor.html

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,20 @@
7474
type: "",
7575
desc: "A plain object with @@iterator should be treated as a sequence for the blobParts argument."
7676
});
77+
test(t => {
78+
const blob = new Blob({
79+
[Symbol.iterator]() {
80+
var i = 0;
81+
return {next: () => [
82+
{done:false, value:'ab'},
83+
{done:false, value:'cde'},
84+
{done:true}
85+
][i++]
86+
};
87+
}
88+
});
89+
assert_equals(blob.size, 5, 'Custom @@iterator should be treated as a sequence');
90+
}, "A plain object with custom @@iterator should be treated as a sequence for the blobParts argument.");
7791
test_blob(function() {
7892
return new Blob({
7993
[Symbol.iterator]: Array.prototype[Symbol.iterator],
@@ -443,13 +457,26 @@
443457
});
444458
});
445459

460+
[
461+
123,
462+
123.4,
463+
true,
464+
'abc'
465+
].forEach(arg => {
466+
test(t => {
467+
assert_throws(new TypeError(), () => new Blob([], arg),
468+
'Blob constructor should throw with invalid property bag');
469+
}, `Passing ${JSON.stringify(arg)} for options should throw`);
470+
});
471+
446472
var type_tests = [
447473
// blobParts, type, expected type
448474
[[], '', ''],
449475
[[], 'a', 'a'],
450476
[[], 'A', 'a'],
451477
[[], 'text/html', 'text/html'],
452478
[[], 'TEXT/HTML', 'text/html'],
479+
[[], 'text/plain;charset=utf-8', 'text/plain;charset=utf-8'],
453480
[[], '\u00E5', ''],
454481
[[], '\uD801\uDC7E', ''], // U+1047E
455482
[[], ' image/gif ', ' image/gif '],

FileAPI/file/File-constructor.html

Lines changed: 101 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,20 @@
66
<script src="/resources/testharnessreport.js"></script>
77
<div id="log"></div>
88
<script>
9+
const to_string_obj = { toString: () => 'a string' };
10+
const to_string_throws = { toString: () => { throw new Error('expected'); } };
11+
912
test(function() {
1013
assert_true("File" in window, "window should have a File property.");
1114
}, "File interface object exists");
1215

16+
test(t => {
17+
assert_throws(new TypeError(), () => new File(),
18+
'Bits argument is required');
19+
assert_throws(new TypeError(), () => new File([]),
20+
'Name argument is required');
21+
}, 'Required arguments');
22+
1323
function test_first_argument(arg1, expectedSize, testName) {
1424
test(function() {
1525
var file = new File(arg1, "dummy");
@@ -22,14 +32,48 @@
2232
}, testName);
2333
}
2434

35+
test_first_argument([], 0, "empty fileBits");
2536
test_first_argument(["bits"], 4, "DOMString fileBits");
2637
test_first_argument(["𝓽𝓮𝔁𝓽"], 16, "Unicode DOMString fileBits");
38+
test_first_argument([new String('string object')], 13, "String object fileBits");
2739
test_first_argument([new Blob()], 0, "Empty Blob fileBits");
2840
test_first_argument([new Blob(["bits"])], 4, "Blob fileBits");
41+
test_first_argument([new File([], 'world.txt')], 0, "Empty File fileBits");
42+
test_first_argument([new File(["bits"], 'world.txt')], 4, "File fileBits");
2943
test_first_argument([new ArrayBuffer(8)], 8, "ArrayBuffer fileBits");
3044
test_first_argument([new Uint8Array([0x50, 0x41, 0x53, 0x53])], 4, "Typed array fileBits");
3145
test_first_argument(["bits", new Blob(["bits"]), new Blob(), new Uint8Array([0x50, 0x41]),
3246
new Uint16Array([0x5353]), new Uint32Array([0x53534150])], 16, "Various fileBits");
47+
test_first_argument([12], 2, "Number in fileBits");
48+
test_first_argument([[1,2,3]], 5, "Array in fileBits");
49+
test_first_argument([{}], 15, "Object in fileBits"); // "[object Object]"
50+
test_first_argument([document], 21, "HTMLDocument in fileBits"); // "[object HTMLDocument]"
51+
test_first_argument([to_string_obj], 8, "Object with toString in fileBits");
52+
test_first_argument({[Symbol.iterator]() {
53+
let i = 0;
54+
return {next: () => [
55+
{done:false, value:'ab'},
56+
{done:false, value:'cde'},
57+
{done:true}
58+
][i++]};
59+
}}, 5, 'Custom @@iterator');
60+
61+
[
62+
'hello',
63+
0,
64+
null
65+
].forEach(arg => {
66+
test(t => {
67+
assert_throws(new TypeError(), () => new File(arg, 'world.html'),
68+
'Constructor should throw for invalid bits argument');
69+
}, `Invalid bits argument: ${JSON.stringify(arg)}`);
70+
});
71+
72+
test(t => {
73+
assert_throws(new Error(), () => new File([to_string_throws], 'name.txt'),
74+
'Constructor should propagate exceptions');
75+
}, 'Bits argument: object that throws');
76+
3377

3478
function test_second_argument(arg2, expectedFileName, testName) {
3579
test(function() {
@@ -41,23 +85,29 @@
4185

4286
test_second_argument("dummy", "dummy", "Using fileName");
4387
test_second_argument("dummy/foo", "dummy:foo", "Using special character in fileName");
88+
test_second_argument(null, "null", "Using null fileName");
89+
test_second_argument(1, "1", "Using number fileName");
90+
test_second_argument('', '', "Using empty string fileName");
91+
test_second_argument(document, '[object HTMLDocument]', "Using object fileName");
4492

4593
// testing the third argument
46-
test(function() {
47-
var file = new File(["bits"], "dummy", { type: "text/plain"});
48-
assert_true(file instanceof File);
49-
assert_equals(file.type, "text/plain");
50-
}, "Using type on the File constructor");
51-
test(function() {
52-
var file = new File(["bits"], "dummy", { type: "TEXT/PLAIN"});
53-
assert_true(file instanceof File);
54-
assert_equals(file.type, "text/plain");
55-
}, "Using uppercase characters in type");
56-
test(function() {
57-
var file = new File(["bits"], "dummy", { type: "𝓽𝓮𝔁𝓽/𝔭𝔩𝔞𝔦𝔫"});
58-
assert_true(file instanceof File);
59-
assert_equals(file.type, "");
60-
}, "Using illegal character for type");
94+
[
95+
{type: 'text/plain', expected: 'text/plain'},
96+
{type: 'text/plain;charset=UTF-8', expected: 'text/plain;charset=utf-8'},
97+
{type: 'TEXT/PLAIN', expected: 'text/plain'},
98+
{type: '𝓽𝓮𝔁𝓽/𝔭𝔩𝔞𝔦𝔫', expected: ''},
99+
{type: 'ascii/nonprintable\u001F', expected: ''},
100+
{type: 'ascii/nonprintable\u007F', expected: ''},
101+
{type: 'nonascii\u00EE', expected: ''},
102+
{type: 'nonascii\u1234', expected: ''},
103+
{type: 'nonparsable', expected: 'nonparsable'}
104+
].forEach(testCase => {
105+
test(t => {
106+
var file = new File(["bits"], "dummy", { type: testCase.type});
107+
assert_true(file instanceof File);
108+
assert_equals(file.type, testCase.expected);
109+
}, `Using type in File constructor: ${testCase.type}`);
110+
});
61111
test(function() {
62112
var file = new File(["bits"], "dummy", { lastModified: 42 });
63113
assert_true(file instanceof File);
@@ -68,5 +118,41 @@
68118
assert_true(file instanceof File);
69119
assert_equals(file.name, "dummy");
70120
}, "Misusing name");
121+
test(function() {
122+
var file = new File(["bits"], "dummy", { unknownKey: "value" });
123+
assert_true(file instanceof File);
124+
assert_equals(file.name, "dummy");
125+
}, "Unknown properties are ignored");
126+
127+
[
128+
123,
129+
123.4,
130+
true,
131+
'abc'
132+
].forEach(arg => {
133+
test(t => {
134+
assert_throws(new TypeError(), () => new File(['bits'], 'name.txt', arg),
135+
'Constructor should throw for invalid property bag type');
136+
}, `Invalid property bag: ${JSON.stringify(arg)}`);
137+
});
138+
139+
[
140+
null,
141+
undefined,
142+
[1,2,3],
143+
/regex/,
144+
function() {}
145+
].forEach(arg => {
146+
test(t => {
147+
assert_equals(new File(['bits'], 'name.txt', arg).size, 4,
148+
'Constructor should accept object-ish property bag type');
149+
}, `Unusual but valid property bag: ${arg}`);
150+
});
151+
152+
test(t => {
153+
assert_throws(new Error(),
154+
() => new File(['bits'], 'name.txt', {type: to_string_throws}),
155+
'Constructor should propagate exceptions');
156+
}, 'Property bag propagates exceptions');
71157

72158
</script>

0 commit comments

Comments
 (0)