File: decode-ignore-bom.any.js

package info (click to toggle)
firefox 144.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,637,504 kB
  • sloc: cpp: 7,576,692; javascript: 6,430,831; ansic: 3,748,119; python: 1,398,978; xml: 628,810; asm: 438,679; java: 186,194; sh: 63,212; makefile: 19,159; objc: 13,086; perl: 12,986; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (38 lines) | stat: -rw-r--r-- 1,701 bytes parent folder | download | duplicates (33)
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}`);
  }
}