1 | <?php |
||
10 | class WhitespaceMinifier implements MinifierInterface |
||
11 | { |
||
12 | /** |
||
13 | * Max allowed html line length for old e.g. browsers, firewalls and routers. |
||
14 | * |
||
15 | * @var int |
||
16 | */ |
||
17 | protected $maxHtmlLineLength = 32000; |
||
18 | |||
19 | /** |
||
20 | * Minification regexp's for replacing redundant whitespaces. |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $minifyRules = [ |
||
25 | '/\s?=\s?/' => '=', |
||
26 | '/\s?\/>/' => '>', |
||
27 | '/>\s</' => '><', |
||
28 | '/\s\s/' => ' ', |
||
29 | '/<\s/' => '<', |
||
30 | '/\s>/' => '>', |
||
31 | '/\t/' => ' ', |
||
32 | '/\r/' => '', |
||
33 | '/\n/' => '', |
||
34 | ]; |
||
35 | |||
36 | /** |
||
37 | * Minify redundant whitespaces. |
||
38 | * |
||
39 | * @param \ArjanSchouten\HtmlMinifier\MinifyContext $context |
||
40 | * |
||
41 | * @return \ArjanSchouten\HtmlMinifier\MinifyContext |
||
42 | */ |
||
43 | 3 | public function process(MinifyContext $context) |
|
51 | |||
52 | /** |
||
53 | * Remove trailing whitespaces around the contents. |
||
54 | * |
||
55 | * @param string $contents |
||
56 | * |
||
57 | * @return string |
||
58 | */ |
||
59 | 4 | public function trailingWhitespaces($contents) |
|
63 | |||
64 | /** |
||
65 | * Loop over the minification rules as long as changes in output occur. |
||
66 | * |
||
67 | * @param string $contents |
||
68 | * |
||
69 | * @return string |
||
70 | */ |
||
71 | 7 | public function runMinificationRules($contents) |
|
80 | |||
81 | /** |
||
82 | * Remove all spaces around placeholders. |
||
83 | * |
||
84 | * @param string $contents |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | 4 | public function removeSpacesAroundPlaceholders($contents) |
|
92 | |||
93 | /** |
||
94 | * Old browsers, firewalls and more can't handle to long lines. |
||
95 | * Therefore add a linebreak after specified character length. |
||
96 | * |
||
97 | * @param int $maxHtmlLineLength |
||
98 | * @param string $contents |
||
99 | * |
||
100 | * @return string |
||
101 | */ |
||
102 | 4 | public function maxHtmlLineLength($contents, $maxHtmlLineLength) |
|
103 | { |
||
104 | 4 | if (strlen($contents) <= $maxHtmlLineLength) { |
|
105 | 3 | return $contents; |
|
106 | } |
||
107 | |||
108 | 1 | $result = ''; |
|
109 | 1 | $splits = str_split($contents, $maxHtmlLineLength); |
|
110 | 1 | foreach ($splits as $split) { |
|
111 | 1 | $pos = strrpos($split, '><'); |
|
112 | 1 | if ($pos === false) { |
|
113 | 1 | $result .= $split; |
|
114 | } else { |
||
115 | 1 | $result .= substr_replace($split, PHP_EOL, $pos + 1, 0); |
|
116 | } |
||
117 | } |
||
118 | |||
119 | 1 | return $result; |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * Indicates if minification rules depends on command options. |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | 3 | public function provides() |
|
131 | } |
||
132 |