-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHtmlMinifierAndFormatter.html
More file actions
534 lines (455 loc) · 15.4 KB
/
Copy pathHtmlMinifierAndFormatter.html
File metadata and controls
534 lines (455 loc) · 15.4 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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML格式化与压缩工具 - JustHTML 只需网页</title>
<script>
/**
* 动态加载(强制去除缓存) CSS 的函数
* @param {string} src - 要加载的 CSS 的 src 链接
* @returns {Promise} 返回一个 Promise 对象,用于处理异步加载
*/
function loadStyle(src) {
return new Promise((resolve, reject) => {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = src + '?v=' + new Date().getTime(); // 使用时间戳避免缓存
link.async = true;
link.onload = resolve;
link.onerror = reject;
document.head.appendChild(link);
});
}
/**
* 动态加载(强制去除缓存) JS 的函数
* @param {string} src - 要加载的 JS 的 src 链接
* @returns {Promise} 返回一个 Promise 对象,用于处理异步加载
*/
function loadScript(src) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = src + '?v=' + new Date().getTime(); // 使用时间戳避免缓存
script.async = true;
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}
</script>
<script>
loadStyle('../assets/style.css')
let scriptArray = [
"../assets/AddHeader_Tools.js",
"../assets/AddFooter.js",
"../assets/AddIco.js",
"../assets/AutoRun.js",
]
scriptArray.forEach(script => {
loadScript(script);
});
</script>
<script src="../package/Vue/code/vue.global.prod.js"></script>
<script type="module" src="../package/Sober/code/main.js"></script>
<style>
/* 自定义样式覆盖 */
textarea {
background-color: var(--bg-primary);
color: var(--text-primary);
border: 1px solid var(--bg-tertiary);
font-family: 'SarasaMonoSC-ExtraLightItalic', 'Courier New', monospace;
min-height: 250px;
padding: 15px;
border-radius: var(--border-radius);
resize: vertical;
width: 100%;
font-size: 14px;
line-height: 1.5;
margin-bottom: 15px;
}
textarea:focus {
outline: none;
border-color: var(--accent-color);
box-shadow: 0 0 0 2px rgba(74, 158, 255, 0.1);
}
.tool-section {
background-color: var(--bg-secondary);
border-radius: var(--border-radius);
padding: 25px;
margin-bottom: 20px;
border: 1px solid var(--bg-tertiary);
}
.tool-section h2 {
color: var(--accent-color);
margin-bottom: 10px;
font-size: 1.8rem;
}
.tool-section p {
color: var(--text-secondary);
margin-bottom: 20px;
font-size: 1rem;
}
.button-group {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin-bottom: 20px;
}
button {
background-color: var(--accent-color);
color: white;
border: none;
padding: 12px 24px;
border-radius: var(--border-radius);
cursor: pointer;
font-size: 14px;
transition: var(--transition);
flex: 1;
min-width: 150px;
}
button:hover {
background-color: #3a8bdf;
transform: translateY(-2px);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}
button:active {
transform: translateY(0);
}
.result-container {
margin-top: 25px;
padding: 20px;
background-color: var(--bg-tertiary);
border-radius: var(--border-radius);
border-left: 4px solid var(--accent-color);
}
.result-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 15px;
}
.result-header h3 {
color: var(--accent-color);
margin: 0;
font-size: 1.2rem;
}
.status-badge {
padding: 6px 12px;
border-radius: 20px;
font-size: 12px;
font-weight: bold;
background-color: rgba(74, 158, 255, 0.2);
color: var(--accent-color);
}
.status-badge.error {
background-color: rgba(255, 107, 107, 0.2);
color: #ff6b6b;
}
.status-badge.success {
background-color: rgba(46, 204, 113, 0.2);
color: #2ecc71;
}
pre {
background-color: var(--bg-primary);
color: var(--text-primary);
padding: 15px;
border-radius: 8px;
overflow-x: auto;
font-family: 'SarasaMonoSC-ExtraLightItalic', 'Courier New', monospace;
font-size: 14px;
line-height: 1.5;
white-space: pre-wrap;
word-wrap: break-word;
border: 1px solid var(--bg-tertiary);
margin: 0;
}
.error-message {
color: #ff6b6b;
padding: 10px 15px;
background-color: rgba(255, 107, 107, 0.1);
border-radius: 6px;
border-left: 4px solid #ff6b6b;
margin-top: 15px;
font-family: 'SarasaMonoSC-ExtraLightItalic';
}
.placeholder-text {
color: #888;
font-style: italic;
}
/* 响应式设计 */
@media (max-width: 768px) {
.button-group {
flex-direction: column;
}
button {
width: 100%;
min-width: unset;
}
.tool-section {
padding: 20px;
}
}
@media (max-width: 480px) {
textarea {
font-size: 13px;
}
.tool-section h2 {
font-size: 1.5rem;
}
}
</style>
</head>
<body>
<div id="app">
<div class="container">
<a href="../index.html" class="back-link">← 返回首页</a>
<div class="tool-section">
<h2>HTML格式化与压缩工具</h2>
<p>格式化混乱的HTML代码或将HTML压缩成一行</p>
<textarea
v-model="inputText"
placeholder="在此输入HTML代码... 例如: <div class="container"> <h1>标题</h1> <p>段落内容</p> </div>"
rows="15"
></textarea>
<div class="button-group">
<button @click="formatHtml" title="格式化HTML代码">格式化HTML</button>
<button @click="minifyHtml" title="压缩HTML为一行">压缩HTML</button>
<button @click="validateHtml" title="验证HTML语法">验证HTML</button>
<button @click="copyResult" title="复制结果到剪贴板">复制结果</button>
<button @click="clearText" title="清空所有内容">清空</button>
</div>
<div class="result-container" v-if="result || error">
<div class="result-header">
<h3>结果:</h3>
<span class="status-badge" :class="{ success: isValid && !error, error: error }">
{{ error ? '错误' : (isValid ? '有效' : '') }}
</span>
</div>
<pre v-if="result && !error">{{ result }}</pre>
<div class="error-message" v-if="error">
<strong>错误:</strong> {{ error }}
</div>
</div>
<div v-else-if="showTips" class="result-container placeholder-text">
这里将显示格式化、压缩或验证的结果
</div>
</div>
</div>
</div>
<script>
const { createApp, ref, computed } = Vue;
createApp({
setup() {
const inputText = ref('');
const result = ref('');
const error = ref('');
const isValid = ref(false);
const showTips = ref(true);
// 修复格式化HTML函数
const formatHtml = () => {
error.value = '';
isValid.value = false;
showTips.value = false;
try {
if (!inputText.value.trim()) {
error.value = '请输入HTML代码';
return;
}
let html = inputText.value;
let indentLevel = 0;
const indentSize = 2;
let formatted = '';
let inTag = false;
let currentTag = '';
let inComment = false;
// 先处理注释
html = html.replace(/<!--[\s\S]*?-->/g, match => {
return '\n' + match + '\n';
});
// 按行处理
const lines = html.split('\n');
for (let i = 0; i < lines.length; i++) {
let line = lines[i].trim();
if (!line) continue;
// 检查是否是注释
if (line.startsWith('<!--')) {
formatted += ' '.repeat(indentLevel * indentSize) + line + '\n';
inComment = line.endsWith('-->') ? false : true;
continue;
}
if (inComment) {
formatted += ' '.repeat(indentLevel * indentSize) + line + '\n';
if (line.endsWith('-->')) inComment = false;
continue;
}
// 分割标签和文本
const parts = line.split(/(<[^>]+>)/g);
for (let part of parts) {
part = part.trim();
if (!part) continue;
if (part.startsWith('<')) {
// 这是标签
if (part.startsWith('</')) {
// 结束标签
indentLevel = Math.max(0, indentLevel - 1);
formatted += ' '.repeat(indentLevel * indentSize) + part + '\n';
} else if (part.endsWith('/>') ||
part.match(/<(area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)\b[^>]*>/i)) {
// 自闭合标签
formatted += ' '.repeat(indentLevel * indentSize) + part + '\n';
} else {
// 开始标签
formatted += ' '.repeat(indentLevel * indentSize) + part + '\n';
indentLevel++;
}
} else {
// 这是文本内容
if (part.trim()) {
formatted += ' '.repeat(indentLevel * indentSize) + part + '\n';
}
}
}
}
// 清理多余的换行
result.value = formatted.trim();
isValid.value = true;
} catch (e) {
error.value = `格式化HTML时出错: ${e.message}`;
result.value = '';
isValid.value = false;
}
};
// 修复压缩HTML函数
const minifyHtml = () => {
error.value = '';
isValid.value = false;
showTips.value = false;
try {
if (!inputText.value.trim()) {
error.value = '请输入HTML代码';
return;
}
let html = inputText.value;
// 移除注释
html = html.replace(/<!--[\s\S]*?-->/g, '');
// 移除多余的空格(保留标签间的必要空格)
html = html.replace(/\s+/g, ' ');
// 移除标签间的空格
html = html.replace(/>\s+</g, '><');
// 移除首尾空白
html = html.trim();
// 特殊处理:保留文本节点中的空格
// 将标签内的文本单独处理
const tagRegex = /(<[^>]+>)([^<]*)/g;
let minified = '';
let lastIndex = 0;
let match;
while ((match = tagRegex.exec(html)) !== null) {
const tag = match[1];
let text = match[2];
// 压缩文本中的多余空格,但保留一个空格
if (text.trim()) {
text = ' ' + text.trim().replace(/\s+/g, ' ') + ' ';
}
minified += tag + text;
lastIndex = tagRegex.lastIndex;
}
// 添加剩余的部分
if (lastIndex < html.length) {
let remaining = html.substring(lastIndex);
if (remaining.trim()) {
remaining = remaining.trim().replace(/\s+/g, ' ');
minified += remaining;
}
}
// 最后清理
minified = minified.replace(/\s+/g, ' ').trim();
result.value = minified;
isValid.value = true;
} catch (e) {
error.value = `压缩HTML时出错: ${e.message}`;
result.value = '';
isValid.value = false;
}
};
// 修复验证HTML函数
const validateHtml = () => {
error.value = '';
isValid.value = false;
showTips.value = false;
try {
if (!inputText.value.trim()) {
error.value = '请输入HTML代码';
return;
}
const html = inputText.value;
// 使用DOMParser验证HTML
const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');
// 检查解析错误
const parserErrors = doc.querySelectorAll("parsererror");
if (parserErrors.length > 0) {
error.value = 'HTML格式无效';
result.value = '';
isValid.value = false;
return;
}
// 检查标签平衡
const tempDiv = document.createElement('div');
tempDiv.innerHTML = html;
// 简单检查:比较innerHTML设置前后
const originalLength = html.replace(/[\s\n]/g, '').length;
const parsedLength = tempDiv.innerHTML.replace(/[\s\n]/g, '').length;
if (Math.abs(originalLength - parsedLength) > 10) {
error.value = 'HTML可能存在未闭合的标签或语法错误';
result.value = '';
isValid.value = false;
return;
}
result.value = '✓ HTML格式有效';
isValid.value = true;
error.value = '';
} catch (e) {
error.value = `验证HTML时出错: ${e.message}`;
result.value = '';
isValid.value = false;
}
};
const copyResult = () => {
if (result.value) {
navigator.clipboard.writeText(result.value)
.then(() => {
alert('HTML代码已复制到剪贴板!');
})
.catch(err => {
console.error('复制失败: ', err);
error.value = '复制失败: ' + err.message;
});
} else {
error.value = '没有可复制的结果';
}
};
const clearText = () => {
inputText.value = '';
result.value = '';
error.value = '';
isValid.value = false;
showTips.value = true;
};
return {
inputText,
result,
error,
isValid,
showTips,
formatHtml,
minifyHtml,
validateHtml,
copyResult,
clearText
};
}
}).mount('#app');
</script>
</body>
</html>