-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
CleanHtmlService.php
415 lines (367 loc) · 14.3 KB
/
CleanHtmlService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<?php
declare(strict_types=1);
namespace HTML\Sourceopt\Service;
use HTML\Sourceopt\Manipulation\ManipulationInterface;
use HTML\Sourceopt\Manipulation\RemoveComments;
use HTML\Sourceopt\Manipulation\RemoveGenerator;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Service: Clean parsed HTML functionality
* Based on the extension 'sourceopt'.
*/
class CleanHtmlService implements SingletonInterface
{
/**
* Enable Debug comment in footer.
*
* @var bool
*/
protected $debugComment = false;
/**
* Format Type.
*
* @var int
*/
protected $formatType = 0;
/**
* Tab character.
*
* @var string
*/
protected $tab = "\t";
/**
* Newline character.
*
* @var string
*/
protected $newline = "\n";
/**
* Configured extra header comment.
*
* @var string
*/
protected $headerComment = '';
/**
* Empty space char.
*
* @var string
*/
protected $emptySpaceChar = ' ';
/**
* Set variables based on given config.
*/
public function setVariables(array $config): void
{
if (!empty($config)) {
if ($config['formatHtml'] && is_numeric($config['formatHtml'])) {
$this->formatType = (int) $config['formatHtml'];
}
if ($config['formatHtml.']['tabSize'] && is_numeric($config['formatHtml.']['tabSize'])) {
$this->tab = str_pad('', (int) $config['formatHtml.']['tabSize'], ' ');
}
if (isset($config['formatHtml.']['debugComment'])) {
$this->debugComment = (bool) $config['formatHtml.']['debugComment'];
}
if (isset($config['headerComment'])) {
$this->headerComment = $config['headerComment'];
}
if (isset($config['dropEmptySpaceChar']) && (bool) $config['dropEmptySpaceChar']) {
$this->emptySpaceChar = '';
}
}
}
/**
* Clean given HTML with formatter.
*
* @param string $html
* @param array $config
*
* @return string
*/
public function clean($html, $config = [])
{
if (!empty($config)) {
$this->setVariables($config);
}
// convert line-breaks to UNIX
$this->convNlOs($html);
$manipulations = [];
if (isset($config['removeGenerator']) && (bool) $config['removeGenerator']) {
$manipulations['removeGenerator'] = GeneralUtility::makeInstance(RemoveGenerator::class);
}
if (isset($config['removeComments']) && (bool) $config['removeComments']) {
$manipulations['removeComments'] = GeneralUtility::makeInstance(RemoveComments::class);
}
if (!empty($this->headerComment)) {
$this->includeHeaderComment($html);
}
foreach ($manipulations as $key => $manipulation) {
/** @var ManipulationInterface $manipulation */
$configuration = isset($config[$key.'.']) && \is_array($config[$key.'.']) ? $config[$key.'.'] : [];
$html = $manipulation->manipulate($html, $configuration);
}
// cleanup HTML5 self-closing elements
if (!isset($GLOBALS['TSFE']->config['config']['doctype'])
|| 'x' !== substr($GLOBALS['TSFE']->config['config']['doctype'], 0, 1)) {
$html = preg_replace(
'/<((?:area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)\s[^>]+?)\s?\/>/',
'<$1>',
$html
);
}
if ($this->formatType > 0) {
$html = $this->formatHtml($html);
}
// remove white space after line ending
$this->rTrimLines($html);
// recover line-breaks
if (Environment::isWindows()) {
$html = str_replace($this->newline, "\r\n", $html);
}
return $html;
}
/**
* Formats the (X)HTML code:
* - taps according to the hirarchy of the tags
* - removes empty spaces between tags
* - removes linebreaks within tags (spares where necessary: pre, textarea, comments, ..)
* choose from five options:
* 0 => off
* 1 => no line break at all (code in one line)
* 2 => minimalistic line breaks (structure defining box-elements)
* 3 => aesthetic line breaks (important box-elements)
* 4 => logic line breaks (all box-elements)
* 5 => max line breaks (all elements).
*
* @param string $html
*
* @return string
*/
protected function formatHtml($html)
{
// Save original formated pre, textarea, comments, styles and scripts & replace them with markers
preg_match_all(
'/(?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',
$html,
$matches
);
$noFormat = $matches[0]; // do not format these block elements
for ($i = 0; $i < \count($noFormat); ++$i) {
$html = str_replace($noFormat[$i], "\n<!-- ELEMENT {$i} -->", $html);
}
// define box elements for formatting
$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';
$functionalBoxElements = 'dd|dt|frameset|li|tbody|td|tfoot|th|thead|tr|colgroup';
$usableBoxElements = 'applet|button|del|iframe|ins|map|object|script';
$imagineBoxElements = 'html|body|head|meta|title|link|script|base|!--';
$allBoxLikeElements = '(?>'.$trueBoxElements.'|'.$functionalBoxElements.'|'.$usableBoxElements.'|'.$imagineBoxElements.')';
$esteticBoxLikeElements = '(?>html|head|body|meta name|title|div|table|h1|h2|h3|h4|h5|h6|p|form|pre|center|!--)';
$structureBoxLikeElements = '(?>html|head|body|div|!--)';
// split html into it's elements
$htmlArrayTemp = preg_split(
'/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/',
$html,
-1,
\PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY
);
if (false === $htmlArrayTemp) {
// Restore saved comments, styles and scripts
for ($i = 0; $i < \count($noFormat); ++$i) {
$html = str_replace("<!-- ELEMENT {$i} -->", $noFormat[$i], $html);
}
return $html;
}
// remove empty lines
$htmlArray = [''];
$index = 1;
for ($x = 0; $x < \count($htmlArrayTemp); ++$x) {
$text = trim($htmlArrayTemp[$x]);
$htmlArray[$index] = '' !== $text ? $htmlArrayTemp[$x] : $this->emptySpaceChar;
++$index;
}
// rebuild html
$html = '';
$tabs = 0;
for ($x = 0; $x < \count($htmlArray); ++$x) {
$htmlArrayBefore = $htmlArray[$x - 1] ?? '';
$htmlArrayCurrent = $htmlArray[$x] ?? '';
// check if the element should stand in a new line
$newline = false;
if ('<?xml' == substr($htmlArrayBefore, 0, 5)) {
$newline = true;
} elseif (2 == $this->formatType && ( // minimalistic line break
// this element has a line break before itself
preg_match(
'/<'.$structureBoxLikeElements.'(.*)>/Usi',
$htmlArrayCurrent
) || preg_match(
'/<'.$structureBoxLikeElements.'(.*) \/>/Usi',
$htmlArrayCurrent
) // one element before is a element that has a line break after
|| preg_match(
'/<\/'.$structureBoxLikeElements.'(.*)>/Usi',
$htmlArrayBefore
) || '<!--' == substr(
$htmlArrayBefore,
0,
4
) || preg_match('/<'.$structureBoxLikeElements.'(.*) \/>/Usi', $htmlArrayBefore))
) {
$newline = true;
} elseif (3 == $this->formatType && ( // aestetic line break
// this element has a line break before itself
preg_match(
'/<'.$esteticBoxLikeElements.'(.*)>/Usi',
$htmlArrayCurrent
) || preg_match(
'/<'.$esteticBoxLikeElements.'(.*) \/>/Usi',
$htmlArrayCurrent
) // one element before is a element that has a line break after
|| preg_match('/<\/'.$esteticBoxLikeElements.'(.*)>/Usi', $htmlArrayBefore) || '<!--' == substr(
$htmlArrayBefore,
0,
4
) || preg_match('/<'.$esteticBoxLikeElements.'(.*) \/>/Usi', $htmlArrayBefore))
) {
$newline = true;
} elseif ($this->formatType >= 4 && ( // logical line break
// this element has a line break before itself
preg_match(
'/<'.$allBoxLikeElements.'(.*)>/Usi',
$htmlArrayCurrent
) || preg_match(
'/<'.$allBoxLikeElements.'(.*) \/>/Usi',
$htmlArrayCurrent
) // one element before is a element that has a line break after
|| preg_match('/<\/'.$allBoxLikeElements.'(.*)>/Usi', $htmlArrayBefore) || '<!--' == substr(
$htmlArrayBefore,
0,
4
) || preg_match('/<'.$allBoxLikeElements.'(.*) \/>/Usi', $htmlArrayBefore))
) {
$newline = true;
}
// count down a tab
if ('</' == substr($htmlArrayCurrent, 0, 2)) {
--$tabs;
}
// add tabs and line breaks in front of the current tag
if ($newline) {
$html .= $this->newline;
for ($y = 0; $y < $tabs; ++$y) {
$html .= $this->tab;
}
}
// remove white spaces and line breaks and add current tag to the html-string
if ('<![CDATA[' == substr($htmlArrayCurrent, 0, 9) // remove multiple white space in CDATA / XML
|| '<?xml' == substr($htmlArrayCurrent, 0, 5)
) {
$html .= $this->killWhiteSpace($htmlArrayCurrent);
} else { // remove all line breaks
$html .= $this->killLineBreaks($htmlArrayCurrent);
}
// count up a tab
if ('<' == substr($htmlArrayCurrent, 0, 1) && '/' != substr($htmlArrayCurrent, 1, 1)) {
if (' ' !== substr($htmlArrayCurrent, 1, 1)
&& 'img' !== substr($htmlArrayCurrent, 1, 3)
&& 'source' !== substr($htmlArrayCurrent, 1, 6)
&& 'br' !== substr($htmlArrayCurrent, 1, 2)
&& 'hr' !== substr($htmlArrayCurrent, 1, 2)
&& 'input' !== substr($htmlArrayCurrent, 1, 5)
&& 'link' !== substr($htmlArrayCurrent, 1, 4)
&& 'meta' !== substr($htmlArrayCurrent, 1, 4)
&& 'col ' !== substr($htmlArrayCurrent, 1, 4)
&& 'frame' !== substr($htmlArrayCurrent, 1, 5)
&& 'isindex' !== substr($htmlArrayCurrent, 1, 7)
&& 'param' !== substr($htmlArrayCurrent, 1, 5)
&& 'area' !== substr($htmlArrayCurrent, 1, 4)
&& 'base' !== substr($htmlArrayCurrent, 1, 4)
&& '<!' !== substr($htmlArrayCurrent, 0, 2)
&& '<?xml' !== substr($htmlArrayCurrent, 0, 5)
) {
++$tabs;
}
}
}
// Remove empty lines
if ($this->formatType > 1) {
$this->removeEmptyLines($html);
}
// Restore saved comments, styles and scripts
for ($i = 0; $i < \count($noFormat); ++$i) {
$html = str_replace("<!-- ELEMENT {$i} -->", $noFormat[$i], $html);
}
// include debug comment at the end
if (0 != $tabs && true === $this->debugComment) {
$html .= "<!-- {$tabs} open elements found -->";
}
return $html;
}
/**
* Remove ALL line breaks and multiple white space.
*
* @param string $html
*
* @return string
*/
protected function killLineBreaks($html)
{
$html = str_replace($this->newline, '', $html);
return preg_replace('/\s\s+/u', ' ', $html);
// ? return preg_replace('/\n|\s+(\s)/u', '$1', $html);
}
/**
* Remove multiple white space, keeps line breaks.
*/
protected function killWhiteSpace(string $html): string
{
$temp = explode($this->newline, $html);
for ($i = 0; $i < \count($temp); ++$i) {
if (!trim($temp[$i])) {
unset($temp[$i]);
continue;
}
$temp[$i] = trim($temp[$i]);
$temp[$i] = preg_replace('/\s\s+/', ' ', $temp[$i]);
}
return implode($this->newline, $temp);
}
/**
* Remove white space at the end of lines, keeps other white space and line breaks.
*/
protected function rTrimLines(string &$html): void
{
$html = preg_replace('/\s+$/m', '', $html);
}
/**
* Convert newlines according to the current OS.
*/
protected function convNlOs(string &$html): void
{
$html = preg_replace("(\r\n|\r)", $this->newline, $html);
}
/**
* Remove empty lines.
*/
protected function removeEmptyLines(string &$html): void
{
$temp = explode($this->newline, $html);
$result = [];
for ($i = 0; $i < \count($temp); ++$i) {
if ('' == trim($temp[$i])) {
continue;
}
$result[] = $temp[$i];
}
$html = implode($this->newline, $result);
}
/**
* Include configured header comment in HTML content block.
*/
public function includeHeaderComment(string &$html): void
{
$html = preg_replace('/^(-->)$/m', "\n\t".$this->headerComment."\n$1", $html);
}
}