forked from jenstornell/tiny-html-minifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiny-html-minifier.php
228 lines (200 loc) · 6.29 KB
/
tiny-html-minifier.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
class TinyHtmlMinifier {
function __construct($options) {
$this->options = $options;
$this->build = [];
$this->skip = 0;
$this->skipName = '';
$this->head = false;
$this->elements = [
'skip' => [
'code',
'pre',
'textarea',
'script'
],
'hard' => [
'!doctype',
'body',
'html',
]
];
}
// Run minifier
function minify($html) {
$html = $this->removeComments($html);
$this->walk($html);
return $this->buildHtml();
}
// Walk trough html
function walk($html) {
$parts = explode('<', $html, 2);
$part = $parts[0];
$tag_parts = explode('>', $part);
$tag_content = $tag_parts[0];
if(!empty($tag_content)) {
$name = $this->findName($tag_content);
$element = $this->toElement($tag_content, $parts[0], $name);
$type = $this->toType($element);
if($name == 'head') {
$this->head = ($type == 'open') ? true : false;
}
$this->build[] = [
'name' => $name,
'content' => $element,
'type' => $type
];
$this->setSkip($name, $type);
if(!empty($tag_content)) {
$content = (isset($tag_parts[1])) ? $tag_parts[1] : '';
if(!empty($content)) {
$this->build[] = [
'content' => $this->compact($content, $name, $element),
'type' => 'content'
];
}
}
}
$rest = (isset($parts[1])) ? $parts[1] : '';
if(!empty($rest)) {
$this->walk($rest);
}
}
// Remove comments
function removeComments($content = '') {
return preg_replace('/<!--(.|\s)*?-->/', '', $content);
}
// Check if string contains string
function contains($needle, $haystack) {
return strpos($haystack, $needle) !== false;
}
// Return type of element
function toType($element) {
$type = (substr($element, 1, 1) == '/') ? 'close' : 'open';
return $type;
}
// Create element
function toElement($element, $noll, $name) {
$element = $this->stripWhitespace($element);
$element = $this->addChevrons($element, $noll);
$element = $this->removeSelfSlash($element);
$element = $this->removeMeta($element, $name);
return $element;
}
// Remove unneeded element meta
function removeMeta($element, $name) {
if($name == 'style') {
$element = str_replace([
' type="text/css"',
"' type='text/css'"
],
['', ''], $element);
} elseif($name == 'script') {
$element = str_replace([
' type="text/javascript"',
" type='text/javascript'"
],
['', ''], $element);
}
return $element;
}
// Strip whitespace from element
function stripWhitespace($element) {
if($this->skip == 0) {
$element = preg_replace('/\s+/', ' ', $element);
}
return $element;
}
// Add chevrons around element
function addChevrons($element, $noll) {
$char = ($this->contains('>', $noll)) ? '>' : '';
$element = '<' . $element . $char;
return $element;
}
// Remove unneeded self slash
function removeSelfSlash($element) {
if(substr($element, -3) == ' />') {
$element = substr($element, 0, -3) . '>';
}
return $element;
}
// Compact content
function compact($content, $name, $element) {
if($this->skip != 0) {
$name = $this->skipName;
} else {
$content = preg_replace('/\s+/', ' ', $content);
}
if(
$this->isSchema($name, $element) &&
!empty($this->options['collapse_json_ld'])
) {
return json_encode(json_decode($content));
} if(in_array($name, $this->elements['skip'])) {
return $content;
} elseif(
in_array($name, $this->elements['hard']) ||
!empty($this->options['collapse_whitespace']) ||
$this->head
) {
return $this->minifyHard($content);
} else {
return $this->minifyKeepSpaces($content);
}
}
function isSchema($name, $element) {
if($name != 'script') return false;
$element = strtolower($element);
if($this->contains('application/ld+json', $element)) return true;
return false;
}
// Build html
function buildHtml() {
$out = '';
foreach($this->build as $build) {
$out .= $build['content'];
}
return $out;
}
// Find name by part
function findName($part) {
$name_cut = explode(" ", $part, 2)[0];
$name_cut = explode(">", $name_cut, 2)[0];
$name_cut = explode("\n", $name_cut, 2)[0];
$name_cut = preg_replace('/\s+/', '', $name_cut);
$name_cut = strtolower(str_replace('/', '', $name_cut));
return $name_cut;
}
// Set skip if elements are blocked from minification
function setSkip($name, $type) {
foreach($this->elements['skip'] as $element) {
if($element == $name && $this->skip == 0) {
$this->skipName = $name;
}
}
if(in_array($name, $this->elements['skip'])) {
if($type == 'open') {
$this->skip++;
}
if($type == 'close') {
$this->skip--;
}
}
}
// Minify all, even spaces between elements
function minifyHard($element) {
$element = preg_replace('!\s+!', ' ', $element);
$element = trim($element);
return trim($element);
}
// Strip but keep one space
function minifyKeepSpaces($element) {
return preg_replace('!\s+!', ' ', $element);
}
}
class TinyMinify {
static function html($html, $options = []) {
$minifier = new TinyHtmlMinifier($options);
return $minifier->minify($html);
}
}