-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathescape-entities.html
More file actions
172 lines (158 loc) · 6.74 KB
/
Copy pathescape-entities.html
File metadata and controls
172 lines (158 loc) · 6.74 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML Entity Escaper</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 600px;
margin: 0 auto;
padding: 20px;
box-sizing: border-box;
}
textarea {
width: 100%;
height: 150px;
margin-bottom: 10px;
padding: 5px;
box-sizing: border-box;
}
button {
background-color: #4CAF50;
border: none;
color: white;
padding: 10px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
border-radius: 4px;
}
label {
font-weight: bold;
display: block;
margin-top: 15px;
margin-bottom: 5px;
}
.mode-selector {
margin: 15px 0;
padding: 10px;
background-color: #f8f8f8;
border-radius: 4px;
}
.mode-selector label {
display: inline;
font-weight: normal;
margin-left: 5px;
margin-right: 15px;
}
.mode-selector input[type="radio"] {
cursor: pointer;
}
.documentation {
background-color: #f0f0f0;
padding: 15px;
border-radius: 4px;
margin-top: 30px;
}
</style>
</head>
<body>
<h1>HTML Entity Escaper</h1>
<div class="mode-selector">
<strong>Mode:</strong>
<input type="radio" id="escapeMode" name="mode" value="escape" checked>
<label for="escapeMode">Escape</label>
<input type="radio" id="unescapeMode" name="mode" value="unescape">
<label for="unescapeMode">Unescape</label>
</div>
<label for="input">Input:</label>
<textarea id="input" placeholder="Paste your text here"></textarea>
<label for="output" id="outputLabel">Output (with escaped entities):</label>
<textarea id="output" readonly></textarea>
<button id="copyButton">Copy to clipboard</button>
<div class="documentation">
<h2>What this tool does:</h2>
<p>This HTML Entity tool helps you convert between special characters and their corresponding HTML entities. It works in two modes:</p>
<h3>Escape Mode (default):</h3>
<p>Converts special characters to HTML entities. This is useful when you need to display code or text containing HTML syntax on a web page without it being interpreted as actual HTML.</p>
<ul>
<li><code>&</code> becomes <code>&amp;</code></li>
<li><code><</code> becomes <code>&lt;</code></li>
<li><code>></code> becomes <code>&gt;</code></li>
<li><code>"</code> becomes <code>&quot;</code></li>
<li><code>'</code> becomes <code>&#39;</code></li>
</ul>
<h3>Unescape Mode:</h3>
<p>Converts HTML entities back to their original characters. This is useful when you have HTML-encoded text and need to decode it for readability or further processing.</p>
<p><strong>Supported formats:</strong></p>
<ul>
<li><strong>Named entities:</strong> <code>&amp;</code> → <code>&</code>, <code>&lt;</code> → <code><</code>, <code>&gt;</code> → <code>></code>, <code>&quot;</code> → <code>"</code></li>
<li><strong>Decimal numeric entities:</strong> <code>&#34;</code> → <code>"</code>, <code>&#160;</code> → non-breaking space</li>
<li><strong>Hexadecimal numeric entities:</strong> <code>&#x22;</code> → <code>"</code>, <code>&#xA0;</code> → non-breaking space</li>
</ul>
<p>The tool handles all standard HTML entities and any numeric character reference.</p>
<p>To use the tool, select your desired mode (Escape or Unescape), then paste your text into the input box. The converted text will automatically appear in the output box. You can then copy the result to your clipboard using the "Copy to clipboard" button.</p>
</div>
<script>
const input = document.getElementById('input');
const output = document.getElementById('output');
const outputLabel = document.getElementById('outputLabel');
const copyButton = document.getElementById('copyButton');
const escapeModeRadio = document.getElementById('escapeMode');
const unescapeModeRadio = document.getElementById('unescapeMode');
function escapeHTML(text) {
const entities = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return text.replace(/[&<>"']/g, char => entities[char]);
}
function unescapeHTML(text) {
const entities = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
''': "'",
''': "'"
};
// First handle named entities and hex/decimal entities we know about
let result = text.replace(/&|<|>|"|'|'/g, entity => entities[entity]);
// Then handle any decimal numeric entities like "
result = result.replace(/&#(\d+);/g, (match, dec) => String.fromCharCode(dec));
// Then handle any hexadecimal numeric entities like "
result = result.replace(/&#x([0-9a-fA-F]+);/g, (match, hex) => String.fromCharCode(parseInt(hex, 16)));
return result;
}
function processText() {
const mode = document.querySelector('input[name="mode"]:checked').value;
if (mode === 'escape') {
output.value = escapeHTML(input.value);
outputLabel.textContent = 'Output (with escaped entities):';
} else {
output.value = unescapeHTML(input.value);
outputLabel.textContent = 'Output (with unescaped entities):';
}
}
input.addEventListener('input', processText);
escapeModeRadio.addEventListener('change', processText);
unescapeModeRadio.addEventListener('change', processText);
copyButton.addEventListener('click', () => {
output.select();
document.execCommand('copy');
copyButton.textContent = 'Copied!';
setTimeout(() => {
copyButton.textContent = 'Copy to clipboard';
}, 1500);
});
</script>
</body>
</html>