-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathEncoding-test.html
398 lines (340 loc) · 9.66 KB
/
Encoding-test.html
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<!DOCTYPE html><html lang="ja"><head><meta charset="utf-8">
<title>TextDecoder/TextEncoder の試用</title>
<meta name="color-scheme" content="light dark">
<link rel="stylesheet" href="common.css" type="text/css">
<style >
body {
margin: 1em;
padding: 0;
}
table {
border-collapse: collapse;
border: solid thin var(--N-color);
}
th, td {
text-align: left;
vertical-align: baseline;
padding: 0.2em 0.5em;
border: solid thin var(--N-color);
}
th {
white-space:nowrap;
}
p {
margin: 0.5em 0;
}
.num-field {
min-width: 6em;
text-align: right;
}
.disabled {
color: var(--dim-color);
}
</style>
<script >
function E(id){
return document.getElementById(id);
}
function setText(id, text){
E(id).textContent = text;
}
function errorText(error){
return (error.name || '') + ':' + (error.message || '');
}
window.onload = function(){
var support = 'addEventListener, FileReader, Uint8Array, TextDecoder'
.replace(/\w+/g, function(feature){
return feature + ': ' + ((feature in window)? 'ok' : '未サポート');
});
E('output').value = '';
setText('error1', support);
resetOutputWrap();
E('file_input').addEventListener('change', function(event){
readFile(event.target.files[0]);
}, false);
E('read_more').addEventListener('click', decode, false);
E('reload').addEventListener('click', function(){
readFile(current_file);
}, false);
E('encode').addEventListener('click', encode, false);
E('output_format').addEventListener('change', function(event){
outputBinaryAsText();
}, false);
E('wrap_output').addEventListener('change', resetOutputWrap, false);
function resetOutputWrap(){
E('output').style.whiteSpace =
E('wrap_output').checked ? 'pre-wrap' : 'pre';
}
}
var current_file = null;
function readFile(file){
if(!file) return;
setText('error', '');
current_file = file;
setText('file_type', file.type || '?');
setText('file_size', file.size || '?');
if(! E('ignore_type').checked && ! /^text\//.test(file.type)) {
setText('error', '入力ファイルはテキストファイルではないようです');
return;
}
var reader = new FileReader();
/*
reader.onprogress = function(event){
console.log('onprogress: ' + event.total);
if(event.lengthComputable && event.total > limit_size) {...}
}
*/
reader.onload = function(event){
if(reader.result.byteLength === 0){
setText('error', '入力ファイルが空です');
return;
}
try {
init_decoder(reader.result);//ArrayBuffer
decode(); // 内容を表示
} catch(e){
setText('error', 'エラー:' + errorText(e));
}
}
reader.onerror = function(event){
setText('error', '読み取りエラー:' + errorText(reader.error));
}
reader.readAsArrayBuffer(file);
}
var init_decoder, decode;
var encode, outputBinaryAsText;
new function(){
var buffer = null;
var decoder = null;
var offset = 0;
init_decoder = function(buf){
setText('read_size', 0);
buffer = buf;
offset = 0;
E('read_more').disabled = false;
decoder = new TextDecoder(
E('labels').value,
{ fatal: !E('ignore_fatal').checked }
);
};
decode = function(){
if(!decoder) return;
var length = parseInt(E('size_filter').value) *
(E('kilo_unit').checked ? 1024 : 1);
if(!length) length = Infinity;
length = Math.min(buffer.byteLength - offset, length);
if(length <= 0) return;
var view = new Uint8Array(buffer, offset, length);
try {
var text = decoder.decode(view, {stream: true});
E('output').value = text;
} catch(e) {
setText('error', '復号エラー:' + errorText(e));
}
offset += length;
setText('read_size', offset);
if(offset >= buffer.byteLength){
E('read_more').disabled = true;
}
};
var encoder = null;
var encoder_buffer = null;
encode = function(){
if(!encoder) {
encoder = new TextEncoder();
E('download').setAttribute('download', 'utf-8-encoded.txt')
}
var text = E('output').value;
encoder_buffer = encoder.encode(text);
if(URL.createObjectURL){
E('download').href = URL.createObjectURL(new Blob([encoder_buffer]));
E('download').parentNode.className = 'enabled';
}
E('encoded_size').textContent = encoder_buffer.byteLength;
outputBinaryAsText();
}
outputBinaryAsText = function(){
if(!encoder_buffer) return;
var output = '';
switch(E('output_format').value){
case 'base64':
encoder_buffer.forEach(function(b){
output += String.fromCharCode( b );
});
output = window.btoa(output);
break;
case 'Hex':
encoder_buffer.forEach(function(b){
output += b.toString( 16 );
});
break;
default:
encoder_buffer.forEach(function(b){
output += String.fromCharCode(0x2800 + b);
});
break;
}
E('encode_output').value = output.replace(/.{64}/g, '$&\n');
}
}
</script>
</head>
<body class="_expanded">
<h1><code>TextDecoder</code> を試す</h1>
<p>
<a href="https://triple-underscore.github.io/Encoding-test.html">このページ</a>
は、
<a href="http://www.whatwg.org/" >WHATWG</a>
による
<a href="http://encoding.spec.whatwg.org/" >Enocoding Standard</a>(
<a href="Encoding-ja.html" >和訳</a>
)の,
<a href="Encoding-ja.html#interface-textdecoder" ><code >TextDecoder</code></a>
インタフェースの
<a href="Encoding-ja.html#dom-textdecoder-decode" ><code >decode</code></a>
メソッドのブラウザ実装を試すものです。
</p>
<div style="margin-left:2em; font-size: smaller;">
<p >
このページを利用するために必要な,閲覧中のブラウザ機能の検査結果:<br>
<span id="error1"></span>
</p>
</div>
<h2 >利用法</h2>
<p>
“入力ファイル” 行から入力ファイルを選択して下さい。
ファイルは “符号化法ラベル” に指定される符号化法に従って復号され、その結果の Unicode テキストが “出力結果” に表示されます。
</p>
<table><tbody>
<tr><th>入力ファイル:
<td colspan="2">
<input type="file" id="file_input" /><!-- accept="text/*" -->
<input type="button" value="再読み込み" id="reload"/>
<tr><th><a href="./encoding-ja.html#label" >符号化法ラベル</a>
<td>
<select size="1" id="labels">
<option selected="selected" >utf-8</option>
<option>ibm866</option>
<option>iso-8859-2</option>
<option>iso-8859-3</option>
<option>iso-8859-4</option>
<option>iso-8859-5</option>
<option>iso-8859-6</option>
<option>iso-8859-7</option>
<option>iso-8859-8</option>
<option>iso-8859-8-i</option>
<option>iso-8859-10</option>
<option>iso-8859-13</option>
<option>iso-8859-14</option>
<option>iso-8859-15</option>
<option>iso-8859-16</option>
<option>koi8-r</option>
<option>koi8-u</option>
<option>macintosh</option>
<option>windows-874</option>
<option>windows-1250</option>
<option>windows-1251</option>
<option>windows-1252</option>
<option>windows-1253</option>
<option>windows-1254</option>
<option>windows-1255</option>
<option>windows-1256</option>
<option>windows-1257</option>
<option>windows-1258</option>
<option>x-mac-cyrillic</option>
<option>gb18030</option>
<option>hz-gb-2312</option>
<option>big5</option>
<option>euc-jp</option>
<option>iso-2022-jp</option>
<option>shift_jis</option>
<option>euc-kr</option>
<option>utf-16be</option>
<option>utf-16le</option>
<option>x-user-defined</option>
</select>
<td>
<label><input type="checkbox" id="ignore_fatal" />
復号エラーを無視</label>(*)
<tr><th><a href="File_API-ja.html#dfn-type">ファイルの種類</a>
<td>
<span id="file_type">?</span>
<td>
<label><input type="checkbox" id="ignore_type" />
非テキストファイルでも処理する</label>(*)
<tr><th><a href="File_API-ja.html#dfn-size" >ファイルのサイズ</a>
<td class="num-field">
<span id="file_size">?</span>
<td>
約
<select size="1" id="size_filter">
<option>1</option>
<option>2</option>
<option>4</option>
<option>8</option>
<option>16</option>
<option>32</option>
<option selected>64</option>
<option>128</option>
<option>256</option>
<option>512</option>
<option>1024</option>
<option>無限</option>
</select>
(<label ><input type="checkbox" id="kilo_unit" checked />k</label>)
バイトごとに分けて読み取る(*)
<tr><th>累積 読み取りサイズ
<td class="num-field">
<span id="read_size">0</span>
<td>
<input type="button" value="続きを読み取る" id="read_more"/>
</tbody></table>
<p><small>
(*)ファイルが巨大な場合、設定によっては、(主に,ブラウザが出力テキストを描画するときの)処理に時間がかかるかもしれません。</small></p>
<!--
<label><input type="checkbox" id="stream" />
追加</label>
-->
<span id="error" style="color: red;"></span><br />
出力結果:
<label ><input type="checkbox" id="wrap_output" />自動改行</label>
<br />
<textarea
id="output"
rows="30" cols="80"
style="width:100%; height:30em; white-space:pre;"
></textarea>
<h2><code>TextEncoder</code> を試す</h2>
<p >
上のテキストエリアの内容を
<input id="encode" type="button" value="符号化する" />
<small>(符号化法は utf-8 のみ。他の符号化法は可用でない。)</small>:
</p>
<ul>
<li class="disabled">
<a
id="download"
download="encoded-text.txt"
>ダウンロード</a>(要 <a href="File_API-ja.html#dfn-createObjectURL"><code>URL.createObjectURL()</code></a> サポート)
</li>
<li>
結果のバイナリデータのサイズ:<span id="encoded_size">?</span> バイト
</li>
<li>
結果のバイナリデータを
<select size="1" id="output_format">
<option selected="selected">Unicode 点字</option>
<option>base64</option>
<option>Hex</option>
</select>
形式により, 64 文字ごとに改行して下のテキストエリアに表示させる:
</li>
</ul>
<textarea
id="encode_output"
autocomplete="off"
rows="30" cols="80"
style="width:100%; height:30em; white-space:pre;"
></textarea>
</body>
</html>