Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
92 lines (73 loc) · 3.23 KB
/
Copy pathscript.js
File metadata and controls
92 lines (73 loc) · 3.23 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
document.addEventListener('DOMContentLoaded', () => {
const textInput = document.getElementById('textInput');
const gridContainer = document.getElementById('gridContainer');
textInput.addEventListener('input', (e) => {
const text = e.target.value;
renderGrid(text);
});
function renderGrid(text) {
gridContainer.innerHTML = '';
if (!text) {
gridContainer.innerHTML = '<div class="empty-state">Start typing to see the magic happen.</div>';
return;
}
// Use Array.from to correctly handle surrogate pairs and emojis
const characters = Array.from(text);
let byteOffset = 0;
characters.forEach((char, index) => {
// Calculate bytes for current character
const encoder = new TextEncoder();
const charBytes = encoder.encode(char).length;
byteOffset += charBytes;
const card = createCharCard(char, index, byteOffset);
gridContainer.appendChild(card);
});
}
function createCharCard(char, index, byteOffset) {
const card = document.createElement('div');
card.className = 'char-card';
// Handle whitespace characters for display
let displayChar = char;
if (char === ' ') displayChar = '␣';
else if (char === '\n') displayChar = '↵';
else if (char === '\t') displayChar = '⇥';
// Get Unicode code point
const codePoint = char.codePointAt(0);
const isAscii = codePoint < 128;
// Detect special characters:
// 1. Control characters (\p{C}) - Include all (e.g. \n, \t)
const isControl = /\p{C}/u.test(char);
// 2. Symbols (\p{S}) - Exclude ASCII symbols (like <, >, =, $)
const isSymbol = /\p{S}/u.test(char) && !isAscii;
// 3. Separators (\p{Z}) - Exclude regular space
const isSeparator = /\p{Z}/u.test(char) && char !== ' ';
// 4. Non-Latin Letters (\p{L} && !\p{Script=Latin})
const isNonLatinLetter = /\p{L}/u.test(char) && !/\p{Script=Latin}/u.test(char);
const isSpecial = isControl || isSymbol || isSeparator || isNonLatinLetter;
if (isSpecial) {
card.classList.add('special-char');
} else if (!isAscii) {
// Multi-byte characters (Code Point > 127) that are not special
// e.g. Accented Latin characters
card.classList.add('multi-byte-char');
}
// Get Unicode code point string
const hexCode = 'U+' + codePoint.toString(16).toUpperCase().padStart(4, '0');
// Get UTF-8 bytes
const encoder = new TextEncoder();
const utf8Bytes = encoder.encode(char);
const utf8Hex = Array.from(utf8Bytes)
.map(b => b.toString(16).toUpperCase().padStart(2, '0'))
.join(' ');
card.innerHTML = `
<span class="char-position">${index + 1}</span>
<div class="char-display">${displayChar}</div>
<div class="char-meta">
<div class="char-code-point">${hexCode}</div>
<div class="char-utf8">${utf8Hex}</div>
</div>
<span class="byte-counter">${byteOffset}</span>
`;
return card;
}
});