• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html>
2<html>
3<head>
4<meta charset="csiso2022jp"> <!-- test breaks if the server overrides this -->
5<title>csiso2022jp encoding (form)</title>
6<meta name="timeout" content="long">
7<meta name="variant" content="?1-1000">
8<meta name="variant" content="?1001-2000">
9<meta name="variant" content="?2001-3000">
10<meta name="variant" content="?3001-4000">
11<meta name="variant" content="?4001-5000">
12<meta name="variant" content="?5001-6000">
13<meta name="variant" content="?6001-7000">
14<meta name="variant" content="?7001-last">
15<script src="/resources/testharness.js"></script>
16<script src="/resources/testharnessreport.js"></script>
17<script src="/common/subset-tests.js"></script>
18<script src="jis0208_index.js"></script>
19<script src="iso2022jp-encoder.js"></script>
20<link rel="author" title="Richard Ishida" href="mailto:ishida@w3.org">
21<link rel="help" href="https://encoding.spec.whatwg.org/#names-and-labels">
22<meta name="assert" content="The browser produces the same encoding behavior for a document labeled 'csiso2022jp' as for a document labeled 'iso-2022-jp'.">
23<style>
24 iframe { display:none }
25 form { display:none }
26</style>
27</head>
28<body>
29<div id="log"></div>
30<script src="../../resources/ranges.js"></script>
31<script>
32var errors = false;
33var separator = "\u0019";
34var ranges = rangesAll;
35
36function expect(result, codepoint) {
37    return result;
38}
39
40function encoder(str) {
41    return getByteSequence(str.codePointAt(0));
42}
43
44function getByteSequence(cp) {
45    // uses the Encoding spec algorithm to derive a sequence of bytes for a character that can be encoded
46    // the result is either a percent-encoded value or null (if the character can't be encoded)
47    // cp: integer, a code point number
48
49    var cps = [cp];
50    var endofstream = 2000000;
51    cps.push(endofstream);
52    var out = "";
53    var encState = "ascii";
54    var finished = false;
55    while (!finished) {
56        cp = cps.shift();
57        if (cp == endofstream && encState != "ascii") {
58            cps.unshift(cp);
59            encState = "ascii";
60            out += "%1B%28%42";
61            continue;
62        }
63        if (cp == endofstream && encState == "ascii") {
64            finished = true;
65            break;
66        }
67        if (encState == "ascii" && cp >= 0x00 && cp <= 0x7f) {
68            out += "%" + cp.toString(16).toUpperCase();
69            continue;
70        }
71        if (
72            encState == "roman" &&
73            ((cp >= 0x00 && cp <= 0x7f && cp !== 0x5c && cp !== 0x7e) ||
74                cp == 0xa5 ||
75                cp == 0x203e)
76        ) {
77            if (cp >= 0x00 && cp <= 0x7f) {
78                // ASCII
79                out += "%" + cp.toString(16).toUpperCase();
80                continue;
81            }
82            if (cp == 0xa5) {
83                out += "%5C";
84                continue;
85            }
86            if (cp == 0x203e) {
87                out += "%7E";
88                continue;
89            }
90        }
91        if (encState != "ascii" && cp >= 0x00 && cp <= 0x7f) {
92            cps.unshift(cp);
93            encState = "ascii";
94            out += "%1B%28%42";
95            continue;
96        }
97        if ((cp == 0xa5 || cp == 0x203e) && encState != "roman") {
98            cps.unshift(cp);
99            encState = "roman";
100            out += "%1B%28%4A";
101            continue;
102        }
103        if (cp == 0x2212) cp = 0xff0d;
104        ptr = indexcodepoints[cp];
105        if (ptr == null) {
106            return null;
107        }
108        if (encState != "jis0208") {
109            cps.unshift(cp);
110            encState = "jis0208";
111            out += "%1B%24%42";
112            continue;
113        }
114        var lead = Math.floor(ptr / 94) + 0x21;
115        var trail = ptr % 94 + 0x21;
116        out +=
117            "%" +
118            lead.toString(16).toUpperCase() +
119            "%" +
120            trail.toString(16).toUpperCase();
121    }
122    return out.trim();
123}
124
125// set up a sparse array of all unicode codepoints listed in the index
126// this will be used for lookup in getByteSequence
127var indexcodepoints = []; // index is unicode cp, value is pointer
128for (p = 0; p < jis0208.length; p++) {
129    if (jis0208[p] != null && indexcodepoints[jis0208[p]] == null) {
130        indexcodepoints[jis0208[p]] = p;
131    }
132}
133</script>
134<script src="../../resources/encode-form-common.js"></script>
135</body>
136</html>
137