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