summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/encoding/legacy-mb-japanese/iso-2022-jp/iso2022jp-encode-form-errors-stateful.html
blob: c95b13d1d65e1745567b74c48a03ad95e50779fd (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
<!DOCTYPE html>
<meta charset="utf-8">
<title>Encoding: ISO-2022-JP unencodable replacement in form submission</title>
<link rel="help" href="https://encoding.spec.whatwg.org/#iso-2022-jp-encoder">
<link rel="author" title="Benjamin C. Wiley Sittler"
      href="mailto:bsittler@chromium.org">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
'use strict';

promise_test(async testCase => {
  const target = Object.assign(document.createElement('iframe'), {
    name: 'target',
  });
  if (document.readyState !== 'complete') {
    await new Promise(resolve => addEventListener('load', resolve));
  }
  document.body.insertBefore(target, document.body.firstChild);
  const form = Object.assign(document.createElement('form'), {
    acceptCharset: 'iso-2022-jp',
    // NOTE: This uses escape and unescape rather than
    // encodeURI/encodeURIComponent and decodeURI/decodeURIComponent
    // intentionally, because:
    //
    // - escape() is required because encodeURI{,Component} encodes
    //   using UTF-8, and would falsely imply that non-ASCII
    //   characters are expected to work here -- which they won't, as
    //   the data URI has ISO-2022-JP charset; likewise
    //
    // - unescape() is required because the encoded byte sequence
    //   we're trying to decode and verify represents ISO-2022-JP data
    //   rather than the UTF-8 expected by decodeURI{,Component}
    //
    // The resulting document inside the IFRAME will look like:
    // <body onload="..."><plaintext>?utf16=...
    action: 'data:text/html;charset=iso-2022-jp,' + escape(
            '<body onload="(' +
            (() => parent.postMessage({
              utf16: document.body.innerText.split('=').pop(),
              iso2022jp: unescape(location.href.split('=').pop()),
            }, '*')) +
            ')()"><plaintext>'),
    target: target.name,
  });
  form.appendChild(Object.assign(document.createElement('input'), {
    name: 'utf16',
    value: 'ABC~¤•★星🌟星★•¤~XYZ',
  }));
  document.body.insertBefore(form, document.body.firstChild);
  const {iso2022jp, utf16} = await new Promise(resolve => {
    addEventListener('message', ({data}) => resolve(data));
    form.submit();
  });
  assert_equals(utf16, 'ABC~&#164;&#8226;★星&#127775;星★&#8226;&#164;~XYZ');
  assert_equals(
      iso2022jp,
      'ABC~&#164;&#8226;\x1b$B!z@1\x1b(B&#127775;\x1b$B@1!z\x1b(B&#8226;&#164;~XYZ');
}, 'Form submission using ISO-2022-JP correctly replaces unencodables');

</script>