-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXmlMinifierAndFormatter.html
More file actions
367 lines (322 loc) · 10.8 KB
/
Copy pathXmlMinifierAndFormatter.html
File metadata and controls
367 lines (322 loc) · 10.8 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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>XML格式化与压缩工具 - 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>
.formatter-options {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 15px;
margin: 15px 0;
}
.option-item {
display: flex;
align-items: center;
}
.option-item input[type="checkbox"] {
margin-right: 8px;
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin: 15px 0;
}
.info-box {
background-color: var(--bg-tertiary);
padding: 10px;
border-radius: var(--border-radius);
margin: 10px 0;
}
.tabs {
display: flex;
margin-bottom: 15px;
border-bottom: 1px solid var(--bg-tertiary);
}
.tab {
padding: 10px 20px;
cursor: pointer;
background-color: var(--bg-secondary);
border: 1px solid var(--bg-tertiary);
border-bottom: none;
border-radius: 5px 5px 0 0;
margin-right: 5px;
}
.tab.active {
background-color: var(--accent-color);
color: white;
}
.code-output {
position: relative;
}
.copy-button {
position: absolute;
top: 10px;
right: 10px;
background-color: var(--accent-color);
color: white;
border: none;
padding: 5px 10px;
border-radius: 4px;
cursor: pointer;
z-index: 10;
}
</style>
</head>
<body>
<div id="app">
<div class="container">
<a href="../index.html" class="back-link">← 返回首页</a>
<div class="tool-section">
<h2>XML格式化与压缩工具</h2>
<p>格式化或压缩XML代码</p>
<div class="info-box">
<p><strong>功能说明:</strong></p>
<p>• 格式化XML代码 - 使代码更易读</p>
<p>• 压缩XML代码 - 移除多余字符减小体积</p>
<p>• 代码美化 - 统一缩进和换行</p>
</div>
<div class="tabs">
<div class="tab" :class="{ active: activeTab === 'format' }" @click="activeTab = 'format'">格式化</div>
<div class="tab" :class="{ active: activeTab === 'minify' }" @click="activeTab = 'minify'">压缩</div>
</div>
<div>
<textarea
v-model="inputCode"
placeholder="在此输入XML代码..."
rows="15"
></textarea>
<div class="formatter-options" v-if="activeTab === 'format'">
<div class="option-item">
<input type="checkbox" id="beautify" v-model="formatOptions.beautify" checked>
<label for="beautify">美化代码</label>
</div>
<div class="option-item">
<input type="checkbox" id="insertSpaces" v-model="formatOptions.insertSpaces" checked>
<label for="insertSpaces">使用空格代替制表符</label>
</div>
<div class="option-item">
<label for="indentSize">缩进大小:</label>
<input type="number" id="indentSize" v-model.number="formatOptions.indentSize" min="1" max="8" style="width: 60px; margin-left: 10px;">
</div>
</div>
<div class="controls">
<button v-if="activeTab === 'format'" @click="formatCode">格式化代码</button>
<button v-if="activeTab === 'minify'" @click="minifyCode">压缩代码</button>
<button @click="clearCode">清空</button>
<button @click="loadExample">加载示例</button>
<button @click="downloadCode">下载代码</button>
</div>
<div class="code-output" v-if="outputCode">
<button class="copy-button" @click="copyCode">复制代码</button>
<textarea
v-model="outputCode"
placeholder="处理后的代码将显示在这里..."
rows="15"
readonly
></textarea>
</div>
</div>
</div>
</div>
</div>
<script>
const { createApp, ref } = Vue;
createApp({
setup() {
const inputCode = ref('');
const outputCode = ref('');
const activeTab = ref('format'); // 'format' or 'minify'
const formatOptions = ref({
beautify: true,
insertSpaces: true,
indentSize: 2
});
// XML格式化函数
const formatCode = () => {
if (!inputCode.value.trim()) {
alert('请先输入XML代码');
return;
}
try {
let xml = inputCode.value;
let formatted = '';
let indent = 0;
const indentChar = formatOptions.value.insertSpaces ?
' '.repeat(formatOptions.value.indentSize) : '\t';
// 将XML分解为标签和内容
xml.split(/</).forEach((part) => {
if (part === '') return;
if (part.charAt(0) === '/') {
// 结束标签
indent--;
formatted += indentChar.repeat(Math.max(0, indent)) + '</' + part;
} else if (part.charAt(0) === '?') {
// XML声明
formatted += indentChar.repeat(indent) + '<?' + part.substring(1);
} else if (part.charAt(0) === '!') {
// 注释或DOCTYPE
formatted += indentChar.repeat(indent) + '<' + part;
} else {
// 开始标签
if (part.indexOf('>') !== -1) {
const tagContent = part.substring(0, part.indexOf('>') + 1);
const content = part.substring(part.indexOf('>') + 1);
formatted += indentChar.repeat(indent) + '<' + tagContent;
if (content.trim()) {
formatted += content.trim();
}
if (!tagContent.includes('/>')) {
indent++;
}
} else {
formatted += indentChar.repeat(indent) + '<' + part;
indent++;
}
}
if (part.indexOf('?>') !== -1 || part.indexOf('>') !== -1) {
formatted += '\n';
}
});
outputCode.value = formatted.trim();
} catch (error) {
alert('格式化失败: ' + error.message);
}
};
// XML压缩函数
const minifyCode = () => {
if (!inputCode.value.trim()) {
alert('请先输入XML代码');
return;
}
try {
// 移除多余的空白字符和注释
let minified = inputCode.value
// 移除XML注释
.replace(/<!--[\s\S]*?-->/g, '')
// 移除多余的空白字符
.replace(/\s+/g, ' ')
// 清理标签内的空格
.replace(/> </g, '><')
.trim();
outputCode.value = minified;
} catch (error) {
alert('压缩失败: ' + error.message);
}
};
const clearCode = () => {
inputCode.value = '';
outputCode.value = '';
};
const loadExample = () => {
inputCode.value = `<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book id="1">
<title>Learning JavaScript</title>
<author>John Doe</author>
<year>2022</year>
<price currency="USD">29.99</price>
</book>
<book id="2">
<title>Advanced CSS</title>
<author>Jane Smith</author>
<year>2023</year>
<price currency="USD">39.99</price>
</book>
</bookstore>`;
if (activeTab.value === 'format') {
formatCode();
} else {
minifyCode();
}
};
const copyCode = async () => {
try {
await navigator.clipboard.writeText(outputCode.value);
alert('代码已复制到剪贴板!');
} catch (err) {
console.error('复制失败:', err);
alert('复制失败,请手动选择并复制代码');
}
};
const downloadCode = () => {
if (!outputCode.value) {
alert('没有可下载的代码');
return;
}
const blob = new Blob([outputCode.value], { type: 'application/xml' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = activeTab.value === 'format' ? 'formatted-xml.xml' : 'minified-xml.xml';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
};
return {
inputCode,
outputCode,
activeTab,
formatOptions,
formatCode,
minifyCode,
clearCode,
loadExample,
copyCode,
downloadCode
};
}
}).mount('#app');
</script>
</body>
</html>