summaryrefslogtreecommitdiffstats
path: root/test/wpt/tests/FileAPI/file/File-constructor-endings.html
blob: 1282b6c5ac2c790803eea11ad74183ae8d78bd07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<!DOCTYPE html>
<meta charset=utf-8>
<title>File constructor: endings option</title>
<link rel=help href="https://w3c.github.io/FileAPI/#file-constructor">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>

// Windows platforms use CRLF as the native line ending. All others use LF.
const crlf = navigator.platform.startsWith('Win');
const native_ending = crlf ? '\r\n' : '\n';

function readBlobAsPromise(blob) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsText(blob);
    reader.onload = e => resolve(reader.result);
    reader.onerror = e => reject(reader.error);
  });
}

[
  'transparent',
  'native'
].forEach(value => test(t => {
  assert_class_string(new File([], "name", {endings: value}), 'File',
                      `Constructor should allow "${value}" endings`);
}, `Valid "endings" value: ${JSON.stringify(value)}`));

[
  null,
  '',
  'invalidEnumValue',
  'Transparent',
  'NATIVE',
  0,
  {}
].forEach(value => test(t => {
  assert_throws_js(TypeError, () => new File([], "name", {endings: value}),
                   'File constructor should throw');
}, `Invalid "endings" value: ${JSON.stringify(value)}`));

test(t => {
  const test_error = {name: 'test'};
  assert_throws_exactly(
    test_error,
    () => new File([], "name", { get endings() { throw test_error; }}),
    'File constructor should propagate exceptions from "endings" property');
}, 'Exception propagation from options');

test(t => {
  let got = false;
  new File([], "name", { get endings() { got = true; } });
  assert_true(got, 'The "endings" property was accessed during construction.');
}, 'The "endings" options property is used');

[
  {name: 'LF', input: '\n', native: native_ending},
  {name: 'CR', input: '\r', native: native_ending},

  {name: 'CRLF', input: '\r\n', native: native_ending},
  {name: 'CRCR', input: '\r\r', native: native_ending.repeat(2)},
  {name: 'LFCR', input: '\n\r', native: native_ending.repeat(2)},
  {name: 'LFLF', input: '\n\n', native: native_ending.repeat(2)},

  {name: 'CRCRLF', input: '\r\r\n', native: native_ending.repeat(2)},
  {name: 'CRLFLF', input: '\r\n\n', native: native_ending.repeat(2)},
  {name: 'CRLFCR', input: '\r\n\r\n', native: native_ending.repeat(2)},

  {name: 'CRLFCRLF', input: '\r\n\r\n', native: native_ending.repeat(2)},
  {name: 'LFCRLFCR', input: '\n\r\n\r', native: native_ending.repeat(3)},

].forEach(testCase => {
  promise_test(async t => {
    const file = new File([testCase.input], "name");
    assert_equals(
      await readBlobAsPromise(file), testCase.input,
      'Newlines should not change with endings unspecified');
  }, `Input ${testCase.name} with endings unspecified`);

  promise_test(async t => {
    const file = new File([testCase.input], "name", {endings: 'transparent'});
    assert_equals(
      await readBlobAsPromise(file), testCase.input,
      'Newlines should not change with endings "transparent"');
  }, `Input ${testCase.name} with endings 'transparent'`);

  promise_test(async t => {
    const file = new File([testCase.input], "name", {endings: 'native'});
    assert_equals(
      await readBlobAsPromise(file), testCase.native,
      'Newlines should match the platform with endings "native"');
  }, `Input ${testCase.name} with endings 'native'`);
});

promise_test(async t => {
  const file = new File(['\r', '\n'], "name", {endings: 'native'});
  const expected = native_ending.repeat(2);
  assert_equals(
    await readBlobAsPromise(file), expected,
    'CR/LF in adjacent strings should be converted to two platform newlines');
}, `CR/LF in adjacent input strings`);

</script>