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