Branch data Line data Source code
1 [ + ]: 1 : import assert from 'node:assert/strict';
2 : 1 : import { EOL } from 'node:os';
3 : 1 : import { File as FilePolyfill } from '../../../../lib/helper/file-polyfill.js';
4 : 1 :
5 : 1 : globalThis.File = FilePolyfill;
6 : 1 :
7 [ + ]: 1 : export default (activeURL) => {
8 : 1 :
9 [ + ]: 1 : [ 'transparent', 'native'].forEach(value => {
10 : 2 : assert_class_string(new File([], 'name', {endings: value}), 'File', `Constructor should allow "${value}" endings`);
11 : 1 : });
12 : 1 :
13 : 1 : [
14 : 1 : null,
15 : 1 : '',
16 : 1 : 'invalidEnumValue',
17 : 1 : 'Transparent',
18 : 1 : 'NATIVE',
19 : 1 : 0,
20 : 1 : {}
21 [ + ]: 1 : ].forEach(value => {
22 : 7 : assert.throws(
23 [ + ]: 7 : () => {
24 : 7 : new File([], 'name', {endings: value});
25 : 7 : },
26 [ + ]: 7 : (err) => {
27 : 7 : assert(err instanceof TypeError, `Invalid "endings" value: ${JSON.stringify(value)}`);
28 : 7 : return true;
29 : 7 : }
30 : 7 : );
31 : 1 : });
32 : 1 :
33 [ + ]: 1 : (function test(){
34 : 1 : const test_error = {name: 'test'};
35 : 1 :
36 : 1 : assert.throws(
37 [ + ]: 1 : () => {
38 [ + ]: 1 : new File([], "name", { get endings() { throw test_error; }});
39 : 1 : },
40 [ + ]: 1 : (err) => {
41 : 1 : assert.deepStrictEqual(err, test_error, 'File constructor should propagate exceptions from "endings" property');
42 : 1 : return true;
43 : 1 : }
44 : 1 : );
45 : 1 : })('Exception propagation from options');
46 : 1 :
47 [ + ]: 1 : (function test(){
48 : 1 : let got = false;
49 [ + ]: 1 : new File([], "name", { get endings() { got = true; } });
50 : 1 :
51 : 1 : assert(got, 'The "endings" property was accessed during construction.');
52 : 1 : })('The "endings" options property is used');
53 : 1 :
54 : 1 : [
55 : 1 : {name: 'LF', input: '\n', native: EOL},
56 : 1 : {name: 'CR', input: '\r', native: '\r'},
57 : 1 :
58 : 1 : {name: 'CRLF', input: '\r\n', native: EOL},
59 : 1 : {name: 'CRCR', input: '\r\r', native: '\r\r'},
60 : 1 : {name: 'LFCR', input: '\n\r', native: EOL + '\r'},
61 : 1 : {name: 'LFLF', input: '\n\n', native: EOL.repeat(2)},
62 : 1 :
63 : 1 : {name: 'CRCRLF', input: '\r\r\n', native: '\r' + EOL},
64 : 1 : {name: 'CRLFLF', input: '\r\n\n', native: EOL.repeat(2)},
65 : 1 : {name: 'CRLFCR', input: '\r\n\r\n', native: EOL.repeat(2)},
66 : 1 :
67 : 1 : {name: 'CRLFCRLF', input: '\r\n\r\n', native: EOL.repeat(2)},
68 : 1 : {name: 'LFCRLFCR', input: '\n\r\n\r', native: EOL.repeat(2) + '\r'},
69 : 1 :
70 [ + ]: 1 : ].forEach(testCase => {
71 : 11 :
72 [ + ]: 11 : (async() => {
73 : 11 : const file = new File([testCase.input], 'name');
74 : 11 : assert.strictEqual(
75 : 11 : await readBlobAsPromise(file),
76 : 11 : testCase.input,
77 : 11 : 'Newlines should not change with endings unspecified');
78 : 11 : })(`Input ${testCase.name} with endings unspecified`);
79 : 11 :
80 [ + ]: 11 : (async() => {
81 : 11 : const file = new File([testCase.input], "name", {endings: 'transparent'});
82 : 11 : assert.strictEqual(
83 : 11 : await readBlobAsPromise(file),
84 : 11 : testCase.input,
85 : 11 : 'Newlines should not change with endings "transparent"');
86 : 11 : })(`Input ${testCase.name} with endings 'transparent'`);
87 : 11 :
88 : 11 :
89 [ + ]: 11 : (async() => {
90 : 11 : const file = new File([testCase.input], "name", {endings: 'native'});
91 : 11 : assert.strictEqual(
92 : 11 : await readBlobAsPromise(file),
93 : 11 : testCase.native,
94 : 11 : 'Newlines should match the platform with endings "native');
95 : 11 : })(`Input ${testCase.name} with endings 'native'`);
96 : 1 : });
97 : 1 :
98 [ + ]: 1 : (async function test(){
99 : 1 : const file = new File(['\r', '\n'], "name", {endings: 'native'});
100 : 1 : const expected = '\r' + EOL;
101 : 1 :
102 : 1 : assert.strictEqual(
103 : 1 : await readBlobAsPromise(file),
104 : 1 : expected,
105 : 1 : 'CR/LF in adjacent strings should be converted to two platform newlines'
106 : 1 : );
107 : 1 : })(`CR/LF in adjacent input strings`);
108 : 1 :
109 [ + ]: 1 : function assert_class_string(object, class_string, description){
110 : 2 : var actual = {}.toString.call(object);
111 : 2 : var expected = "[object " + class_string + "]";
112 : 2 :
113 : 2 : assert.strictEqual(actual, expected, description);
114 : 2 : }
115 : 1 :
116 [ + ]: 1 : function readBlobAsPromise(blob){
117 : 34 : return blob.text();
118 : 34 : }
119 : 1 : }
120 : 1 :
121 : 1 : /*
122 : 1 : * File-constructor-endings.html
123 : 1 : *
124 : 1 :
125 : 1 : <!DOCTYPE html>
126 : 1 : <meta charset=utf-8>
127 : 1 : <title>File constructor: endings option</title>
128 : 1 : <link rel=help href="https://w3c.github.io/FileAPI/#file-constructor">
129 : 1 : <script src="/resources/testharness.js"></script>
130 : 1 : <script src="/resources/testharnessreport.js"></script>
131 : 1 : <script>
132 : 1 :
133 : 1 : // Windows platforms use CRLF as the native line ending. All others use LF.
134 : 1 : const crlf = navigator.platform.startsWith('Win');
135 : 1 : const native_ending = crlf ? '\r\n' : '\n';
136 : 1 :
137 : 1 : function readBlobAsPromise(blob) {
138 : 1 : return new Promise((resolve, reject) => {
139 : 1 : const reader = new FileReader();
140 : 1 : reader.readAsText(blob);
141 : 1 : reader.onload = e => resolve(reader.result);
142 : 1 : reader.onerror = e => reject(reader.error);
143 : 1 : });
144 : 1 : }
145 : 1 :
146 : 1 : [
147 : 1 : 'transparent',
148 : 1 : 'native'
149 : 1 : ].forEach(value => test(t => {
150 : 1 : assert_class_string(new File([], "name", {endings: value}), 'File',
151 : 1 : `Constructor should allow "${value}" endings`);
152 : 1 : }, `Valid "endings" value: ${JSON.stringify(value)}`));
153 : 1 :
154 : 1 : [
155 : 1 : null,
156 : 1 : '',
157 : 1 : 'invalidEnumValue',
158 : 1 : 'Transparent',
159 : 1 : 'NATIVE',
160 : 1 : 0,
161 : 1 : {}
162 : 1 : ].forEach(value => test(t => {
163 : 1 : assert_throws_js(TypeError, () => new File([], "name", {endings: value}),
164 : 1 : 'File constructor should throw');
165 : 1 : }, `Invalid "endings" value: ${JSON.stringify(value)}`));
166 : 1 :
167 : 1 : test(t => {
168 : 1 : const test_error = {name: 'test'};
169 : 1 : assert_throws_exactly(
170 : 1 : test_error,
171 : 1 : () => new File([], "name", { get endings() { throw test_error; }}),
172 : 1 : 'File constructor should propagate exceptions from "endings" property');
173 : 1 : }, 'Exception propagation from options');
174 : 1 :
175 : 1 : test(t => {
176 : 1 : let got = false;
177 : 1 : new File([], "name", { get endings() { got = true; } });
178 : 1 : assert_true(got, 'The "endings" property was accessed during construction.');
179 : 1 : }, 'The "endings" options property is used');
180 : 1 :
181 : 1 : [
182 : 1 : {name: 'LF', input: '\n', native: native_ending},
183 : 1 : {name: 'CR', input: '\r', native: native_ending},
184 : 1 :
185 : 1 : {name: 'CRLF', input: '\r\n', native: native_ending},
186 : 1 : {name: 'CRCR', input: '\r\r', native: native_ending.repeat(2)},
187 : 1 : {name: 'LFCR', input: '\n\r', native: native_ending.repeat(2)},
188 : 1 : {name: 'LFLF', input: '\n\n', native: native_ending.repeat(2)},
189 : 1 :
190 : 1 : {name: 'CRCRLF', input: '\r\r\n', native: native_ending.repeat(2)},
191 : 1 : {name: 'CRLFLF', input: '\r\n\n', native: native_ending.repeat(2)},
192 : 1 : {name: 'CRLFCR', input: '\r\n\r\n', native: native_ending.repeat(2)},
193 : 1 :
194 : 1 : {name: 'CRLFCRLF', input: '\r\n\r\n', native: native_ending.repeat(2)},
195 : 1 : {name: 'LFCRLFCR', input: '\n\r\n\r', native: native_ending.repeat(3)},
196 : 1 :
197 : 1 : ].forEach(testCase => {
198 : 1 : promise_test(async t => {
199 : 1 : const file = new File([testCase.input], "name");
200 : 1 : assert_equals(
201 : 1 : await readBlobAsPromise(file), testCase.input,
202 : 1 : 'Newlines should not change with endings unspecified');
203 : 1 : }, `Input ${testCase.name} with endings unspecified`);
204 : 1 :
205 : 1 : promise_test(async t => {
206 : 1 : const file = new File([testCase.input], "name", {endings: 'transparent'});
207 : 1 : assert_equals(
208 : 1 : await readBlobAsPromise(file), testCase.input,
209 : 1 : 'Newlines should not change with endings "transparent"');
210 : 1 : }, `Input ${testCase.name} with endings 'transparent'`);
211 : 1 :
212 : 1 : promise_test(async t => {
213 : 1 : const file = new File([testCase.input], "name", {endings: 'native'});
214 : 1 : assert_equals(
215 : 1 : await readBlobAsPromise(file), testCase.native,
216 : 1 : 'Newlines should match the platform with endings "native"');
217 : 1 : }, `Input ${testCase.name} with endings 'native'`);
218 : 1 : });
219 : 1 :
220 : 1 : promise_test(async t => {
221 : 1 : const file = new File(['\r', '\n'], "name", {endings: 'native'});
222 : 1 : const expected = native_ending.repeat(2);
223 : 1 : assert_equals(
224 : 1 : await readBlobAsPromise(file), expected,
225 : 1 : 'CR/LF in adjacent strings should be converted to two platform newlines');
226 : 1 : }, `CR/LF in adjacent input strings`);
227 : 1 :
228 : 1 : </script>
229 : 1 :
230 : 1 : *
231 : 1 : *
232 : 1 : */
|