Skip to content

Commit 643ced1

Browse files
committed
Added declare(strict_types=1) to all files and fixed type errors.
Added code comments to index.php. comment class is now implementing the token interface. Fixed bug in htmldoc::open() where fopen had the context argument in the wrong place. Fixed bug in htmldoc::htmlentities() where preg_split() has an incorrect argument. Fixed bugs in tag::parseChildren() where strcasecmp() can't handle nulls. Fixed bug in tag::minify() where trim cannot accept a null. Bumped to version 0.8.6.
1 parent f203533 commit 643ced1

13 files changed

+86
-49
lines changed

index.php

+27-1
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,57 @@
1212
'start' => memory_get_usage()
1313
);
1414

15+
// create object and retrieve config
1516
$doc = new hexydec\html\htmldoc();
1617
$options = $doc->getConfig('minify');
18+
19+
// process form submmission
1720
if (!empty($_POST['action'])) {
1821
$timing['fetch'] = microtime(true);
1922
$mem['fetch'] = memory_get_usage();
23+
24+
// handle a URL
2025
if (!empty($_POST['url'])) {
26+
27+
// parse the URL
2128
if (($url = parse_url($_POST['url'])) === false) {
2229
trigger_error('Could not parse URL: The URL is not valid', E_USER_WARNING);
30+
31+
// check the host name
2332
} elseif (!isset($url['host'])) {
2433
trigger_error('Could not parse URL: No host was supplied', E_USER_WARNING);
34+
35+
// open the document
2536
} elseif (($input = $doc->open($_POST['url'], null, $error)) === false) {
2637
trigger_error('Could not load HTML: '.$error, E_USER_WARNING);
38+
39+
// save base URL
2740
} else {
2841
$base = $_POST['url'];
2942
}
43+
44+
// handle directly entered source code
3045
} elseif (empty($_POST['source'])) {
3146
trigger_error('No URL or HTML source was posted', E_USER_WARNING);
47+
48+
// load the source code
3249
} elseif (!$doc->load($_POST['source'], null, $error)) {
3350
trigger_error('Could not parse HTML: '.$error, E_USER_WARNING);
51+
52+
// record the HTML
3453
} else {
3554
$input = $_POST['source'];
3655
}
3756

57+
// if there is some input
3858
if ($input) {
3959
$timing['parse'] = microtime(true);
4060
$mem['parse'] = memory_get_usage();
61+
62+
// retrieve the user posted options
4163
$isset = isset($_POST['minify']) && is_array($_POST['minify']);
4264
foreach ($options AS $key => $item) {
43-
$minify[$key] = $isset && in_array($key, $_POST['minify']) ? (is_array($item) ? Array() : (is_bool($options[$key]) ? true : $options[$key])) : false;
65+
$minify[$key] = $isset && in_array($key, $_POST['minify']) ? (is_array($item) ? [] : (is_bool($options[$key]) ? true : $options[$key])) : false;
4466
if (is_array($item)) {
4567
foreach ($item AS $sub => $value) {
4668
if ($minify[$key] !== false && isset($_POST['minify'][$key]) && is_array($_POST['minify'][$key]) && in_array($sub, $_POST['minify'][$key])) {
@@ -51,9 +73,13 @@
5173
}
5274
}
5375
}
76+
77+
// minify the input
5478
if ($minify) {
5579
$doc->minify($minify);
5680
}
81+
82+
// record timings
5783
$timing['minify'] = microtime(true);
5884
$mem['minify'] = memory_get_usage();
5985
$output = $doc->save();

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "minify",
3-
"version": "0.8.5",
3+
"version": "0.8.6",
44
"description": "A token based HTML Document parser and minifier written in PHP",
55
"main": "index.js",
66
"directories": {

readme.md

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
A tokeniser based HTML and CSS document parser and minifier, written in PHP.
44

55
![Licence](https://img.shields.io/badge/Licence-MIT-lightgrey.svg)
6-
![Project Status](https://img.shields.io/badge/Project%20Status-Beta-yellow.svg)
76
[![Build Status](https://api.travis-ci.org/hexydec/htmldoc.svg?branch=master)](https://travis-ci.org/hexydec/htmldoc)
87

98
## Description

src/htmldoc/cssmin.php

+35-34
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<?php
2+
declare(strict_types = 1);
23
namespace hexydec\html;
34

45
class cssmin {
56

6-
protected static $tokens = Array(
7+
protected static $tokens = [
78
'whitespace' => '\s++',
89
'comment' => '\\/\\*[\d\D]*?\\*\\/',
910
'quotes' => '(?<!\\\\)("(?:[^"\\\\]++|\\\\.)*+"|\'(?:[^\'\\\\]++|\\\\.)*+\')',
@@ -19,9 +20,9 @@ class cssmin {
1920
'colon' => ':',
2021
'semicolon' => ';',
2122
'string' => '!?[^\[\]{}\(\):;,>+=~\^$!"\/ \n\r\t]++'
22-
);
23+
];
2324

24-
protected static $config = Array(
25+
protected static $config = [
2526
'removesemicolon' => true,
2627
'removezerounits' => true,
2728
'removeleadingzero' => true,
@@ -32,9 +33,9 @@ class cssmin {
3233
'email' => false,
3334
'maxline' => false,
3435
'output' => 'minify'
35-
);
36+
];
3637

37-
public static function minify(string $code, array $config = Array()) {
38+
public static function minify(string $code, array $config = []) : string {
3839
$config = array_merge(self::$config, $config);
3940

4041
// set email options
@@ -66,12 +67,12 @@ public static function minify(string $code, array $config = Array()) {
6667
}
6768

6869
protected static function parse(array &$tokens) {
69-
$rules = Array();
70+
$rules = [];
7071
$select = true;
7172
$comment = null;
7273
$media = false;
73-
$selectors = Array();
74-
$properties = Array();
74+
$selectors = [];
75+
$properties = [];
7576
$token = current($tokens);
7677
do {
7778

@@ -84,11 +85,11 @@ protected static function parse(array &$tokens) {
8485
return $rules;
8586
} elseif (in_array($token['type'], ['string', 'colon'])) {
8687
if ($token['value'] == '@media') {
87-
$rules[] = Array(
88+
$rules[] = [
8889
'media' => self::parseMediaQuery($tokens),
8990
'rules' => self::parse($tokens),
9091
'comment' => $comment
91-
);
92+
];
9293
$comment = false;
9394
} else {
9495
// prev($tokens);
@@ -106,28 +107,28 @@ protected static function parse(array &$tokens) {
106107
while (($token = next($tokens)) !== false) {
107108
if ($token['type'] == 'colon') {
108109
$important = false;
109-
$properties[] = Array(
110+
$properties[] = [
110111
'property' => $prop,
111112
'value' => self::parsePropertyValue($tokens, $important, $propcomment),
112113
'important' => $important,
113114
'semicolon' => ';',
114115
'comment' => $propcomment
115-
);
116+
];
116117
break;
117118
}
118119
}
119120

120121
// end rule
121122
} elseif ($token['type'] == 'curlyclose') {
122123
if ($selectors && $properties) {
123-
$rules[] = Array(
124+
$rules[] = [
124125
'selectors' => $selectors,
125126
'properties' => $properties,
126127
'comment' => $comment
127-
);
128+
];
128129
}
129-
$selectors = Array();
130-
$properties = Array();
130+
$selectors = [];
131+
$properties = [];
131132
$select = true;
132133
$comment = false;
133134
}
@@ -136,13 +137,13 @@ protected static function parse(array &$tokens) {
136137
}
137138

138139
protected static function parseMediaQuery(array &$tokens) {
139-
$media = Array();
140-
$default = $rule = Array(
140+
$media = [];
141+
$default = $rule = [
141142
'media' => false,
142143
'only' => false,
143144
'not' => false,
144-
'properties' => Array()
145-
);
145+
'properties' => []
146+
];
146147
while (($token = next($tokens)) !== false) {
147148
switch ($token['type']) {
148149
case 'string':
@@ -197,7 +198,7 @@ protected static function parseMediaQuery(array &$tokens) {
197198
}
198199

199200
protected static function parseSelectors(array &$tokens) {
200-
$selector = Array();
201+
$selector = [];
201202
$join = false;
202203
$token = current($tokens);
203204
do {
@@ -208,10 +209,10 @@ protected static function parseSelectors(array &$tokens) {
208209
}
209210
break;
210211
case 'string':
211-
$selector[] = Array(
212+
$selector[] = [
212213
'selector' => $token['value'],
213214
'join' => $join
214-
);
215+
];
215216
$join = false;
216217
break;
217218
case 'colon':
@@ -230,7 +231,7 @@ protected static function parseSelectors(array &$tokens) {
230231
}
231232

232233
// capture selector
233-
} elseif (!in_array($token['type'], Array('whitespace', 'comma', 'curlyopen'))) {
234+
} elseif (!in_array($token['type'], ['whitespace', 'comma', 'curlyopen'])) {
234235
$parts .= $token['value'];
235236

236237
// stop here
@@ -241,10 +242,10 @@ protected static function parseSelectors(array &$tokens) {
241242
}
242243

243244
// save selector
244-
$selector[] = Array(
245+
$selector[] = [
245246
'selector' => $parts,
246247
'join' => $join
247-
);
248+
];
248249
$join = false;
249250
break;
250251
case 'squareopen':
@@ -259,10 +260,10 @@ protected static function parseSelectors(array &$tokens) {
259260
}
260261
}
261262
}
262-
$selector[] = Array(
263+
$selector[] = [
263264
'selector' => '['.$parts.']',
264265
'join' => $join
265-
);
266+
];
266267
$join = false;
267268
break;
268269
case 'curlyopen':
@@ -277,19 +278,19 @@ protected static function parseSelectors(array &$tokens) {
277278
}
278279

279280
protected static function parsePropertyValue(array &$tokens, bool &$important = false, string &$comment = null) {
280-
$properties = Array();
281-
$values = Array();
281+
$properties = [];
282+
$values = [];
282283
$important = false;
283284
$comment = null;
284285
while (($token = next($tokens)) !== false) {
285286
if ($token['type'] == 'comma') {
286287
$properties[] = $values;
287-
$values = Array();
288+
$values = [];
288289
} elseif ($token['value'] == '!important') {
289290
$important = true;
290291
} elseif ($token['type'] == 'bracketopen') {
291292
$values[] = self::parsePropertyValue($tokens);
292-
} elseif (in_array($token['type'], Array('semicolon', 'bracketclose'))) {
293+
} elseif (in_array($token['type'], ['semicolon', 'bracketclose'])) {
293294
while (($token = next($tokens)) !== false) {
294295
if ($token['type'] == 'comment') {
295296
$comment = $token['value'];
@@ -460,8 +461,8 @@ protected static function compile(array $ast, array $config, int $indent = 0) {
460461
return rtrim($css);
461462
}
462463

463-
protected static function compileProperty(array $value, int $b, string $join = ' ') {
464-
$properties = Array();
464+
protected static function compileProperty(array $value, bool $b = false, string $join = ' ') {
465+
$properties = [];
465466
foreach ($value AS $group) {
466467
$compiled = '';
467468
foreach ($group AS $item) {

src/htmldoc/htmldoc.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types = 1);
23
namespace hexydec\html;
34

45
class htmldoc {
@@ -123,7 +124,7 @@ class htmldoc {
123124
protected $output = [
124125
'charset' => null, // set the output charset
125126
'quotestyle' => 'double', // double, single, minimal
126-
'singletonclose' => false, // string to close singleton tags, or false to leave as is
127+
'singletonclose' => null, // string to close singleton tags, or false to leave as is
127128
'closetags' => false, // whether to force tags to have a closing tag (true) or follow tag::close
128129
'xml' => false // sets the output presets to produce XML valid code
129130
];
@@ -177,7 +178,7 @@ public function toArray() : array {
177178
* @param string $key... One or more array keys indicating the configuration value to retrieve
178179
* @return mixed The value requested, or null if the value doesn't exist
179180
*/
180-
public function getConfig(...$keys) {
181+
public function getConfig(string ...$keys) {
181182
$config = $this->config;
182183
foreach ($keys AS $item) {
183184
if (isset($config[$item])) {
@@ -200,7 +201,7 @@ public function getConfig(...$keys) {
200201
public function open(string $url, resource $context = null, string &$error = null) {
201202

202203
// open a handle to the stream
203-
if (($handle = @fopen($url, 'rb', $context)) === false) {
204+
if (($handle = @fopen($url, 'rb', false, $context)) === false) {
204205
$error = 'Could not open file "'.$url.'"';
205206

206207
// retrieve the stream contents
@@ -702,7 +703,7 @@ protected function htmlentities(string $html, string $charset) : string {
702703

703704
// build html entities conversion map
704705
$replace = [];
705-
foreach (preg_split('//u', $str, null, PREG_SPLIT_NO_EMPTY) AS $chr) {
706+
foreach (preg_split('//u', $str, -1, PREG_SPLIT_NO_EMPTY) AS $chr) {
706707
$ent = mb_convert_encoding($chr, 'HTML-ENTITIES');
707708
if ($ent != $chr) {
708709
$replace[$chr] = $ent;

src/htmldoc/tokenise.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types = 1);
23
namespace hexydec\html;
34

45
class tokenise {
@@ -13,7 +14,7 @@ class tokenise {
1314
public static function tokenise(string $input, array $tokens) {
1415

1516
// prepare regexp and extract strings
16-
$patterns = Array();
17+
$patterns = [];
1718
foreach ($tokens AS $key => $item) {
1819
$patterns[] = '(?<'.$key.'>'.$item.')';
1920
}
@@ -26,10 +27,10 @@ public static function tokenise(string $input, array $tokens) {
2627
// go through tokens and find which one matched
2728
foreach ($keys AS $key) {
2829
if ($match[$key] !== '') {
29-
$output[] = Array(
30+
$output[] = [
3031
'type' => $key,
3132
'value' => $match[$key]
32-
);
33+
];
3334
break;
3435
}
3536
}

src/htmldoc/tokens/comment.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
2+
declare(strict_types = 1);
23
namespace hexydec\html;
34

4-
class comment {
5+
class comment implements token {
56

67
/**
78
* @var string The text content of this object
@@ -35,7 +36,7 @@ public function parse(array &$tokens) : void {
3536
* @param array $minify An array of minification options controlling which operations are performed
3637
* @return void
3738
*/
38-
public function minify(array $minify) {
39+
public function minify(array $minify) : void {
3940
if ($minify['comments'] && (empty($minify['comments']['ie']) || (mb_strpos($this->content, '[if ') !== 0 && $this->content != '<![endif]'))) {
4041
$this->content = null;
4142
}

src/htmldoc/tokens/doctype.php

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types = 1);
23
namespace hexydec\html;
34

45
class doctype implements token {

0 commit comments

Comments
 (0)