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