|
1
|
|
|
<?php |
|
2
|
|
|
namespace PHPWee; |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
|
|
7
|
|
|
PHPWee PHP Minifier Package - http://searchturbine.com/php/phpwee |
|
8
|
|
|
|
|
9
|
|
|
Copyright (c) 2015, SearchTurbine - Enterprise Search for Everyone |
|
10
|
|
|
http://searchturbine.com/ |
|
11
|
|
|
|
|
12
|
|
|
All rights reserved. |
|
13
|
|
|
|
|
14
|
|
|
Redistribution and use in source and binary forms, with or without |
|
15
|
|
|
modification, are permitted provided that the following conditions are met: |
|
16
|
|
|
|
|
17
|
|
|
Redistributions of source code must retain the above copyright notice, this |
|
18
|
|
|
list of conditions and the following disclaimer. |
|
19
|
|
|
|
|
20
|
|
|
Redistributions in binary form must reproduce the above copyright notice, |
|
21
|
|
|
this list of conditions and the following disclaimer in the documentation |
|
22
|
|
|
and/or other materials provided with the distribution. |
|
23
|
|
|
|
|
24
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
25
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
26
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
27
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
|
28
|
|
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
29
|
|
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|
30
|
|
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
31
|
|
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
|
32
|
|
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
33
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
*/ |
|
37
|
|
|
// -- Class Name : HtmlMin |
|
38
|
|
|
// -- Purpose : PHP class to minify html code. |
|
39
|
|
|
// -- Usage: echo PHPWee\Minify::html($myhtml); |
|
40
|
|
|
// -- notes: aply data-no-min to a style or script node to exempt it |
|
41
|
|
|
// -- HTML 4, XHTML, and HTML 5 compliant |
|
42
|
|
|
|
|
43
|
|
|
class HtmlMin{ |
|
44
|
|
|
// -- Function Name : minify - Params : $html, $js = true, $css = true |
|
45
|
|
|
public static function minify($html, $js = true, $css = true){ |
|
46
|
|
|
$doc = new \DOMDocument(); |
|
47
|
|
|
$doc->preserveWhiteSpace = false; |
|
48
|
|
|
@$doc->loadHTML($html); |
|
|
|
|
|
|
49
|
|
|
$xpath = new \DOMXPath($doc); |
|
50
|
|
|
foreach ($xpath->query('//comment()') as $comment) { |
|
51
|
|
|
$val= $comment->nodeValue; |
|
52
|
|
|
if( strpos($val,'[')!==0){ |
|
53
|
|
|
$comment->parentNode->removeChild($comment); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
$doc->normalizeDocument(); |
|
57
|
|
|
$textnodes = $xpath->query('//text()'); |
|
58
|
|
|
$skip = ["style","pre","code","script","textarea"]; |
|
59
|
|
|
foreach($textnodes as $t){ |
|
60
|
|
|
$xp = $t->getNodePath(); |
|
61
|
|
|
$doskip = false; |
|
62
|
|
|
foreach($skip as $pattern){ |
|
63
|
|
|
if(strpos($xp,"/$pattern")!==false){ |
|
64
|
|
|
$doskip = true; |
|
65
|
|
|
break; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
if($doskip){ |
|
69
|
|
|
continue; |
|
70
|
|
|
} |
|
71
|
|
|
$t->nodeValue = preg_replace("/\s{2,}/", " ", $t->nodeValue); |
|
72
|
|
|
} |
|
73
|
|
|
$doc->normalizeDocument(); |
|
74
|
|
|
$divnodes = $xpath->query('//div|//p|//nav|//footer|//article|//script|//hr|//br'); |
|
75
|
|
|
foreach($divnodes as $d){ |
|
76
|
|
|
$candidates = []; |
|
77
|
|
|
if(count($d->childNodes)){ |
|
78
|
|
|
$candidates[] = $d->firstChild; |
|
79
|
|
|
$candidates[] = $d->lastChild; |
|
80
|
|
|
$candidates[] = $d->previousSibling; |
|
81
|
|
|
$candidates[] = $d->nextSibling; |
|
82
|
|
|
} |
|
83
|
|
|
foreach($candidates as $c){ |
|
84
|
|
|
if($c==null){ |
|
85
|
|
|
continue; |
|
86
|
|
|
} |
|
87
|
|
|
if($c->nodeType==3){ |
|
88
|
|
|
$c->nodeValue = trim($c->nodeValue); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
$doc->normalizeDocument(); |
|
93
|
|
|
if($js){ |
|
94
|
|
|
$scriptnodes = $xpath->query('//script'); |
|
95
|
|
|
foreach($scriptnodes as $d){ |
|
96
|
|
|
if($d->hasAttribute("type") && strtolower($d->getAttribute("type"))!=='text/javascript' ){ |
|
97
|
|
|
continue; |
|
98
|
|
|
} |
|
99
|
|
|
if($d->hasAttribute("data-no-min")){ |
|
100
|
|
|
continue; |
|
101
|
|
|
} |
|
102
|
|
|
if(trim($d->nodeValue)==""){ |
|
103
|
|
|
continue; |
|
104
|
|
|
} |
|
105
|
|
|
$d->nodeValue = JSMin::minify( $d->nodeValue); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
if($css){ |
|
109
|
|
|
$cssnodes = $xpath->query('//style'); |
|
110
|
|
|
foreach($cssnodes as $d){ |
|
111
|
|
|
if($d->hasAttribute("data-no-min")){ |
|
112
|
|
|
continue; |
|
113
|
|
|
} |
|
114
|
|
|
if(trim($d->nodeValue)==""){ |
|
115
|
|
|
continue; |
|
116
|
|
|
} |
|
117
|
|
|
$d->nodeValue = CssMin::minify( $d->nodeValue); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
return ($doc->saveHTML()); |
|
121
|
|
|
} |
|
122
|
|
|
} |
If you suppress an error, we recommend checking for the error condition explicitly: