|
1 | 1 | <?php |
2 | 2 | require(__DIR__.'/vendor/autoload.php'); |
3 | 3 |
|
| 4 | +$error = null; |
| 5 | +set_error_handler(function (int $type, string $msg) use (&$error) { |
| 6 | + $error = $msg; |
| 7 | +}); |
| 8 | + |
4 | 9 | $base = empty($_POST['base']) ? '' : $_POST['base']; |
5 | 10 | $input = ''; |
6 | 11 | $output = ''; |
|
18 | 23 |
|
19 | 24 | // process form submmission |
20 | 25 | if (!empty($_POST['action'])) { |
21 | | - $timing['fetch'] = microtime(true); |
22 | | - $mem['fetch'] = memory_get_peak_usage(); |
23 | 26 |
|
24 | 27 | // handle a URL |
25 | 28 | if (!empty($_POST['url'])) { |
|
32 | 35 | } elseif (!isset($url['host'])) { |
33 | 36 | trigger_error('Could not parse URL: No host was supplied', E_USER_WARNING); |
34 | 37 |
|
| 38 | + // open the document manually so we can time it |
| 39 | + } elseif (($input = file_get_contents($_POST['url'])) === false) { |
| 40 | + trigger_error('Could not load HTML: The file could not be accessed'.$error, E_USER_WARNING); |
| 41 | + |
35 | 42 | // open the document |
36 | 43 | } elseif (($input = $doc->open($_POST['url'], null, $error)) === false) { |
37 | 44 | trigger_error('Could not load HTML: '.$error, E_USER_WARNING); |
|
45 | 52 | } elseif (empty($_POST['source'])) { |
46 | 53 | trigger_error('No URL or HTML source was posted', E_USER_WARNING); |
47 | 54 |
|
48 | | - // load the source code |
49 | | - } elseif (!$doc->load($_POST['source'], null, $error)) { |
50 | | - trigger_error('Could not parse HTML: '.$error, E_USER_WARNING); |
51 | | - |
52 | 55 | // record the HTML |
53 | 56 | } else { |
54 | 57 | $input = $_POST['source']; |
55 | 58 | } |
| 59 | + $timing['fetch'] = microtime(true); |
| 60 | + $mem['fetch'] = memory_get_peak_usage(); |
56 | 61 |
|
57 | | - // if there is some input |
| 62 | + // load the source code |
58 | 63 | if ($input) { |
59 | | - $timing['parse'] = microtime(true); |
60 | | - $mem['parse'] = memory_get_peak_usage(); |
| 64 | + if (!$doc->load($input, null, $error)) { |
| 65 | + trigger_error('Could not parse HTML: '.$error, E_USER_WARNING); |
| 66 | + |
| 67 | + // minify the output |
| 68 | + } else { |
| 69 | + $timing['parse'] = microtime(true); |
| 70 | + $mem['parse'] = memory_get_peak_usage(); |
61 | 71 |
|
62 | | - // retrieve the user posted options |
63 | | - $isset = isset($_POST['minify']) && is_array($_POST['minify']); |
64 | | - foreach ($options AS $key => $item) { |
65 | | - if ($key != 'elements') { |
66 | | - $minify[$key] = $isset && in_array($key, $_POST['minify']) ? (is_array($item) ? [] : (is_bool($options[$key]) ? true : $options[$key])) : false; |
67 | | - if (is_array($item)) { |
68 | | - foreach ($item AS $sub => $value) { |
69 | | - if ($minify[$key] !== false && isset($_POST['minify'][$key]) && is_array($_POST['minify'][$key]) && in_array($sub, $_POST['minify'][$key])) { |
70 | | - $minify[$key][$sub] = true; |
71 | | - } elseif ($minify[$key]) { |
72 | | - $minify[$key][$sub] = false; |
| 72 | + // retrieve the user posted options |
| 73 | + $isset = isset($_POST['minify']) && is_array($_POST['minify']); |
| 74 | + foreach ($options AS $key => $item) { |
| 75 | + if ($key != 'elements') { |
| 76 | + $minify[$key] = $isset && in_array($key, $_POST['minify']) ? (is_array($item) ? [] : (is_bool($options[$key]) ? true : $options[$key])) : false; |
| 77 | + if (is_array($item)) { |
| 78 | + foreach ($item AS $sub => $value) { |
| 79 | + if ($minify[$key] !== false && isset($_POST['minify'][$key]) && is_array($_POST['minify'][$key]) && in_array($sub, $_POST['minify'][$key])) { |
| 80 | + $minify[$key][$sub] = true; |
| 81 | + } elseif ($minify[$key]) { |
| 82 | + $minify[$key][$sub] = false; |
| 83 | + } |
73 | 84 | } |
74 | 85 | } |
| 86 | + } else { |
| 87 | + unset($options[$key]); |
75 | 88 | } |
76 | | - } else { |
77 | | - unset($options[$key]); |
78 | 89 | } |
79 | | - } |
80 | 90 |
|
81 | | - // minify the input |
82 | | - if ($minify) { |
83 | | - $doc->minify($minify); |
84 | | - } |
| 91 | + // minify the input |
| 92 | + if ($minify) { |
| 93 | + $doc->minify($minify); |
| 94 | + } |
85 | 95 |
|
86 | | - // record timings |
87 | | - $timing['minify'] = microtime(true); |
88 | | - $mem['minify'] = memory_get_peak_usage(); |
89 | | - $output = $doc->save(); |
90 | | - $timing['output'] = microtime(true); |
91 | | - $mem['output'] = memory_get_peak_usage(); |
| 96 | + // record timings |
| 97 | + $timing['minify'] = microtime(true); |
| 98 | + $mem['minify'] = memory_get_peak_usage(); |
| 99 | + $output = $doc->save(); |
| 100 | + $timing['output'] = microtime(true); |
| 101 | + $mem['output'] = memory_get_peak_usage(); |
| 102 | + } |
92 | 103 | } |
93 | 104 | } else { |
94 | 105 | $minify = $options; |
|
111 | 122 | display: flex; |
112 | 123 | flex-direction: column; |
113 | 124 | flex: 1 1 auto; |
| 125 | + margin-bottom: 10px; |
114 | 126 | } |
115 | 127 | .minify__form-heading { |
116 | 128 | margin: 10px 10px 0 10px; |
117 | 129 | flex: 0 0 auto; |
118 | 130 | } |
| 131 | + .minify__form-error { |
| 132 | + padding: 10px; |
| 133 | + background: red; |
| 134 | + font-weight bold; |
| 135 | + color: #FFF; |
| 136 | + margin: 10px 10px 0 10px; |
| 137 | + } |
119 | 138 | .minify__form-input { |
120 | 139 | flex: 1 1 auto; |
121 | 140 | display: flex; |
|
171 | 190 | <form action="<?= htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post" accept-charset="<?= mb_internal_encoding(); ?>" class="minify__form"> |
172 | 191 | <div class="minify__form-wrap"> |
173 | 192 | <h1 class="minify__form-heading">HTML Minifier</h1> |
| 193 | + <?php if ($error) { ?> |
| 194 | + <div class="minify__form-error"><?= htmlspecialchars($error); ?></div> |
| 195 | + <?php } ?> |
174 | 196 | <div class="minify__form-input"> |
175 | 197 | <label for="source">Paste HTML:</label> |
176 | 198 | <textarea name="source" id="source" class="minify__form-input-box"><?= htmlspecialchars($input); ?></textarea> |
|
0 commit comments