Completed
Push — master ( 23cfa7...bcdc61 )
by Tim
12:38
created
Classes/Service/CleanHtmlService.php 2 patches
Indentation   +408 added lines, -408 removed lines patch added patch discarded remove patch
@@ -17,412 +17,412 @@
 block discarded – undo
17 17
  */
18 18
 class CleanHtmlService implements SingletonInterface
19 19
 {
20
-    /**
21
-     * Enable Debug comment in footer.
22
-     *
23
-     * @var bool
24
-     */
25
-    protected $debugComment = false;
26
-
27
-    /**
28
-     * Format Type.
29
-     *
30
-     * @var int
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
-     *
58
-     * @var string
59
-     */
60
-    protected $emptySpaceChar = ' ';
61
-
62
-    /**
63
-     * Set variables based on given config.
64
-     */
65
-    public function setVariables(array $config): void
66
-    {
67
-        if (!empty($config)) {
68
-            if ($config['formatHtml'] && is_numeric($config['formatHtml'])) {
69
-                $this->formatType = (int) $config['formatHtml'];
70
-            }
71
-
72
-            if ($config['formatHtml.']['tabSize'] && is_numeric($config['formatHtml.']['tabSize'])) {
73
-                $this->tab = str_pad('', $config['formatHtml.']['tabSize'], ' ');
74
-            }
75
-
76
-            if (isset($config['formatHtml.']['debugComment'])) {
77
-                $this->debugComment = (bool) $config['formatHtml.']['debugComment'];
78
-            }
79
-
80
-            if (isset($config['headerComment'])) {
81
-                $this->headerComment = $config['headerComment'];
82
-            }
83
-
84
-            if (isset($config['dropEmptySpaceChar']) && (bool) $config['dropEmptySpaceChar']) {
85
-                $this->emptySpaceChar = '';
86
-            }
87
-        }
88
-    }
89
-
90
-    /**
91
-     * Clean given HTML with formatter.
92
-     *
93
-     * @param string $html
94
-     * @param array  $config
95
-     *
96
-     * @return string
97
-     */
98
-    public function clean($html, $config = [])
99
-    {
100
-        if (!empty($config)) {
101
-            $this->setVariables($config);
102
-        }
103
-        // convert line-breaks to UNIX
104
-        $this->convNlOs($html);
105
-
106
-        $manipulations = [];
107
-
108
-        if (isset($config['removeGenerator']) && (bool) $config['removeGenerator']) {
109
-            $manipulations['removeGenerator'] = GeneralUtility::makeInstance(RemoveGenerator::class);
110
-        }
111
-
112
-        if (isset($config['removeComments']) && (bool) $config['removeComments']) {
113
-            $manipulations['removeComments'] = GeneralUtility::makeInstance(RemoveComments::class);
114
-        }
115
-
116
-        if (!empty($this->headerComment)) {
117
-            $this->includeHeaderComment($html);
118
-        }
119
-
120
-        foreach ($manipulations as $key => $manipulation) {
121
-            /** @var ManipulationInterface $manipulation */
122
-            $configuration = isset($config[$key.'.']) && \is_array($config[$key.'.']) ? $config[$key.'.'] : [];
123
-            $html = $manipulation->manipulate($html, $configuration);
124
-        }
125
-
126
-        // cleanup HTML5 self-closing elements
127
-        if (!isset($GLOBALS['TSFE']->config['config']['doctype'])
128
-            || 'x' !== substr($GLOBALS['TSFE']->config['config']['doctype'], 0, 1)) {
129
-            $html = preg_replace(
130
-                '/<((?:area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)\s[^>]+?)\s?\/>/',
131
-                '<$1>',
132
-                $html
133
-            );
134
-        }
135
-
136
-        if ($this->formatType > 0) {
137
-            $html = $this->formatHtml($html);
138
-        }
139
-        // remove white space after line ending
140
-        $this->rTrimLines($html);
141
-
142
-        // recover line-breaks
143
-        if (Environment::isWindows()) {
144
-            $html = str_replace($this->newline, "\r\n", $html);
145
-        }
146
-
147
-        return $html;
148
-    }
149
-
150
-    /**
151
-     * Formats the (X)HTML code:
152
-     *  - taps according to the hirarchy of the tags
153
-     *  - removes empty spaces between tags
154
-     *  - removes linebreaks within tags (spares where necessary: pre, textarea, comments, ..)
155
-     *  choose from five options:
156
-     *    0 => off
157
-     *    1 => no line break at all  (code in one line)
158
-     *    2 => minimalistic line breaks (structure defining box-elements)
159
-     *    3 => aesthetic line breaks (important box-elements)
160
-     *    4 => logic line breaks (all box-elements)
161
-     *    5 => max line breaks (all elements).
162
-     *
163
-     * @param string $html
164
-     *
165
-     * @return string
166
-     */
167
-    protected function formatHtml($html)
168
-    {
169
-        // Save original formated comments, pre, textarea, styles and java-scripts & replace them with markers
170
-        preg_match_all(
171
-            '/(?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',
172
-            $html,
173
-            $matches
174
-        );
175
-        $noFormat = $matches[0]; // do not format these block elements
176
-        for ($i = 0; $i < \count($noFormat); ++$i) {
177
-            $html = str_replace($noFormat[$i], "\n<!-- ELEMENT {$i} -->", $html);
178
-        }
179
-
180
-        // define box elements for formatting
181
-        $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';
182
-        $functionalBoxElements = 'dd|dt|frameset|li|tbody|td|tfoot|th|thead|tr|colgroup';
183
-        $usableBoxElements = 'applet|button|del|iframe|ins|map|object|script';
184
-        $imagineBoxElements = 'html|body|head|meta|title|link|script|base|!--';
185
-        $allBoxLikeElements = '(?>'.$trueBoxElements.'|'.$functionalBoxElements.'|'.$usableBoxElements.'|'.$imagineBoxElements.')';
186
-        $esteticBoxLikeElements = '(?>html|head|body|meta name|title|div|table|h1|h2|h3|h4|h5|h6|p|form|pre|center|!--)';
187
-        $structureBoxLikeElements = '(?>html|head|body|div|!--)';
188
-
189
-        // split html into it's elements
190
-        $htmlArrayTemp = preg_split(
191
-            '/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/',
192
-            $html,
193
-            -1,
194
-            \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY
195
-        );
196
-
197
-        if (false === $htmlArrayTemp) {
198
-            // Restore saved comments, styles and java-scripts
199
-            for ($i = 0; $i < \count($noFormat); ++$i) {
200
-                $html = str_replace("<!-- ELEMENT {$i} -->", $noFormat[$i], $html);
201
-            }
202
-
203
-            return $html;
204
-        }
205
-        // remove empty lines
206
-        $htmlArray = [''];
207
-        $index = 1;
208
-        for ($x = 0; $x < \count($htmlArrayTemp); ++$x) {
209
-            $text = trim($htmlArrayTemp[$x]);
210
-            $htmlArray[$index] = '' !== $text ? $htmlArrayTemp[$x] : $this->emptySpaceChar;
211
-            ++$index;
212
-        }
213
-
214
-        // rebuild html
215
-        $html = '';
216
-        $tabs = 0;
217
-        for ($x = 0; $x < \count($htmlArray); ++$x) {
218
-            // check if the element should stand in a new line
219
-            $newline = false;
220
-            if ('<?xml' == substr($htmlArray[$x - 1], 0, 5)) {
221
-                $newline = true;
222
-            } elseif (2 == $this->formatType && ( // minimalistic line break
223
-                    // this element has a line break before itself
224
-                    preg_match(
225
-                        '/<'.$structureBoxLikeElements.'(.*)>/Usi',
226
-                        $htmlArray[$x]
227
-                    ) || preg_match(
228
-                        '/<'.$structureBoxLikeElements.'(.*) \/>/Usi',
229
-                        $htmlArray[$x]
230
-                    ) // one element before is a element that has a line break after
231
-                    || preg_match(
232
-                        '/<\/'.$structureBoxLikeElements.'(.*)>/Usi',
233
-                        $htmlArray[$x - 1]
234
-                    ) || '<!--' == substr(
235
-                        $htmlArray[$x - 1],
236
-                        0,
237
-                        4
238
-                    ) || preg_match('/<'.$structureBoxLikeElements.'(.*) \/>/Usi', $htmlArray[$x - 1]))
239
-            ) {
240
-                $newline = true;
241
-            } elseif (3 == $this->formatType && ( // aestetic line break
242
-                    // this element has a line break before itself
243
-                    preg_match(
244
-                        '/<'.$esteticBoxLikeElements.'(.*)>/Usi',
245
-                        $htmlArray[$x]
246
-                    ) || preg_match(
247
-                        '/<'.$esteticBoxLikeElements.'(.*) \/>/Usi',
248
-                        $htmlArray[$x]
249
-                    ) // one element before is a element that has a line break after
250
-                    || preg_match('/<\/'.$esteticBoxLikeElements.'(.*)>/Usi', $htmlArray[$x - 1]) || '<!--' == substr(
251
-                        $htmlArray[$x - 1],
252
-                        0,
253
-                        4
254
-                    ) || preg_match('/<'.$esteticBoxLikeElements.'(.*) \/>/Usi', $htmlArray[$x - 1]))
255
-            ) {
256
-                $newline = true;
257
-            } elseif ($this->formatType >= 4 && ( // logical line break
258
-                    // this element has a line break before itself
259
-                    preg_match(
260
-                        '/<'.$allBoxLikeElements.'(.*)>/Usi',
261
-                        $htmlArray[$x]
262
-                    ) || preg_match(
263
-                        '/<'.$allBoxLikeElements.'(.*) \/>/Usi',
264
-                        $htmlArray[$x]
265
-                    ) // one element before is a element that has a line break after
266
-                    || preg_match('/<\/'.$allBoxLikeElements.'(.*)>/Usi', $htmlArray[$x - 1]) || '<!--' == substr(
267
-                        $htmlArray[$x - 1],
268
-                        0,
269
-                        4
270
-                    ) || preg_match('/<'.$allBoxLikeElements.'(.*) \/>/Usi', $htmlArray[$x - 1]))
271
-            ) {
272
-                $newline = true;
273
-            }
274
-
275
-            // count down a tab
276
-            if ('</' == substr($htmlArray[$x], 0, 2)) {
277
-                --$tabs;
278
-            }
279
-
280
-            // add tabs and line breaks in front of the current tag
281
-            if ($newline) {
282
-                $html .= $this->newline;
283
-                for ($y = 0; $y < $tabs; ++$y) {
284
-                    $html .= $this->tab;
285
-                }
286
-            }
287
-
288
-            // remove white spaces and line breaks and add current tag to the html-string
289
-            if ('<![CDATA[' == substr($htmlArray[$x], 0, 9) // remove multiple white space in CDATA / XML
290
-                || '<?xml' == substr($htmlArray[$x], 0, 5)
291
-            ) {
292
-                $html .= $this->killWhiteSpace($htmlArray[$x]);
293
-            } else { // remove all line breaks
294
-                $html .= $this->killLineBreaks($htmlArray[$x]);
295
-            }
296
-
297
-            // count up a tab
298
-            if ('<' == substr($htmlArray[$x], 0, 1) && '/' != substr($htmlArray[$x], 1, 1)) {
299
-                if (' ' !== substr($htmlArray[$x], 1, 1)
300
-                    && 'img' !== substr($htmlArray[$x], 1, 3)
301
-                    && 'source' !== substr($htmlArray[$x], 1, 6)
302
-                    && 'br' !== substr($htmlArray[$x], 1, 2)
303
-                    && 'hr' !== substr($htmlArray[$x], 1, 2)
304
-                    && 'input' !== substr($htmlArray[$x], 1, 5)
305
-                    && 'link' !== substr($htmlArray[$x], 1, 4)
306
-                    && 'meta' !== substr($htmlArray[$x], 1, 4)
307
-                    && 'col ' !== substr($htmlArray[$x], 1, 4)
308
-                    && 'frame' !== substr($htmlArray[$x], 1, 5)
309
-                    && 'isindex' !== substr($htmlArray[$x], 1, 7)
310
-                    && 'param' !== substr($htmlArray[$x], 1, 5)
311
-                    && 'area' !== substr($htmlArray[$x], 1, 4)
312
-                    && 'base' !== substr($htmlArray[$x], 1, 4)
313
-                    && '<!' !== substr($htmlArray[$x], 0, 2)
314
-                    && '<?xml' !== substr($htmlArray[$x], 0, 5)
315
-                ) {
316
-                    ++$tabs;
317
-                }
318
-            }
319
-        }
320
-
321
-        // Remove empty lines
322
-        if ($this->formatType > 1) {
323
-            $this->removeEmptyLines($html);
324
-        }
325
-
326
-        // Restore saved comments, styles and java-scripts
327
-        for ($i = 0; $i < \count($noFormat); ++$i) {
328
-            $html = str_replace("<!-- ELEMENT {$i} -->", $noFormat[$i], $html);
329
-        }
330
-
331
-        // include debug comment at the end
332
-        if (0 != $tabs && true === $this->debugComment) {
333
-            $html .= "<!-- {$tabs} open elements found -->";
334
-        }
335
-
336
-        return $html;
337
-    }
338
-
339
-    /**
340
-     * Remove ALL line breaks and multiple white space.
341
-     *
342
-     * @param string $html
343
-     *
344
-     * @return string
345
-     */
346
-    protected function killLineBreaks($html)
347
-    {
348
-        $html = str_replace($this->newline, '', $html);
349
-
350
-        return preg_replace('/\s\s+/u', ' ', $html);
351
-        //? return preg_replace('/\n|\s+(\s)/u', '$1', $html);
352
-    }
353
-
354
-    /**
355
-     * Remove multiple white space, keeps line breaks.
356
-     *
357
-     * @param string $html
358
-     *
359
-     * @return string
360
-     */
361
-    protected function killWhiteSpace($html)
362
-    {
363
-        $temp = explode($this->newline, $html);
364
-        for ($i = 0; $i < \count($temp); ++$i) {
365
-            if (!trim($temp[$i])) {
366
-                unset($temp[$i]);
367
-                continue;
368
-            }
369
-
370
-            $temp[$i] = trim($temp[$i]);
371
-            $temp[$i] = preg_replace('/\s\s+/', ' ', $temp[$i]);
372
-        }
373
-
374
-        return implode($this->newline, $temp);
375
-    }
376
-
377
-    /**
378
-     * Remove white space at the end of lines, keeps other white space and line breaks.
379
-     *
380
-     * @param string $html
381
-     *
382
-     * @return string
383
-     */
384
-    protected function rTrimLines(&$html)
385
-    {
386
-        $html = preg_replace('/\s+$/m', '', $html);
387
-    }
388
-
389
-    /**
390
-     * Convert newlines according to the current OS.
391
-     *
392
-     * @param string $html
393
-     *
394
-     * @return string
395
-     */
396
-    protected function convNlOs(&$html)
397
-    {
398
-        $html = preg_replace("(\r\n|\r)", $this->newline, $html);
399
-    }
400
-
401
-    /**
402
-     * Remove empty lines.
403
-     *
404
-     * @param string $html
405
-     */
406
-    protected function removeEmptyLines(&$html): void
407
-    {
408
-        $temp = explode($this->newline, $html);
409
-        $result = [];
410
-        for ($i = 0; $i < \count($temp); ++$i) {
411
-            if ('' == trim($temp[$i])) {
412
-                continue;
413
-            }
414
-            $result[] = $temp[$i];
415
-        }
416
-        $html = implode($this->newline, $result);
417
-    }
418
-
419
-    /**
420
-     * Include configured header comment in HTML content block.
421
-     *
422
-     * @param $html
423
-     */
424
-    public function includeHeaderComment(&$html): void
425
-    {
426
-        $html = preg_replace('/^(-->)$/m', "\n\t".$this->headerComment."\n$1", $html);
427
-    }
20
+	/**
21
+	 * Enable Debug comment in footer.
22
+	 *
23
+	 * @var bool
24
+	 */
25
+	protected $debugComment = false;
26
+
27
+	/**
28
+	 * Format Type.
29
+	 *
30
+	 * @var int
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
+	 *
58
+	 * @var string
59
+	 */
60
+	protected $emptySpaceChar = ' ';
61
+
62
+	/**
63
+	 * Set variables based on given config.
64
+	 */
65
+	public function setVariables(array $config): void
66
+	{
67
+		if (!empty($config)) {
68
+			if ($config['formatHtml'] && is_numeric($config['formatHtml'])) {
69
+				$this->formatType = (int) $config['formatHtml'];
70
+			}
71
+
72
+			if ($config['formatHtml.']['tabSize'] && is_numeric($config['formatHtml.']['tabSize'])) {
73
+				$this->tab = str_pad('', $config['formatHtml.']['tabSize'], ' ');
74
+			}
75
+
76
+			if (isset($config['formatHtml.']['debugComment'])) {
77
+				$this->debugComment = (bool) $config['formatHtml.']['debugComment'];
78
+			}
79
+
80
+			if (isset($config['headerComment'])) {
81
+				$this->headerComment = $config['headerComment'];
82
+			}
83
+
84
+			if (isset($config['dropEmptySpaceChar']) && (bool) $config['dropEmptySpaceChar']) {
85
+				$this->emptySpaceChar = '';
86
+			}
87
+		}
88
+	}
89
+
90
+	/**
91
+	 * Clean given HTML with formatter.
92
+	 *
93
+	 * @param string $html
94
+	 * @param array  $config
95
+	 *
96
+	 * @return string
97
+	 */
98
+	public function clean($html, $config = [])
99
+	{
100
+		if (!empty($config)) {
101
+			$this->setVariables($config);
102
+		}
103
+		// convert line-breaks to UNIX
104
+		$this->convNlOs($html);
105
+
106
+		$manipulations = [];
107
+
108
+		if (isset($config['removeGenerator']) && (bool) $config['removeGenerator']) {
109
+			$manipulations['removeGenerator'] = GeneralUtility::makeInstance(RemoveGenerator::class);
110
+		}
111
+
112
+		if (isset($config['removeComments']) && (bool) $config['removeComments']) {
113
+			$manipulations['removeComments'] = GeneralUtility::makeInstance(RemoveComments::class);
114
+		}
115
+
116
+		if (!empty($this->headerComment)) {
117
+			$this->includeHeaderComment($html);
118
+		}
119
+
120
+		foreach ($manipulations as $key => $manipulation) {
121
+			/** @var ManipulationInterface $manipulation */
122
+			$configuration = isset($config[$key.'.']) && \is_array($config[$key.'.']) ? $config[$key.'.'] : [];
123
+			$html = $manipulation->manipulate($html, $configuration);
124
+		}
125
+
126
+		// cleanup HTML5 self-closing elements
127
+		if (!isset($GLOBALS['TSFE']->config['config']['doctype'])
128
+			|| 'x' !== substr($GLOBALS['TSFE']->config['config']['doctype'], 0, 1)) {
129
+			$html = preg_replace(
130
+				'/<((?:area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)\s[^>]+?)\s?\/>/',
131
+				'<$1>',
132
+				$html
133
+			);
134
+		}
135
+
136
+		if ($this->formatType > 0) {
137
+			$html = $this->formatHtml($html);
138
+		}
139
+		// remove white space after line ending
140
+		$this->rTrimLines($html);
141
+
142
+		// recover line-breaks
143
+		if (Environment::isWindows()) {
144
+			$html = str_replace($this->newline, "\r\n", $html);
145
+		}
146
+
147
+		return $html;
148
+	}
149
+
150
+	/**
151
+	 * Formats the (X)HTML code:
152
+	 *  - taps according to the hirarchy of the tags
153
+	 *  - removes empty spaces between tags
154
+	 *  - removes linebreaks within tags (spares where necessary: pre, textarea, comments, ..)
155
+	 *  choose from five options:
156
+	 *    0 => off
157
+	 *    1 => no line break at all  (code in one line)
158
+	 *    2 => minimalistic line breaks (structure defining box-elements)
159
+	 *    3 => aesthetic line breaks (important box-elements)
160
+	 *    4 => logic line breaks (all box-elements)
161
+	 *    5 => max line breaks (all elements).
162
+	 *
163
+	 * @param string $html
164
+	 *
165
+	 * @return string
166
+	 */
167
+	protected function formatHtml($html)
168
+	{
169
+		// Save original formated comments, pre, textarea, styles and java-scripts & replace them with markers
170
+		preg_match_all(
171
+			'/(?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',
172
+			$html,
173
+			$matches
174
+		);
175
+		$noFormat = $matches[0]; // do not format these block elements
176
+		for ($i = 0; $i < \count($noFormat); ++$i) {
177
+			$html = str_replace($noFormat[$i], "\n<!-- ELEMENT {$i} -->", $html);
178
+		}
179
+
180
+		// define box elements for formatting
181
+		$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';
182
+		$functionalBoxElements = 'dd|dt|frameset|li|tbody|td|tfoot|th|thead|tr|colgroup';
183
+		$usableBoxElements = 'applet|button|del|iframe|ins|map|object|script';
184
+		$imagineBoxElements = 'html|body|head|meta|title|link|script|base|!--';
185
+		$allBoxLikeElements = '(?>'.$trueBoxElements.'|'.$functionalBoxElements.'|'.$usableBoxElements.'|'.$imagineBoxElements.')';
186
+		$esteticBoxLikeElements = '(?>html|head|body|meta name|title|div|table|h1|h2|h3|h4|h5|h6|p|form|pre|center|!--)';
187
+		$structureBoxLikeElements = '(?>html|head|body|div|!--)';
188
+
189
+		// split html into it's elements
190
+		$htmlArrayTemp = preg_split(
191
+			'/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/',
192
+			$html,
193
+			-1,
194
+			\PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY
195
+		);
196
+
197
+		if (false === $htmlArrayTemp) {
198
+			// Restore saved comments, styles and java-scripts
199
+			for ($i = 0; $i < \count($noFormat); ++$i) {
200
+				$html = str_replace("<!-- ELEMENT {$i} -->", $noFormat[$i], $html);
201
+			}
202
+
203
+			return $html;
204
+		}
205
+		// remove empty lines
206
+		$htmlArray = [''];
207
+		$index = 1;
208
+		for ($x = 0; $x < \count($htmlArrayTemp); ++$x) {
209
+			$text = trim($htmlArrayTemp[$x]);
210
+			$htmlArray[$index] = '' !== $text ? $htmlArrayTemp[$x] : $this->emptySpaceChar;
211
+			++$index;
212
+		}
213
+
214
+		// rebuild html
215
+		$html = '';
216
+		$tabs = 0;
217
+		for ($x = 0; $x < \count($htmlArray); ++$x) {
218
+			// check if the element should stand in a new line
219
+			$newline = false;
220
+			if ('<?xml' == substr($htmlArray[$x - 1], 0, 5)) {
221
+				$newline = true;
222
+			} elseif (2 == $this->formatType && ( // minimalistic line break
223
+					// this element has a line break before itself
224
+					preg_match(
225
+						'/<'.$structureBoxLikeElements.'(.*)>/Usi',
226
+						$htmlArray[$x]
227
+					) || preg_match(
228
+						'/<'.$structureBoxLikeElements.'(.*) \/>/Usi',
229
+						$htmlArray[$x]
230
+					) // one element before is a element that has a line break after
231
+					|| preg_match(
232
+						'/<\/'.$structureBoxLikeElements.'(.*)>/Usi',
233
+						$htmlArray[$x - 1]
234
+					) || '<!--' == substr(
235
+						$htmlArray[$x - 1],
236
+						0,
237
+						4
238
+					) || preg_match('/<'.$structureBoxLikeElements.'(.*) \/>/Usi', $htmlArray[$x - 1]))
239
+			) {
240
+				$newline = true;
241
+			} elseif (3 == $this->formatType && ( // aestetic line break
242
+					// this element has a line break before itself
243
+					preg_match(
244
+						'/<'.$esteticBoxLikeElements.'(.*)>/Usi',
245
+						$htmlArray[$x]
246
+					) || preg_match(
247
+						'/<'.$esteticBoxLikeElements.'(.*) \/>/Usi',
248
+						$htmlArray[$x]
249
+					) // one element before is a element that has a line break after
250
+					|| preg_match('/<\/'.$esteticBoxLikeElements.'(.*)>/Usi', $htmlArray[$x - 1]) || '<!--' == substr(
251
+						$htmlArray[$x - 1],
252
+						0,
253
+						4
254
+					) || preg_match('/<'.$esteticBoxLikeElements.'(.*) \/>/Usi', $htmlArray[$x - 1]))
255
+			) {
256
+				$newline = true;
257
+			} elseif ($this->formatType >= 4 && ( // logical line break
258
+					// this element has a line break before itself
259
+					preg_match(
260
+						'/<'.$allBoxLikeElements.'(.*)>/Usi',
261
+						$htmlArray[$x]
262
+					) || preg_match(
263
+						'/<'.$allBoxLikeElements.'(.*) \/>/Usi',
264
+						$htmlArray[$x]
265
+					) // one element before is a element that has a line break after
266
+					|| preg_match('/<\/'.$allBoxLikeElements.'(.*)>/Usi', $htmlArray[$x - 1]) || '<!--' == substr(
267
+						$htmlArray[$x - 1],
268
+						0,
269
+						4
270
+					) || preg_match('/<'.$allBoxLikeElements.'(.*) \/>/Usi', $htmlArray[$x - 1]))
271
+			) {
272
+				$newline = true;
273
+			}
274
+
275
+			// count down a tab
276
+			if ('</' == substr($htmlArray[$x], 0, 2)) {
277
+				--$tabs;
278
+			}
279
+
280
+			// add tabs and line breaks in front of the current tag
281
+			if ($newline) {
282
+				$html .= $this->newline;
283
+				for ($y = 0; $y < $tabs; ++$y) {
284
+					$html .= $this->tab;
285
+				}
286
+			}
287
+
288
+			// remove white spaces and line breaks and add current tag to the html-string
289
+			if ('<![CDATA[' == substr($htmlArray[$x], 0, 9) // remove multiple white space in CDATA / XML
290
+				|| '<?xml' == substr($htmlArray[$x], 0, 5)
291
+			) {
292
+				$html .= $this->killWhiteSpace($htmlArray[$x]);
293
+			} else { // remove all line breaks
294
+				$html .= $this->killLineBreaks($htmlArray[$x]);
295
+			}
296
+
297
+			// count up a tab
298
+			if ('<' == substr($htmlArray[$x], 0, 1) && '/' != substr($htmlArray[$x], 1, 1)) {
299
+				if (' ' !== substr($htmlArray[$x], 1, 1)
300
+					&& 'img' !== substr($htmlArray[$x], 1, 3)
301
+					&& 'source' !== substr($htmlArray[$x], 1, 6)
302
+					&& 'br' !== substr($htmlArray[$x], 1, 2)
303
+					&& 'hr' !== substr($htmlArray[$x], 1, 2)
304
+					&& 'input' !== substr($htmlArray[$x], 1, 5)
305
+					&& 'link' !== substr($htmlArray[$x], 1, 4)
306
+					&& 'meta' !== substr($htmlArray[$x], 1, 4)
307
+					&& 'col ' !== substr($htmlArray[$x], 1, 4)
308
+					&& 'frame' !== substr($htmlArray[$x], 1, 5)
309
+					&& 'isindex' !== substr($htmlArray[$x], 1, 7)
310
+					&& 'param' !== substr($htmlArray[$x], 1, 5)
311
+					&& 'area' !== substr($htmlArray[$x], 1, 4)
312
+					&& 'base' !== substr($htmlArray[$x], 1, 4)
313
+					&& '<!' !== substr($htmlArray[$x], 0, 2)
314
+					&& '<?xml' !== substr($htmlArray[$x], 0, 5)
315
+				) {
316
+					++$tabs;
317
+				}
318
+			}
319
+		}
320
+
321
+		// Remove empty lines
322
+		if ($this->formatType > 1) {
323
+			$this->removeEmptyLines($html);
324
+		}
325
+
326
+		// Restore saved comments, styles and java-scripts
327
+		for ($i = 0; $i < \count($noFormat); ++$i) {
328
+			$html = str_replace("<!-- ELEMENT {$i} -->", $noFormat[$i], $html);
329
+		}
330
+
331
+		// include debug comment at the end
332
+		if (0 != $tabs && true === $this->debugComment) {
333
+			$html .= "<!-- {$tabs} open elements found -->";
334
+		}
335
+
336
+		return $html;
337
+	}
338
+
339
+	/**
340
+	 * Remove ALL line breaks and multiple white space.
341
+	 *
342
+	 * @param string $html
343
+	 *
344
+	 * @return string
345
+	 */
346
+	protected function killLineBreaks($html)
347
+	{
348
+		$html = str_replace($this->newline, '', $html);
349
+
350
+		return preg_replace('/\s\s+/u', ' ', $html);
351
+		//? return preg_replace('/\n|\s+(\s)/u', '$1', $html);
352
+	}
353
+
354
+	/**
355
+	 * Remove multiple white space, keeps line breaks.
356
+	 *
357
+	 * @param string $html
358
+	 *
359
+	 * @return string
360
+	 */
361
+	protected function killWhiteSpace($html)
362
+	{
363
+		$temp = explode($this->newline, $html);
364
+		for ($i = 0; $i < \count($temp); ++$i) {
365
+			if (!trim($temp[$i])) {
366
+				unset($temp[$i]);
367
+				continue;
368
+			}
369
+
370
+			$temp[$i] = trim($temp[$i]);
371
+			$temp[$i] = preg_replace('/\s\s+/', ' ', $temp[$i]);
372
+		}
373
+
374
+		return implode($this->newline, $temp);
375
+	}
376
+
377
+	/**
378
+	 * Remove white space at the end of lines, keeps other white space and line breaks.
379
+	 *
380
+	 * @param string $html
381
+	 *
382
+	 * @return string
383
+	 */
384
+	protected function rTrimLines(&$html)
385
+	{
386
+		$html = preg_replace('/\s+$/m', '', $html);
387
+	}
388
+
389
+	/**
390
+	 * Convert newlines according to the current OS.
391
+	 *
392
+	 * @param string $html
393
+	 *
394
+	 * @return string
395
+	 */
396
+	protected function convNlOs(&$html)
397
+	{
398
+		$html = preg_replace("(\r\n|\r)", $this->newline, $html);
399
+	}
400
+
401
+	/**
402
+	 * Remove empty lines.
403
+	 *
404
+	 * @param string $html
405
+	 */
406
+	protected function removeEmptyLines(&$html): void
407
+	{
408
+		$temp = explode($this->newline, $html);
409
+		$result = [];
410
+		for ($i = 0; $i < \count($temp); ++$i) {
411
+			if ('' == trim($temp[$i])) {
412
+				continue;
413
+			}
414
+			$result[] = $temp[$i];
415
+		}
416
+		$html = implode($this->newline, $result);
417
+	}
418
+
419
+	/**
420
+	 * Include configured header comment in HTML content block.
421
+	 *
422
+	 * @param $html
423
+	 */
424
+	public function includeHeaderComment(&$html): void
425
+	{
426
+		$html = preg_replace('/^(-->)$/m', "\n\t".$this->headerComment."\n$1", $html);
427
+	}
428 428
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -191,7 +191,7 @@
 block discarded – undo
191 191
             '/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/',
192 192
             $html,
193 193
             -1,
194
-            \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY
194
+            \PREG_SPLIT_DELIM_CAPTURE|\PREG_SPLIT_NO_EMPTY
195 195
         );
196 196
 
197 197
         if (false === $htmlArrayTemp) {
Please login to merge, or discard this patch.
Classes/Service/SvgStoreService.php 2 patches
Indentation   +193 added lines, -193 removed lines patch added patch discarded remove patch
@@ -12,197 +12,197 @@
 block discarded – undo
12 12
  */
13 13
 class SvgStoreService implements SingletonInterface
14 14
 {
15
-    /**
16
-     * SVG-Sprite storage DIR.
17
-     *
18
-     * @var string
19
-     */
20
-    protected $outputDir = '/typo3temp/assets/svg/'; // fallback
21
-
22
-    public function __construct()
23
-    {
24
-        //$this->styl = []; # https://stackoverflow.com/questions/39583880/external-svg-fails-to-apply-internal-css
25
-        //$this->defs = []; # https://bugs.chromium.org/p/chromium/issues/detail?id=751733#c14
26
-        $this->svgs = [];
27
-
28
-        $this->sitePath = \TYPO3\CMS\Core\Core\Environment::getPublicPath(); // [^/]$
29
-
30
-        if (isset($GLOBALS['TSFE']->config['config']['svgstore.']['outputDir']) && !empty($GLOBALS['TSFE']->config['config']['svgstore.']['outputDir'])) {
31
-            $this->outputDir = '/typo3temp/'.$GLOBALS['TSFE']->config['config']['svgstore.']['outputDir'];
32
-        }
33
-
34
-        $this->connPool = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class);
35
-        $this->svgCache = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class)->getCache('svgstore');
36
-    }
37
-
38
-    public function process(string $html): string
39
-    {
40
-        $this->spritePath = $this->svgCache->get('spritePath');
41
-        $this->svgFileArr = $this->svgCache->get('svgFileArr');
42
-
43
-        if (empty($this->spritePath) && !$this->populateCache()) {
44
-            throw new \Exception('could not write file: '.$this->sitePath.$this->spritePath);
45
-        }
46
-
47
-        if (!file_exists($this->sitePath.$this->spritePath)) {
48
-            throw new \Exception('file does not exists: '.$this->sitePath.$this->spritePath);
49
-        }
50
-
51
-        if (!preg_match('/(?<head>.+?<\/head>)(?<body>.+)/s', $html, $html) && 5 == \count($html)) {
52
-            throw new \Exception('fix HTML!');
53
-        }
54
-
55
-        // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attributes
56
-    $html['body'] = preg_replace_callback('/<img(?<pre>[^>]*)src="(?<src>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>/s', function (array $matches): string {// ^[/]
57
-      if (!isset($this->svgFileArr[$matches['src']])) {// check usage
58
-        return $matches[0];
59
-      }
60
-
61
-        $attr = preg_replace('/\s(?:alt|ismap|loading|title|sizes|srcset|usemap)="[^"]*"/', '', $matches['pre'].$matches['post']); // cleanup
62
-
63
-        return sprintf('<svg%s %s><use href="%s#%s"/></svg>', $this->svgFileArr[$matches['src']]['attr'], $attr, $this->spritePath, $this->convertFilePath($matches['src']));
64
-    }, $html['body']);
65
-
66
-        // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/object#attributes
67
-    $html['body'] = preg_replace_callback('/<object(?<pre>[^>]*)data="(?<data>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?:<\/object>)/s', function (array $matches): string {// ^[/]
68
-      if (!isset($this->svgFileArr[$matches['data']])) {// check usage
69
-        return $matches[0];
70
-      }
71
-
72
-        $attr = preg_replace('/\s(?:form|name|type|usemap)="[^"]*"/', '', $matches['pre'].$matches['post']); // cleanup
73
-
74
-        return sprintf('<svg%s %s><use href="%s#%s"/></svg>', $this->svgFileArr[$matches['src']]['attr'], $attr, $this->spritePath, $this->convertFilePath($matches['data']));
75
-    }, $html['body']);
76
-
77
-        return $html['head'].$html['body'];
78
-    }
79
-
80
-    private function convertFilePath(string $path): string
81
-    {
82
-        return preg_replace('/.svg$|[^\w\-]/', '', str_replace('/', '-', ltrim($path, '/'))); // ^[^/]
83
-    }
84
-
85
-    private function addFileToSpriteArr(string $hash, string $path): ?array
86
-    {
87
-        if (1 === preg_match('/;base64/', $svg = file_get_contents($this->sitePath.$path))) {// noop!
88
-            return null;
89
-        }
90
-
91
-        if (1 === preg_match('/<(?:style|defs|url\()/', $svg)) {// check links @ __construct
92
-            return null;
93
-        }
94
-
95
-        $svg = preg_replace('/<\/svg>.*|xlink:|\s(?:(?:width|height|version|xmlns)|(?:[a-z\-]+\:[a-z\-]+))="[^"]*"/s', '', $svg); // clean !?: \s+(?<atr>[\w\-]+)=["\'](?<val>[^"\']+)["\']
96
-
97
-        //$svg = preg_replace('/((?:id|class)=")/','$1'.$hash.'__',$svg);// extend  IDs
98
-        //$svg = preg_replace('/(href="|url\()#/','$1#'.$hash.'__',$svg);// recover IDs
99
-
100
-        //$svg = preg_replace_callback('/<style[^>]*>(?<styl>.+?)<\/style>|<defs[^>]*>(?<defs>.+?)<\/defs>/s',function(array $matches) use($hash): string
101
-        //{
102
-        //  if(isset($matches['styl']))
103
-        //  {
104
-        //    $this->styl[] = preg_replace('/\s*(\.|#){1}(.+?)\s*\{/','$1'.$hash.'__$2{',$matches['styl']); // patch CSS # https://mathiasbynens.be/notes/css-escapes
105
-        //  }
106
-        //  if(isset($matches['defs']))
107
-        //  {
108
-        //    $this->defs[] = trim($matches['defs']);
109
-        //  }
110
-        //  return '';
111
-        //},$svg);
112
-
113
-        $this->svgs[] = preg_replace('/.*<svg((?:(?!id=)[^>])+)(?:id="[^"]*")?([^>]*>)/s', 'id="'.$this->convertFilePath($path).'"$1$2', $svg, 1); // change ID;
114
-
115
-        return preg_match('/\s+viewBox="\s*([+-]?[\d\.]+(?:\s+[+-]?[\d\.]+){3})\s*"/', $svg, $match) ? ['attr' => ' viewBox="'.preg_replace('/\s+/', ' ', $match[1]).'"', 'hash' => $hash] : null;
116
-    }
117
-
118
-    private function populateCache(): bool
119
-    {
120
-        $storageArr = $this->getStorageArrayFromDB();
121
-        $svgFileArr = $this->getSvgFilesArrayFromDB(array_keys($storageArr));
122
-
123
-        $this->svgFileArr = [];
124
-        foreach ($svgFileArr as $index => $row) {
125
-            if (!$this->svgFileArr[($row['path'] = '/'.$storageArr[$row['storage']].$row['identifier'])] = $this->addFileToSpriteArr($row['sha1'], $row['path'])) {// ^[/]
126
-                unset($this->svgFileArr[$row['path']]);
127
-            }
128
-        }
129
-
130
-        $svg = preg_replace_callback(
131
-        '/<use(?<pre>.*?)(?:xlink:)?href="(?<href>\/.+?\.svg)#[^"]+"(?<post>.*?)[\s\/]*>(?:<\/use>)?/s',
132
-        function (array $matches): string {
133
-          return sprintf('<use%s href="#%s"/>', $matches['pre'].$matches['post'], $this->convertFilePath($matches['href']));
134
-      },
135
-        '<svg xmlns="http://www.w3.org/2000/svg">'
136
-      //."\n<style>\n".implode("\n",$this->styl)."\n</style>"
137
-      //."\n<defs>\n".implode("\n",$this->defs)."\n</defs>"
138
-      ."\n<symbol ".implode("</symbol>\n<symbol ", $this->svgs)."</symbol>\n"
139
-      .'</svg>'
140
-    );
141
-
142
-        //unset($this->styl);// save MEM
143
-    //unset($this->defs);// save MEM
144
-    unset($this->svgs); // save MEM
145
-
146
-    if (\is_int($var = $GLOBALS['TSFE']->config['config']['sourceopt.']['formatHtml']) && 1 == $var) {
147
-        $svg = preg_replace('/[\n\r\t\v\0]|\s{2,}/', '', $svg);
148
-    }
149
-
150
-        $svg = preg_replace('/<([a-z]+)\s*(\/|>\s*<\/\1)>\s*/i', '', $svg); // remove emtpy
151
-    $svg = preg_replace('/<((circle|ellipse|line|path|polygon|polyline|rect|stop|use)\s[^>]+?)\s*>\s*<\/\2>/', '<$1/>', $svg); // shorten/minify
152
-
153
-    if (!is_dir($this->sitePath.$this->outputDir)) {
154
-        GeneralUtility::mkdir_deep($this->sitePath.$this->outputDir);
155
-    }
156
-
157
-        $this->spritePath = $this->outputDir.hash('sha1', serialize($this->svgFileArr)).'.svg';
158
-        if (false === file_put_contents($this->sitePath.$this->spritePath, $svg)) {
159
-            return false;
160
-        }
161
-        unset($svg); // save MEM
162
-
163
-        $this->svgCache->set('svgFileArr', $this->svgFileArr);
164
-        $this->svgCache->set('spritePath', $this->spritePath);
165
-
166
-        return true;
167
-    }
168
-
169
-    private function getStorageArrayFromDB(): array
170
-    {
171
-        $storageResources = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\StorageRepository::class)->findAll();
172
-        foreach ($storageResources as $storage) {
173
-            if ('relative' == $storage->getConfiguration()['pathType']) {
174
-                $storageResources[$storage->getUid()] = rtrim($storage->getConfiguration()['basePath'], '/'); // [^/]$
175
-            }
176
-        }
177
-
178
-        unset($storageResources[0]); // keep!
179
-
180
-        return $storageResources;
181
-    }
182
-
183
-    private function getSvgFilesArrayFromDB(array $storageIds): array
184
-    {
185
-        return ($queryBuilder = $this->connPool->getQueryBuilderForTable('sys_file'))
186
-            ->select('sys_file.storage', 'sys_file.identifier', 'sys_file.sha1')
187
-            ->from('sys_file')
188
-            ->innerJoin(
189
-                'sys_file',
190
-                'sys_file_reference',
191
-                'sys_file_reference',
192
-                $queryBuilder->expr()->eq(
193
-                    'sys_file_reference.uid_local',
194
-                    $queryBuilder->quoteIdentifier('sys_file.uid')
195
-                )
196
-            )
197
-            ->where(
198
-                $queryBuilder->expr()->in('sys_file.storage', $queryBuilder->createNamedParameter($storageIds, \TYPO3\CMS\Core\Database\Connection::PARAM_INT_ARRAY)),
199
-                $queryBuilder->expr()->eq('sys_file.mime_type', $queryBuilder->createNamedParameter('image/svg+xml')),
200
-                $queryBuilder->expr()->lt('sys_file.size', $queryBuilder->createNamedParameter($GLOBALS['TSFE']->config['config']['svgstore.']['fileSize'])),
201
-            )
202
-            ->groupBy('sys_file.uid')
203
-            ->orderBy('sys_file.uid')
204
-            ->execute()
205
-            ->fetchAll()// TODO; use stdClass
206
-        ;
207
-    }
15
+	/**
16
+	 * SVG-Sprite storage DIR.
17
+	 *
18
+	 * @var string
19
+	 */
20
+	protected $outputDir = '/typo3temp/assets/svg/'; // fallback
21
+
22
+	public function __construct()
23
+	{
24
+		//$this->styl = []; # https://stackoverflow.com/questions/39583880/external-svg-fails-to-apply-internal-css
25
+		//$this->defs = []; # https://bugs.chromium.org/p/chromium/issues/detail?id=751733#c14
26
+		$this->svgs = [];
27
+
28
+		$this->sitePath = \TYPO3\CMS\Core\Core\Environment::getPublicPath(); // [^/]$
29
+
30
+		if (isset($GLOBALS['TSFE']->config['config']['svgstore.']['outputDir']) && !empty($GLOBALS['TSFE']->config['config']['svgstore.']['outputDir'])) {
31
+			$this->outputDir = '/typo3temp/'.$GLOBALS['TSFE']->config['config']['svgstore.']['outputDir'];
32
+		}
33
+
34
+		$this->connPool = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class);
35
+		$this->svgCache = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class)->getCache('svgstore');
36
+	}
37
+
38
+	public function process(string $html): string
39
+	{
40
+		$this->spritePath = $this->svgCache->get('spritePath');
41
+		$this->svgFileArr = $this->svgCache->get('svgFileArr');
42
+
43
+		if (empty($this->spritePath) && !$this->populateCache()) {
44
+			throw new \Exception('could not write file: '.$this->sitePath.$this->spritePath);
45
+		}
46
+
47
+		if (!file_exists($this->sitePath.$this->spritePath)) {
48
+			throw new \Exception('file does not exists: '.$this->sitePath.$this->spritePath);
49
+		}
50
+
51
+		if (!preg_match('/(?<head>.+?<\/head>)(?<body>.+)/s', $html, $html) && 5 == \count($html)) {
52
+			throw new \Exception('fix HTML!');
53
+		}
54
+
55
+		// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attributes
56
+	$html['body'] = preg_replace_callback('/<img(?<pre>[^>]*)src="(?<src>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>/s', function (array $matches): string {// ^[/]
57
+	  if (!isset($this->svgFileArr[$matches['src']])) {// check usage
58
+		return $matches[0];
59
+	  }
60
+
61
+		$attr = preg_replace('/\s(?:alt|ismap|loading|title|sizes|srcset|usemap)="[^"]*"/', '', $matches['pre'].$matches['post']); // cleanup
62
+
63
+		return sprintf('<svg%s %s><use href="%s#%s"/></svg>', $this->svgFileArr[$matches['src']]['attr'], $attr, $this->spritePath, $this->convertFilePath($matches['src']));
64
+	}, $html['body']);
65
+
66
+		// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/object#attributes
67
+	$html['body'] = preg_replace_callback('/<object(?<pre>[^>]*)data="(?<data>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?:<\/object>)/s', function (array $matches): string {// ^[/]
68
+	  if (!isset($this->svgFileArr[$matches['data']])) {// check usage
69
+		return $matches[0];
70
+	  }
71
+
72
+		$attr = preg_replace('/\s(?:form|name|type|usemap)="[^"]*"/', '', $matches['pre'].$matches['post']); // cleanup
73
+
74
+		return sprintf('<svg%s %s><use href="%s#%s"/></svg>', $this->svgFileArr[$matches['src']]['attr'], $attr, $this->spritePath, $this->convertFilePath($matches['data']));
75
+	}, $html['body']);
76
+
77
+		return $html['head'].$html['body'];
78
+	}
79
+
80
+	private function convertFilePath(string $path): string
81
+	{
82
+		return preg_replace('/.svg$|[^\w\-]/', '', str_replace('/', '-', ltrim($path, '/'))); // ^[^/]
83
+	}
84
+
85
+	private function addFileToSpriteArr(string $hash, string $path): ?array
86
+	{
87
+		if (1 === preg_match('/;base64/', $svg = file_get_contents($this->sitePath.$path))) {// noop!
88
+			return null;
89
+		}
90
+
91
+		if (1 === preg_match('/<(?:style|defs|url\()/', $svg)) {// check links @ __construct
92
+			return null;
93
+		}
94
+
95
+		$svg = preg_replace('/<\/svg>.*|xlink:|\s(?:(?:width|height|version|xmlns)|(?:[a-z\-]+\:[a-z\-]+))="[^"]*"/s', '', $svg); // clean !?: \s+(?<atr>[\w\-]+)=["\'](?<val>[^"\']+)["\']
96
+
97
+		//$svg = preg_replace('/((?:id|class)=")/','$1'.$hash.'__',$svg);// extend  IDs
98
+		//$svg = preg_replace('/(href="|url\()#/','$1#'.$hash.'__',$svg);// recover IDs
99
+
100
+		//$svg = preg_replace_callback('/<style[^>]*>(?<styl>.+?)<\/style>|<defs[^>]*>(?<defs>.+?)<\/defs>/s',function(array $matches) use($hash): string
101
+		//{
102
+		//  if(isset($matches['styl']))
103
+		//  {
104
+		//    $this->styl[] = preg_replace('/\s*(\.|#){1}(.+?)\s*\{/','$1'.$hash.'__$2{',$matches['styl']); // patch CSS # https://mathiasbynens.be/notes/css-escapes
105
+		//  }
106
+		//  if(isset($matches['defs']))
107
+		//  {
108
+		//    $this->defs[] = trim($matches['defs']);
109
+		//  }
110
+		//  return '';
111
+		//},$svg);
112
+
113
+		$this->svgs[] = preg_replace('/.*<svg((?:(?!id=)[^>])+)(?:id="[^"]*")?([^>]*>)/s', 'id="'.$this->convertFilePath($path).'"$1$2', $svg, 1); // change ID;
114
+
115
+		return preg_match('/\s+viewBox="\s*([+-]?[\d\.]+(?:\s+[+-]?[\d\.]+){3})\s*"/', $svg, $match) ? ['attr' => ' viewBox="'.preg_replace('/\s+/', ' ', $match[1]).'"', 'hash' => $hash] : null;
116
+	}
117
+
118
+	private function populateCache(): bool
119
+	{
120
+		$storageArr = $this->getStorageArrayFromDB();
121
+		$svgFileArr = $this->getSvgFilesArrayFromDB(array_keys($storageArr));
122
+
123
+		$this->svgFileArr = [];
124
+		foreach ($svgFileArr as $index => $row) {
125
+			if (!$this->svgFileArr[($row['path'] = '/'.$storageArr[$row['storage']].$row['identifier'])] = $this->addFileToSpriteArr($row['sha1'], $row['path'])) {// ^[/]
126
+				unset($this->svgFileArr[$row['path']]);
127
+			}
128
+		}
129
+
130
+		$svg = preg_replace_callback(
131
+		'/<use(?<pre>.*?)(?:xlink:)?href="(?<href>\/.+?\.svg)#[^"]+"(?<post>.*?)[\s\/]*>(?:<\/use>)?/s',
132
+		function (array $matches): string {
133
+		  return sprintf('<use%s href="#%s"/>', $matches['pre'].$matches['post'], $this->convertFilePath($matches['href']));
134
+	  },
135
+		'<svg xmlns="http://www.w3.org/2000/svg">'
136
+	  //."\n<style>\n".implode("\n",$this->styl)."\n</style>"
137
+	  //."\n<defs>\n".implode("\n",$this->defs)."\n</defs>"
138
+	  ."\n<symbol ".implode("</symbol>\n<symbol ", $this->svgs)."</symbol>\n"
139
+	  .'</svg>'
140
+	);
141
+
142
+		//unset($this->styl);// save MEM
143
+	//unset($this->defs);// save MEM
144
+	unset($this->svgs); // save MEM
145
+
146
+	if (\is_int($var = $GLOBALS['TSFE']->config['config']['sourceopt.']['formatHtml']) && 1 == $var) {
147
+		$svg = preg_replace('/[\n\r\t\v\0]|\s{2,}/', '', $svg);
148
+	}
149
+
150
+		$svg = preg_replace('/<([a-z]+)\s*(\/|>\s*<\/\1)>\s*/i', '', $svg); // remove emtpy
151
+	$svg = preg_replace('/<((circle|ellipse|line|path|polygon|polyline|rect|stop|use)\s[^>]+?)\s*>\s*<\/\2>/', '<$1/>', $svg); // shorten/minify
152
+
153
+	if (!is_dir($this->sitePath.$this->outputDir)) {
154
+		GeneralUtility::mkdir_deep($this->sitePath.$this->outputDir);
155
+	}
156
+
157
+		$this->spritePath = $this->outputDir.hash('sha1', serialize($this->svgFileArr)).'.svg';
158
+		if (false === file_put_contents($this->sitePath.$this->spritePath, $svg)) {
159
+			return false;
160
+		}
161
+		unset($svg); // save MEM
162
+
163
+		$this->svgCache->set('svgFileArr', $this->svgFileArr);
164
+		$this->svgCache->set('spritePath', $this->spritePath);
165
+
166
+		return true;
167
+	}
168
+
169
+	private function getStorageArrayFromDB(): array
170
+	{
171
+		$storageResources = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\StorageRepository::class)->findAll();
172
+		foreach ($storageResources as $storage) {
173
+			if ('relative' == $storage->getConfiguration()['pathType']) {
174
+				$storageResources[$storage->getUid()] = rtrim($storage->getConfiguration()['basePath'], '/'); // [^/]$
175
+			}
176
+		}
177
+
178
+		unset($storageResources[0]); // keep!
179
+
180
+		return $storageResources;
181
+	}
182
+
183
+	private function getSvgFilesArrayFromDB(array $storageIds): array
184
+	{
185
+		return ($queryBuilder = $this->connPool->getQueryBuilderForTable('sys_file'))
186
+			->select('sys_file.storage', 'sys_file.identifier', 'sys_file.sha1')
187
+			->from('sys_file')
188
+			->innerJoin(
189
+				'sys_file',
190
+				'sys_file_reference',
191
+				'sys_file_reference',
192
+				$queryBuilder->expr()->eq(
193
+					'sys_file_reference.uid_local',
194
+					$queryBuilder->quoteIdentifier('sys_file.uid')
195
+				)
196
+			)
197
+			->where(
198
+				$queryBuilder->expr()->in('sys_file.storage', $queryBuilder->createNamedParameter($storageIds, \TYPO3\CMS\Core\Database\Connection::PARAM_INT_ARRAY)),
199
+				$queryBuilder->expr()->eq('sys_file.mime_type', $queryBuilder->createNamedParameter('image/svg+xml')),
200
+				$queryBuilder->expr()->lt('sys_file.size', $queryBuilder->createNamedParameter($GLOBALS['TSFE']->config['config']['svgstore.']['fileSize'])),
201
+			)
202
+			->groupBy('sys_file.uid')
203
+			->orderBy('sys_file.uid')
204
+			->execute()
205
+			->fetchAll()// TODO; use stdClass
206
+		;
207
+	}
208 208
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -53,7 +53,7 @@  discard block
 block discarded – undo
53 53
         }
54 54
 
55 55
         // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attributes
56
-    $html['body'] = preg_replace_callback('/<img(?<pre>[^>]*)src="(?<src>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>/s', function (array $matches): string {// ^[/]
56
+    $html['body'] = preg_replace_callback('/<img(?<pre>[^>]*)src="(?<src>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>/s', function(array $matches): string {// ^[/]
57 57
       if (!isset($this->svgFileArr[$matches['src']])) {// check usage
58 58
         return $matches[0];
59 59
       }
@@ -64,7 +64,7 @@  discard block
 block discarded – undo
64 64
     }, $html['body']);
65 65
 
66 66
         // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/object#attributes
67
-    $html['body'] = preg_replace_callback('/<object(?<pre>[^>]*)data="(?<data>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?:<\/object>)/s', function (array $matches): string {// ^[/]
67
+    $html['body'] = preg_replace_callback('/<object(?<pre>[^>]*)data="(?<data>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?:<\/object>)/s', function(array $matches): string {// ^[/]
68 68
       if (!isset($this->svgFileArr[$matches['data']])) {// check usage
69 69
         return $matches[0];
70 70
       }
@@ -129,7 +129,7 @@  discard block
 block discarded – undo
129 129
 
130 130
         $svg = preg_replace_callback(
131 131
         '/<use(?<pre>.*?)(?:xlink:)?href="(?<href>\/.+?\.svg)#[^"]+"(?<post>.*?)[\s\/]*>(?:<\/use>)?/s',
132
-        function (array $matches): string {
132
+        function(array $matches): string {
133 133
           return sprintf('<use%s href="#%s"/>', $matches['pre'].$matches['post'], $this->convertFilePath($matches['href']));
134 134
       },
135 135
         '<svg xmlns="http://www.w3.org/2000/svg">'
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
@@ -14,67 +14,67 @@
 block discarded – undo
14 14
  */
15 15
 class RemoveComments implements ManipulationInterface
16 16
 {
17
-    /**
18
-     * Patterns for white-listing comments inside content.
19
-     *
20
-     * @var array
21
-     */
22
-    protected $whiteListCommentsPatterns = [];
17
+	/**
18
+	 * Patterns for white-listing comments inside content.
19
+	 *
20
+	 * @var array
21
+	 */
22
+	protected $whiteListCommentsPatterns = [];
23 23
 
24
-    /**
25
-     * @param string $html          The original HTML
26
-     * @param array  $configuration Configuration
27
-     *
28
-     * @return string the manipulated HTML
29
-     */
30
-    public function manipulate($html, array $configuration = [])
31
-    {
32
-        if (isset($configuration['keep.'])) {
33
-            $this->whiteListCommentsPatterns = $configuration['keep.'];
34
-        }
24
+	/**
25
+	 * @param string $html          The original HTML
26
+	 * @param array  $configuration Configuration
27
+	 *
28
+	 * @return string the manipulated HTML
29
+	 */
30
+	public function manipulate($html, array $configuration = [])
31
+	{
32
+		if (isset($configuration['keep.'])) {
33
+			$this->whiteListCommentsPatterns = $configuration['keep.'];
34
+		}
35 35
 
36
-        // match all styles, scripts and comments
37
-        $matches = [];
38
-        preg_match_all(
39
-            '/(?s)((<!--.*?-->)|(<[ \n\r]*style[^>]*>.*?<[ \n\r]*\/style[^>]*>)|(<[ \n\r]*script[^>]*>.*?<[ \n\r]*\/script[^>]*>))/im',
40
-            $html,
41
-            $matches
42
-        );
43
-        foreach ($matches[0] as $tag) {
44
-            if (false === $this->keepComment($tag)) {
45
-                $html = str_replace($tag, '', $html);
46
-            }
47
-        }
36
+		// match all styles, scripts and comments
37
+		$matches = [];
38
+		preg_match_all(
39
+			'/(?s)((<!--.*?-->)|(<[ \n\r]*style[^>]*>.*?<[ \n\r]*\/style[^>]*>)|(<[ \n\r]*script[^>]*>.*?<[ \n\r]*\/script[^>]*>))/im',
40
+			$html,
41
+			$matches
42
+		);
43
+		foreach ($matches[0] as $tag) {
44
+			if (false === $this->keepComment($tag)) {
45
+				$html = str_replace($tag, '', $html);
46
+			}
47
+		}
48 48
 
49
-        return $html;
50
-    }
49
+		return $html;
50
+	}
51 51
 
52
-    /**
53
-     * Check if a comment is defined to be kept in a pattern whiteListOfComments.
54
-     *
55
-     * @param string $commentHtml
56
-     *
57
-     * @return bool
58
-     */
59
-    protected function keepComment($commentHtml)
60
-    {
61
-        // if not even a comment, skip this
62
-        if (!preg_match('/^\<\!\-\-(.*?)\-\-\>$/usi', $commentHtml)) {
63
-            return true;
64
-        }
52
+	/**
53
+	 * Check if a comment is defined to be kept in a pattern whiteListOfComments.
54
+	 *
55
+	 * @param string $commentHtml
56
+	 *
57
+	 * @return bool
58
+	 */
59
+	protected function keepComment($commentHtml)
60
+	{
61
+		// if not even a comment, skip this
62
+		if (!preg_match('/^\<\!\-\-(.*?)\-\-\>$/usi', $commentHtml)) {
63
+			return true;
64
+		}
65 65
 
66
-        // if not defined in white list
67
-        if (!empty($this->whiteListCommentsPatterns)) {
68
-            $commentHtml = str_replace('<!--', '', $commentHtml);
69
-            $commentHtml = str_replace('-->', '', $commentHtml);
70
-            $commentHtml = trim($commentHtml);
71
-            foreach ($this->whiteListCommentsPatterns as $pattern) {
72
-                if (!empty($pattern) && preg_match($pattern, $commentHtml)) {
73
-                    return true;
74
-                }
75
-            }
76
-        }
66
+		// if not defined in white list
67
+		if (!empty($this->whiteListCommentsPatterns)) {
68
+			$commentHtml = str_replace('<!--', '', $commentHtml);
69
+			$commentHtml = str_replace('-->', '', $commentHtml);
70
+			$commentHtml = trim($commentHtml);
71
+			foreach ($this->whiteListCommentsPatterns as $pattern) {
72
+				if (!empty($pattern) && preg_match($pattern, $commentHtml)) {
73
+					return true;
74
+				}
75
+			}
76
+		}
77 77
 
78
-        return false;
79
-    }
78
+		return false;
79
+	}
80 80
 }
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
@@ -14,11 +14,11 @@
 block discarded – undo
14 14
  */
15 15
 interface ManipulationInterface
16 16
 {
17
-    /**
18
-     * @param string $html          The original HTML
19
-     * @param array  $configuration Configuration
20
-     *
21
-     * @return string the manipulated HTML
22
-     */
23
-    public function manipulate($html, array $configuration = []);
17
+	/**
18
+	 * @param string $html          The original HTML
19
+	 * @param array  $configuration Configuration
20
+	 *
21
+	 * @return string the manipulated HTML
22
+	 */
23
+	public function manipulate($html, array $configuration = []);
24 24
 }
Please login to merge, or discard this patch.
Classes/Manipulation/RemoveGenerator.php 1 patch
Indentation   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -14,16 +14,16 @@
 block discarded – undo
14 14
  */
15 15
 class RemoveGenerator implements ManipulationInterface
16 16
 {
17
-    /**
18
-     * @param string $html          The original HTML
19
-     * @param array  $configuration Configuration
20
-     *
21
-     * @return string the manipulated HTML
22
-     */
23
-    public function manipulate($html, array $configuration = [])
24
-    {
25
-        $regex = '<meta name=["\']?generator["\']? [^>]+>';
17
+	/**
18
+	 * @param string $html          The original HTML
19
+	 * @param array  $configuration Configuration
20
+	 *
21
+	 * @return string the manipulated HTML
22
+	 */
23
+	public function manipulate($html, array $configuration = [])
24
+	{
25
+		$regex = '<meta name=["\']?generator["\']? [^>]+>';
26 26
 
27
-        return preg_replace('/'.$regex.'/is', '', $html);
28
-    }
27
+		return preg_replace('/'.$regex.'/is', '', $html);
28
+	}
29 29
 }
Please login to merge, or discard this patch.
Classes/Middleware/CleanHtmlMiddleware.php 1 patch
Indentation   +35 added lines, -35 removed lines patch added patch discarded remove patch
@@ -19,39 +19,39 @@
 block discarded – undo
19 19
  */
20 20
 class CleanHtmlMiddleware implements MiddlewareInterface
21 21
 {
22
-    /**
23
-     * @var CleanHtmlService
24
-     */
25
-    protected $cleanHtmlService;
26
-
27
-    public function __construct()
28
-    {
29
-        $this->cleanHtmlService = GeneralUtility::makeInstance(CleanHtmlService::class);
30
-    }
31
-
32
-    /**
33
-     * Clean the HTML output.
34
-     */
35
-    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
36
-    {
37
-        $response = $handler->handle($request);
38
-
39
-        if (!($response instanceof NullResponse)
40
-        && $GLOBALS['TSFE'] instanceof TypoScriptFrontendController
41
-        && $GLOBALS['TSFE']->isOutputting()
42
-        && false !== (bool) $GLOBALS['TSFE']->config['config']['sourceopt.']['enabled']
43
-        ) {
44
-            $processedHtml = $this->cleanHtmlService->clean(
45
-                $response->getBody()->__toString(),
46
-                $GLOBALS['TSFE']->config['config']['sourceopt.']
47
-            );
48
-
49
-            // Replace old body with $processedHtml
50
-            $responseBody = new Stream('php://temp', 'rw');
51
-            $responseBody->write($processedHtml);
52
-            $response = $response->withBody($responseBody);
53
-        }
54
-
55
-        return $response;
56
-    }
22
+	/**
23
+	 * @var CleanHtmlService
24
+	 */
25
+	protected $cleanHtmlService;
26
+
27
+	public function __construct()
28
+	{
29
+		$this->cleanHtmlService = GeneralUtility::makeInstance(CleanHtmlService::class);
30
+	}
31
+
32
+	/**
33
+	 * Clean the HTML output.
34
+	 */
35
+	public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
36
+	{
37
+		$response = $handler->handle($request);
38
+
39
+		if (!($response instanceof NullResponse)
40
+		&& $GLOBALS['TSFE'] instanceof TypoScriptFrontendController
41
+		&& $GLOBALS['TSFE']->isOutputting()
42
+		&& false !== (bool) $GLOBALS['TSFE']->config['config']['sourceopt.']['enabled']
43
+		) {
44
+			$processedHtml = $this->cleanHtmlService->clean(
45
+				$response->getBody()->__toString(),
46
+				$GLOBALS['TSFE']->config['config']['sourceopt.']
47
+			);
48
+
49
+			// Replace old body with $processedHtml
50
+			$responseBody = new Stream('php://temp', 'rw');
51
+			$responseBody->write($processedHtml);
52
+			$response = $response->withBody($responseBody);
53
+		}
54
+
55
+		return $response;
56
+	}
57 57
 }
Please login to merge, or discard this patch.
Classes/Middleware/SvgStoreMiddleware.php 1 patch
Indentation   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -18,27 +18,27 @@
 block discarded – undo
18 18
  */
19 19
 class SvgStoreMiddleware implements MiddlewareInterface
20 20
 {
21
-    /**
22
-     * Search/Extract/Merge SVGs @ HTML output.
23
-     */
24
-    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
25
-    {
26
-        $response = $handler->handle($request);
21
+	/**
22
+	 * Search/Extract/Merge SVGs @ HTML output.
23
+	 */
24
+	public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
25
+	{
26
+		$response = $handler->handle($request);
27 27
 
28
-        if (!($response instanceof NullResponse)
29
-        && $GLOBALS['TSFE'] instanceof TypoScriptFrontendController
30
-        && $GLOBALS['TSFE']->isOutputting()
31
-        && false !== (bool) $GLOBALS['TSFE']->config['config']['svgstore.']['enabled']
32
-        ) {
33
-            $processedHtml = GeneralUtility::makeInstance(\HTML\Sourceopt\Service\SvgStoreService::class)
34
-                ->process($response->getBody()->__toString())
35
-            ;
28
+		if (!($response instanceof NullResponse)
29
+		&& $GLOBALS['TSFE'] instanceof TypoScriptFrontendController
30
+		&& $GLOBALS['TSFE']->isOutputting()
31
+		&& false !== (bool) $GLOBALS['TSFE']->config['config']['svgstore.']['enabled']
32
+		) {
33
+			$processedHtml = GeneralUtility::makeInstance(\HTML\Sourceopt\Service\SvgStoreService::class)
34
+				->process($response->getBody()->__toString())
35
+			;
36 36
 
37
-            $responseBody = new Stream('php://temp', 'rw');
38
-            $responseBody->write($processedHtml);
39
-            $response = $response->withBody($responseBody);
40
-        }
37
+			$responseBody = new Stream('php://temp', 'rw');
38
+			$responseBody->write($processedHtml);
39
+			$response = $response->withBody($responseBody);
40
+		}
41 41
 
42
-        return $response;
43
-    }
42
+		return $response;
43
+	}
44 44
 }
Please login to merge, or discard this patch.