-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPicoMinify.php
214 lines (136 loc) · 4.52 KB
/
PicoMinify.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
208
209
210
211
212
213
214
<?php
/**
* PicoCMS plugin to minify the HTML output.
* This class is a modified version of the Wordpress-Minify class by http://fastwp.de/snippets/html-minify/
*
* @author Niklas Teich <[email protected]>
* @link https://github.com/NiklasTeich/Pico-Minify Github Repository
* @link http://fastwp.de/snippets/html-minify/ Original class reference
* @version 1.1
*/
/**
* Class PicoMinify
*/
class PicoMinify extends AbstractPicoPlugin
{
/**
* This plugin is enabled by default
*
* @see AbstractPicoPlugin::$enabled
* @var boolean
*/
protected $enabled = true;
/**
* Main-flag to activate/deactivate the Pico Minify plugin.
*
* @var bool
*/
protected $minify = true;
/**
* Flag to compress css.
*
* @var bool
*/
protected $compress_css = true;
/**
* Flag to compress js.
*
* @var bool
*/
protected $compress_js = true;
/**
* Flag to remove comments.
*
* @var bool
*/
protected $remove_comments = true;
/**
* Hook helper-function to manipulate the Pico output.
*
* @param string &$output
* @return void
*/
public function onPageRendered(&$output)
{
if ($this->minify) {
$output = $this->minifyHTML($output);
}
}
/**
* Hook to load the (optional) custom config settings.
*
* @see Pico::getConfig()
* @param mixed[] &$settings
* @return void
*/
public function onConfigLoaded(array &$settings)
{
$this->minify = isset($settings['pico_minify']['minify']) ? $settings['pico_minify']['minify'] : $this->isEnabled();
$this->compress_css = isset($settings['pico_minify']['compress_css']) ? $settings['pico_minify']['compress_css'] : true;
$this->compress_js = isset($settings['pico_minify']['compress_js']) ? $settings['pico_minify']['compress_js'] : true;
$this->remove_comments = isset($settings['pico_minify']['remove_comments']) ? $settings['pico_minify']['remove_comments'] : true;
}
/**
* Main-function to minify the HTML content.
*
* @param string $html
* @return string
*/
public function minifyHTML($html) {
$pattern = '/<(?<script>script).*?<\/script\s*>|<(?<style>style).*?<\/style\s*>|<!(?<comment>--).*?-->|<(?<tag>[\/\w.:-]*)(?:".*?"|\'.*?\'|[^\'">]+)*>|(?<text>((<[^!\/\w.:-])?[^<]*)+)|/si';
preg_match_all($pattern, $html, $matches, PREG_SET_ORDER);
$overriding = false;
$raw_tag = false;
$html = '';
foreach ($matches as $token) {
$strip = true;
$tag = (isset($token['tag'])) ? strtolower($token['tag']) : null;
$content = $token[0];
if (is_null($tag)) {
if (!empty($token['script'])) {
$strip = $this->compress_js;
} else if (!empty($token['style'])) {
$strip = $this->compress_css;
} else if ($this->remove_comments) {
if (!$overriding && $raw_tag != 'textarea') {
$content = preg_replace('/<!--(?!\s*(?:\[if [^\]]+]|<!|>))(?:(?!-->).)*-->/s', '', $content);
}
}
} else {
if ($tag == 'pre' || $tag == 'textarea') {
$raw_tag = $tag;
} else if ($tag == '/pre' || $tag == '/textarea') {
$raw_tag = false;
} else {
if ($raw_tag || $overriding) {
$strip = false;
} else {
$strip = true;
$content = preg_replace('/(\s+)(\w++(?<!\baction|\balt|\bcontent|\bsrc)="")/', '$1', $content);
$content = str_replace(' />', '/>', $content);
}
}
}
if ($strip) {
$content = $this->removeWhiteSpace($content);
}
$html .= $content;
}
return $html;
}
/**
* Helper-function to remove whitespaces from a given string.
*
* @param string $str
* @return mixed
*/
protected function removeWhiteSpace($str) {
$str = str_replace("\t", ' ', $str);
$str = str_replace("\n", '', $str);
$str = str_replace("\r", '', $str);
while (stristr($str, ' ')) {
$str = str_replace(' ', '', $str);
}
return $str;
}
}