Blob-slice.any.js (7020B)
1 // META: title=Blob slice 2 // META: script=../support/Blob.js 3 'use strict'; 4 5 test_blob(function() { 6 var blobTemp = new Blob(["PASS"]); 7 return blobTemp.slice(); 8 }, { 9 expected: "PASS", 10 type: "", 11 desc: "no-argument Blob slice" 12 }); 13 14 test(function() { 15 var blob1 = new Blob(["squiggle"]); 16 var blob2 = new Blob(["steak"], {type: "content/type"}); 17 18 test_blob(() => blob1, 19 { 20 expected: "squiggle", 21 type: "", 22 desc: "blob1." 23 }); 24 25 test_blob(() => blob2, 26 { 27 expected: "steak", 28 type: "content/type", 29 desc: "blob2." 30 }); 31 32 test_blob(function() { 33 var blob = new Blob(["abcd"]); 34 return blob.slice(undefined, undefined, "content/type"); 35 }, { 36 expected: "abcd", 37 type: "content/type", 38 desc: "undefined start/end Blob slice" 39 }); 40 41 test_blob(function() { 42 var blob = new Blob(["abcd"]); 43 return blob.slice(undefined, 2, "content/type"); 44 }, { 45 expected: "ab", 46 type: "content/type", 47 desc: "undefined start Blob slice" 48 }); 49 50 test_blob(function() { 51 var blob = new Blob(["abcd"]); 52 return blob.slice(2, undefined, "content/type"); 53 }, { 54 expected: "cd", 55 type: "content/type", 56 desc: "undefined end Blob slice" 57 }); 58 59 test_blob(function() { 60 return new Blob().slice(0,0,null); 61 }, { 62 expected: "", 63 type: "null", 64 desc: "null type Blob slice" 65 }); 66 67 test_blob(function() { 68 return new Blob().slice(0,0,undefined); 69 }, { 70 expected: "", 71 type: "", 72 desc: "undefined type Blob slice" 73 }); 74 75 test_blob(function() { 76 return new Blob().slice(0,0); 77 }, { 78 expected: "", 79 type: "", 80 desc: "no type Blob slice" 81 }); 82 83 var arrayBuffer = new ArrayBuffer(16); 84 var int8View = new Int8Array(arrayBuffer); 85 for (var i = 0; i < 16; i++) { 86 int8View[i] = i + 65; 87 } 88 89 var testData = [ 90 [ 91 ["PASSSTRING"], 92 [{start: -6, contents: "STRING"}, 93 {start: -12, contents: "PASSSTRING"}, 94 {start: 4, contents: "STRING"}, 95 {start: 12, contents: ""}, 96 {start: 0, end: -6, contents: "PASS"}, 97 {start: 0, end: -12, contents: ""}, 98 {start: 0, end: 4, contents: "PASS"}, 99 {start: 0, end: 12, contents: "PASSSTRING"}, 100 {start: 7, end: 4, contents: ""}] 101 ], 102 103 // Test double start/end values 104 [ 105 ["abcd"], 106 [{start: 0.5, contents: "abcd"}, 107 {start: 1.5, contents: "cd"}, 108 {start: 2.5, contents: "cd"}, 109 {start: 3.5, contents: ""}, 110 {start: 0, end: 0.5, contents: ""}, 111 {start: 0, end: 1.5, contents: "ab"}, 112 {start: 0, end: 2.5, contents: "ab"}, 113 {start: 0, end: 3.5, contents: "abcd"}, 114 {start: 1.5, end: 2.5, contents: ""}, 115 {start: 1.5, end: 3.5, contents: "cd"}] 116 ], 117 118 // Test 3 strings 119 [ 120 ["foo", "bar", "baz"], 121 [{start: 0, end: 9, contents: "foobarbaz"}, 122 {start: 0, end: 3, contents: "foo"}, 123 {start: 3, end: 9, contents: "barbaz"}, 124 {start: 6, end: 9, contents: "baz"}, 125 {start: 6, end: 12, contents: "baz"}, 126 {start: 0, end: 9, contents: "foobarbaz"}, 127 {start: 0, end: 11, contents: "foobarbaz"}, 128 {start: 10, end: 15, contents: ""}] 129 ], 130 131 // Test string, Blob, string 132 [ 133 ["foo", blob1, "baz"], 134 [{start: 0, end: 3, contents: "foo"}, 135 {start: 3, end: 11, contents: "squiggle"}, 136 {start: 2, end: 4, contents: "os"}, 137 {start: 10, end: 12, contents: "eb"}] 138 ], 139 140 // Test blob, string, blob 141 [ 142 [blob1, "foo", blob1], 143 [{start: 0, end: 8, contents: "squiggle"}, 144 {start: 7, end: 9, contents: "ef"}, 145 {start: 10, end: 12, contents: "os"}, 146 {start: 1, end: 4, contents: "qui"}, 147 {start: 12, end: 15, contents: "qui"}, 148 {start: 40, end: 60, contents: ""}] 149 ], 150 151 // Test blobs all the way down 152 [ 153 [blob2, blob1, blob2], 154 [{start: 0, end: 5, contents: "steak"}, 155 {start: 5, end: 13, contents: "squiggle"}, 156 {start: 13, end: 18, contents: "steak"}, 157 {start: 1, end: 3, contents: "te"}, 158 {start: 6, end: 10, contents: "quig"}] 159 ], 160 161 // Test an ArrayBufferView 162 [ 163 [int8View, blob1, "foo"], 164 [{start: 0, end: 8, contents: "ABCDEFGH"}, 165 {start: 8, end: 18, contents: "IJKLMNOPsq"}, 166 {start: 17, end: 20, contents: "qui"}, 167 {start: 4, end: 12, contents: "EFGHIJKL"}] 168 ], 169 170 // Test a partial ArrayBufferView 171 [ 172 [new Uint8Array(arrayBuffer, 3, 5), blob1, "foo"], 173 [{start: 0, end: 8, contents: "DEFGHsqu"}, 174 {start: 8, end: 18, contents: "igglefoo"}, 175 {start: 4, end: 12, contents: "Hsquiggl"}] 176 ], 177 178 // Test type coercion of a number 179 [ 180 [3, int8View, "foo"], 181 [{start: 0, end: 8, contents: "3ABCDEFG"}, 182 {start: 8, end: 18, contents: "HIJKLMNOPf"}, 183 {start: 17, end: 21, contents: "foo"}, 184 {start: 4, end: 12, contents: "DEFGHIJK"}] 185 ], 186 187 [ 188 [(new Uint8Array([0, 255, 0])).buffer, 189 new Blob(['abcd']), 190 'efgh', 191 'ijklmnopqrstuvwxyz'], 192 [{start: 1, end: 4, contents: "\uFFFD\u0000a"}, 193 {start: 4, end: 8, contents: "bcde"}, 194 {start: 8, end: 12, contents: "fghi"}, 195 {start: 1, end: 12, contents: "\uFFFD\u0000abcdefghi"}] 196 ] 197 ]; 198 199 testData.forEach(function(data, i) { 200 var blobs = data[0]; 201 var tests = data[1]; 202 tests.forEach(function(expectations, j) { 203 test(function() { 204 var blob = new Blob(blobs); 205 assert_true(blob instanceof Blob); 206 assert_false(blob instanceof File); 207 208 test_blob(function() { 209 return expectations.end === undefined 210 ? blob.slice(expectations.start) 211 : blob.slice(expectations.start, expectations.end); 212 }, { 213 expected: expectations.contents, 214 type: "", 215 desc: "Slicing test: slice (" + i + "," + j + ")." 216 }); 217 }, "Slicing test (" + i + "," + j + ")."); 218 }); 219 }); 220 }, "Slices"); 221 222 var invalidTypes = [ 223 "\xFF", 224 "te\x09xt/plain", 225 "te\x00xt/plain", 226 "te\x1Fxt/plain", 227 "te\x7Fxt/plain" 228 ]; 229 invalidTypes.forEach(function(type) { 230 test_blob(function() { 231 var blob = new Blob(["PASS"]); 232 return blob.slice(0, 4, type); 233 }, { 234 expected: "PASS", 235 type: "", 236 desc: "Invalid contentType (" + format_value(type) + ")" 237 }); 238 }); 239 240 var validTypes = [ 241 "te(xt/plain", 242 "te)xt/plain", 243 "te<xt/plain", 244 "te>xt/plain", 245 "te@xt/plain", 246 "te,xt/plain", 247 "te;xt/plain", 248 "te:xt/plain", 249 "te\\xt/plain", 250 "te\"xt/plain", 251 "te/xt/plain", 252 "te[xt/plain", 253 "te]xt/plain", 254 "te?xt/plain", 255 "te=xt/plain", 256 "te{xt/plain", 257 "te}xt/plain", 258 "te\x20xt/plain", 259 "TEXT/PLAIN", 260 "text/plain;charset = UTF-8", 261 "text/plain;charset=UTF-8" 262 ]; 263 validTypes.forEach(function(type) { 264 test_blob(function() { 265 var blob = new Blob(["PASS"]); 266 return blob.slice(0, 4, type); 267 }, { 268 expected: "PASS", 269 type: type.toLowerCase(), 270 desc: "Valid contentType (" + format_value(type) + ")" 271 }); 272 });