11cb0ef41Sopenharmony_ci<!DOCTYPE html>
21cb0ef41Sopenharmony_ci<meta charset=utf-8>
31cb0ef41Sopenharmony_ci<title>File constructor: endings option</title>
41cb0ef41Sopenharmony_ci<link rel=help href="https://w3c.github.io/FileAPI/#file-constructor">
51cb0ef41Sopenharmony_ci<script src="/resources/testharness.js"></script>
61cb0ef41Sopenharmony_ci<script src="/resources/testharnessreport.js"></script>
71cb0ef41Sopenharmony_ci<script>
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci// Windows platforms use CRLF as the native line ending. All others use LF.
101cb0ef41Sopenharmony_ciconst crlf = navigator.platform.startsWith('Win');
111cb0ef41Sopenharmony_ciconst native_ending = crlf ? '\r\n' : '\n';
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cifunction readBlobAsPromise(blob) {
141cb0ef41Sopenharmony_ci  return new Promise((resolve, reject) => {
151cb0ef41Sopenharmony_ci    const reader = new FileReader();
161cb0ef41Sopenharmony_ci    reader.readAsText(blob);
171cb0ef41Sopenharmony_ci    reader.onload = e => resolve(reader.result);
181cb0ef41Sopenharmony_ci    reader.onerror = e => reject(reader.error);
191cb0ef41Sopenharmony_ci  });
201cb0ef41Sopenharmony_ci}
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci[
231cb0ef41Sopenharmony_ci  'transparent',
241cb0ef41Sopenharmony_ci  'native'
251cb0ef41Sopenharmony_ci].forEach(value => test(t => {
261cb0ef41Sopenharmony_ci  assert_class_string(new File([], "name", {endings: value}), 'File',
271cb0ef41Sopenharmony_ci                      `Constructor should allow "${value}" endings`);
281cb0ef41Sopenharmony_ci}, `Valid "endings" value: ${JSON.stringify(value)}`));
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci[
311cb0ef41Sopenharmony_ci  null,
321cb0ef41Sopenharmony_ci  '',
331cb0ef41Sopenharmony_ci  'invalidEnumValue',
341cb0ef41Sopenharmony_ci  'Transparent',
351cb0ef41Sopenharmony_ci  'NATIVE',
361cb0ef41Sopenharmony_ci  0,
371cb0ef41Sopenharmony_ci  {}
381cb0ef41Sopenharmony_ci].forEach(value => test(t => {
391cb0ef41Sopenharmony_ci  assert_throws_js(TypeError, () => new File([], "name", {endings: value}),
401cb0ef41Sopenharmony_ci                   'File constructor should throw');
411cb0ef41Sopenharmony_ci}, `Invalid "endings" value: ${JSON.stringify(value)}`));
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_citest(t => {
441cb0ef41Sopenharmony_ci  const test_error = {name: 'test'};
451cb0ef41Sopenharmony_ci  assert_throws_exactly(
461cb0ef41Sopenharmony_ci    test_error,
471cb0ef41Sopenharmony_ci    () => new File([], "name", { get endings() { throw test_error; }}),
481cb0ef41Sopenharmony_ci    'File constructor should propagate exceptions from "endings" property');
491cb0ef41Sopenharmony_ci}, 'Exception propagation from options');
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_citest(t => {
521cb0ef41Sopenharmony_ci  let got = false;
531cb0ef41Sopenharmony_ci  new File([], "name", { get endings() { got = true; } });
541cb0ef41Sopenharmony_ci  assert_true(got, 'The "endings" property was accessed during construction.');
551cb0ef41Sopenharmony_ci}, 'The "endings" options property is used');
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci[
581cb0ef41Sopenharmony_ci  {name: 'LF', input: '\n', native: native_ending},
591cb0ef41Sopenharmony_ci  {name: 'CR', input: '\r', native: native_ending},
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  {name: 'CRLF', input: '\r\n', native: native_ending},
621cb0ef41Sopenharmony_ci  {name: 'CRCR', input: '\r\r', native: native_ending.repeat(2)},
631cb0ef41Sopenharmony_ci  {name: 'LFCR', input: '\n\r', native: native_ending.repeat(2)},
641cb0ef41Sopenharmony_ci  {name: 'LFLF', input: '\n\n', native: native_ending.repeat(2)},
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  {name: 'CRCRLF', input: '\r\r\n', native: native_ending.repeat(2)},
671cb0ef41Sopenharmony_ci  {name: 'CRLFLF', input: '\r\n\n', native: native_ending.repeat(2)},
681cb0ef41Sopenharmony_ci  {name: 'CRLFCR', input: '\r\n\r\n', native: native_ending.repeat(2)},
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci  {name: 'CRLFCRLF', input: '\r\n\r\n', native: native_ending.repeat(2)},
711cb0ef41Sopenharmony_ci  {name: 'LFCRLFCR', input: '\n\r\n\r', native: native_ending.repeat(3)},
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci].forEach(testCase => {
741cb0ef41Sopenharmony_ci  promise_test(async t => {
751cb0ef41Sopenharmony_ci    const file = new File([testCase.input], "name");
761cb0ef41Sopenharmony_ci    assert_equals(
771cb0ef41Sopenharmony_ci      await readBlobAsPromise(file), testCase.input,
781cb0ef41Sopenharmony_ci      'Newlines should not change with endings unspecified');
791cb0ef41Sopenharmony_ci  }, `Input ${testCase.name} with endings unspecified`);
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci  promise_test(async t => {
821cb0ef41Sopenharmony_ci    const file = new File([testCase.input], "name", {endings: 'transparent'});
831cb0ef41Sopenharmony_ci    assert_equals(
841cb0ef41Sopenharmony_ci      await readBlobAsPromise(file), testCase.input,
851cb0ef41Sopenharmony_ci      'Newlines should not change with endings "transparent"');
861cb0ef41Sopenharmony_ci  }, `Input ${testCase.name} with endings 'transparent'`);
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci  promise_test(async t => {
891cb0ef41Sopenharmony_ci    const file = new File([testCase.input], "name", {endings: 'native'});
901cb0ef41Sopenharmony_ci    assert_equals(
911cb0ef41Sopenharmony_ci      await readBlobAsPromise(file), testCase.native,
921cb0ef41Sopenharmony_ci      'Newlines should match the platform with endings "native"');
931cb0ef41Sopenharmony_ci  }, `Input ${testCase.name} with endings 'native'`);
941cb0ef41Sopenharmony_ci});
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_cipromise_test(async t => {
971cb0ef41Sopenharmony_ci  const file = new File(['\r', '\n'], "name", {endings: 'native'});
981cb0ef41Sopenharmony_ci  const expected = native_ending.repeat(2);
991cb0ef41Sopenharmony_ci  assert_equals(
1001cb0ef41Sopenharmony_ci    await readBlobAsPromise(file), expected,
1011cb0ef41Sopenharmony_ci    'CR/LF in adjacent strings should be converted to two platform newlines');
1021cb0ef41Sopenharmony_ci}, `CR/LF in adjacent input strings`);
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ci</script>
105