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