-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.php
307 lines (291 loc) · 9.3 KB
/
index.php
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
<?php
require __DIR__.'/vendor/autoload.php';
$error = null;
\set_error_handler(function (int $type, string $msg) use (&$error) {
if (\in_array($type, [E_USER_WARNING, E_USER_NOTICE])) {
$error = $msg;
} else {
return false;
}
});
$base = $_POST['base'] ?? null;
$input = '';
$output = '';
$minify = [];
$timing = [
'start' => \microtime(true)
];
$mem = [
'start' => \memory_get_peak_usage()
];
// create object and retrieve config
$doc = new hexydec\html\htmldoc();
$options = $doc->config['minify'];
// process form submmission
if (!empty($_POST['action'])) {
// handle a URL
if (!empty($_POST['url'])) {
// parse the URL
if (($url = \parse_url($_POST['url'])) === false) {
\trigger_error('Could not parse URL: The URL is not valid', E_USER_WARNING);
// check the host name
} elseif (!isset($url['host'])) {
\trigger_error('Could not parse URL: No host was supplied', E_USER_WARNING);
// open the document manually so we can time it
} elseif (($input = \file_get_contents($_POST['url'])) === false) {
\trigger_error('Could not load HTML: The file could not be accessed'.$error, E_USER_WARNING);
// save base URL
} else {
$base = $_POST['url'];
}
// handle directly entered source code
} elseif (empty($_POST['source'])) {
\trigger_error('No URL or HTML source was posted', E_USER_WARNING);
// record the HTML
} else {
$input = $_POST['source'];
}
$timing['fetch'] = \microtime(true);
$mem['fetch'] = \memory_get_peak_usage();
// load the source code
if ($input) {
if (!$doc->load($input, null, $error)) {
\trigger_error('Could not parse HTML: '.$error, E_USER_WARNING);
// minify the output
} else {
$timing['parse'] = \microtime(true);
$mem['parse'] = \memory_get_peak_usage();
// retrieve the user posted options
$isset = isset($_POST['minify']) && \is_array($_POST['minify']);
foreach ($options AS $key => $item) {
if ($key != 'elements') {
$minify[$key] = $isset && \in_array($key, $_POST['minify']) ? (\is_array($item) ? [] : (\is_bool($options[$key]) ? true : $options[$key])) : false;
if (\is_array($item)) {
foreach ($item AS $sub => $value) {
if ($minify[$key] !== false && isset($_POST['minify'][$key]) && \is_array($_POST['minify'][$key]) && \in_array($sub, $_POST['minify'][$key])) {
$minify[$key][$sub] = true;
} elseif ($minify[$key]) {
$minify[$key][$sub] = false;
}
}
}
} else {
unset($options[$key]);
}
}
// minify the input
if ($minify) {
$doc->minify($minify);
}
// record timings
$timing['minify'] = \microtime(true);
$mem['minify'] = \memory_get_peak_usage();
$output = $doc->save();
$timing['output'] = \microtime(true);
$mem['output'] = \memory_get_peak_usage();
}
}
} else {
$minify = $options;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Hexydec HTML Minifier</title>
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<link rel="shortcut icon" type="image/svg" href="docs/htmldoc.svg" />
<style>
html, body {
margin: 0;
}
body, input {
font-family: Segoe UI, Tahoma, Geneva, Verdana, sans-serif;
}
.minify__form {
height: 100vh;
display: flex;
}
.minify__form-wrap {
display: flex;
flex-direction: column;
flex: 1 1 auto;
margin-bottom: 10px;
}
.minify__form-heading {
margin: 10px 10px 0 10px;
flex: 0 0 auto;
display: flex;
align-items: center;
gap: 0 10px;
}
.minify__form-error {
padding: 10px;
background: red;
font-weight: bold;
color: #FFF;
margin: 10px 10px 0 10px;
}
.minify__form-input {
flex: 1 1 auto;
display: flex;
flex-direction: column;
margin: 10px 10px 0 10px;
}
.minify__form-url {
display: flex;
margin: 10px 10px 0 10px;
}
.minify__form-url-box {
flex: 1 1 auto;
}
.minify__form-input-box {
display: block;
box-sizing: border-box;
width: 100%;
flex: 1 1 auto;
}
.minify__table {
margin: 10px;
font-size: 0.9em;
}
.minify__table th, .minify__table td {
padding: 5px;
text-align: center;
border-bottom: 1px solid #CCC;
}
.minify__table td:first-child {
text-align: left;
font-weight: bold;
}
.minify__preview {
flex: 1 1 40%;
}
.minify__options {
flex: 0 0 auto;
padding: 10px;
background: #003ea4;
color: #FFF;
overflow: auto;
}
.minify__options-list {
list-style: none;
margin: 0;
padding: 0;
}
.minify__options-list .minify__options-list {
padding-left: 20px;
}
</style>
</head>
<body>
<form action="<?= \htmlspecialchars($_SERVER['REQUEST_URI']); ?>" method="post" accept-charset="<?= \htmlspecialchars(\mb_internal_encoding()); ?>" class="minify__form">
<div class="minify__form-wrap">
<h1 class="minify__form-heading">
<img src="docs/htmldoc.svg" alt="HTMLdoc" height="50" />
HTML Minifier
</h1>
<?php if ($error) { ?>
<div class="minify__form-error"><?= \htmlspecialchars($error); ?></div>
<?php } ?>
<div class="minify__form-input">
<label for="source">Paste HTML:</label>
<textarea name="source" id="source" class="minify__form-input-box"><?= \htmlspecialchars($input); ?></textarea>
</div>
<div class="minify__form-url">
<label for="url">or External URL:</label>
<input type="url" name="url" id="url" class="minify__form-url-box" />
<button name="action" value="url">Go</button>
</div>
<?php if ($output) { ?>
<div class="minify__form-input">
<label for="output">Output HTML:</label>
<textarea id="output" class="minify__form-input-box"><?= \htmlspecialchars($output); ?></textarea>
</div>
<table class="minify__table">
<thead>
<tr>
<th></th>
<th>Input (bytes)</th>
<th>Output (bytes)</th>
<th>Diff (bytes)</th>
<th>Compression</th>
<th></th>
<th>Load</th>
<th>Parse</th>
<th>Minify</th>
<th>Output</th>
<th>Total (sec) / Peak (kb)</th>
</tr>
</thead>
<tbody>
<?php
$ilen = \strlen($input);
$olen = \strlen($output);
$gilen = \strlen(\gzencode($input));
$golen = \strlen(\gzencode($output));
?>
<tr>
<td>Uncompressed</td>
<td><?= \htmlspecialchars(\number_format($ilen)); ?></td>
<td><?= \htmlspecialchars(\number_format($olen)); ?></td>
<td><?= \htmlspecialchars(\number_format($ilen - $olen)); ?></td>
<td><?= \htmlspecialchars(\number_format((100 / $ilen) * $olen)); ?>%</td>
<td style="font-weight:bold;">Time (sec)</td>
<td><?= \htmlspecialchars(\number_format($timing['fetch'] - $timing['start'], 4)); ?>s</td>
<td><?= \htmlspecialchars(\number_format($timing['parse'] - $timing['fetch'], 4)); ?>s</td>
<td><?= \htmlspecialchars(\number_format($timing['minify'] - $timing['parse'], 4)); ?>s</td>
<td><?= \htmlspecialchars(\number_format($timing['output'] - $timing['minify'], 4)); ?>s</td>
<td><?= \htmlspecialchars(\number_format($timing['output'] - $timing['fetch'], 4)); ?>s</td>
</tr>
<tr>
<td>Compressed</td>
<td><?= \htmlspecialchars(\number_format($gilen)); ?></td>
<td><?= \htmlspecialchars(\number_format($golen)); ?></td>
<td><?= \htmlspecialchars(\number_format($gilen - $golen)); ?></td>
<td><?= \htmlspecialchars(\number_format((100 / $gilen) * $golen)); ?>%</td>
<td style="font-weight:bold;">Peak (kb)</td>
<td><?= \htmlspecialchars(\number_format($mem['fetch'] / 1024, 0)); ?>kb</td>
<td><?= \htmlspecialchars(\number_format($mem['parse'] / 1024, 0)); ?>kb</td>
<td><?= \htmlspecialchars(\number_format($mem['minify'] / 1024, 0)); ?>kb</td>
<td><?= \htmlspecialchars(\number_format($mem['output'] / 1024, 0)); ?>kb</td>
<td><?= \htmlspecialchars(\number_format(\memory_get_peak_usage() / 1024, 0)); ?>kb</td>
</tr>
</tbody>
</table>
<?php } ?>
</div>
<?php if ($output) {
if ($base) { ?>
<input type="hidden" name="base" value="<?= \htmlspecialchars($base); ?>" />
<?php } ?>
<iframe class="minify__preview" srcdoc="<?= \htmlspecialchars($base === null ? $output : \preg_replace('/<head[^>]*>/i', '$0<base href="'.\htmlspecialchars($base).'">', $output)); ?>"></iframe>
<?php } ?>
<div class="minify__options">
<h3>Options</h3>
<ul class="minify__options-list">
<?php foreach ($options AS $key => $item) {
if (\is_bool($item) || \is_array($item)) { ?>
<li>
<label>
<input type="checkbox" name="minify[]" value="<?= $key; ?>"<?= !isset($minify[$key]) || $minify[$key] === false ? '' : ' checked="checked"'; ?> /><?= \htmlspecialchars(\ucfirst($key)); ?>
</label>
<?php if (\is_array($item)) { ?>
<ul class="minify__options-list">
<?php foreach ($item AS $sub => $value) { ?>
<li>
<label>
<input type="checkbox" name="minify[<?= $key; ?>][]" value="<?= $sub; ?>"<?= !isset($minify[$key][$sub]) || $minify[$key][$sub] === false ? '' : ' checked="checked"'; ?> /><?= \htmlspecialchars(\ucfirst($sub)); ?>
</label>
</li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php }
} ?>
</ul>
</div>
</form>
</body>
</html>