-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathpaste-html-subset.html
More file actions
393 lines (336 loc) · 10.7 KB
/
paste-html-subset.html
File metadata and controls
393 lines (336 loc) · 10.7 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rich Paste to HTML Subset</title>
<style>
* {
box-sizing: border-box;
}
body {
font-family: Helvetica, Arial, sans-serif;
max-width: 1000px;
margin: 0 auto;
padding: 20px;
line-height: 1.5;
}
h1, h2 {
color: #333;
}
#paste-area {
width: 100%;
min-height: 200px;
padding: 15px;
border: 2px dashed #ccc;
border-radius: 4px;
margin-bottom: 15px;
font-size: 16px;
font-family: Helvetica, Arial, sans-serif;
}
#paste-area:focus {
outline: none;
border-color: #4a88e5;
}
.info-text {
color: #666;
margin-bottom: 20px;
font-style: italic;
}
#output-container {
display: none;
margin-top: 30px;
}
#html-output {
width: 100%;
height: 200px;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
font-family: monospace;
font-size: 16px;
margin-bottom: 15px;
white-space: pre-wrap;
}
.button {
background-color: #33aa44;
color: white;
border: none;
padding: 10px 15px;
font-size: 16px;
border-radius: 4px;
cursor: pointer;
margin-right: 10px;
margin-bottom: 20px;
}
.button:hover {
background-color: #2a9038;
}
#preview-container {
margin-top: 20px;
border: 1px solid #eee;
padding: 20px;
border-radius: 4px;
}
#preview-content {
font-family: Helvetica, Arial, sans-serif;
}
.success-message {
color: green;
font-weight: bold;
}
.tag-list {
background-color: #f8f8f8;
border-left: 4px solid #4a88e5;
padding: 15px;
margin-bottom: 20px;
}
.tag-list ul {
margin: 0;
padding-left: 20px;
}
</style>
</head>
<body>
<h1>Rich Paste to HTML Subset</h1>
<div class="tag-list">
<h3>Supported HTML elements</h3>
<ul>
<li><strong>p</strong> - Paragraphs</li>
<li><strong>strong</strong> - Bold text</li>
<li><strong>em</strong> - Italic text</li>
<li><strong>blockquote</strong> - Quoted text</li>
<li><strong>code</strong> - Inline code snippets</li>
<li><strong>pre</strong> - Preformatted text blocks</li>
<li><strong>a</strong> - Links (href attribute is preserved)</li>
<li><strong>h1-h6</strong> - Headings</li>
<li><strong>ul</strong> - Unordered lists</li>
<li><strong>ol</strong> - Ordered lists</li>
<li><strong>li</strong> - List items</li>
<li><strong>dl</strong> - Definition lists</li>
<li><strong>dt</strong> - Definition terms</li>
<li><strong>dd</strong> - Definition descriptions</li>
</ul>
</div>
<div id="paste-area" contenteditable="true"></div>
<p class="info-text">Paste using Ctrl+V or ⌘+V to capture the rich text HTML. On mobile, use "Paste as text with formatting" from your context menu.</p>
<div id="output-container">
<h2>Clean HTML code</h2>
<textarea id="html-output" readonly></textarea>
<button id="copy-button" class="button">Copy HTML</button>
<button id="clear-button" class="button">Clear all</button>
<div id="preview-container">
<h2>Preview</h2>
<iframe id="preview-frame" style="width: 100%; height: 300px; border: 1px solid #ddd; border-radius: 4px;"></iframe>
</div>
</div>
<script>
// Function to escape HTML entities for display
function escapeHtml(html) {
return html
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
// Function to sanitize HTML and keep only allowed tags and attributes
function sanitizeHtml(html) {
const allowedTags = [
'p', 'strong', 'em', 'blockquote', 'code', 'pre', 'a',
'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
'ul', 'ol', 'li', 'dl', 'dt', 'dd'
];
// Use a simpler approach that creates a new DOM tree
const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');
const result = document.createElement('div');
// Function to process a node and its children
function cleanNode(node) {
// For text nodes, just create a copy
if (node.nodeType === Node.TEXT_NODE) {
return document.createTextNode(node.textContent);
}
// Skip non-element nodes
if (node.nodeType !== Node.ELEMENT_NODE) {
return null;
}
const tagName = node.tagName.toLowerCase();
// If it's not an allowed tag, just process its children
if (!allowedTags.includes(tagName)) {
const fragment = document.createDocumentFragment();
for (const child of node.childNodes) {
const cleanedChild = cleanNode(child);
if (cleanedChild) {
fragment.appendChild(cleanedChild);
}
}
return fragment;
}
// Create a new clean element
const cleanElement = document.createElement(tagName);
// Only copy the href attribute for links
if (tagName === 'a' && node.hasAttribute('href')) {
cleanElement.setAttribute('href', node.getAttribute('href'));
}
// Process all child nodes
for (const child of node.childNodes) {
const cleanedChild = cleanNode(child);
if (cleanedChild) {
cleanElement.appendChild(cleanedChild);
}
}
return cleanElement;
}
// Process the body content
const bodyContent = doc.body;
for (const childNode of bodyContent.childNodes) {
const cleanedNode = cleanNode(childNode);
if (cleanedNode) {
result.appendChild(cleanedNode);
}
}
return result.innerHTML;
}
// Function to pretty format HTML with indentation
function formatHtml(html) {
let formatted = '';
let indent = 0;
// Function to get indentation string
function getIndent(level) {
// Ensure level is non-negative to avoid RangeError
const safeLevel = Math.max(0, level);
return ' '.repeat(safeLevel);
}
// Split HTML by tags
const parts = html.split(/(<\/?[^>]+>)/);
const preStack = [];
for (let i = 0; i < parts.length; i++) {
const rawPart = parts[i];
if (!rawPart) continue;
const part = rawPart.trim();
if (!part && preStack.length === 0) continue;
// Check if it's a tag
if (part.startsWith('<')) {
// Closing tag
if (part.startsWith('</')) {
if (/^<\/pre\b/i.test(part) && preStack.length > 0) {
preStack.pop();
}
indent = Math.max(0, indent - 1); // Prevent negative indent
formatted += getIndent(indent) + part + '\n';
}
// Self-closing tag
else if (part.endsWith('/>')) {
formatted += getIndent(indent) + part + '\n';
}
// Opening tag
else {
formatted += getIndent(indent) + part + '\n';
if (/^<pre\b/i.test(part)) {
preStack.push('pre');
}
// Don't increase indent for inline elements
if (!part.match(/<(strong|em|code|a)[^>]*>/i)) {
indent++;
}
}
}
// Content
else {
const content = preStack.length > 0 ? rawPart : part;
if (content || preStack.length > 0) {
formatted += getIndent(indent) + content + '\n';
}
}
}
return formatted;
}
// Get DOM elements
const pasteArea = document.getElementById('paste-area');
const outputContainer = document.getElementById('output-container');
const htmlOutput = document.getElementById('html-output');
const previewContent = document.getElementById('preview-content');
const copyButton = document.getElementById('copy-button');
const clearButton = document.getElementById('clear-button');
// Handle paste event
pasteArea.addEventListener('paste', function(e) {
// Let the paste happen normally first
setTimeout(function() {
// Get pasted content
const pastedContent = pasteArea.innerHTML;
if (pastedContent) {
// Sanitize the HTML to only keep allowed tags
const sanitizedHtml = sanitizeHtml(pastedContent);
// Format the HTML nicely
const formattedHtml = formatHtml(sanitizedHtml);
// Display the HTML code with escaped characters
htmlOutput.value = formattedHtml;
// Show the output container
outputContainer.style.display = 'block';
try {
// Get the iframe element
const previewFrame = document.getElementById('preview-frame');
// Create a blob with the HTML content
const blob = new Blob([`
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
font-family: Helvetica, Arial, sans-serif;
line-height: 1.5;
padding: 15px;
margin: 0;
}
</style>
</head>
<body>
${sanitizedHtml}
</body>
</html>
`], {type: 'text/html'});
// Create a URL for the blob
const blobURL = URL.createObjectURL(blob);
// Set the iframe source to the blob URL
previewFrame.src = blobURL;
// Clean up the blob URL when the iframe loads
previewFrame.onload = function() {
URL.revokeObjectURL(blobURL);
};
} catch (error) {
console.error('Error updating preview:', error);
}
// Show success message
pasteArea.innerHTML = '<p class="success-message">Content pasted. HTML extracted and cleaned.</p>';
}
}, 10);
});
// Reset paste area when clicked
pasteArea.addEventListener('focus', function() {
if (pasteArea.textContent.includes('Content pasted')) {
pasteArea.innerHTML = '';
}
});
// Copy HTML to clipboard
copyButton.addEventListener('click', function() {
htmlOutput.select();
document.execCommand('copy');
const originalText = copyButton.textContent;
copyButton.textContent = 'Copied!';
setTimeout(function() {
copyButton.textContent = originalText;
}, 1500);
});
// Clear all content
clearButton.addEventListener('click', function() {
pasteArea.innerHTML = '';
htmlOutput.value = '';
document.getElementById('preview-frame').src = 'about:blank';
outputContainer.style.display = 'none';
});
</script>
</body>
</html>