Completed
Push — master ( ce223a...e3e355 )
by Tim
13:02
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   +508 added lines, -508 removed lines patch added patch discarded remove patch
@@ -17,512 +17,512 @@
 block discarded – undo
17 17
 class CleanHtmlService implements SingletonInterface
18 18
 {
19 19
 
20
-    /**
21
-     * Enable Debug comment in footer
22
-     *
23
-     * @var boolean
24
-     */
25
-    protected $debugComment = false;
26
-
27
-    /**
28
-     * Format Type
29
-     *
30
-     * @var integer
31
-     */
32
-    protected $formatType = 0;
33
-
34
-    /**
35
-     * Tab character
36
-     *
37
-     * @var string
38
-     */
39
-    protected $tab = "\t";
40
-
41
-    /**
42
-     * Newline character
43
-     *
44
-     * @var string
45
-     */
46
-    protected $newline = "\n";
47
-
48
-    /**
49
-     * Configured extra header comment
50
-     *
51
-     * @var string
52
-     */
53
-    protected $headerComment = '';
54
-
55
-    /**
56
-     * Empty space char
57
-     * @var string
58
-     */
59
-    protected $emptySpaceChar = ' ';
60
-
61
-    /**
62
-     * Set variables based on given config
63
-     *
64
-     * @param array $config
65
-     *
66
-     * @return void
67
-     */
68
-    public function setVariables(array $config)
69
-    {
70
-        // Set newline based on OS
71
-        if (Environment::isWindows()) {
72
-            $this->newline = "\r\n";
73
-        } else {
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 string
107
-     */
108
-    public function clean($html, $config = [])
109
-    {
110
-        if (!empty($config)) {
111
-            if ((bool)$config['enabled'] === false) {
112
-                return $html;
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
-            $html = $this->formatHtml($html);
144
-        }
145
-
146
-        return $html;
147
-    }
148
-
149
-    /**
150
-     * Formats the (X)HTML code:
151
-     *  - taps according to the hirarchy of the tags
152
-     *  - removes empty spaces between tags
153
-     *  - removes linebreaks within tags (spares where necessary: pre, textarea, comments, ..)
154
-     *  choose from five options:
155
-     *    0 => off
156
-     *    1 => no line break at all  (code in one line)
157
-     *    2 => minimalistic line breaks (structure defining box-elements)
158
-     *    3 => aesthetic line breaks (important box-elements)
159
-     *    4 => logic line breaks (all box-elements)
160
-     *    5 => max line breaks (all elements)
161
-     *
162
-     * @param string $html
163
-     *
164
-     * @return string
165
-     */
166
-    protected function formatHtml($html)
167
-    {
168
-        // Save original formated comments, pre, textarea, styles and java-scripts & replace them with markers
169
-        preg_match_all(
170
-            '/(?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',
171
-            $html,
172
-            $matches
173
-        );
174
-        $noFormat = $matches[0]; // do not format these block elements
175
-        for ($i = 0; $i < count($noFormat); $i++) {
176
-            $html = str_replace($noFormat[$i], "\n<!-- ELEMENT $i -->", $html);
177
-        }
178
-
179
-        // define box elements for formatting
180
-        $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';
181
-        $functionalBoxElements = 'dd|dt|frameset|li|tbody|td|tfoot|th|thead|tr|colgroup';
182
-        $usableBoxElements = 'applet|button|del|iframe|ins|map|object|script';
183
-        $imagineBoxElements = 'html|body|head|meta|title|link|script|base|!--';
184
-        $allBoxLikeElements = '(?>' . $trueBoxElements . '|' . $functionalBoxElements . '|' . $usableBoxElements . '|' . $imagineBoxElements . ')';
185
-        $esteticBoxLikeElements = '(?>html|head|body|meta name|title|div|table|h1|h2|h3|h4|h5|h6|p|form|pre|center|!--)';
186
-        $structureBoxLikeElements = '(?>html|head|body|div|!--)';
187
-
188
-        // split html into it's elements
189
-        $htmlArrayTemp = preg_split(
190
-            '/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/',
191
-            $html,
192
-            -1,
193
-            PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
194
-        );
195
-
196
-        if ($htmlArrayTemp === false) {
197
-            return $html;
198
-        }
199
-        // remove empty lines
200
-        $htmlArray = [''];
201
-        $z = 1;
202
-        for ($x = 0; $x < count($htmlArrayTemp); $x++) {
203
-            $t = trim($htmlArrayTemp[$x]);
204
-            if ($t !== '') {
205
-                $htmlArray[$z] = $htmlArrayTemp[$x];
206
-                $z++;
207
-            } else {
208
-                $htmlArray[$z] = $this->emptySpaceChar;
209
-                $z++;
210
-            }
211
-        }
212
-
213
-        // rebuild html
214
-        $html = '';
215
-        $tabs = 0;
216
-        for ($x = 0; $x < count($htmlArray); $x++) {
217
-            // check if the element should stand in a new line
218
-            $newline = false;
219
-            if (substr($htmlArray[$x - 1], 0, 5) == '<?xml') {
220
-                $newline = true;
221
-            } elseif ($this->formatType == 2 && ( // minimalistic line break
222
-                    # this element has a line break before itself
223
-                    preg_match(
224
-                        '/<' . $structureBoxLikeElements . '(.*)>/Usi',
225
-                        $htmlArray[$x]
226
-                    ) || preg_match(
227
-                        '/<' . $structureBoxLikeElements . '(.*) \/>/Usi',
228
-                        $htmlArray[$x]
229
-                    ) || # one element before is a element that has a line break after
230
-                    preg_match(
231
-                        '/<\/' . $structureBoxLikeElements . '(.*)>/Usi',
232
-                        $htmlArray[$x - 1]
233
-                    ) || substr(
234
-                        $htmlArray[$x - 1],
235
-                        0,
236
-                        4
237
-                    ) == '<!--' || preg_match('/<' . $structureBoxLikeElements . '(.*) \/>/Usi', $htmlArray[$x - 1]))
238
-            ) {
239
-                $newline = true;
240
-            } elseif ($this->formatType == 3 && ( // aestetic line break
241
-                    # this element has a line break before itself
242
-                    preg_match(
243
-                        '/<' . $esteticBoxLikeElements . '(.*)>/Usi',
244
-                        $htmlArray[$x]
245
-                    ) || preg_match(
246
-                        '/<' . $esteticBoxLikeElements . '(.*) \/>/Usi',
247
-                        $htmlArray[$x]
248
-                    ) || # one element before is a element that has a line break after
249
-                    preg_match('/<\/' . $esteticBoxLikeElements . '(.*)>/Usi', $htmlArray[$x - 1]) || substr(
250
-                        $htmlArray[$x - 1],
251
-                        0,
252
-                        4
253
-                    ) == '<!--' || preg_match('/<' . $esteticBoxLikeElements . '(.*) \/>/Usi', $htmlArray[$x - 1]))
254
-            ) {
255
-                $newline = true;
256
-            } elseif ($this->formatType >= 4 && ( // logical line break
257
-                    # this element has a line break before itself
258
-                    preg_match(
259
-                        '/<' . $allBoxLikeElements . '(.*)>/Usi',
260
-                        $htmlArray[$x]
261
-                    ) || preg_match(
262
-                        '/<' . $allBoxLikeElements . '(.*) \/>/Usi',
263
-                        $htmlArray[$x]
264
-                    ) || # one element before is a element that has a line break after
265
-                    preg_match('/<\/' . $allBoxLikeElements . '(.*)>/Usi', $htmlArray[$x - 1]) || substr(
266
-                        $htmlArray[$x - 1],
267
-                        0,
268
-                        4
269
-                    ) == '<!--' || preg_match('/<' . $allBoxLikeElements . '(.*) \/>/Usi', $htmlArray[$x - 1]))
270
-            ) {
271
-                $newline = true;
272
-            }
273
-
274
-            // count down a tab
275
-            if (substr($htmlArray[$x], 0, 2) == '</') {
276
-                $tabs--;
277
-            }
278
-
279
-            // add tabs and line breaks in front of the current tag
280
-            if ($newline) {
281
-                $html .= $this->newline;
282
-                for ($y = 0; $y < $tabs; $y++) {
283
-                    $html .= $this->tab;
284
-                }
285
-            }
286
-
287
-            // remove white spaces and line breaks and add current tag to the html-string
288
-            if (substr($htmlArray[$x - 1], 0, 4) == '<pre' // remove white space after line ending in PRE / TEXTAREA / comment
289
-                || substr($htmlArray[$x - 1], 0, 9) == '<textarea' || substr($htmlArray[$x - 1], 0, 4) == '<!--'
290
-            ) {
291
-                $html .= $this->rTrimLines($htmlArray[$x]);
292
-            } elseif (substr($htmlArray[$x], 0, 9) == '<![CDATA[' // remove multiple white space in CDATA / XML
293
-                || substr($htmlArray[$x], 0, 5) == '<?xml'
294
-            ) {
295
-                $html .= $this->killWhiteSpace($htmlArray[$x]);
296
-            } else { // remove all line breaks
297
-                $html .= $this->killLineBreaks($htmlArray[$x]);
298
-            }
299
-
300
-            // count up a tab
301
-            if (substr($htmlArray[$x], 0, 1) == '<' && substr($htmlArray[$x], 1, 1) != '/') {
302
-                if (
303
-                    substr($htmlArray[$x], 1, 1) !== ' '
304
-                    && substr($htmlArray[$x], 1, 3) !== 'img'
305
-                    && substr($htmlArray[$x], 1, 6) !== 'source'
306
-                    && substr($htmlArray[$x], 1, 2) !== 'br'
307
-                    && substr($htmlArray[$x], 1, 2) !== 'hr'
308
-                    && substr($htmlArray[$x], 1, 5) !== 'input'
309
-                    && substr($htmlArray[$x], 1, 4) !== 'link'
310
-                    && substr($htmlArray[$x], 1, 4) !== 'meta'
311
-                    && substr($htmlArray[$x], 1, 4) !== 'col '
312
-                    && substr($htmlArray[$x], 1, 5) !== 'frame'
313
-                    && substr($htmlArray[$x], 1, 7) !== 'isindex'
314
-                    && substr($htmlArray[$x], 1, 5) !== 'param'
315
-                    && substr($htmlArray[$x], 1, 4) !== 'area'
316
-                    && substr($htmlArray[$x], 1, 4) !== 'base'
317
-                    && substr($htmlArray[$x], 0, 2) !== '<!'
318
-                    && substr($htmlArray[$x], 0, 5) !== '<?xml'
319
-                ) {
320
-                    $tabs++;
321
-                }
322
-            }
323
-        }
324
-
325
-        // Remove empty lines
326
-        if ($this->formatType > 1) {
327
-            $this->removeEmptyLines($html);
328
-        }
329
-
330
-        // Restore saved comments, styles and java-scripts
331
-        for ($i = 0; $i < count($noFormat); $i++) {
332
-            $noFormat[$i] = $this->rTrimLines($noFormat[$i]); // remove white space after line ending
333
-            $html = str_replace("<!-- ELEMENT $i -->", $noFormat[$i], $html);
334
-        }
335
-
336
-        // include debug comment at the end
337
-        if ($tabs != 0 && $this->debugComment === true) {
338
-            $html .= '<!--' . $tabs . " open elements found-->\r\n";
339
-        }
340
-
341
-        return $html;
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 (Environment::isWindows()) {
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
-    }
20
+	/**
21
+	 * Enable Debug comment in footer
22
+	 *
23
+	 * @var boolean
24
+	 */
25
+	protected $debugComment = false;
26
+
27
+	/**
28
+	 * Format Type
29
+	 *
30
+	 * @var integer
31
+	 */
32
+	protected $formatType = 0;
33
+
34
+	/**
35
+	 * Tab character
36
+	 *
37
+	 * @var string
38
+	 */
39
+	protected $tab = "\t";
40
+
41
+	/**
42
+	 * Newline character
43
+	 *
44
+	 * @var string
45
+	 */
46
+	protected $newline = "\n";
47
+
48
+	/**
49
+	 * Configured extra header comment
50
+	 *
51
+	 * @var string
52
+	 */
53
+	protected $headerComment = '';
54
+
55
+	/**
56
+	 * Empty space char
57
+	 * @var string
58
+	 */
59
+	protected $emptySpaceChar = ' ';
60
+
61
+	/**
62
+	 * Set variables based on given config
63
+	 *
64
+	 * @param array $config
65
+	 *
66
+	 * @return void
67
+	 */
68
+	public function setVariables(array $config)
69
+	{
70
+		// Set newline based on OS
71
+		if (Environment::isWindows()) {
72
+			$this->newline = "\r\n";
73
+		} else {
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 string
107
+	 */
108
+	public function clean($html, $config = [])
109
+	{
110
+		if (!empty($config)) {
111
+			if ((bool)$config['enabled'] === false) {
112
+				return $html;
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
+			$html = $this->formatHtml($html);
144
+		}
145
+
146
+		return $html;
147
+	}
148
+
149
+	/**
150
+	 * Formats the (X)HTML code:
151
+	 *  - taps according to the hirarchy of the tags
152
+	 *  - removes empty spaces between tags
153
+	 *  - removes linebreaks within tags (spares where necessary: pre, textarea, comments, ..)
154
+	 *  choose from five options:
155
+	 *    0 => off
156
+	 *    1 => no line break at all  (code in one line)
157
+	 *    2 => minimalistic line breaks (structure defining box-elements)
158
+	 *    3 => aesthetic line breaks (important box-elements)
159
+	 *    4 => logic line breaks (all box-elements)
160
+	 *    5 => max line breaks (all elements)
161
+	 *
162
+	 * @param string $html
163
+	 *
164
+	 * @return string
165
+	 */
166
+	protected function formatHtml($html)
167
+	{
168
+		// Save original formated comments, pre, textarea, styles and java-scripts & replace them with markers
169
+		preg_match_all(
170
+			'/(?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',
171
+			$html,
172
+			$matches
173
+		);
174
+		$noFormat = $matches[0]; // do not format these block elements
175
+		for ($i = 0; $i < count($noFormat); $i++) {
176
+			$html = str_replace($noFormat[$i], "\n<!-- ELEMENT $i -->", $html);
177
+		}
178
+
179
+		// define box elements for formatting
180
+		$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';
181
+		$functionalBoxElements = 'dd|dt|frameset|li|tbody|td|tfoot|th|thead|tr|colgroup';
182
+		$usableBoxElements = 'applet|button|del|iframe|ins|map|object|script';
183
+		$imagineBoxElements = 'html|body|head|meta|title|link|script|base|!--';
184
+		$allBoxLikeElements = '(?>' . $trueBoxElements . '|' . $functionalBoxElements . '|' . $usableBoxElements . '|' . $imagineBoxElements . ')';
185
+		$esteticBoxLikeElements = '(?>html|head|body|meta name|title|div|table|h1|h2|h3|h4|h5|h6|p|form|pre|center|!--)';
186
+		$structureBoxLikeElements = '(?>html|head|body|div|!--)';
187
+
188
+		// split html into it's elements
189
+		$htmlArrayTemp = preg_split(
190
+			'/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/',
191
+			$html,
192
+			-1,
193
+			PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
194
+		);
195
+
196
+		if ($htmlArrayTemp === false) {
197
+			return $html;
198
+		}
199
+		// remove empty lines
200
+		$htmlArray = [''];
201
+		$z = 1;
202
+		for ($x = 0; $x < count($htmlArrayTemp); $x++) {
203
+			$t = trim($htmlArrayTemp[$x]);
204
+			if ($t !== '') {
205
+				$htmlArray[$z] = $htmlArrayTemp[$x];
206
+				$z++;
207
+			} else {
208
+				$htmlArray[$z] = $this->emptySpaceChar;
209
+				$z++;
210
+			}
211
+		}
212
+
213
+		// rebuild html
214
+		$html = '';
215
+		$tabs = 0;
216
+		for ($x = 0; $x < count($htmlArray); $x++) {
217
+			// check if the element should stand in a new line
218
+			$newline = false;
219
+			if (substr($htmlArray[$x - 1], 0, 5) == '<?xml') {
220
+				$newline = true;
221
+			} elseif ($this->formatType == 2 && ( // minimalistic line break
222
+					# this element has a line break before itself
223
+					preg_match(
224
+						'/<' . $structureBoxLikeElements . '(.*)>/Usi',
225
+						$htmlArray[$x]
226
+					) || preg_match(
227
+						'/<' . $structureBoxLikeElements . '(.*) \/>/Usi',
228
+						$htmlArray[$x]
229
+					) || # one element before is a element that has a line break after
230
+					preg_match(
231
+						'/<\/' . $structureBoxLikeElements . '(.*)>/Usi',
232
+						$htmlArray[$x - 1]
233
+					) || substr(
234
+						$htmlArray[$x - 1],
235
+						0,
236
+						4
237
+					) == '<!--' || preg_match('/<' . $structureBoxLikeElements . '(.*) \/>/Usi', $htmlArray[$x - 1]))
238
+			) {
239
+				$newline = true;
240
+			} elseif ($this->formatType == 3 && ( // aestetic line break
241
+					# this element has a line break before itself
242
+					preg_match(
243
+						'/<' . $esteticBoxLikeElements . '(.*)>/Usi',
244
+						$htmlArray[$x]
245
+					) || preg_match(
246
+						'/<' . $esteticBoxLikeElements . '(.*) \/>/Usi',
247
+						$htmlArray[$x]
248
+					) || # one element before is a element that has a line break after
249
+					preg_match('/<\/' . $esteticBoxLikeElements . '(.*)>/Usi', $htmlArray[$x - 1]) || substr(
250
+						$htmlArray[$x - 1],
251
+						0,
252
+						4
253
+					) == '<!--' || preg_match('/<' . $esteticBoxLikeElements . '(.*) \/>/Usi', $htmlArray[$x - 1]))
254
+			) {
255
+				$newline = true;
256
+			} elseif ($this->formatType >= 4 && ( // logical line break
257
+					# this element has a line break before itself
258
+					preg_match(
259
+						'/<' . $allBoxLikeElements . '(.*)>/Usi',
260
+						$htmlArray[$x]
261
+					) || preg_match(
262
+						'/<' . $allBoxLikeElements . '(.*) \/>/Usi',
263
+						$htmlArray[$x]
264
+					) || # one element before is a element that has a line break after
265
+					preg_match('/<\/' . $allBoxLikeElements . '(.*)>/Usi', $htmlArray[$x - 1]) || substr(
266
+						$htmlArray[$x - 1],
267
+						0,
268
+						4
269
+					) == '<!--' || preg_match('/<' . $allBoxLikeElements . '(.*) \/>/Usi', $htmlArray[$x - 1]))
270
+			) {
271
+				$newline = true;
272
+			}
273
+
274
+			// count down a tab
275
+			if (substr($htmlArray[$x], 0, 2) == '</') {
276
+				$tabs--;
277
+			}
278
+
279
+			// add tabs and line breaks in front of the current tag
280
+			if ($newline) {
281
+				$html .= $this->newline;
282
+				for ($y = 0; $y < $tabs; $y++) {
283
+					$html .= $this->tab;
284
+				}
285
+			}
286
+
287
+			// remove white spaces and line breaks and add current tag to the html-string
288
+			if (substr($htmlArray[$x - 1], 0, 4) == '<pre' // remove white space after line ending in PRE / TEXTAREA / comment
289
+				|| substr($htmlArray[$x - 1], 0, 9) == '<textarea' || substr($htmlArray[$x - 1], 0, 4) == '<!--'
290
+			) {
291
+				$html .= $this->rTrimLines($htmlArray[$x]);
292
+			} elseif (substr($htmlArray[$x], 0, 9) == '<![CDATA[' // remove multiple white space in CDATA / XML
293
+				|| substr($htmlArray[$x], 0, 5) == '<?xml'
294
+			) {
295
+				$html .= $this->killWhiteSpace($htmlArray[$x]);
296
+			} else { // remove all line breaks
297
+				$html .= $this->killLineBreaks($htmlArray[$x]);
298
+			}
299
+
300
+			// count up a tab
301
+			if (substr($htmlArray[$x], 0, 1) == '<' && substr($htmlArray[$x], 1, 1) != '/') {
302
+				if (
303
+					substr($htmlArray[$x], 1, 1) !== ' '
304
+					&& substr($htmlArray[$x], 1, 3) !== 'img'
305
+					&& substr($htmlArray[$x], 1, 6) !== 'source'
306
+					&& substr($htmlArray[$x], 1, 2) !== 'br'
307
+					&& substr($htmlArray[$x], 1, 2) !== 'hr'
308
+					&& substr($htmlArray[$x], 1, 5) !== 'input'
309
+					&& substr($htmlArray[$x], 1, 4) !== 'link'
310
+					&& substr($htmlArray[$x], 1, 4) !== 'meta'
311
+					&& substr($htmlArray[$x], 1, 4) !== 'col '
312
+					&& substr($htmlArray[$x], 1, 5) !== 'frame'
313
+					&& substr($htmlArray[$x], 1, 7) !== 'isindex'
314
+					&& substr($htmlArray[$x], 1, 5) !== 'param'
315
+					&& substr($htmlArray[$x], 1, 4) !== 'area'
316
+					&& substr($htmlArray[$x], 1, 4) !== 'base'
317
+					&& substr($htmlArray[$x], 0, 2) !== '<!'
318
+					&& substr($htmlArray[$x], 0, 5) !== '<?xml'
319
+				) {
320
+					$tabs++;
321
+				}
322
+			}
323
+		}
324
+
325
+		// Remove empty lines
326
+		if ($this->formatType > 1) {
327
+			$this->removeEmptyLines($html);
328
+		}
329
+
330
+		// Restore saved comments, styles and java-scripts
331
+		for ($i = 0; $i < count($noFormat); $i++) {
332
+			$noFormat[$i] = $this->rTrimLines($noFormat[$i]); // remove white space after line ending
333
+			$html = str_replace("<!-- ELEMENT $i -->", $noFormat[$i], $html);
334
+		}
335
+
336
+		// include debug comment at the end
337
+		if ($tabs != 0 && $this->debugComment === true) {
338
+			$html .= '<!--' . $tabs . " open elements found-->\r\n";
339
+		}
340
+
341
+		return $html;
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 (Environment::isWindows()) {
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
 }
Please login to merge, or discard this patch.
Spacing   +26 added lines, -26 removed lines patch added patch discarded remove patch
@@ -76,7 +76,7 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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 $html;
113 113
             }
114 114
 
@@ -117,15 +117,15 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 
@@ -181,7 +181,7 @@  discard block
 block discarded – undo
181 181
         $functionalBoxElements = 'dd|dt|frameset|li|tbody|td|tfoot|th|thead|tr|colgroup';
182 182
         $usableBoxElements = 'applet|button|del|iframe|ins|map|object|script';
183 183
         $imagineBoxElements = 'html|body|head|meta|title|link|script|base|!--';
184
-        $allBoxLikeElements = '(?>' . $trueBoxElements . '|' . $functionalBoxElements . '|' . $usableBoxElements . '|' . $imagineBoxElements . ')';
184
+        $allBoxLikeElements = '(?>'.$trueBoxElements.'|'.$functionalBoxElements.'|'.$usableBoxElements.'|'.$imagineBoxElements.')';
185 185
         $esteticBoxLikeElements = '(?>html|head|body|meta name|title|div|table|h1|h2|h3|h4|h5|h6|p|form|pre|center|!--)';
186 186
         $structureBoxLikeElements = '(?>html|head|body|div|!--)';
187 187
 
@@ -190,7 +190,7 @@  discard block
 block discarded – undo
190 190
             '/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/',
191 191
             $html,
192 192
             -1,
193
-            PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
193
+            PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY
194 194
         );
195 195
 
196 196
         if ($htmlArrayTemp === false) {
@@ -221,52 +221,52 @@  discard block
 block discarded – undo
221 221
             } elseif ($this->formatType == 2 && ( // minimalistic line break
222 222
                     # this element has a line break before itself
223 223
                     preg_match(
224
-                        '/<' . $structureBoxLikeElements . '(.*)>/Usi',
224
+                        '/<'.$structureBoxLikeElements.'(.*)>/Usi',
225 225
                         $htmlArray[$x]
226 226
                     ) || preg_match(
227
-                        '/<' . $structureBoxLikeElements . '(.*) \/>/Usi',
227
+                        '/<'.$structureBoxLikeElements.'(.*) \/>/Usi',
228 228
                         $htmlArray[$x]
229 229
                     ) || # one element before is a element that has a line break after
230 230
                     preg_match(
231
-                        '/<\/' . $structureBoxLikeElements . '(.*)>/Usi',
231
+                        '/<\/'.$structureBoxLikeElements.'(.*)>/Usi',
232 232
                         $htmlArray[$x - 1]
233 233
                     ) || substr(
234 234
                         $htmlArray[$x - 1],
235 235
                         0,
236 236
                         4
237
-                    ) == '<!--' || preg_match('/<' . $structureBoxLikeElements . '(.*) \/>/Usi', $htmlArray[$x - 1]))
237
+                    ) == '<!--' || preg_match('/<'.$structureBoxLikeElements.'(.*) \/>/Usi', $htmlArray[$x - 1]))
238 238
             ) {
239 239
                 $newline = true;
240 240
             } elseif ($this->formatType == 3 && ( // aestetic line break
241 241
                     # this element has a line break before itself
242 242
                     preg_match(
243
-                        '/<' . $esteticBoxLikeElements . '(.*)>/Usi',
243
+                        '/<'.$esteticBoxLikeElements.'(.*)>/Usi',
244 244
                         $htmlArray[$x]
245 245
                     ) || preg_match(
246
-                        '/<' . $esteticBoxLikeElements . '(.*) \/>/Usi',
246
+                        '/<'.$esteticBoxLikeElements.'(.*) \/>/Usi',
247 247
                         $htmlArray[$x]
248 248
                     ) || # one element before is a element that has a line break after
249
-                    preg_match('/<\/' . $esteticBoxLikeElements . '(.*)>/Usi', $htmlArray[$x - 1]) || substr(
249
+                    preg_match('/<\/'.$esteticBoxLikeElements.'(.*)>/Usi', $htmlArray[$x - 1]) || substr(
250 250
                         $htmlArray[$x - 1],
251 251
                         0,
252 252
                         4
253
-                    ) == '<!--' || preg_match('/<' . $esteticBoxLikeElements . '(.*) \/>/Usi', $htmlArray[$x - 1]))
253
+                    ) == '<!--' || preg_match('/<'.$esteticBoxLikeElements.'(.*) \/>/Usi', $htmlArray[$x - 1]))
254 254
             ) {
255 255
                 $newline = true;
256 256
             } elseif ($this->formatType >= 4 && ( // logical line break
257 257
                     # this element has a line break before itself
258 258
                     preg_match(
259
-                        '/<' . $allBoxLikeElements . '(.*)>/Usi',
259
+                        '/<'.$allBoxLikeElements.'(.*)>/Usi',
260 260
                         $htmlArray[$x]
261 261
                     ) || preg_match(
262
-                        '/<' . $allBoxLikeElements . '(.*) \/>/Usi',
262
+                        '/<'.$allBoxLikeElements.'(.*) \/>/Usi',
263 263
                         $htmlArray[$x]
264 264
                     ) || # one element before is a element that has a line break after
265
-                    preg_match('/<\/' . $allBoxLikeElements . '(.*)>/Usi', $htmlArray[$x - 1]) || substr(
265
+                    preg_match('/<\/'.$allBoxLikeElements.'(.*)>/Usi', $htmlArray[$x - 1]) || substr(
266 266
                         $htmlArray[$x - 1],
267 267
                         0,
268 268
                         4
269
-                    ) == '<!--' || preg_match('/<' . $allBoxLikeElements . '(.*) \/>/Usi', $htmlArray[$x - 1]))
269
+                    ) == '<!--' || preg_match('/<'.$allBoxLikeElements.'(.*) \/>/Usi', $htmlArray[$x - 1]))
270 270
             ) {
271 271
                 $newline = true;
272 272
             }
@@ -335,7 +335,7 @@  discard block
 block discarded – undo
335 335
 
336 336
         // include debug comment at the end
337 337
         if ($tabs != 0 && $this->debugComment === true) {
338
-            $html .= '<!--' . $tabs . " open elements found-->\r\n";
338
+            $html .= '<!--'.$tabs." open elements found-->\r\n";
339 339
         }
340 340
 
341 341
         return $html;
@@ -466,7 +466,7 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
     }
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/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.
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 (!empty($pattern) && 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 (!empty($pattern) && 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/RemoveGenerator.php 2 patches
Indentation   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -13,15 +13,15 @@
 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
-        $regex = '<meta name=["\']?generator["\']? [^>]+>';
25
-        return preg_replace('/' . $regex . '/is', '', $html);
26
-    }
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
+		$regex = '<meta name=["\']?generator["\']? [^>]+>';
25
+		return preg_replace('/' . $regex . '/is', '', $html);
26
+	}
27 27
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -22,6 +22,6 @@
 block discarded – undo
22 22
     public function manipulate($html, array $configuration = [])
23 23
     {
24 24
         $regex = '<meta name=["\']?generator["\']? [^>]+>';
25
-        return preg_replace('/' . $regex . '/is', '', $html);
25
+        return preg_replace('/'.$regex.'/is', '', $html);
26 26
     }
27 27
 }
Please login to merge, or discard this patch.
Classes/Middleware/CleanHtmlMiddleware.php 1 patch
Indentation   +39 added lines, -39 removed lines patch added patch discarded remove patch
@@ -17,43 +17,43 @@
 block discarded – undo
17 17
  */
18 18
 class CleanHtmlMiddleware implements MiddlewareInterface
19 19
 {
20
-    /**
21
-     * @var CleanHtmlService
22
-     */
23
-    protected $cleanHtmlService = null;
24
-
25
-    public function __construct()
26
-    {
27
-        $this->cleanHtmlService = GeneralUtility::makeInstance(CleanHtmlService::class);
28
-    }
29
-
30
-    /**
31
-     * Clean the HTML output
32
-     *
33
-     * @param ServerRequestInterface $request
34
-     * @param RequestHandlerInterface $handler
35
-     * @return ResponseInterface
36
-     */
37
-    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
38
-    {
39
-        $response = $handler->handle($request);
40
-
41
-        if (
42
-            !($response instanceof NullResponse)
43
-            && $GLOBALS['TSFE'] instanceof TypoScriptFrontendController
44
-            && $GLOBALS['TSFE']->isOutputting()) {
45
-
46
-            $processedHtml = $this->cleanHtmlService->clean(
47
-                $response->getBody()->__toString(),
48
-                $GLOBALS['TSFE']->config['config']['sourceopt.']
49
-            );
50
-
51
-            // Replace old body with $processedHtml
52
-            $responseBody = new Stream('php://temp', 'rw');
53
-            $responseBody->write($processedHtml);
54
-            $response = $response->withBody($responseBody);
55
-        }
56
-
57
-        return $response;
58
-    }
20
+	/**
21
+	 * @var CleanHtmlService
22
+	 */
23
+	protected $cleanHtmlService = null;
24
+
25
+	public function __construct()
26
+	{
27
+		$this->cleanHtmlService = GeneralUtility::makeInstance(CleanHtmlService::class);
28
+	}
29
+
30
+	/**
31
+	 * Clean the HTML output
32
+	 *
33
+	 * @param ServerRequestInterface $request
34
+	 * @param RequestHandlerInterface $handler
35
+	 * @return ResponseInterface
36
+	 */
37
+	public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
38
+	{
39
+		$response = $handler->handle($request);
40
+
41
+		if (
42
+			!($response instanceof NullResponse)
43
+			&& $GLOBALS['TSFE'] instanceof TypoScriptFrontendController
44
+			&& $GLOBALS['TSFE']->isOutputting()) {
45
+
46
+			$processedHtml = $this->cleanHtmlService->clean(
47
+				$response->getBody()->__toString(),
48
+				$GLOBALS['TSFE']->config['config']['sourceopt.']
49
+			);
50
+
51
+			// Replace old body with $processedHtml
52
+			$responseBody = new Stream('php://temp', 'rw');
53
+			$responseBody->write($processedHtml);
54
+			$response = $response->withBody($responseBody);
55
+		}
56
+
57
+		return $response;
58
+	}
59 59
 }
Please login to merge, or discard this patch.