@@ -16,513 +16,513 @@ |
||
16 | 16 | class CleanHtmlService implements SingletonInterface |
17 | 17 | { |
18 | 18 | |
19 | - /** |
|
20 | - * Enable Debug comment in footer |
|
21 | - * |
|
22 | - * @var boolean |
|
23 | - */ |
|
24 | - protected $debugComment = false; |
|
25 | - |
|
26 | - /** |
|
27 | - * Format Type |
|
28 | - * |
|
29 | - * @var integer |
|
30 | - */ |
|
31 | - protected $formatType = 0; |
|
32 | - |
|
33 | - /** |
|
34 | - * Tab character |
|
35 | - * |
|
36 | - * @var string |
|
37 | - */ |
|
38 | - protected $tab = "\t"; |
|
39 | - |
|
40 | - /** |
|
41 | - * Newline character |
|
42 | - * |
|
43 | - * @var string |
|
44 | - */ |
|
45 | - protected $newline = "\n"; |
|
46 | - |
|
47 | - /** |
|
48 | - * Configured extra header comment |
|
49 | - * |
|
50 | - * @var string |
|
51 | - */ |
|
52 | - protected $headerComment = ''; |
|
53 | - |
|
54 | - /** |
|
55 | - * Empty space char |
|
56 | - * @var string |
|
57 | - */ |
|
58 | - protected $emptySpaceChar = ' '; |
|
59 | - |
|
60 | - /** |
|
61 | - * Set variables based on given config |
|
62 | - * |
|
63 | - * @param array $config |
|
64 | - * |
|
65 | - * @return void |
|
66 | - */ |
|
67 | - public function setVariables(array $config) |
|
68 | - { |
|
69 | - switch (TYPO3_OS) { // set newline |
|
70 | - case 'WIN': |
|
71 | - $this->newline = "\r\n"; |
|
72 | - break; |
|
73 | - default: |
|
74 | - $this->newline = "\n"; |
|
75 | - } |
|
76 | - |
|
77 | - if (!empty($config)) { |
|
78 | - if ($config['formatHtml'] && is_numeric($config['formatHtml'])) { |
|
79 | - $this->formatType = (int)$config['formatHtml']; |
|
80 | - } |
|
81 | - |
|
82 | - if ($config['formatHtml.']['tabSize'] && is_numeric($config['formatHtml.']['tabSize'])) { |
|
83 | - $this->tab = str_pad('', $config['formatHtml.']['tabSize'], ' '); |
|
84 | - } |
|
85 | - |
|
86 | - if (isset($config['formatHtml.']['debugComment'])) { |
|
87 | - $this->debugComment = (bool)$config['formatHtml.']['debugComment']; |
|
88 | - } |
|
89 | - |
|
90 | - if (isset($config['headerComment'])) { |
|
91 | - $this->headerComment = $config['headerComment']; |
|
92 | - } |
|
93 | - |
|
94 | - if (isset($config['dropEmptySpaceChar']) && (bool)$config['dropEmptySpaceChar']) { |
|
95 | - $this->emptySpaceChar = ''; |
|
96 | - } |
|
97 | - } |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Clean given HTML with formatter |
|
102 | - * |
|
103 | - * @param string $html |
|
104 | - * @param array $config |
|
105 | - * |
|
106 | - * @return void |
|
107 | - */ |
|
108 | - public function clean(&$html, $config = []) |
|
109 | - { |
|
110 | - if (!empty($config)) { |
|
111 | - if ((bool)$config['enabled'] === false) { |
|
112 | - return; |
|
113 | - } |
|
114 | - |
|
115 | - $this->setVariables($config); |
|
116 | - } |
|
117 | - |
|
118 | - $manipulations = []; |
|
119 | - |
|
120 | - if (isset($config['removeGenerator']) && (bool)$config['removeGenerator']) { |
|
121 | - $manipulations['removeGenerator'] = GeneralUtility::makeInstance(RemoveGenerator::class); |
|
122 | - } |
|
123 | - |
|
124 | - if (isset($config['removeComments']) && (bool)$config['removeComments']) { |
|
125 | - $manipulations['removeComments'] = GeneralUtility::makeInstance(RemoveComments::class); |
|
126 | - } |
|
127 | - |
|
128 | - if (isset($config['removeBlurScript']) && (bool)$config['removeBlurScript']) { |
|
129 | - $manipulations['removeBlurScript'] = GeneralUtility::makeInstance(RemoveBlurScript::class); |
|
130 | - } |
|
131 | - |
|
132 | - if (!empty($this->headerComment)) { |
|
133 | - $this->includeHeaderComment($html); |
|
134 | - } |
|
135 | - |
|
136 | - foreach ($manipulations as $key => $manipulation) { |
|
137 | - /** @var ManipulationInterface $manipulation */ |
|
138 | - $configuration = isset($config[$key . '.']) && is_array($config[$key . '.']) ? $config[$key . '.'] : []; |
|
139 | - $html = $manipulation->manipulate($html, $configuration); |
|
140 | - } |
|
141 | - |
|
142 | - if ($this->formatType > 0) { |
|
143 | - $this->formatHtml($html); |
|
144 | - } |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * Formats the (X)HTML code: |
|
149 | - * - taps according to the hirarchy of the tags |
|
150 | - * - removes empty spaces between tags |
|
151 | - * - removes linebreaks within tags (spares where necessary: pre, textarea, comments, ..) |
|
152 | - * choose from five options: |
|
153 | - * 0 => off |
|
154 | - * 1 => no line break at all (code in one line) |
|
155 | - * 2 => minimalistic line breaks (structure defining box-elements) |
|
156 | - * 3 => aesthetic line breaks (important box-elements) |
|
157 | - * 4 => logic line breaks (all box-elements) |
|
158 | - * 5 => max line breaks (all elements) |
|
159 | - * |
|
160 | - * @param string $html |
|
161 | - * |
|
162 | - * @return void |
|
163 | - */ |
|
164 | - protected function formatHtml(&$html) |
|
165 | - { |
|
166 | - // Save original formated comments, pre, textarea, styles and java-scripts & replace them with markers |
|
167 | - preg_match_all( |
|
168 | - '/(?s)((<!--.*?-->)|(<[ \n\r]*pre[^>]*>.*?<[ \n\r]*\/pre[^>]*>)|(<[ \n\r]*textarea[^>]*>.*?<[ \n\r]*\/textarea[^>]*>)|(<[ \n\r]*style[^>]*>.*?<[ \n\r]*\/style[^>]*>)|(<[ \n\r]*script[^>]*>.*?<[ \n\r]*\/script[^>]*>))/im', |
|
169 | - $html, |
|
170 | - $matches |
|
171 | - ); |
|
172 | - $noFormat = $matches[0]; // do not format these block elements |
|
173 | - for ($i = 0; $i < count($noFormat); $i++) { |
|
174 | - $html = str_replace($noFormat[$i], "\n<!-- ELEMENT $i -->", $html); |
|
175 | - } |
|
176 | - |
|
177 | - // define box elements for formatting |
|
178 | - $trueBoxElements = 'address|blockquote|center|dir|div|dl|fieldset|form|h1|h2|h3|h4|h5|h6|hr|isindex|menu|noframes|noscript|ol|p|pre|table|ul|article|aside|details|figcaption|figure|footer|header|hgroup|menu|nav|section'; |
|
179 | - $functionalBoxElements = 'dd|dt|frameset|li|tbody|td|tfoot|th|thead|tr|colgroup'; |
|
180 | - $usableBoxElements = 'applet|button|del|iframe|ins|map|object|script'; |
|
181 | - $imagineBoxElements = 'html|body|head|meta|title|link|script|base|!--'; |
|
182 | - $allBoxLikeElements = '(?>' . $trueBoxElements . '|' . $functionalBoxElements . '|' . $usableBoxElements . '|' . $imagineBoxElements . ')'; |
|
183 | - $esteticBoxLikeElements = '(?>html|head|body|meta name|title|div|table|h1|h2|h3|h4|h5|h6|p|form|pre|center|!--)'; |
|
184 | - $structureBoxLikeElements = '(?>html|head|body|div|!--)'; |
|
185 | - |
|
186 | - // split html into it's elements |
|
187 | - $htmlArrayTemp = preg_split( |
|
188 | - '/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/', |
|
189 | - $html, |
|
190 | - -1, |
|
191 | - PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY |
|
192 | - ); |
|
193 | - // remove empty lines |
|
194 | - $htmlArray = ['']; |
|
195 | - $z = 1; |
|
196 | - for ($x = 0; $x < count($htmlArrayTemp); $x++) { |
|
197 | - $t = trim($htmlArrayTemp[$x]); |
|
198 | - if ($t !== '') { |
|
199 | - $htmlArray[$z] = $htmlArrayTemp[$x]; |
|
200 | - $z++; |
|
201 | - } else { |
|
202 | - $htmlArray[$z] = $this->emptySpaceChar; |
|
203 | - $z++; |
|
204 | - } |
|
205 | - } |
|
206 | - |
|
207 | - // rebuild html |
|
208 | - $html = ''; |
|
209 | - $tabs = 0; |
|
210 | - for ($x = 0; $x < count($htmlArray); $x++) { |
|
211 | - // check if the element should stand in a new line |
|
212 | - $newline = false; |
|
213 | - if (substr($htmlArray[$x - 1], 0, 5) == '<?xml') { |
|
214 | - $newline = true; |
|
215 | - } elseif ($this->formatType == 2 && ( // minimalistic line break |
|
216 | - # this element has a line break before itself |
|
217 | - preg_match( |
|
218 | - '/<' . $structureBoxLikeElements . '(.*)>/Usi', |
|
219 | - $htmlArray[$x] |
|
220 | - ) || preg_match( |
|
221 | - '/<' . $structureBoxLikeElements . '(.*) \/>/Usi', |
|
222 | - $htmlArray[$x] |
|
223 | - ) || # one element before is a element that has a line break after |
|
224 | - preg_match( |
|
225 | - '/<\/' . $structureBoxLikeElements . '(.*)>/Usi', |
|
226 | - $htmlArray[$x - 1] |
|
227 | - ) || substr( |
|
228 | - $htmlArray[$x - 1], |
|
229 | - 0, |
|
230 | - 4 |
|
231 | - ) == '<!--' || preg_match('/<' . $structureBoxLikeElements . '(.*) \/>/Usi', $htmlArray[$x - 1])) |
|
232 | - ) { |
|
233 | - $newline = true; |
|
234 | - } elseif ($this->formatType == 3 && ( // aestetic line break |
|
235 | - # this element has a line break before itself |
|
236 | - preg_match( |
|
237 | - '/<' . $esteticBoxLikeElements . '(.*)>/Usi', |
|
238 | - $htmlArray[$x] |
|
239 | - ) || preg_match( |
|
240 | - '/<' . $esteticBoxLikeElements . '(.*) \/>/Usi', |
|
241 | - $htmlArray[$x] |
|
242 | - ) || # one element before is a element that has a line break after |
|
243 | - preg_match('/<\/' . $esteticBoxLikeElements . '(.*)>/Usi', $htmlArray[$x - 1]) || substr( |
|
244 | - $htmlArray[$x - 1], |
|
245 | - 0, |
|
246 | - 4 |
|
247 | - ) == '<!--' || preg_match('/<' . $esteticBoxLikeElements . '(.*) \/>/Usi', $htmlArray[$x - 1])) |
|
248 | - ) { |
|
249 | - $newline = true; |
|
250 | - } elseif ($this->formatType >= 4 && ( // logical line break |
|
251 | - # this element has a line break before itself |
|
252 | - preg_match( |
|
253 | - '/<' . $allBoxLikeElements . '(.*)>/Usi', |
|
254 | - $htmlArray[$x] |
|
255 | - ) || preg_match( |
|
256 | - '/<' . $allBoxLikeElements . '(.*) \/>/Usi', |
|
257 | - $htmlArray[$x] |
|
258 | - ) || # one element before is a element that has a line break after |
|
259 | - preg_match('/<\/' . $allBoxLikeElements . '(.*)>/Usi', $htmlArray[$x - 1]) || substr( |
|
260 | - $htmlArray[$x - 1], |
|
261 | - 0, |
|
262 | - 4 |
|
263 | - ) == '<!--' || preg_match('/<' . $allBoxLikeElements . '(.*) \/>/Usi', $htmlArray[$x - 1])) |
|
264 | - ) { |
|
265 | - $newline = true; |
|
266 | - } |
|
267 | - |
|
268 | - // count down a tab |
|
269 | - if (substr($htmlArray[$x], 0, 2) == '</') { |
|
270 | - $tabs--; |
|
271 | - } |
|
272 | - |
|
273 | - // add tabs and line breaks in front of the current tag |
|
274 | - if ($newline) { |
|
275 | - $html .= $this->newline; |
|
276 | - for ($y = 0; $y < $tabs; $y++) { |
|
277 | - $html .= $this->tab; |
|
278 | - } |
|
279 | - } |
|
280 | - |
|
281 | - // remove white spaces and line breaks and add current tag to the html-string |
|
282 | - if (substr($htmlArray[$x - 1], 0, 4) == '<pre' // remove white space after line ending in PRE / TEXTAREA / comment |
|
283 | - || substr($htmlArray[$x - 1], 0, 9) == '<textarea' || substr($htmlArray[$x - 1], 0, 4) == '<!--' |
|
284 | - ) { |
|
285 | - $html .= $this->rTrimLines($htmlArray[$x]); |
|
286 | - } elseif (substr($htmlArray[$x], 0, 9) == '<![CDATA[' // remove multiple white space in CDATA / XML |
|
287 | - || substr($htmlArray[$x], 0, 5) == '<?xml' |
|
288 | - ) { |
|
289 | - $html .= $this->killWhiteSpace($htmlArray[$x]); |
|
290 | - } else { // remove all line breaks |
|
291 | - $html .= $this->killLineBreaks($htmlArray[$x]); |
|
292 | - } |
|
293 | - |
|
294 | - // count up a tab |
|
295 | - if (substr($htmlArray[$x], 0, 1) == '<' && substr($htmlArray[$x], 1, 1) != '/') { |
|
296 | - if (substr($htmlArray[$x], 1, 1) != ' ' && substr($htmlArray[$x], 1, 3) != 'img' && substr( |
|
297 | - $htmlArray[$x], |
|
298 | - 1, |
|
299 | - 2 |
|
300 | - ) != 'br' && substr($htmlArray[$x], 1, 2) != 'hr' && substr( |
|
301 | - $htmlArray[$x], |
|
302 | - 1, |
|
303 | - 5 |
|
304 | - ) != 'input' && substr($htmlArray[$x], 1, 4) != 'link' && substr( |
|
305 | - $htmlArray[$x], |
|
306 | - 1, |
|
307 | - 4 |
|
308 | - ) != 'meta' && substr($htmlArray[$x], 1, 4) != 'col ' && substr( |
|
309 | - $htmlArray[$x], |
|
310 | - 1, |
|
311 | - 5 |
|
312 | - ) != 'frame' && substr($htmlArray[$x], 1, 7) != 'isindex' && substr( |
|
313 | - $htmlArray[$x], |
|
314 | - 1, |
|
315 | - 5 |
|
316 | - ) != 'param' && substr($htmlArray[$x], 1, 4) != 'area' && substr( |
|
317 | - $htmlArray[$x], |
|
318 | - 1, |
|
319 | - 4 |
|
320 | - ) != 'base' && substr($htmlArray[$x], 0, 2) != '<!' && substr($htmlArray[$x], 0, 5) != '<?xml' |
|
321 | - ) { |
|
322 | - $tabs++; |
|
323 | - } |
|
324 | - } |
|
325 | - } |
|
326 | - |
|
327 | - // Remove empty lines |
|
328 | - if ($this->formatType > 1) { |
|
329 | - $this->removeEmptyLines($html); |
|
330 | - } |
|
331 | - |
|
332 | - // Restore saved comments, styles and java-scripts |
|
333 | - for ($i = 0; $i < count($noFormat); $i++) { |
|
334 | - $noFormat[$i] = $this->rTrimLines($noFormat[$i]); // remove white space after line ending |
|
335 | - $html = str_replace("<!-- ELEMENT $i -->", $noFormat[$i], $html); |
|
336 | - } |
|
337 | - |
|
338 | - // include debug comment at the end |
|
339 | - if ($tabs != 0 && $this->debugComment === true) { |
|
340 | - $html .= '<!--' . $tabs . " open elements found-->\r\n"; |
|
341 | - } |
|
342 | - } |
|
343 | - |
|
344 | - /** |
|
345 | - * Remove ALL line breaks and multiple white space |
|
346 | - * |
|
347 | - * @param string $html |
|
348 | - * |
|
349 | - * @return string |
|
350 | - */ |
|
351 | - protected function killLineBreaks($html) |
|
352 | - { |
|
353 | - $html = $this->convNlOs($html); |
|
354 | - $html = str_replace($this->newline, "", $html); |
|
355 | - $html = preg_replace('/\s\s+/u', ' ', $html); |
|
356 | - return $html; |
|
357 | - } |
|
358 | - |
|
359 | - /** |
|
360 | - * Remove multiple white space, keeps line breaks |
|
361 | - * |
|
362 | - * @param string $html |
|
363 | - * |
|
364 | - * @return string |
|
365 | - */ |
|
366 | - protected function killWhiteSpace($html) |
|
367 | - { |
|
368 | - $html = $this->convNlOs($html); |
|
369 | - $temp = explode($this->newline, $html); |
|
370 | - for ($i = 0; $i < count($temp); $i++) { |
|
371 | - if (!trim($temp[$i])) { |
|
372 | - unset($temp[$i]); |
|
373 | - } else { |
|
374 | - $temp[$i] = trim($temp[$i]); |
|
375 | - $temp[$i] = preg_replace('/\s\s+/', ' ', $temp[$i]); |
|
376 | - } |
|
377 | - } |
|
378 | - $html = implode($this->newline, $temp); |
|
379 | - return $html; |
|
380 | - } |
|
381 | - |
|
382 | - /** |
|
383 | - * Remove white space at the end of lines, keeps other white space and line breaks |
|
384 | - * |
|
385 | - * @param string $html |
|
386 | - * |
|
387 | - * @return string |
|
388 | - */ |
|
389 | - protected function rTrimLines($html) |
|
390 | - { |
|
391 | - $html = $this->convNlOs($html); |
|
392 | - $temp = explode($this->newline, $html); |
|
393 | - for ($i = 0; $i < count($temp); $i++) { |
|
394 | - $temp[$i] = rtrim($temp[$i]); |
|
395 | - } |
|
396 | - $html = implode($this->newline, $temp); |
|
397 | - return $html; |
|
398 | - } |
|
399 | - |
|
400 | - /** |
|
401 | - * Convert newlines according to the current OS |
|
402 | - * |
|
403 | - * @param string $html |
|
404 | - * |
|
405 | - * @return string |
|
406 | - */ |
|
407 | - protected function convNlOs($html) |
|
408 | - { |
|
409 | - $html = preg_replace("(\r\n|\n|\r)", $this->newline, $html); |
|
410 | - return $html; |
|
411 | - } |
|
412 | - |
|
413 | - /** |
|
414 | - * Remove tabs and empty spaces before and after lines, transforms linebreaks system conform |
|
415 | - * |
|
416 | - * @param string $html Html-Code |
|
417 | - * |
|
418 | - * @return void |
|
419 | - */ |
|
420 | - protected function trimLines(&$html) |
|
421 | - { |
|
422 | - $html = str_replace("\t", "", $html); |
|
423 | - // convert newlines according to the current OS |
|
424 | - if (TYPO3_OS == "WIN") { |
|
425 | - $html = str_replace("\n", "\r\n", $html); |
|
426 | - } else { |
|
427 | - $html = str_replace("\r\n", "\n", $html); |
|
428 | - } |
|
429 | - $temp = explode($this->newline, $html); |
|
430 | - $temp = array_map('trim', $temp); |
|
431 | - $html = implode($this->newline, $temp); |
|
432 | - unset($temp); |
|
433 | - } |
|
434 | - |
|
435 | - /** |
|
436 | - * Remove empty lines |
|
437 | - * |
|
438 | - * @param string $html |
|
439 | - * |
|
440 | - * @return void |
|
441 | - */ |
|
442 | - protected function removeEmptyLines(&$html) |
|
443 | - { |
|
444 | - $temp = explode($this->newline, $html); |
|
445 | - $result = []; |
|
446 | - for ($i = 0; $i < count($temp); ++$i) { |
|
447 | - if ("" == trim($temp[$i])) { |
|
448 | - continue; |
|
449 | - } |
|
450 | - $result[] = $temp[$i]; |
|
451 | - } |
|
452 | - $html = implode($this->newline, $result); |
|
453 | - } |
|
454 | - |
|
455 | - /** |
|
456 | - * Remove new lines where unnecessary |
|
457 | - * spares line breaks within: pre, textarea, ... |
|
458 | - * |
|
459 | - * @param string $html |
|
460 | - * |
|
461 | - * @return void |
|
462 | - */ |
|
463 | - protected function removeNewLines(&$html) |
|
464 | - { |
|
465 | - $splitArray = [ |
|
466 | - 'textarea', |
|
467 | - 'pre' |
|
468 | - ]; // eventuell auch: span, script, style |
|
469 | - $peaces = preg_split('#(<(' . implode('|', $splitArray) . ').*>.*</\2>)#Uis', $html, -1, PREG_SPLIT_DELIM_CAPTURE); |
|
470 | - $html = ""; |
|
471 | - for ($i = 0; $i < count($peaces); $i++) { |
|
472 | - if (($i + 1) % 3 == 0) { |
|
473 | - continue; |
|
474 | - } |
|
475 | - $html .= (($i - 1) % 3 != 0) ? $this->killLineBreaks($peaces[$i]) : $peaces[$i]; |
|
476 | - } |
|
477 | - } |
|
478 | - |
|
479 | - /** |
|
480 | - * Remove obsolete link schema |
|
481 | - * |
|
482 | - * @param string $html |
|
483 | - * |
|
484 | - * @return void |
|
485 | - */ |
|
486 | - protected function removeLinkSchema(&$html) |
|
487 | - { |
|
488 | - $html = preg_replace("/<link rel=\"?schema.dc\"?.+?>/is", "", $html); |
|
489 | - } |
|
490 | - |
|
491 | - /** |
|
492 | - * Remove empty alt tags |
|
493 | - * |
|
494 | - * @param string $html |
|
495 | - * |
|
496 | - * @return void |
|
497 | - */ |
|
498 | - protected function removeEmptyAltAtr(&$html) |
|
499 | - { |
|
500 | - $html = str_replace("alt=\"\"", "", $html); |
|
501 | - } |
|
502 | - |
|
503 | - /** |
|
504 | - * Remove broken links in <a> tags |
|
505 | - * |
|
506 | - * @param string $html |
|
507 | - * |
|
508 | - * @return void |
|
509 | - */ |
|
510 | - protected function removeRealUrlBrokenRootLink(&$html) |
|
511 | - { |
|
512 | - $html = str_replace('href=".html"', 'href=""', $html); |
|
513 | - } |
|
514 | - |
|
515 | - /** |
|
516 | - * Include configured header comment in HTML content block |
|
517 | - * |
|
518 | - * @param $html |
|
519 | - */ |
|
520 | - public function includeHeaderComment(&$html) |
|
521 | - { |
|
522 | - if (!empty($this->headerComment)) { |
|
523 | - $html = preg_replace_callback('/<meta http-equiv(.*)>/Usi', function ($matches) { |
|
524 | - return trim($matches[0] . $this->newline . $this->tab . $this->tab . '<!-- ' . $this->headerComment . '-->'); |
|
525 | - }, $html, 1); |
|
526 | - } |
|
527 | - } |
|
19 | + /** |
|
20 | + * Enable Debug comment in footer |
|
21 | + * |
|
22 | + * @var boolean |
|
23 | + */ |
|
24 | + protected $debugComment = false; |
|
25 | + |
|
26 | + /** |
|
27 | + * Format Type |
|
28 | + * |
|
29 | + * @var integer |
|
30 | + */ |
|
31 | + protected $formatType = 0; |
|
32 | + |
|
33 | + /** |
|
34 | + * Tab character |
|
35 | + * |
|
36 | + * @var string |
|
37 | + */ |
|
38 | + protected $tab = "\t"; |
|
39 | + |
|
40 | + /** |
|
41 | + * Newline character |
|
42 | + * |
|
43 | + * @var string |
|
44 | + */ |
|
45 | + protected $newline = "\n"; |
|
46 | + |
|
47 | + /** |
|
48 | + * Configured extra header comment |
|
49 | + * |
|
50 | + * @var string |
|
51 | + */ |
|
52 | + protected $headerComment = ''; |
|
53 | + |
|
54 | + /** |
|
55 | + * Empty space char |
|
56 | + * @var string |
|
57 | + */ |
|
58 | + protected $emptySpaceChar = ' '; |
|
59 | + |
|
60 | + /** |
|
61 | + * Set variables based on given config |
|
62 | + * |
|
63 | + * @param array $config |
|
64 | + * |
|
65 | + * @return void |
|
66 | + */ |
|
67 | + public function setVariables(array $config) |
|
68 | + { |
|
69 | + switch (TYPO3_OS) { // set newline |
|
70 | + case 'WIN': |
|
71 | + $this->newline = "\r\n"; |
|
72 | + break; |
|
73 | + default: |
|
74 | + $this->newline = "\n"; |
|
75 | + } |
|
76 | + |
|
77 | + if (!empty($config)) { |
|
78 | + if ($config['formatHtml'] && is_numeric($config['formatHtml'])) { |
|
79 | + $this->formatType = (int)$config['formatHtml']; |
|
80 | + } |
|
81 | + |
|
82 | + if ($config['formatHtml.']['tabSize'] && is_numeric($config['formatHtml.']['tabSize'])) { |
|
83 | + $this->tab = str_pad('', $config['formatHtml.']['tabSize'], ' '); |
|
84 | + } |
|
85 | + |
|
86 | + if (isset($config['formatHtml.']['debugComment'])) { |
|
87 | + $this->debugComment = (bool)$config['formatHtml.']['debugComment']; |
|
88 | + } |
|
89 | + |
|
90 | + if (isset($config['headerComment'])) { |
|
91 | + $this->headerComment = $config['headerComment']; |
|
92 | + } |
|
93 | + |
|
94 | + if (isset($config['dropEmptySpaceChar']) && (bool)$config['dropEmptySpaceChar']) { |
|
95 | + $this->emptySpaceChar = ''; |
|
96 | + } |
|
97 | + } |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Clean given HTML with formatter |
|
102 | + * |
|
103 | + * @param string $html |
|
104 | + * @param array $config |
|
105 | + * |
|
106 | + * @return void |
|
107 | + */ |
|
108 | + public function clean(&$html, $config = []) |
|
109 | + { |
|
110 | + if (!empty($config)) { |
|
111 | + if ((bool)$config['enabled'] === false) { |
|
112 | + return; |
|
113 | + } |
|
114 | + |
|
115 | + $this->setVariables($config); |
|
116 | + } |
|
117 | + |
|
118 | + $manipulations = []; |
|
119 | + |
|
120 | + if (isset($config['removeGenerator']) && (bool)$config['removeGenerator']) { |
|
121 | + $manipulations['removeGenerator'] = GeneralUtility::makeInstance(RemoveGenerator::class); |
|
122 | + } |
|
123 | + |
|
124 | + if (isset($config['removeComments']) && (bool)$config['removeComments']) { |
|
125 | + $manipulations['removeComments'] = GeneralUtility::makeInstance(RemoveComments::class); |
|
126 | + } |
|
127 | + |
|
128 | + if (isset($config['removeBlurScript']) && (bool)$config['removeBlurScript']) { |
|
129 | + $manipulations['removeBlurScript'] = GeneralUtility::makeInstance(RemoveBlurScript::class); |
|
130 | + } |
|
131 | + |
|
132 | + if (!empty($this->headerComment)) { |
|
133 | + $this->includeHeaderComment($html); |
|
134 | + } |
|
135 | + |
|
136 | + foreach ($manipulations as $key => $manipulation) { |
|
137 | + /** @var ManipulationInterface $manipulation */ |
|
138 | + $configuration = isset($config[$key . '.']) && is_array($config[$key . '.']) ? $config[$key . '.'] : []; |
|
139 | + $html = $manipulation->manipulate($html, $configuration); |
|
140 | + } |
|
141 | + |
|
142 | + if ($this->formatType > 0) { |
|
143 | + $this->formatHtml($html); |
|
144 | + } |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * Formats the (X)HTML code: |
|
149 | + * - taps according to the hirarchy of the tags |
|
150 | + * - removes empty spaces between tags |
|
151 | + * - removes linebreaks within tags (spares where necessary: pre, textarea, comments, ..) |
|
152 | + * choose from five options: |
|
153 | + * 0 => off |
|
154 | + * 1 => no line break at all (code in one line) |
|
155 | + * 2 => minimalistic line breaks (structure defining box-elements) |
|
156 | + * 3 => aesthetic line breaks (important box-elements) |
|
157 | + * 4 => logic line breaks (all box-elements) |
|
158 | + * 5 => max line breaks (all elements) |
|
159 | + * |
|
160 | + * @param string $html |
|
161 | + * |
|
162 | + * @return void |
|
163 | + */ |
|
164 | + protected function formatHtml(&$html) |
|
165 | + { |
|
166 | + // Save original formated comments, pre, textarea, styles and java-scripts & replace them with markers |
|
167 | + preg_match_all( |
|
168 | + '/(?s)((<!--.*?-->)|(<[ \n\r]*pre[^>]*>.*?<[ \n\r]*\/pre[^>]*>)|(<[ \n\r]*textarea[^>]*>.*?<[ \n\r]*\/textarea[^>]*>)|(<[ \n\r]*style[^>]*>.*?<[ \n\r]*\/style[^>]*>)|(<[ \n\r]*script[^>]*>.*?<[ \n\r]*\/script[^>]*>))/im', |
|
169 | + $html, |
|
170 | + $matches |
|
171 | + ); |
|
172 | + $noFormat = $matches[0]; // do not format these block elements |
|
173 | + for ($i = 0; $i < count($noFormat); $i++) { |
|
174 | + $html = str_replace($noFormat[$i], "\n<!-- ELEMENT $i -->", $html); |
|
175 | + } |
|
176 | + |
|
177 | + // define box elements for formatting |
|
178 | + $trueBoxElements = 'address|blockquote|center|dir|div|dl|fieldset|form|h1|h2|h3|h4|h5|h6|hr|isindex|menu|noframes|noscript|ol|p|pre|table|ul|article|aside|details|figcaption|figure|footer|header|hgroup|menu|nav|section'; |
|
179 | + $functionalBoxElements = 'dd|dt|frameset|li|tbody|td|tfoot|th|thead|tr|colgroup'; |
|
180 | + $usableBoxElements = 'applet|button|del|iframe|ins|map|object|script'; |
|
181 | + $imagineBoxElements = 'html|body|head|meta|title|link|script|base|!--'; |
|
182 | + $allBoxLikeElements = '(?>' . $trueBoxElements . '|' . $functionalBoxElements . '|' . $usableBoxElements . '|' . $imagineBoxElements . ')'; |
|
183 | + $esteticBoxLikeElements = '(?>html|head|body|meta name|title|div|table|h1|h2|h3|h4|h5|h6|p|form|pre|center|!--)'; |
|
184 | + $structureBoxLikeElements = '(?>html|head|body|div|!--)'; |
|
185 | + |
|
186 | + // split html into it's elements |
|
187 | + $htmlArrayTemp = preg_split( |
|
188 | + '/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/', |
|
189 | + $html, |
|
190 | + -1, |
|
191 | + PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY |
|
192 | + ); |
|
193 | + // remove empty lines |
|
194 | + $htmlArray = ['']; |
|
195 | + $z = 1; |
|
196 | + for ($x = 0; $x < count($htmlArrayTemp); $x++) { |
|
197 | + $t = trim($htmlArrayTemp[$x]); |
|
198 | + if ($t !== '') { |
|
199 | + $htmlArray[$z] = $htmlArrayTemp[$x]; |
|
200 | + $z++; |
|
201 | + } else { |
|
202 | + $htmlArray[$z] = $this->emptySpaceChar; |
|
203 | + $z++; |
|
204 | + } |
|
205 | + } |
|
206 | + |
|
207 | + // rebuild html |
|
208 | + $html = ''; |
|
209 | + $tabs = 0; |
|
210 | + for ($x = 0; $x < count($htmlArray); $x++) { |
|
211 | + // check if the element should stand in a new line |
|
212 | + $newline = false; |
|
213 | + if (substr($htmlArray[$x - 1], 0, 5) == '<?xml') { |
|
214 | + $newline = true; |
|
215 | + } elseif ($this->formatType == 2 && ( // minimalistic line break |
|
216 | + # this element has a line break before itself |
|
217 | + preg_match( |
|
218 | + '/<' . $structureBoxLikeElements . '(.*)>/Usi', |
|
219 | + $htmlArray[$x] |
|
220 | + ) || preg_match( |
|
221 | + '/<' . $structureBoxLikeElements . '(.*) \/>/Usi', |
|
222 | + $htmlArray[$x] |
|
223 | + ) || # one element before is a element that has a line break after |
|
224 | + preg_match( |
|
225 | + '/<\/' . $structureBoxLikeElements . '(.*)>/Usi', |
|
226 | + $htmlArray[$x - 1] |
|
227 | + ) || substr( |
|
228 | + $htmlArray[$x - 1], |
|
229 | + 0, |
|
230 | + 4 |
|
231 | + ) == '<!--' || preg_match('/<' . $structureBoxLikeElements . '(.*) \/>/Usi', $htmlArray[$x - 1])) |
|
232 | + ) { |
|
233 | + $newline = true; |
|
234 | + } elseif ($this->formatType == 3 && ( // aestetic line break |
|
235 | + # this element has a line break before itself |
|
236 | + preg_match( |
|
237 | + '/<' . $esteticBoxLikeElements . '(.*)>/Usi', |
|
238 | + $htmlArray[$x] |
|
239 | + ) || preg_match( |
|
240 | + '/<' . $esteticBoxLikeElements . '(.*) \/>/Usi', |
|
241 | + $htmlArray[$x] |
|
242 | + ) || # one element before is a element that has a line break after |
|
243 | + preg_match('/<\/' . $esteticBoxLikeElements . '(.*)>/Usi', $htmlArray[$x - 1]) || substr( |
|
244 | + $htmlArray[$x - 1], |
|
245 | + 0, |
|
246 | + 4 |
|
247 | + ) == '<!--' || preg_match('/<' . $esteticBoxLikeElements . '(.*) \/>/Usi', $htmlArray[$x - 1])) |
|
248 | + ) { |
|
249 | + $newline = true; |
|
250 | + } elseif ($this->formatType >= 4 && ( // logical line break |
|
251 | + # this element has a line break before itself |
|
252 | + preg_match( |
|
253 | + '/<' . $allBoxLikeElements . '(.*)>/Usi', |
|
254 | + $htmlArray[$x] |
|
255 | + ) || preg_match( |
|
256 | + '/<' . $allBoxLikeElements . '(.*) \/>/Usi', |
|
257 | + $htmlArray[$x] |
|
258 | + ) || # one element before is a element that has a line break after |
|
259 | + preg_match('/<\/' . $allBoxLikeElements . '(.*)>/Usi', $htmlArray[$x - 1]) || substr( |
|
260 | + $htmlArray[$x - 1], |
|
261 | + 0, |
|
262 | + 4 |
|
263 | + ) == '<!--' || preg_match('/<' . $allBoxLikeElements . '(.*) \/>/Usi', $htmlArray[$x - 1])) |
|
264 | + ) { |
|
265 | + $newline = true; |
|
266 | + } |
|
267 | + |
|
268 | + // count down a tab |
|
269 | + if (substr($htmlArray[$x], 0, 2) == '</') { |
|
270 | + $tabs--; |
|
271 | + } |
|
272 | + |
|
273 | + // add tabs and line breaks in front of the current tag |
|
274 | + if ($newline) { |
|
275 | + $html .= $this->newline; |
|
276 | + for ($y = 0; $y < $tabs; $y++) { |
|
277 | + $html .= $this->tab; |
|
278 | + } |
|
279 | + } |
|
280 | + |
|
281 | + // remove white spaces and line breaks and add current tag to the html-string |
|
282 | + if (substr($htmlArray[$x - 1], 0, 4) == '<pre' // remove white space after line ending in PRE / TEXTAREA / comment |
|
283 | + || substr($htmlArray[$x - 1], 0, 9) == '<textarea' || substr($htmlArray[$x - 1], 0, 4) == '<!--' |
|
284 | + ) { |
|
285 | + $html .= $this->rTrimLines($htmlArray[$x]); |
|
286 | + } elseif (substr($htmlArray[$x], 0, 9) == '<![CDATA[' // remove multiple white space in CDATA / XML |
|
287 | + || substr($htmlArray[$x], 0, 5) == '<?xml' |
|
288 | + ) { |
|
289 | + $html .= $this->killWhiteSpace($htmlArray[$x]); |
|
290 | + } else { // remove all line breaks |
|
291 | + $html .= $this->killLineBreaks($htmlArray[$x]); |
|
292 | + } |
|
293 | + |
|
294 | + // count up a tab |
|
295 | + if (substr($htmlArray[$x], 0, 1) == '<' && substr($htmlArray[$x], 1, 1) != '/') { |
|
296 | + if (substr($htmlArray[$x], 1, 1) != ' ' && substr($htmlArray[$x], 1, 3) != 'img' && substr( |
|
297 | + $htmlArray[$x], |
|
298 | + 1, |
|
299 | + 2 |
|
300 | + ) != 'br' && substr($htmlArray[$x], 1, 2) != 'hr' && substr( |
|
301 | + $htmlArray[$x], |
|
302 | + 1, |
|
303 | + 5 |
|
304 | + ) != 'input' && substr($htmlArray[$x], 1, 4) != 'link' && substr( |
|
305 | + $htmlArray[$x], |
|
306 | + 1, |
|
307 | + 4 |
|
308 | + ) != 'meta' && substr($htmlArray[$x], 1, 4) != 'col ' && substr( |
|
309 | + $htmlArray[$x], |
|
310 | + 1, |
|
311 | + 5 |
|
312 | + ) != 'frame' && substr($htmlArray[$x], 1, 7) != 'isindex' && substr( |
|
313 | + $htmlArray[$x], |
|
314 | + 1, |
|
315 | + 5 |
|
316 | + ) != 'param' && substr($htmlArray[$x], 1, 4) != 'area' && substr( |
|
317 | + $htmlArray[$x], |
|
318 | + 1, |
|
319 | + 4 |
|
320 | + ) != 'base' && substr($htmlArray[$x], 0, 2) != '<!' && substr($htmlArray[$x], 0, 5) != '<?xml' |
|
321 | + ) { |
|
322 | + $tabs++; |
|
323 | + } |
|
324 | + } |
|
325 | + } |
|
326 | + |
|
327 | + // Remove empty lines |
|
328 | + if ($this->formatType > 1) { |
|
329 | + $this->removeEmptyLines($html); |
|
330 | + } |
|
331 | + |
|
332 | + // Restore saved comments, styles and java-scripts |
|
333 | + for ($i = 0; $i < count($noFormat); $i++) { |
|
334 | + $noFormat[$i] = $this->rTrimLines($noFormat[$i]); // remove white space after line ending |
|
335 | + $html = str_replace("<!-- ELEMENT $i -->", $noFormat[$i], $html); |
|
336 | + } |
|
337 | + |
|
338 | + // include debug comment at the end |
|
339 | + if ($tabs != 0 && $this->debugComment === true) { |
|
340 | + $html .= '<!--' . $tabs . " open elements found-->\r\n"; |
|
341 | + } |
|
342 | + } |
|
343 | + |
|
344 | + /** |
|
345 | + * Remove ALL line breaks and multiple white space |
|
346 | + * |
|
347 | + * @param string $html |
|
348 | + * |
|
349 | + * @return string |
|
350 | + */ |
|
351 | + protected function killLineBreaks($html) |
|
352 | + { |
|
353 | + $html = $this->convNlOs($html); |
|
354 | + $html = str_replace($this->newline, "", $html); |
|
355 | + $html = preg_replace('/\s\s+/u', ' ', $html); |
|
356 | + return $html; |
|
357 | + } |
|
358 | + |
|
359 | + /** |
|
360 | + * Remove multiple white space, keeps line breaks |
|
361 | + * |
|
362 | + * @param string $html |
|
363 | + * |
|
364 | + * @return string |
|
365 | + */ |
|
366 | + protected function killWhiteSpace($html) |
|
367 | + { |
|
368 | + $html = $this->convNlOs($html); |
|
369 | + $temp = explode($this->newline, $html); |
|
370 | + for ($i = 0; $i < count($temp); $i++) { |
|
371 | + if (!trim($temp[$i])) { |
|
372 | + unset($temp[$i]); |
|
373 | + } else { |
|
374 | + $temp[$i] = trim($temp[$i]); |
|
375 | + $temp[$i] = preg_replace('/\s\s+/', ' ', $temp[$i]); |
|
376 | + } |
|
377 | + } |
|
378 | + $html = implode($this->newline, $temp); |
|
379 | + return $html; |
|
380 | + } |
|
381 | + |
|
382 | + /** |
|
383 | + * Remove white space at the end of lines, keeps other white space and line breaks |
|
384 | + * |
|
385 | + * @param string $html |
|
386 | + * |
|
387 | + * @return string |
|
388 | + */ |
|
389 | + protected function rTrimLines($html) |
|
390 | + { |
|
391 | + $html = $this->convNlOs($html); |
|
392 | + $temp = explode($this->newline, $html); |
|
393 | + for ($i = 0; $i < count($temp); $i++) { |
|
394 | + $temp[$i] = rtrim($temp[$i]); |
|
395 | + } |
|
396 | + $html = implode($this->newline, $temp); |
|
397 | + return $html; |
|
398 | + } |
|
399 | + |
|
400 | + /** |
|
401 | + * Convert newlines according to the current OS |
|
402 | + * |
|
403 | + * @param string $html |
|
404 | + * |
|
405 | + * @return string |
|
406 | + */ |
|
407 | + protected function convNlOs($html) |
|
408 | + { |
|
409 | + $html = preg_replace("(\r\n|\n|\r)", $this->newline, $html); |
|
410 | + return $html; |
|
411 | + } |
|
412 | + |
|
413 | + /** |
|
414 | + * Remove tabs and empty spaces before and after lines, transforms linebreaks system conform |
|
415 | + * |
|
416 | + * @param string $html Html-Code |
|
417 | + * |
|
418 | + * @return void |
|
419 | + */ |
|
420 | + protected function trimLines(&$html) |
|
421 | + { |
|
422 | + $html = str_replace("\t", "", $html); |
|
423 | + // convert newlines according to the current OS |
|
424 | + if (TYPO3_OS == "WIN") { |
|
425 | + $html = str_replace("\n", "\r\n", $html); |
|
426 | + } else { |
|
427 | + $html = str_replace("\r\n", "\n", $html); |
|
428 | + } |
|
429 | + $temp = explode($this->newline, $html); |
|
430 | + $temp = array_map('trim', $temp); |
|
431 | + $html = implode($this->newline, $temp); |
|
432 | + unset($temp); |
|
433 | + } |
|
434 | + |
|
435 | + /** |
|
436 | + * Remove empty lines |
|
437 | + * |
|
438 | + * @param string $html |
|
439 | + * |
|
440 | + * @return void |
|
441 | + */ |
|
442 | + protected function removeEmptyLines(&$html) |
|
443 | + { |
|
444 | + $temp = explode($this->newline, $html); |
|
445 | + $result = []; |
|
446 | + for ($i = 0; $i < count($temp); ++$i) { |
|
447 | + if ("" == trim($temp[$i])) { |
|
448 | + continue; |
|
449 | + } |
|
450 | + $result[] = $temp[$i]; |
|
451 | + } |
|
452 | + $html = implode($this->newline, $result); |
|
453 | + } |
|
454 | + |
|
455 | + /** |
|
456 | + * Remove new lines where unnecessary |
|
457 | + * spares line breaks within: pre, textarea, ... |
|
458 | + * |
|
459 | + * @param string $html |
|
460 | + * |
|
461 | + * @return void |
|
462 | + */ |
|
463 | + protected function removeNewLines(&$html) |
|
464 | + { |
|
465 | + $splitArray = [ |
|
466 | + 'textarea', |
|
467 | + 'pre' |
|
468 | + ]; // eventuell auch: span, script, style |
|
469 | + $peaces = preg_split('#(<(' . implode('|', $splitArray) . ').*>.*</\2>)#Uis', $html, -1, PREG_SPLIT_DELIM_CAPTURE); |
|
470 | + $html = ""; |
|
471 | + for ($i = 0; $i < count($peaces); $i++) { |
|
472 | + if (($i + 1) % 3 == 0) { |
|
473 | + continue; |
|
474 | + } |
|
475 | + $html .= (($i - 1) % 3 != 0) ? $this->killLineBreaks($peaces[$i]) : $peaces[$i]; |
|
476 | + } |
|
477 | + } |
|
478 | + |
|
479 | + /** |
|
480 | + * Remove obsolete link schema |
|
481 | + * |
|
482 | + * @param string $html |
|
483 | + * |
|
484 | + * @return void |
|
485 | + */ |
|
486 | + protected function removeLinkSchema(&$html) |
|
487 | + { |
|
488 | + $html = preg_replace("/<link rel=\"?schema.dc\"?.+?>/is", "", $html); |
|
489 | + } |
|
490 | + |
|
491 | + /** |
|
492 | + * Remove empty alt tags |
|
493 | + * |
|
494 | + * @param string $html |
|
495 | + * |
|
496 | + * @return void |
|
497 | + */ |
|
498 | + protected function removeEmptyAltAtr(&$html) |
|
499 | + { |
|
500 | + $html = str_replace("alt=\"\"", "", $html); |
|
501 | + } |
|
502 | + |
|
503 | + /** |
|
504 | + * Remove broken links in <a> tags |
|
505 | + * |
|
506 | + * @param string $html |
|
507 | + * |
|
508 | + * @return void |
|
509 | + */ |
|
510 | + protected function removeRealUrlBrokenRootLink(&$html) |
|
511 | + { |
|
512 | + $html = str_replace('href=".html"', 'href=""', $html); |
|
513 | + } |
|
514 | + |
|
515 | + /** |
|
516 | + * Include configured header comment in HTML content block |
|
517 | + * |
|
518 | + * @param $html |
|
519 | + */ |
|
520 | + public function includeHeaderComment(&$html) |
|
521 | + { |
|
522 | + if (!empty($this->headerComment)) { |
|
523 | + $html = preg_replace_callback('/<meta http-equiv(.*)>/Usi', function ($matches) { |
|
524 | + return trim($matches[0] . $this->newline . $this->tab . $this->tab . '<!-- ' . $this->headerComment . '-->'); |
|
525 | + }, $html, 1); |
|
526 | + } |
|
527 | + } |
|
528 | 528 | } |
@@ -76,7 +76,7 @@ discard block |
||
76 | 76 | |
77 | 77 | if (!empty($config)) { |
78 | 78 | if ($config['formatHtml'] && is_numeric($config['formatHtml'])) { |
79 | - $this->formatType = (int)$config['formatHtml']; |
|
79 | + $this->formatType = (int) $config['formatHtml']; |
|
80 | 80 | } |
81 | 81 | |
82 | 82 | if ($config['formatHtml.']['tabSize'] && is_numeric($config['formatHtml.']['tabSize'])) { |
@@ -84,14 +84,14 @@ discard block |
||
84 | 84 | } |
85 | 85 | |
86 | 86 | if (isset($config['formatHtml.']['debugComment'])) { |
87 | - $this->debugComment = (bool)$config['formatHtml.']['debugComment']; |
|
87 | + $this->debugComment = (bool) $config['formatHtml.']['debugComment']; |
|
88 | 88 | } |
89 | 89 | |
90 | 90 | if (isset($config['headerComment'])) { |
91 | 91 | $this->headerComment = $config['headerComment']; |
92 | 92 | } |
93 | 93 | |
94 | - if (isset($config['dropEmptySpaceChar']) && (bool)$config['dropEmptySpaceChar']) { |
|
94 | + if (isset($config['dropEmptySpaceChar']) && (bool) $config['dropEmptySpaceChar']) { |
|
95 | 95 | $this->emptySpaceChar = ''; |
96 | 96 | } |
97 | 97 | } |
@@ -108,7 +108,7 @@ discard block |
||
108 | 108 | public function clean(&$html, $config = []) |
109 | 109 | { |
110 | 110 | if (!empty($config)) { |
111 | - if ((bool)$config['enabled'] === false) { |
|
111 | + if ((bool) $config['enabled'] === false) { |
|
112 | 112 | return; |
113 | 113 | } |
114 | 114 | |
@@ -117,15 +117,15 @@ discard block |
||
117 | 117 | |
118 | 118 | $manipulations = []; |
119 | 119 | |
120 | - if (isset($config['removeGenerator']) && (bool)$config['removeGenerator']) { |
|
120 | + if (isset($config['removeGenerator']) && (bool) $config['removeGenerator']) { |
|
121 | 121 | $manipulations['removeGenerator'] = GeneralUtility::makeInstance(RemoveGenerator::class); |
122 | 122 | } |
123 | 123 | |
124 | - if (isset($config['removeComments']) && (bool)$config['removeComments']) { |
|
124 | + if (isset($config['removeComments']) && (bool) $config['removeComments']) { |
|
125 | 125 | $manipulations['removeComments'] = GeneralUtility::makeInstance(RemoveComments::class); |
126 | 126 | } |
127 | 127 | |
128 | - if (isset($config['removeBlurScript']) && (bool)$config['removeBlurScript']) { |
|
128 | + if (isset($config['removeBlurScript']) && (bool) $config['removeBlurScript']) { |
|
129 | 129 | $manipulations['removeBlurScript'] = GeneralUtility::makeInstance(RemoveBlurScript::class); |
130 | 130 | } |
131 | 131 | |
@@ -135,7 +135,7 @@ discard block |
||
135 | 135 | |
136 | 136 | foreach ($manipulations as $key => $manipulation) { |
137 | 137 | /** @var ManipulationInterface $manipulation */ |
138 | - $configuration = isset($config[$key . '.']) && is_array($config[$key . '.']) ? $config[$key . '.'] : []; |
|
138 | + $configuration = isset($config[$key.'.']) && is_array($config[$key.'.']) ? $config[$key.'.'] : []; |
|
139 | 139 | $html = $manipulation->manipulate($html, $configuration); |
140 | 140 | } |
141 | 141 | |
@@ -179,7 +179,7 @@ discard block |
||
179 | 179 | $functionalBoxElements = 'dd|dt|frameset|li|tbody|td|tfoot|th|thead|tr|colgroup'; |
180 | 180 | $usableBoxElements = 'applet|button|del|iframe|ins|map|object|script'; |
181 | 181 | $imagineBoxElements = 'html|body|head|meta|title|link|script|base|!--'; |
182 | - $allBoxLikeElements = '(?>' . $trueBoxElements . '|' . $functionalBoxElements . '|' . $usableBoxElements . '|' . $imagineBoxElements . ')'; |
|
182 | + $allBoxLikeElements = '(?>'.$trueBoxElements.'|'.$functionalBoxElements.'|'.$usableBoxElements.'|'.$imagineBoxElements.')'; |
|
183 | 183 | $esteticBoxLikeElements = '(?>html|head|body|meta name|title|div|table|h1|h2|h3|h4|h5|h6|p|form|pre|center|!--)'; |
184 | 184 | $structureBoxLikeElements = '(?>html|head|body|div|!--)'; |
185 | 185 | |
@@ -188,7 +188,7 @@ discard block |
||
188 | 188 | '/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/', |
189 | 189 | $html, |
190 | 190 | -1, |
191 | - PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY |
|
191 | + PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY |
|
192 | 192 | ); |
193 | 193 | // remove empty lines |
194 | 194 | $htmlArray = ['']; |
@@ -215,52 +215,52 @@ discard block |
||
215 | 215 | } elseif ($this->formatType == 2 && ( // minimalistic line break |
216 | 216 | # this element has a line break before itself |
217 | 217 | preg_match( |
218 | - '/<' . $structureBoxLikeElements . '(.*)>/Usi', |
|
218 | + '/<'.$structureBoxLikeElements.'(.*)>/Usi', |
|
219 | 219 | $htmlArray[$x] |
220 | 220 | ) || preg_match( |
221 | - '/<' . $structureBoxLikeElements . '(.*) \/>/Usi', |
|
221 | + '/<'.$structureBoxLikeElements.'(.*) \/>/Usi', |
|
222 | 222 | $htmlArray[$x] |
223 | 223 | ) || # one element before is a element that has a line break after |
224 | 224 | preg_match( |
225 | - '/<\/' . $structureBoxLikeElements . '(.*)>/Usi', |
|
225 | + '/<\/'.$structureBoxLikeElements.'(.*)>/Usi', |
|
226 | 226 | $htmlArray[$x - 1] |
227 | 227 | ) || substr( |
228 | 228 | $htmlArray[$x - 1], |
229 | 229 | 0, |
230 | 230 | 4 |
231 | - ) == '<!--' || preg_match('/<' . $structureBoxLikeElements . '(.*) \/>/Usi', $htmlArray[$x - 1])) |
|
231 | + ) == '<!--' || preg_match('/<'.$structureBoxLikeElements.'(.*) \/>/Usi', $htmlArray[$x - 1])) |
|
232 | 232 | ) { |
233 | 233 | $newline = true; |
234 | 234 | } elseif ($this->formatType == 3 && ( // aestetic line break |
235 | 235 | # this element has a line break before itself |
236 | 236 | preg_match( |
237 | - '/<' . $esteticBoxLikeElements . '(.*)>/Usi', |
|
237 | + '/<'.$esteticBoxLikeElements.'(.*)>/Usi', |
|
238 | 238 | $htmlArray[$x] |
239 | 239 | ) || preg_match( |
240 | - '/<' . $esteticBoxLikeElements . '(.*) \/>/Usi', |
|
240 | + '/<'.$esteticBoxLikeElements.'(.*) \/>/Usi', |
|
241 | 241 | $htmlArray[$x] |
242 | 242 | ) || # one element before is a element that has a line break after |
243 | - preg_match('/<\/' . $esteticBoxLikeElements . '(.*)>/Usi', $htmlArray[$x - 1]) || substr( |
|
243 | + preg_match('/<\/'.$esteticBoxLikeElements.'(.*)>/Usi', $htmlArray[$x - 1]) || substr( |
|
244 | 244 | $htmlArray[$x - 1], |
245 | 245 | 0, |
246 | 246 | 4 |
247 | - ) == '<!--' || preg_match('/<' . $esteticBoxLikeElements . '(.*) \/>/Usi', $htmlArray[$x - 1])) |
|
247 | + ) == '<!--' || preg_match('/<'.$esteticBoxLikeElements.'(.*) \/>/Usi', $htmlArray[$x - 1])) |
|
248 | 248 | ) { |
249 | 249 | $newline = true; |
250 | 250 | } elseif ($this->formatType >= 4 && ( // logical line break |
251 | 251 | # this element has a line break before itself |
252 | 252 | preg_match( |
253 | - '/<' . $allBoxLikeElements . '(.*)>/Usi', |
|
253 | + '/<'.$allBoxLikeElements.'(.*)>/Usi', |
|
254 | 254 | $htmlArray[$x] |
255 | 255 | ) || preg_match( |
256 | - '/<' . $allBoxLikeElements . '(.*) \/>/Usi', |
|
256 | + '/<'.$allBoxLikeElements.'(.*) \/>/Usi', |
|
257 | 257 | $htmlArray[$x] |
258 | 258 | ) || # one element before is a element that has a line break after |
259 | - preg_match('/<\/' . $allBoxLikeElements . '(.*)>/Usi', $htmlArray[$x - 1]) || substr( |
|
259 | + preg_match('/<\/'.$allBoxLikeElements.'(.*)>/Usi', $htmlArray[$x - 1]) || substr( |
|
260 | 260 | $htmlArray[$x - 1], |
261 | 261 | 0, |
262 | 262 | 4 |
263 | - ) == '<!--' || preg_match('/<' . $allBoxLikeElements . '(.*) \/>/Usi', $htmlArray[$x - 1])) |
|
263 | + ) == '<!--' || preg_match('/<'.$allBoxLikeElements.'(.*) \/>/Usi', $htmlArray[$x - 1])) |
|
264 | 264 | ) { |
265 | 265 | $newline = true; |
266 | 266 | } |
@@ -337,7 +337,7 @@ discard block |
||
337 | 337 | |
338 | 338 | // include debug comment at the end |
339 | 339 | if ($tabs != 0 && $this->debugComment === true) { |
340 | - $html .= '<!--' . $tabs . " open elements found-->\r\n"; |
|
340 | + $html .= '<!--'.$tabs." open elements found-->\r\n"; |
|
341 | 341 | } |
342 | 342 | } |
343 | 343 | |
@@ -466,7 +466,7 @@ discard block |
||
466 | 466 | 'textarea', |
467 | 467 | 'pre' |
468 | 468 | ]; // eventuell auch: span, script, style |
469 | - $peaces = preg_split('#(<(' . implode('|', $splitArray) . ').*>.*</\2>)#Uis', $html, -1, PREG_SPLIT_DELIM_CAPTURE); |
|
469 | + $peaces = preg_split('#(<('.implode('|', $splitArray).').*>.*</\2>)#Uis', $html, -1, PREG_SPLIT_DELIM_CAPTURE); |
|
470 | 470 | $html = ""; |
471 | 471 | for ($i = 0; $i < count($peaces); $i++) { |
472 | 472 | if (($i + 1) % 3 == 0) { |
@@ -520,8 +520,8 @@ discard block |
||
520 | 520 | public function includeHeaderComment(&$html) |
521 | 521 | { |
522 | 522 | if (!empty($this->headerComment)) { |
523 | - $html = preg_replace_callback('/<meta http-equiv(.*)>/Usi', function ($matches) { |
|
524 | - return trim($matches[0] . $this->newline . $this->tab . $this->tab . '<!-- ' . $this->headerComment . '-->'); |
|
523 | + $html = preg_replace_callback('/<meta http-equiv(.*)>/Usi', function($matches) { |
|
524 | + return trim($matches[0].$this->newline.$this->tab.$this->tab.'<!-- '.$this->headerComment.'-->'); |
|
525 | 525 | }, $html, 1); |
526 | 526 | } |
527 | 527 | } |