Blob-constructor-endings.html (3637B)
1 <!DOCTYPE html> 2 <meta charset=utf-8> 3 <title>Blob constructor: endings option</title> 4 <link rel=help href="https://w3c.github.io/FileAPI/#constructorBlob"> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 <script> 8 9 // Windows platforms use CRLF as the native line ending. All others use LF. 10 const crlf = navigator.platform.startsWith('Win'); 11 const native_ending = crlf ? '\r\n' : '\n'; 12 13 function readBlobAsPromise(blob) { 14 return new Promise((resolve, reject) => { 15 const reader = new FileReader(); 16 reader.readAsText(blob); 17 reader.onload = e => resolve(reader.result); 18 reader.onerror = e => reject(reader.error); 19 }); 20 } 21 22 [ 23 'transparent', 24 'native' 25 ].forEach(value => test(t => { 26 assert_class_string(new Blob([], {endings: value}), 'Blob', 27 `Constructor should allow "${value}" endings`); 28 }, `Valid "endings" value: ${JSON.stringify(value)}`)); 29 30 [ 31 null, 32 '', 33 'invalidEnumValue', 34 'Transparent', 35 'NATIVE', 36 0, 37 {} 38 ].forEach(value => test(t => { 39 assert_throws_js(TypeError, () => new Blob([], {endings: value}), 40 'Blob constructor should throw'); 41 }, `Invalid "endings" value: ${JSON.stringify(value)}`)); 42 43 test(t => { 44 const test_error = {name: 'test'}; 45 assert_throws_exactly( 46 test_error, 47 () => new Blob([], { get endings() { throw test_error; }}), 48 'Blob constructor should propagate exceptions from "endings" property'); 49 }, 'Exception propagation from options'); 50 51 test(t => { 52 let got = false; 53 new Blob([], { get endings() { got = true; } }); 54 assert_true(got, 'The "endings" property was accessed during construction.'); 55 }, 'The "endings" options property is used'); 56 57 [ 58 {name: 'LF', input: '\n', native: native_ending}, 59 {name: 'CR', input: '\r', native: native_ending}, 60 61 {name: 'CRLF', input: '\r\n', native: native_ending}, 62 {name: 'CRCR', input: '\r\r', native: native_ending.repeat(2)}, 63 {name: 'LFCR', input: '\n\r', native: native_ending.repeat(2)}, 64 {name: 'LFLF', input: '\n\n', native: native_ending.repeat(2)}, 65 66 {name: 'CRCRLF', input: '\r\r\n', native: native_ending.repeat(2)}, 67 {name: 'CRLFLF', input: '\r\n\n', native: native_ending.repeat(2)}, 68 {name: 'CRLFCR', input: '\r\n\r\n', native: native_ending.repeat(2)}, 69 70 {name: 'CRLFCRLF', input: '\r\n\r\n', native: native_ending.repeat(2)}, 71 {name: 'LFCRLFCR', input: '\n\r\n\r', native: native_ending.repeat(3)}, 72 73 ].forEach(testCase => { 74 promise_test(async t => { 75 const blob = new Blob([testCase.input]); 76 assert_equals( 77 await readBlobAsPromise(blob), testCase.input, 78 'Newlines should not change with endings unspecified'); 79 }, `Input ${testCase.name} with endings unspecified`); 80 81 promise_test(async t => { 82 const blob = new Blob([testCase.input], {endings: 'transparent'}); 83 assert_equals( 84 await readBlobAsPromise(blob), testCase.input, 85 'Newlines should not change with endings "transparent"'); 86 }, `Input ${testCase.name} with endings 'transparent'`); 87 88 promise_test(async t => { 89 const blob = new Blob([testCase.input], {endings: 'native'}); 90 assert_equals( 91 await readBlobAsPromise(blob), testCase.native, 92 'Newlines should match the platform with endings "native"'); 93 }, `Input ${testCase.name} with endings 'native'`); 94 }); 95 96 promise_test(async t => { 97 const blob = new Blob(['\r', '\n'], {endings: 'native'}); 98 const expected = native_ending.repeat(2); 99 assert_equals( 100 await readBlobAsPromise(blob), expected, 101 'CR/LF in adjacent strings should be converted to two platform newlines'); 102 }, `CR/LF in adjacent input strings`); 103 104 </script>