Skip to content

Commit bcddf29

Browse files
committed
Fixed bug in style::minify() and script::minify() where if the minify option is passed as false, it caused an error.
Updated index.php to load external HTML separately so it can be timed and not included in the HTML processing time. Updated some tests.
1 parent 9eb8974 commit bcddf29

File tree

6 files changed

+74
-40
lines changed

6 files changed

+74
-40
lines changed

index.php

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<?php
22
require(__DIR__.'/vendor/autoload.php');
33

4+
$error = null;
5+
set_error_handler(function (int $type, string $msg) use (&$error) {
6+
$error = $msg;
7+
});
8+
49
$base = empty($_POST['base']) ? '' : $_POST['base'];
510
$input = '';
611
$output = '';
@@ -18,8 +23,6 @@
1823

1924
// process form submmission
2025
if (!empty($_POST['action'])) {
21-
$timing['fetch'] = microtime(true);
22-
$mem['fetch'] = memory_get_peak_usage();
2326

2427
// handle a URL
2528
if (!empty($_POST['url'])) {
@@ -32,6 +35,10 @@
3235
} elseif (!isset($url['host'])) {
3336
trigger_error('Could not parse URL: No host was supplied', E_USER_WARNING);
3437

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+
3542
// open the document
3643
} elseif (($input = $doc->open($_POST['url'], null, $error)) === false) {
3744
trigger_error('Could not load HTML: '.$error, E_USER_WARNING);
@@ -45,50 +52,54 @@
4552
} elseif (empty($_POST['source'])) {
4653
trigger_error('No URL or HTML source was posted', E_USER_WARNING);
4754

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-
5255
// record the HTML
5356
} else {
5457
$input = $_POST['source'];
5558
}
59+
$timing['fetch'] = microtime(true);
60+
$mem['fetch'] = memory_get_peak_usage();
5661

57-
// if there is some input
62+
// load the source code
5863
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();
6171

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+
}
7384
}
7485
}
86+
} else {
87+
unset($options[$key]);
7588
}
76-
} else {
77-
unset($options[$key]);
7889
}
79-
}
8090

81-
// minify the input
82-
if ($minify) {
83-
$doc->minify($minify);
84-
}
91+
// minify the input
92+
if ($minify) {
93+
$doc->minify($minify);
94+
}
8595

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+
}
92103
}
93104
} else {
94105
$minify = $options;
@@ -111,11 +122,19 @@
111122
display: flex;
112123
flex-direction: column;
113124
flex: 1 1 auto;
125+
margin-bottom: 10px;
114126
}
115127
.minify__form-heading {
116128
margin: 10px 10px 0 10px;
117129
flex: 0 0 auto;
118130
}
131+
.minify__form-error {
132+
padding: 10px;
133+
background: red;
134+
font-weight bold;
135+
color: #FFF;
136+
margin: 10px 10px 0 10px;
137+
}
119138
.minify__form-input {
120139
flex: 1 1 auto;
121140
display: flex;
@@ -171,6 +190,9 @@
171190
<form action="<?= htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post" accept-charset="<?= mb_internal_encoding(); ?>" class="minify__form">
172191
<div class="minify__form-wrap">
173192
<h1 class="minify__form-heading">HTML Minifier</h1>
193+
<?php if ($error) { ?>
194+
<div class="minify__form-error"><?= htmlspecialchars($error); ?></div>
195+
<?php } ?>
174196
<div class="minify__form-input">
175197
<label for="source">Paste HTML:</label>
176198
<textarea name="source" id="source" class="minify__form-input-box"><?= htmlspecialchars($input); ?></textarea>

src/htmldoc/tokens/script.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,11 @@ public function parse(tokenise $tokens) : void {
5959
* @return void
6060
*/
6161
public function minify(array $minify) : void {
62-
$func = $this->root->getConfig('custom', 'script', 'config', 'minifier');
63-
if ($func) {
64-
$this->content = call_user_func($func, $this->content, $minify['script']);
62+
if (!isset($minify['script']) || $minify['script'] !== false) {
63+
$func = $this->root->getConfig('custom', 'script', 'config', 'minifier');
64+
if ($func) {
65+
$this->content = call_user_func($func, $this->content, $minify['script']);
66+
}
6567
}
6668
}
6769

src/htmldoc/tokens/style.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ public function parse(tokenise $tokens) : void {
5555
* @return void
5656
*/
5757
public function minify(array $minify) : void {
58-
$func = $this->root->getConfig('custom', 'style', 'config', 'minifier');
59-
if ($func) {
60-
$this->content = call_user_func($func, $this->content, $minify['style']);
58+
if (!isset($minify['style']) || $minify['style'] !== false) {
59+
$func = $this->root->getConfig('custom', 'style', 'config', 'minifier');
60+
if ($func) {
61+
$this->content = call_user_func($func, $this->content, $minify['style']);
62+
}
6163
}
6264
}
6365

tests/templates/document.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ <h2>Heading</h2>
2626
<option value="test2">This is also a test
2727
</select>
2828
<input type="text" class=" " value="" />
29+
<p>Text with a <strong>lot of bold</strong> and <em>italics</em> in it</p>
2930
</form>
3031
<script type="text/javascript" nomodule="nomodule">
3132
alert("hi");

tests/templates/whitespace-minified.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!DOCTYPE html><html><head><title>Test HTML Document</title></head><body>This is <em class="test">a</em> <em class="test test2">test</em> yo<div><em>Open</em> and close <em>test</em></div><pre class="pre">
1+
<!DOCTYPE html><html><head><title>Test HTML Document</title></head><body>This is <em class="test">a</em> <em class="test test2">test</em> yo<div><em>Open</em> and close <em>test</em></div><p>Text with a <strong>lot of bold</strong> and <em>italics</em> in it on multiple lines.</p><pre class="pre">
22
preformatted
33

44
content

tests/templates/whitespace.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
<div>
1313
<em>Open</em> and close <em>test</em>
1414
</div>
15+
<p>
16+
Text with a
17+
<strong>lot of bold</strong>
18+
and
19+
<em>italics</em>
20+
in it on multiple lines.
21+
</p>
1522
<pre class="pre">
1623
preformatted
1724

0 commit comments

Comments
 (0)