summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/encoding/streams/decode-ignore-bom.any.js
blob: 92f89c80158c358cdfdc06fdaae6e48e068f9f9f (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
// META: global=window,worker
// META: script=resources/readable-stream-from-array.js
// META: script=resources/readable-stream-to-array.js

const cases = [
    {encoding: 'utf-8', bytes: [0xEF, 0xBB, 0xBF, 0x61, 0x62, 0x63]},
    {encoding: 'utf-16le', bytes: [0xFF, 0xFE, 0x61, 0x00, 0x62, 0x00, 0x63, 0x00]},
    {encoding: 'utf-16be', bytes: [0xFE, 0xFF, 0x00, 0x61, 0x00, 0x62, 0x00, 0x63]}
];
const BOM = '\uFEFF';

// |inputChunks| is an array of chunks, each represented by an array of
// integers. |ignoreBOM| is true or false. The result value is the output of the
// pipe, concatenated into a single string.
async function pipeAndAssemble(inputChunks, encoding, ignoreBOM) {
  const chunksAsUint8 = inputChunks.map(values => new Uint8Array(values));
  const readable = readableStreamFromArray(chunksAsUint8);
  const outputArray = await readableStreamToArray(readable.pipeThrough(
      new TextDecoderStream(encoding, {ignoreBOM})));
  return outputArray.join('');
}

for (const testCase of cases) {
  for (let splitPoint = 0; splitPoint < 4; ++splitPoint) {
    promise_test(async () => {
      const inputChunks = [testCase.bytes.slice(0, splitPoint),
                           testCase.bytes.slice(splitPoint)];
      const withIgnoreBOM =
            await pipeAndAssemble(inputChunks, testCase.encoding, true);
      assert_equals(withIgnoreBOM, BOM + 'abc', 'BOM should be preserved');

      const withoutIgnoreBOM =
            await pipeAndAssemble(inputChunks, testCase.encoding, false);
      assert_equals(withoutIgnoreBOM, 'abc', 'BOM should be stripped')
    }, `ignoreBOM should work for encoding ${testCase.encoding}, split at ` +
       `character ${splitPoint}`);
  }
}