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