|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace gulch\Minify\Processor; |
|
4
|
|
|
|
|
5
|
|
|
use gulch\Minify\Contract\ProcessorInterface; |
|
6
|
|
|
|
|
7
|
|
|
class WhitespacesRemover implements ProcessorInterface |
|
8
|
|
|
{ |
|
9
|
|
|
private const BLOCK_ELEMENTS = [ |
|
10
|
|
|
'address', |
|
11
|
|
|
'article', |
|
12
|
|
|
'aside', |
|
13
|
|
|
'audio', |
|
14
|
|
|
'blockquote', |
|
15
|
|
|
'body', |
|
16
|
|
|
'canvas', |
|
17
|
|
|
'caption', |
|
18
|
|
|
'colgroup', |
|
19
|
|
|
'datalist', |
|
20
|
|
|
'details', |
|
21
|
|
|
'div', |
|
22
|
|
|
'dl', |
|
23
|
|
|
'dt', |
|
24
|
|
|
'embed', |
|
25
|
|
|
'fieldset', |
|
26
|
|
|
'figcaption', |
|
27
|
|
|
'figure', |
|
28
|
|
|
'footer', |
|
29
|
|
|
'form', |
|
30
|
|
|
'h[1-6]', |
|
31
|
|
|
'head', |
|
32
|
|
|
'header', |
|
33
|
|
|
'hr', |
|
34
|
|
|
'html', |
|
35
|
|
|
|
|
36
|
|
|
'li', |
|
37
|
|
|
'link', |
|
38
|
|
|
'main', |
|
39
|
|
|
'map', |
|
40
|
|
|
'meta', |
|
41
|
|
|
'meter', |
|
42
|
|
|
'nav', |
|
43
|
|
|
'noframes', |
|
44
|
|
|
'noscript', |
|
45
|
|
|
'object', |
|
46
|
|
|
'ol', |
|
47
|
|
|
'optgroup', |
|
48
|
|
|
'option', |
|
49
|
|
|
'p', |
|
50
|
|
|
'param', |
|
51
|
|
|
'pre', |
|
52
|
|
|
'progress', |
|
53
|
|
|
'ruby', |
|
54
|
|
|
'script', |
|
55
|
|
|
'section', |
|
56
|
|
|
'select', |
|
57
|
|
|
'source', |
|
58
|
|
|
'style', |
|
59
|
|
|
'summary', |
|
60
|
|
|
'table', |
|
61
|
|
|
'tbody', |
|
62
|
|
|
'td', |
|
63
|
|
|
'tfoot', |
|
64
|
|
|
'th', |
|
65
|
|
|
'thead', |
|
66
|
|
|
'title', |
|
67
|
|
|
'tr', |
|
68
|
|
|
'track', |
|
69
|
|
|
'ul', |
|
70
|
|
|
'video' |
|
71
|
|
|
]; |
|
72
|
|
|
|
|
73
|
|
|
public function process(string $buffer): string |
|
74
|
|
|
{ |
|
75
|
|
|
if (\strlen($buffer) === 0) { |
|
76
|
|
|
return ''; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// Find all the <pre>,<code>,<textarea>, and <javascript> tags |
|
80
|
|
|
// We'll want to return them to this unprocessed state later. |
|
81
|
|
|
\preg_match_all('{<pre.+</pre>}msU', $buffer, $pres_source); |
|
82
|
|
|
\preg_match_all('{<code.+</code>}msU', $buffer, $codes_source); |
|
83
|
|
|
\preg_match_all('{<textarea.+</textarea>}msU', $buffer, $textareas_source); |
|
84
|
|
|
\preg_match_all('{<script.+</script>}msU', $buffer, $javascript_source); |
|
85
|
|
|
|
|
86
|
|
|
// Replace multiple spaces with a single space. |
|
87
|
|
|
$buffer = \preg_replace('/\s{2,}/', ' ', $buffer); |
|
88
|
|
|
|
|
89
|
|
|
// Remove spaces around block-level elements. |
|
90
|
|
|
$buffer = \preg_replace( |
|
91
|
|
|
'/\s*(<\/?(' . \implode('|', self::BLOCK_ELEMENTS) . ')[^>]*>)\s*/is', |
|
92
|
|
|
'$1', |
|
93
|
|
|
$buffer |
|
94
|
|
|
); |
|
95
|
|
|
|
|
96
|
|
|
// Replace edited <pre> tags with unprocessed ones. |
|
97
|
|
|
if (\sizeof($pres_source)) { |
|
98
|
|
|
\preg_match_all('{<pre.+</pre>}msU', $buffer, $pres_edited); |
|
99
|
|
|
$buffer = \str_replace($pres_edited[0], $pres_source[0], $buffer); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
// Replace edited <code> tags with unprocessed ones. |
|
103
|
|
|
if (\sizeof($codes_source)) { |
|
104
|
|
|
\preg_match_all('{<code.+</code>}msU', $buffer, $codes_edited); |
|
105
|
|
|
$buffer = \str_replace($codes_edited[0], $codes_source[0], $buffer); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
// Replace edited <textarea> tags with unprocessed ones. |
|
109
|
|
|
if (\sizeof($textareas_source)) { |
|
110
|
|
|
\preg_match_all('{<textarea.+</textarea>}msU', $buffer, $textareas_edited); |
|
111
|
|
|
$buffer = \str_replace($textareas_edited[0], $textareas_source[0], $buffer); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
// Replace edited <script> tags with unprocessed ones. |
|
115
|
|
|
if (\sizeof($javascript_source)) { |
|
116
|
|
|
\preg_match_all('{<script.+</script>}msU', $buffer, $javascript_edited); |
|
117
|
|
|
$buffer = \str_replace($javascript_edited[0], $javascript_source[0], $buffer); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $buffer; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|