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