-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunicode.html
238 lines (215 loc) · 7.75 KB
/
unicode.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
<!DOCTYPE html>
<html>
<head>
<title>Unicode</title>
<style>
textarea { font-family: monospace, sans-serif, serif; width: 100%; min-height: 10rem; line-height: 2; }
table { font-family: Consolas, 'Courier New', Courier, Unifont, monospace, sans-serif, serif; border-collapse: collapse; background-color: #fff; color: #000; }
table th, table td { border: 1px solid black; padding: 4px; }
table th, table td:first-child { text-align: center; }
table td:first-child { background-color: #ccc; }
table td:first-child span { background-color: #fff; color: #000; font-size: 2rem; }
tr.nfd { background-color: #ccc; }
tr.nfkd { background-color: #aaa; }
tr.ascii { color: #A00; }
tr.eu { color: #00F; }
</style>
</head>
<body>
<h1>Unicode</h1>
<textarea id="uinput" placeholder="Type here..."></textarea>
<div>Normalise: <button id="toNFC" type="button">NFC</button> <button id="toNFD" type="button">NFD</button> <button id="toNFKC" type="button">NFKC</button> <button id="toNFKD" type="button">NFKD</button> Show: <label><input id="showNFD" type="checkbox">NFD</label> <label><input id="showNFKD" type="checkbox">NFKD</label></div>
<table>
<thead><tr><th>character</th><th>codepoint</th><th>name</th><th>block</th><th>plane</th><th>category</th><th>subcategory</th><th>script</th><th>NFD</th><th>NFKD</th></tr></thead>
<tbody id="atab"></tbody>
<tfoot><tr><th>character</th><th>codepoint</th><th>name</th><th>block</th><th>plane</th><th>category</th><th>subcategory</th><th>script</th><th>NFD</th><th>NFKD</th></tr></tfoot>
</table>
<script src="unicodeData.js"></script>
<script src="unicodeCodePoints.js"></script>
<script>"use strict";
function generateRow(/** @type string */ char, /** @type string | null */ normC, /** @type string | null */ normK) {
const codePoint = char.codePointAt(0) | 0;
const row = document.createElement("tr");
{ //character
const valueCell = document.createElement("td");
const valueSpan = document.createElement("span");
valueSpan.innerText = char;
valueCell.appendChild(valueSpan);
row.appendChild(valueCell);
}
{ //codepoint
const codeCell = document.createElement("td");
codeCell.innerText = "U+" + codePoint.toString(16).toUpperCase().padStart(4, "0");
row.appendChild(codeCell);
}
{ //name
const nameCell = document.createElement("td");
/** @type string | undefined */
let name;
if((codePoint & 0xFFFE) == 0xFFFE) {
name = "{noncharacter}";
} else {
name = codePointNames[codePoint];
}
nameCell.innerText = name || "";
row.appendChild(nameCell);
}
{ //block
const blockCell = document.createElement("td");
/** @type string | undefined */
let block;
if(codePoint < 0x200) { //vast majority of cases
block = Block[codePoint >> 7][2];
} else {
const cpid = codePoint >> 4;
let a = 0;
let b = blockCount - 1;
while(a != b) {
let c = (a + b) >> 1;
/** @type [number, number, string] */
const blk = Block[c];
if(cpid < blk[0]) {
b = c;
continue;
}
if(blk[1] < cpid) {
if(a == c) break; //nonexistent block
a = c;
continue;
}
block = blk[2];
break;
};
}
blockCell.innerText = block || "";
row.appendChild(blockCell);
}
{ //plane
const planeCell = document.createElement("td");
const planeId = codePoint >> 16;
planeCell.innerText = "#" + planeId + " " + (Plane[planeId] || "");
row.appendChild(planeCell);
}
{ //category, subcategory
let genCat = "", subCat = "";
for(const catNam in GeneralCategory) {
/** @type [RegExp, {[id: string]: RegExp}] */
const category = GeneralCategory[catNam];
if(category[0].test(char)) {
if(genCat) genCat += ";\n"; //in case of error
genCat += catNam + " " + generalCategoryNames[catNam];
const subcategories = category[1];
for(const subcategoryName in subcategories) {
const subcategory = subcategories[subcategoryName];
if(subcategory.test(char)) {
if(subCat) subCat += ";\n"; //in case of error
subCat += subcategoryName + " " + generalCategoryNames[subcategoryName];
}
}
}
}
const valueCategory = document.createElement("td");
valueCategory.innerText = genCat;
row.appendChild(valueCategory);
const valueSubcategory = document.createElement("td");
valueSubcategory.innerText = subCat;
row.appendChild(valueSubcategory);
}
{ //script
let scpt = "";
for(const scptNam in Script) {
/** @type RegExp */
const scp = Script[scptNam];
if(scp.test(char)) {
if(scpt) scpt += ";\n"; //in case of error
scpt += scptNam;
}
}
const valueScript = document.createElement("td");
valueScript.innerText = scpt;
row.appendChild(valueScript);
}
{ //NFD
const valueNorm = document.createElement("td");
if(normC) {
for(const nrm of normC) {
if(valueNorm.innerText) valueNorm.innerText += " ";
valueNorm.innerText += "U+" + nrm.codePointAt(0).toString(16).toUpperCase().padStart(4, "0");
}
}
row.appendChild(valueNorm);
}
{ //NFKD
const valueNorm = document.createElement("td");
if(normK) {
for(const nrm of normK) {
if(valueNorm.innerText) valueNorm.innerText += " ";
valueNorm.innerText += "U+" + nrm.codePointAt(0).toString(16).toUpperCase().padStart(4, "0");
}
}
row.appendChild(valueNorm);
}
if(codePoint < 128)
row.className = "ascii";
else
if(codePoint < 0x180)
row.className = "eu";
return row;
}
function fillTable(/** @type HTMLTableSectionElement */ table, /** @type string */ text) {
for(const char of text) {
const normC = showNFD ? char.normalize("NFD") : char;
const normK = showNFKD ? char.normalize("NFKD") : normC;
const denormC = (normC === char);
const denormK = (normK === normC);
const row = generateRow(char, denormC ? null : normC, denormK ? null : normK);
table.appendChild(row);
if(!denormC) {
for(const nrm of normC) {
const extraNorm = showNFKD ? nrm.normalize("NFKD") : nrm;
const exnorm = (extraNorm === nrm);
const nfd = generateRow(nrm, null, exnorm ? null : extraNorm);
nfd.classList.add("nfd");
table.appendChild(nfd);
if(!exnorm) {
for(const nrm of extraNorm) {
const nfkd = generateRow(nrm, null, null);
nfkd.classList.add("nfd");
nfkd.classList.add("nfkd");
table.appendChild(nfkd);
}
}
}
} else {
if(!denormK) {
for(const nrm of normK) {
const nfkd = generateRow(nrm, null, null);
nfkd.classList.add("nfkd");
table.appendChild(nfkd);
}
}
}
}
}
/** @type HTMLTextAreaElement */
const input = document.getElementById("uinput");
/** @type HTMLTableSectionElement */
const table = document.getElementById("atab");
let showNFD = false;
let showNFKD = false;
function analyze(/** @type Event */ event) {
/** @type string */
const text = input.value;
table.replaceChildren();
fillTable(table, text);
}
input.oninput = analyze;
document.getElementById("toNFC").onclick = (e) => { input.value = input.value.normalize("NFC"); analyze(e); };
document.getElementById("toNFD").onclick = (e) => { input.value = input.value.normalize("NFD"); analyze(e); };
document.getElementById("toNFKC").onclick = (e) => { input.value = input.value.normalize("NFKC"); analyze(e); };
document.getElementById("toNFKD").onclick = (e) => { input.value = input.value.normalize("NFKD"); analyze(e); };
document.getElementById("showNFD").onchange = (e) => { showNFD = e.target.checked; analyze(e); };
document.getElementById("showNFKD").onchange = (e) => { showNFKD = e.target.checked; analyze(e); };
</script>
</body>
</html>