-
Notifications
You must be signed in to change notification settings - Fork 0
/
HtmlCompressor.php
207 lines (182 loc) · 5.77 KB
/
HtmlCompressor.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
<?php
/**
* HtmlCompressor.php
* @author Revin Roman
* @link https://rmrevin.ru
*/
namespace appmake\yii2\minify;
/**
* Class HtmlCompressor
* @package rmrevin\yii\minify
*/
class HtmlCompressor
{
/**
* @param string $data is either a handle to an open file, or an HTML string
* @param null|array $options key => value array of execute options
* The possible keys are:
*
* - `c` or `no-comments` - removes HTML comments
* - `s` or `stats` - output filesize savings calculation
* - `x` or `extra` - perform extra (possibly unsafe) compression operations
*
* Example: HtmlCompressor::compress($HtmlCode, $options = ['no-comments' => true])
*
* @return string
*/
public static function compress($data, $options = null) {
return (new static)->htmlCompress($data, $options);
}
/**
* HTML Compressor 1.0.1
* Original Author: Tyler Hall <tylerhall@gmail.com>
* Edited by: Revin Roman <xgismox@gmail.com>
* Latest Source and Bug Tracker: http://github.com/tylerhall/html-compressor
*
* Attemps to reduce the filesize of an HTML document by removing unnecessary
* whitespace at the beginning and end of lines, inside closing tags, and
* stripping blank lines completely. <pre> tags are respected and their contents
* are left alone. Warning, nested <pre> tags may exhibit unexpected behaviour.
*
* This code is licensed under the MIT Open Source License.
* Copyright (c) 2010 tylerhall@gmail.com
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @param $data
* @param null|array $options
*
* @return bool|mixed|string
*/
private function htmlCompress($data, $options = null) {
if (!isset($options)) {
$options = [];
}
$data .= "\n";
$out = '';
$inside_pre = false;
$bytecount = 0;
while ($line = $this->getLine($data)) {
$bytecount += strlen($line);
if (!$inside_pre) {
if (strpos($line, '<pre') === false) {
// Since we're not inside a <pre> block, we can trim both ends of the line
$line = trim($line);
// And condense multiple spaces down to one
$line = preg_replace('/\s\s+/', ' ', $line);
} else {
// Only trim the beginning since we just entered a <pre> block...
$line = ltrim($line);
// If the <pre> ends on the same line, don't turn on $inside_pre...
list($line, $inside_pre) = $this->checkInsidePre($line);
}
} else {
list($line, $inside_pre) = $this->checkInsidePre($line);
}
// Filter out any blank lines that aren't inside a <pre> block...
if ($inside_pre) {
$out .= $line;
} elseif ($line != '') {
$out .= $line . "\n";
}
}
// Perform any extra (unsafe) compression techniques...
if (array_key_exists('x', $options) || array_key_exists('extra', $options)) {
// Can break layouts that are dependent on whitespace between tags
$out = str_replace(">\n<", '><', $out);
}
// Remove HTML comments...
if (array_key_exists('c', $options) || array_key_exists('no-comments', $options)) {
$out = preg_replace('/(<!--.*?-->)/ms', '', $out);
$out = str_replace('<!>', '', $out);
}
// Remove the trailing \n
$out = trim($out);
// Output either our stats or the compressed data...
if (array_key_exists('s', $options) || array_key_exists('stats', $options)) {
$echo = '';
$echo .= "Original Size: $bytecount\n";
$echo .= "Compressed Size: " . strlen($out) . "\n";
$echo .= "Savings: " . round((1 - strlen($out) / $bytecount) * 100, 2) . "%\n";
echo $echo;
} else {
return $out;
}
return false;
}
/**
* @param $line
*
* @return array
*/
private function checkInsidePre($line) {
$inside_pre = true;
if ((strpos($line, '</pre') !== false) && (strripos($line, '</pre') >= strripos($line, '<pre'))) {
$line = rtrim($line);
$inside_pre = false;
}
return [$line, $inside_pre];
}
/**
* Returns the next line from an open file handle or a string
*
* @param $data
*
* @return bool|string
*/
private function getLine(&$data) {
if (is_resource($data)) {
return fgets($data);
}
if (is_string($data)) {
if (strlen($data) > 0) {
$pos = strpos($data, "\n");
$return = substr($data, 0, $pos) . "\n";
$data = substr($data, $pos + 1);
return $return;
} else {
return false;
}
}
return false;
}
/**
* @param $data
* @param null|array $options
*
* @return bool|mixed|string
* @deprecated
* @codeCoverageIgnore
*/
private function html_compress($data, $options = null) {
return $this->htmlCompress($data, $options);
}
/**
* Returns the next line from an open file handle or a string
*
* @param $data
*
* @return bool|string
* @deprecated
* @codeCoverageIgnore
*/
private function get_line(&$data) {
\Yii::warning(sprintf('You are using an deprecated method `%s`.', 'get_line'));
return $this->getLine($data);
}
}