-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
309 lines (291 loc) · 9.46 KB
/
Copy pathindex.html
File metadata and controls
309 lines (291 loc) · 9.46 KB
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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>字符编码工具</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="../global.css">
<style>
.toolbar {
display: flex;
gap: 10px;
flex-wrap: wrap;
align-items: center;
margin-top: 14px;
}
.hint {
font-size: 13px;
color: var(--text-secondary);
}
#msg {
font-size: 13px;
color: #d63031;
margin-top: 10px;
}
#msg:empty {
display: none;
}
#output {
font-family: var(--mono);
font-size: 13px;
line-height: 1.7;
background: var(--bg);
border: 1px solid var(--border);
border-radius: 8px;
padding: 12px 14px;
white-space: pre-wrap;
word-break: break-all;
min-height: 46px;
}
.codepoint-table {
width: 100%;
border-collapse: collapse;
font-size: 13px;
}
.codepoint-table th,
.codepoint-table td {
padding: 8px 10px;
border-bottom: 1px solid var(--border);
text-align: left;
}
.codepoint-table th {
font-size: 12px;
color: var(--text-secondary);
letter-spacing: .5px;
}
.codepoint-table td {
font-family: var(--mono);
}
.codepoint-table tr:last-child td {
border-bottom: none;
}
</style>
</head>
<body>
<div class="container">
<a class="back-link" href="../index.html">← 返回首页</a>
<header class="page-header">
<h1>字符编码工具</h1>
<p>字符串与 JS 字符串字面值互转,查看字符码点与 UTF-8 字节</p>
</header>
<section class="panel">
<h2>输入</h2>
<textarea id="input" class="mono-input" rows="6" placeholder='输入要转换的字符串,或 JS 字符串字面值(如 "你好\n")'></textarea>
<div class="toolbar">
<button class="btn btn-primary" id="btn-encode">编码为 JS 字面值</button>
<button class="btn btn-ghost" id="btn-decode">从字面值解码</button>
<button class="btn btn-ghost" id="btn-copy">复制结果</button>
<span class="hint">Ctrl + Enter 快捷编码</span>
</div>
<div id="msg"></div>
</section>
<section class="panel">
<h2>结果</h2>
<pre id="output"></pre>
</section>
<section class="panel" id="codepoint-panel" hidden>
<h2>码点明细</h2>
<table class="codepoint-table">
<thead>
<tr><th>字符</th><th>Unicode</th><th>UTF-8 字节</th></tr>
</thead>
<tbody id="codepoint-body"></tbody>
</table>
</section>
</div>
<script>
'use strict';
const MAX_ROWS = 200;
const encoder = new TextEncoder();
const input = document.getElementById('input');
const output = document.getElementById('output');
const msg = document.getElementById('msg');
const codepointPanel = document.getElementById('codepoint-panel');
const codepointBody = document.getElementById('codepoint-body');
const btnEncode = document.getElementById('btn-encode');
const btnDecode = document.getElementById('btn-decode');
const btnCopy = document.getElementById('btn-copy');
function hex(b) {
return b.toString(16).toUpperCase().padStart(2, '0');
}
function toJsLiteral(text) {
let out = '"';
for (const ch of text) {
const cp = ch.codePointAt(0);
if (ch === '"') out += '\\"';
else if (ch === '\\') out += '\\\\';
else if (ch === '\n') out += '\\n';
else if (ch === '\r') out += '\\r';
else if (ch === '\t') out += '\\t';
else if (ch === '\b') out += '\\b';
else if (ch === '\f') out += '\\f';
else if (ch === '\v') out += '\\v';
else if (cp < 0x20 || cp === 0x7F) out += '\\x' + hex(cp);
else if (cp < 0x7F) out += ch;
else if (cp <= 0xFFFF) out += '\\u' + cp.toString(16).toUpperCase().padStart(4, '0');
else out += '\\u{' + cp.toString(16).toUpperCase() + '}';
}
return out + '"';
}
function encode() {
const text = input.value;
if (!text) {
output.textContent = '';
codepointPanel.hidden = true;
msg.textContent = '请输入内容';
return;
}
output.textContent = toJsLiteral(text);
const truncated = renderCodepoints(text);
msg.textContent = truncated ? `字符较多,码点表仅显示前 ${MAX_ROWS} 个` : '';
}
function decode() {
let text = input.value;
if (!text) {
output.textContent = '';
codepointPanel.hidden = true;
msg.textContent = '请输入内容';
return;
}
// 整体被引号包裹时视为完整字面值,去掉首尾引号
if (text.length >= 2) {
const q = text[0];
if ((q === '"' || q === "'" || q === '`') && text[text.length - 1] === q) {
text = text.slice(1, -1);
}
}
let out = '';
let i = 0;
let invalidCount = 0;
let octalCount = 0;
const simple = { n: '\n', r: '\r', t: '\t', b: '\b', f: '\f', v: '\v' };
while (i < text.length) {
if (text[i] !== '\\') {
let j = text.indexOf('\\', i);
if (j === -1) j = text.length;
out += text.slice(i, j);
i = j;
continue;
}
const rest = text.slice(i);
const n = text[i + 1];
if (n === undefined) {
// 末尾孤立反斜杠按字面保留
out += '\\';
invalidCount++;
i++;
continue;
}
// 行延续:\ 后紧跟换行不产生字符
if (n === '\n') { i += 2; continue; }
if (n === '\r') { i += (text[i + 2] === '\n') ? 3 : 2; continue; }
if (n.codePointAt(0) === 0x2028 || n.codePointAt(0) === 0x2029) { i += 2; continue; }
if (Object.hasOwn(simple, n)) { out += simple[n]; i += 2; continue; }
let m = rest.match(/^\\x([0-9a-fA-F]{2})/);
if (m) {
out += String.fromCharCode(parseInt(m[1], 16));
i += m[0].length;
continue;
}
m = rest.match(/^\\u\{([0-9a-fA-F]+)\}/);
if (m) {
const cp = parseInt(m[1], 16);
if (cp <= 0x10FFFF) {
out += String.fromCodePoint(cp);
} else {
out += m[0];
invalidCount++;
}
i += m[0].length;
continue;
}
m = rest.match(/^\\u([0-9a-fA-F]{4})/);
if (m) {
out += String.fromCharCode(parseInt(m[1], 16));
i += m[0].length;
continue;
}
if (/^\\[xu]/.test(rest)) {
out += '\\';
invalidCount++;
i++;
continue;
}
// \0 之外的八进制转义仅非严格模式有效
m = rest.match(/^\\([0-3][0-7]{0,2}|[4-7][0-7]?)/);
if (m) {
out += String.fromCharCode(parseInt(m[1], 8));
if (m[1] !== '0') octalCount++;
i += m[0].length;
continue;
}
// \ 后跟其他字符即该字符本身(如 \\、\"、\a)
out += n;
i += 2;
}
output.textContent = out;
const truncated = renderCodepoints(out);
const notes = [];
if (invalidCount) notes.push(`${invalidCount} 处无效或超出范围的转义已按字面保留`);
if (octalCount) notes.push(`${octalCount} 处八进制转义在严格模式下会报错`);
if (truncated) notes.push(`字符较多,码点表仅显示前 ${MAX_ROWS} 个`);
msg.textContent = notes.join(';');
}
function displayChar(ch) {
switch (ch) {
case '\n': return '\\n(换行)';
case '\r': return '\\r(回车)';
case '\t': return '\\t(制表)';
case ' ': return '␣(空格)';
}
const cp = ch.codePointAt(0);
if (cp < 0x20 || cp === 0x7F) return '\\x' + hex(cp);
return ch;
}
function renderCodepoints(text) {
codepointBody.textContent = '';
const chars = Array.from(text);
if (!chars.length) {
codepointPanel.hidden = true;
return false;
}
codepointPanel.hidden = false;
for (const ch of chars.slice(0, MAX_ROWS)) {
const cp = ch.codePointAt(0);
const tr = document.createElement('tr');
const tdChar = document.createElement('td');
tdChar.textContent = displayChar(ch);
const tdCp = document.createElement('td');
tdCp.textContent = 'U+' + cp.toString(16).toUpperCase().padStart(4, '0');
const tdBytes = document.createElement('td');
tdBytes.textContent = Array.from(encoder.encode(ch), hex).join(' ');
tr.append(tdChar, tdCp, tdBytes);
codepointBody.appendChild(tr);
}
return chars.length > MAX_ROWS;
}
btnEncode.addEventListener('click', encode);
btnDecode.addEventListener('click', decode);
btnCopy.addEventListener('click', async () => {
const text = output.textContent;
if (!text) {
msg.textContent = '暂无结果可复制';
return;
}
try {
await navigator.clipboard.writeText(text);
btnCopy.textContent = '已复制 ✓';
setTimeout(() => { btnCopy.textContent = '复制结果'; }, 1500);
} catch {
msg.textContent = '复制失败,请手动选择文本复制';
}
});
input.addEventListener('keydown', (e) => {
if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') {
e.preventDefault();
encode();
}
});
</script>
</body>
</html>