-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml-entities.html
More file actions
165 lines (139 loc) · 5.36 KB
/
html-entities.html
File metadata and controls
165 lines (139 loc) · 5.36 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML Entity Encoder/Decoder</title>
<link rel="stylesheet" href="common.css">
<style>
body {
display: flex;
flex-direction: column;
}
</style>
</head>
<body>
<a href="index.html" class="back-link">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M19 12H5M12 19l-7-7 7-7"/>
</svg>
All Tools
</a>
<header>
<h1>HTML Entity Encoder/Decoder</h1>
<p class="subtitle">Real-time bidirectional conversion</p>
</header>
<div class="controls">
<button id="clearBtn" class="secondary">Clear All</button>
</div>
<div class="container">
<div class="pane">
<div class="pane-header">
<span class="label">
<svg class="icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path>
<polyline points="14 2 14 8 20 8"></polyline>
</svg>
Plain Text
</span>
<button id="copyPlain" class="copy-btn">Copy</button>
</div>
<textarea id="plainArea" placeholder="Type or paste text here...
Special characters like <, >, &, " will be encoded." spellcheck="false"></textarea>
</div>
<div class="pane">
<div class="pane-header">
<span class="label">
<svg class="icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<polyline points="16 18 22 12 16 6"></polyline>
<polyline points="8 6 2 12 8 18"></polyline>
</svg>
HTML Entities
<span id="statusIndicator" class="status-indicator"></span>
</span>
<button id="copyEncoded" class="copy-btn">Copy</button>
</div>
<textarea id="encodedArea" placeholder="Type or paste HTML entities here...
Example: <div> & "text"" spellcheck="false"></textarea>
</div>
</div>
<script>
const plainArea = document.getElementById('plainArea');
const encodedArea = document.getElementById('encodedArea');
const clearBtn = document.getElementById('clearBtn');
const copyPlain = document.getElementById('copyPlain');
const copyEncoded = document.getElementById('copyEncoded');
const statusIndicator = document.getElementById('statusIndicator');
let updating = false;
// HTML entities map
const encodeMap = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
'`': '`'
};
function encodeHTML(str) {
return str.replace(/[&<>"'`]/g, char => encodeMap[char]);
}
function decodeHTML(str) {
const textarea = document.createElement('textarea');
textarea.innerHTML = str;
return textarea.value;
}
function encodePlain() {
if (updating) return;
updating = true;
statusIndicator.className = 'status-indicator';
const text = plainArea.value;
if (!text) {
encodedArea.value = '';
updating = false;
return;
}
encodedArea.value = encodeHTML(text);
statusIndicator.className = 'status-indicator valid';
updating = false;
}
function decodePlain() {
if (updating) return;
updating = true;
statusIndicator.className = 'status-indicator';
const encoded = encodedArea.value;
if (!encoded) {
plainArea.value = '';
updating = false;
return;
}
plainArea.value = decodeHTML(encoded);
statusIndicator.className = 'status-indicator valid';
updating = false;
}
plainArea.addEventListener('input', encodePlain);
encodedArea.addEventListener('input', decodePlain);
clearBtn.addEventListener('click', () => {
plainArea.value = '';
encodedArea.value = '';
statusIndicator.className = 'status-indicator';
plainArea.focus();
});
function setupCopyButton(btn, textarea) {
btn.addEventListener('click', () => {
if (!textarea.value) return;
navigator.clipboard.writeText(textarea.value).then(() => {
const originalText = btn.textContent;
btn.textContent = 'Copied!';
btn.classList.add('copied');
setTimeout(() => {
btn.textContent = originalText;
btn.classList.remove('copied');
}, 2000);
});
});
}
setupCopyButton(copyPlain, plainArea);
setupCopyButton(copyEncoded, encodedArea);
</script>
</body>
</html>