@@ -13,192 +13,192 @@ |
||
13 | 13 | */ |
14 | 14 | class SvgStoreService implements \TYPO3\CMS\Core\SingletonInterface |
15 | 15 | { |
16 | - /** |
|
17 | - * SVG-Sprite storage directory. |
|
18 | - * |
|
19 | - * @var string |
|
20 | - */ |
|
21 | - protected $outputDir = '/typo3temp/assets/svg/'; |
|
22 | - |
|
23 | - public function __construct() |
|
24 | - { |
|
25 | - //$this->styl = []; # https://stackoverflow.com/questions/39583880/external-svg-fails-to-apply-internal-css |
|
26 | - //$this->defs = []; # https://bugs.chromium.org/p/chromium/issues/detail?id=751733#c14 |
|
27 | - $this->svgs = []; |
|
28 | - |
|
29 | - $this->sitePath = \TYPO3\CMS\Core\Core\Environment::getPublicPath(); // [^/]$ |
|
30 | - $this->svgCache = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class)->getCache('svgstore'); |
|
31 | - } |
|
32 | - |
|
33 | - public function process(string $html): string |
|
34 | - { |
|
35 | - $this->spritePath = $this->svgCache->get('spritePath'); |
|
36 | - $this->svgFileArr = $this->svgCache->get('svgFileArr'); |
|
37 | - |
|
38 | - if (empty($this->spritePath) && !$this->populateCache()) { |
|
39 | - throw new \Exception('could not write file: '.$this->sitePath.$this->spritePath); |
|
40 | - } |
|
41 | - |
|
42 | - if (!file_exists($this->sitePath.$this->spritePath)) { |
|
43 | - throw new \Exception('file does not exists: '.$this->sitePath.$this->spritePath); |
|
44 | - } |
|
45 | - |
|
46 | - if ($GLOBALS['TSFE']->config['config']['disableAllHeaderCode'] ?? false) { |
|
47 | - $dom = ['head' => '', 'body' => $html]; |
|
48 | - } elseif (!preg_match('/(?<head>.+?<\/head>)(?<body>.+)/s', $html, $dom) && 5 == \count($dom)) { |
|
49 | - return $html; |
|
50 | - } |
|
51 | - |
|
52 | - // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attributes |
|
53 | - $dom['body'] = preg_replace_callback('/<img(?<pre>[^>]*)src="(?<src>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?!\s*<\/picture>)/s', function (array $match): string { // ^[/] |
|
54 | - if (!isset($this->svgFileArr[$match['src']])) { // check usage |
|
55 | - return $match[0]; |
|
56 | - } |
|
57 | - $attr = preg_replace('/\s(?:alt|ismap|loading|title|sizes|srcset|usemap|crossorigin|decoding|referrerpolicy)="[^"]*"/', '', $match['pre'].$match['post']); // cleanup |
|
58 | - |
|
59 | - return sprintf('<svg %s %s><use href="%s#%s"/></svg>', $this->svgFileArr[$match['src']]['attr'], trim($attr), $this->spritePath, $this->convertFilePath($match['src'])); |
|
60 | - }, $dom['body']); |
|
61 | - |
|
62 | - // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/object#attributes |
|
63 | - $dom['body'] = preg_replace_callback('/<object(?<pre>[^>]*)data="(?<data>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?:<\/object>)/s', function (array $match): string { // ^[/] |
|
64 | - if (!isset($this->svgFileArr[$match['data']])) { // check usage |
|
65 | - return $match[0]; |
|
66 | - } |
|
67 | - $attr = preg_replace('/\s(?:form|name|type|usemap)="[^"]*"/', '', $match['pre'].$match['post']); // cleanup |
|
68 | - |
|
69 | - return sprintf('<svg %s %s><use href="%s#%s"/></svg>', $this->svgFileArr[$match['data']]['attr'], trim($attr), $this->spritePath, $this->convertFilePath($match['data'])); |
|
70 | - }, $dom['body']); |
|
71 | - |
|
72 | - return $dom['head'].$dom['body']; |
|
73 | - } |
|
74 | - |
|
75 | - private function convertFilePath(string $path): string |
|
76 | - { |
|
77 | - return preg_replace('/.svg$|[^\w\-]/', '', str_replace('/', '-', ltrim($path, '/'))); // ^[^/] |
|
78 | - } |
|
79 | - |
|
80 | - private function addFileToSpriteArr(string $hash, string $path): ?array |
|
81 | - { |
|
82 | - if (!file_exists($this->sitePath.$path)) { |
|
83 | - return null; |
|
84 | - } |
|
85 | - |
|
86 | - if (preg_match('/(?:;base64|i:a?i?pgf)/', $svg = file_get_contents($this->sitePath.$path))) { // noop! |
|
87 | - return null; |
|
88 | - } |
|
89 | - |
|
90 | - if (preg_match('/<(?:style|defs)|url\(/', $svg)) { |
|
91 | - return null; // check links @ __construct |
|
92 | - } |
|
93 | - |
|
94 | - //$svg = preg_replace('/((?:id|class)=")/', '$1'.$hash.'__', $svg); // extend IDs |
|
95 | - //$svg = preg_replace('/(href="|url\()#/', '$1#'.$hash.'__', $svg); // recover IDs |
|
96 | - |
|
97 | - //$svg = preg_replace_callback('/<style[^>]*>(?<styl>.+?)<\/style>|<defs[^>]*>(?<defs>.+?)<\/defs>/s', function(array $match) use($hash): string { |
|
98 | - // |
|
99 | - // if(isset($match['styl'])) |
|
100 | - // { |
|
101 | - // $this->styl[] = preg_replace('/\s*(\.|#){1}(.+?)\s*\{/', '$1'.$hash.'__$2{', $match['styl']); // patch CSS # https://mathiasbynens.be/notes/css-escapes |
|
102 | - // } |
|
103 | - // if(isset($match['defs'])) |
|
104 | - // { |
|
105 | - // $this->defs[] = trim($match['defs']); |
|
106 | - // } |
|
107 | - // return ''; |
|
108 | - //}, $svg); |
|
109 | - |
|
110 | - // https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/xlink:href |
|
111 | - $svg = preg_replace('/^.*?<svg|\s*(<\/svg>)(?!.*\1).*$|xlink:|\s(?:(?:version|xmlns)|(?:[a-z\-]+\:[a-z\-]+))="[^"]*"/s', '', $svg); // cleanup |
|
112 | - |
|
113 | - // https://developer.mozilla.org/en-US/docs/Web/SVG/Element/svg#attributes |
|
114 | - $svg = preg_replace_callback('/([^>]*)\s*(?=>)/s', function (array $match) use (&$attr): string { |
|
115 | - if (false === preg_match_all('/(?!\s)(?<attr>[\w\-]+)="\s*(?<value>[^"]+)\s*"/', $match[1], $matches)) { |
|
116 | - return $match[0]; |
|
117 | - } |
|
118 | - foreach ($matches['attr'] as $index => $attribute) { |
|
119 | - switch ($attribute) { |
|
120 | - case 'id': |
|
121 | - case 'width': |
|
122 | - case 'height': |
|
123 | - unset($matches[0][$index]); |
|
124 | - break; |
|
125 | - |
|
126 | - case 'viewBox': |
|
127 | - if (false !== preg_match('/\S+\s\S+\s\+?(?<width>[\d\.]+)\s\+?(?<height>[\d\.]+)/', $matches['value'][$index], $match)) { |
|
128 | - $attr[] = sprintf('%s="0 0 %s %s"', $attribute, $match['width'], $match['height']); // save! |
|
129 | - } |
|
130 | - } |
|
131 | - } |
|
132 | - |
|
133 | - return implode(' ', $matches[0]); |
|
134 | - }, $svg, 1); |
|
135 | - |
|
136 | - if ($attr) { // TODO; beautify |
|
137 | - $this->svgs[] = sprintf('id="%s" %s', $this->convertFilePath($path), $svg); // prepend ID |
|
138 | - |
|
139 | - return ['attr' => implode(' ', $attr), 'hash' => $hash]; |
|
140 | - } |
|
141 | - |
|
142 | - return null; |
|
143 | - } |
|
144 | - |
|
145 | - private function populateCache(): bool |
|
146 | - { |
|
147 | - $storageArr = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\StorageRepository::class)->findAll(); |
|
148 | - foreach ($storageArr as $storage) { |
|
149 | - if ('relative' == $storage->getConfiguration()['pathType']) { |
|
150 | - $storageArr[$storage->getUid()] = rtrim($storage->getConfiguration()['basePath'], '/'); // [^/]$ |
|
151 | - } |
|
152 | - } |
|
153 | - unset($storageArr[0]); // keep! |
|
154 | - |
|
155 | - $svgFileArr = GeneralUtility::makeInstance(\HTML\Sourceopt\Resource\SvgFileRepository::class)->findAllByStorageUids(\array_keys($storageArr)); |
|
156 | - foreach ($svgFileArr as $index => $row) { |
|
157 | - if (!$this->svgFileArr[($row['path'] = '/'.$storageArr[$row['storage']].$row['identifier'])] = $this->addFileToSpriteArr($row['sha1'], $row['path'])) { // ^[/] |
|
158 | - unset($this->svgFileArr[$row['path']]); |
|
159 | - } |
|
160 | - } |
|
161 | - unset($storageArr, $svgFileArr); // save MEM |
|
162 | - |
|
163 | - $svg = preg_replace_callback( |
|
164 | - '/<use(?<pre>.*?)(?:xlink:)?href="(?<href>\/.+?\.svg)#[^"]+"(?<post>.*?)[\s\/]*>(?:<\/use>)?/s', |
|
165 | - function (array $match): string { |
|
166 | - if (!isset($this->svgFileArr[$match['href']])) { // check usage |
|
167 | - return $match[0]; |
|
168 | - } |
|
169 | - |
|
170 | - return sprintf('<use%s href="#%s"/>', $match['pre'].$match['post'], $this->convertFilePath($match['href'])); |
|
171 | - }, |
|
172 | - '<svg xmlns="http://www.w3.org/2000/svg">' |
|
173 | - //."\n<style>\n".implode("\n", $this->styl)."\n</style>" |
|
174 | - //."\n<defs>\n".implode("\n", $this->defs)."\n</defs>" |
|
175 | - ."\n<symbol ".implode("</symbol>\n<symbol ", $this->svgs)."</symbol>\n" |
|
176 | - .'</svg>' |
|
177 | - ); |
|
178 | - |
|
179 | - //unset($this->styl); // save MEM |
|
180 | - //unset($this->defs); // save MEM |
|
181 | - unset($this->svgs); // save MEM |
|
182 | - |
|
183 | - if ($GLOBALS['TSFE']->config['config']['sourceopt.']['formatHtml'] ?? false) { |
|
184 | - $svg = preg_replace('/[\n\r\t\v\0]|\s{2,}/', '', $svg); |
|
185 | - } |
|
186 | - |
|
187 | - $svg = preg_replace('/<([a-z]+)\s*(\/|>\s*<\/\1)>\s*/i', '', $svg); // remove emtpy |
|
188 | - $svg = preg_replace('/<((circle|ellipse|line|path|polygon|polyline|rect|stop|use)\s[^>]+?)\s*>\s*<\/\2>/', '<$1/>', $svg); // shorten/minify |
|
189 | - |
|
190 | - if (!is_dir($this->sitePath.$this->outputDir)) { |
|
191 | - GeneralUtility::mkdir_deep($this->sitePath.$this->outputDir); |
|
192 | - } |
|
193 | - |
|
194 | - $this->spritePath = $this->outputDir.hash('sha1', serialize($this->svgFileArr)).'.svg'; |
|
195 | - if (false === file_put_contents($this->sitePath.$this->spritePath, $svg)) { |
|
196 | - return false; |
|
197 | - } |
|
198 | - |
|
199 | - $this->svgCache->set('spritePath', $this->spritePath); |
|
200 | - $this->svgCache->set('svgFileArr', $this->svgFileArr); |
|
201 | - |
|
202 | - return true; |
|
203 | - } |
|
16 | + /** |
|
17 | + * SVG-Sprite storage directory. |
|
18 | + * |
|
19 | + * @var string |
|
20 | + */ |
|
21 | + protected $outputDir = '/typo3temp/assets/svg/'; |
|
22 | + |
|
23 | + public function __construct() |
|
24 | + { |
|
25 | + //$this->styl = []; # https://stackoverflow.com/questions/39583880/external-svg-fails-to-apply-internal-css |
|
26 | + //$this->defs = []; # https://bugs.chromium.org/p/chromium/issues/detail?id=751733#c14 |
|
27 | + $this->svgs = []; |
|
28 | + |
|
29 | + $this->sitePath = \TYPO3\CMS\Core\Core\Environment::getPublicPath(); // [^/]$ |
|
30 | + $this->svgCache = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class)->getCache('svgstore'); |
|
31 | + } |
|
32 | + |
|
33 | + public function process(string $html): string |
|
34 | + { |
|
35 | + $this->spritePath = $this->svgCache->get('spritePath'); |
|
36 | + $this->svgFileArr = $this->svgCache->get('svgFileArr'); |
|
37 | + |
|
38 | + if (empty($this->spritePath) && !$this->populateCache()) { |
|
39 | + throw new \Exception('could not write file: '.$this->sitePath.$this->spritePath); |
|
40 | + } |
|
41 | + |
|
42 | + if (!file_exists($this->sitePath.$this->spritePath)) { |
|
43 | + throw new \Exception('file does not exists: '.$this->sitePath.$this->spritePath); |
|
44 | + } |
|
45 | + |
|
46 | + if ($GLOBALS['TSFE']->config['config']['disableAllHeaderCode'] ?? false) { |
|
47 | + $dom = ['head' => '', 'body' => $html]; |
|
48 | + } elseif (!preg_match('/(?<head>.+?<\/head>)(?<body>.+)/s', $html, $dom) && 5 == \count($dom)) { |
|
49 | + return $html; |
|
50 | + } |
|
51 | + |
|
52 | + // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attributes |
|
53 | + $dom['body'] = preg_replace_callback('/<img(?<pre>[^>]*)src="(?<src>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?!\s*<\/picture>)/s', function (array $match): string { // ^[/] |
|
54 | + if (!isset($this->svgFileArr[$match['src']])) { // check usage |
|
55 | + return $match[0]; |
|
56 | + } |
|
57 | + $attr = preg_replace('/\s(?:alt|ismap|loading|title|sizes|srcset|usemap|crossorigin|decoding|referrerpolicy)="[^"]*"/', '', $match['pre'].$match['post']); // cleanup |
|
58 | + |
|
59 | + return sprintf('<svg %s %s><use href="%s#%s"/></svg>', $this->svgFileArr[$match['src']]['attr'], trim($attr), $this->spritePath, $this->convertFilePath($match['src'])); |
|
60 | + }, $dom['body']); |
|
61 | + |
|
62 | + // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/object#attributes |
|
63 | + $dom['body'] = preg_replace_callback('/<object(?<pre>[^>]*)data="(?<data>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?:<\/object>)/s', function (array $match): string { // ^[/] |
|
64 | + if (!isset($this->svgFileArr[$match['data']])) { // check usage |
|
65 | + return $match[0]; |
|
66 | + } |
|
67 | + $attr = preg_replace('/\s(?:form|name|type|usemap)="[^"]*"/', '', $match['pre'].$match['post']); // cleanup |
|
68 | + |
|
69 | + return sprintf('<svg %s %s><use href="%s#%s"/></svg>', $this->svgFileArr[$match['data']]['attr'], trim($attr), $this->spritePath, $this->convertFilePath($match['data'])); |
|
70 | + }, $dom['body']); |
|
71 | + |
|
72 | + return $dom['head'].$dom['body']; |
|
73 | + } |
|
74 | + |
|
75 | + private function convertFilePath(string $path): string |
|
76 | + { |
|
77 | + return preg_replace('/.svg$|[^\w\-]/', '', str_replace('/', '-', ltrim($path, '/'))); // ^[^/] |
|
78 | + } |
|
79 | + |
|
80 | + private function addFileToSpriteArr(string $hash, string $path): ?array |
|
81 | + { |
|
82 | + if (!file_exists($this->sitePath.$path)) { |
|
83 | + return null; |
|
84 | + } |
|
85 | + |
|
86 | + if (preg_match('/(?:;base64|i:a?i?pgf)/', $svg = file_get_contents($this->sitePath.$path))) { // noop! |
|
87 | + return null; |
|
88 | + } |
|
89 | + |
|
90 | + if (preg_match('/<(?:style|defs)|url\(/', $svg)) { |
|
91 | + return null; // check links @ __construct |
|
92 | + } |
|
93 | + |
|
94 | + //$svg = preg_replace('/((?:id|class)=")/', '$1'.$hash.'__', $svg); // extend IDs |
|
95 | + //$svg = preg_replace('/(href="|url\()#/', '$1#'.$hash.'__', $svg); // recover IDs |
|
96 | + |
|
97 | + //$svg = preg_replace_callback('/<style[^>]*>(?<styl>.+?)<\/style>|<defs[^>]*>(?<defs>.+?)<\/defs>/s', function(array $match) use($hash): string { |
|
98 | + // |
|
99 | + // if(isset($match['styl'])) |
|
100 | + // { |
|
101 | + // $this->styl[] = preg_replace('/\s*(\.|#){1}(.+?)\s*\{/', '$1'.$hash.'__$2{', $match['styl']); // patch CSS # https://mathiasbynens.be/notes/css-escapes |
|
102 | + // } |
|
103 | + // if(isset($match['defs'])) |
|
104 | + // { |
|
105 | + // $this->defs[] = trim($match['defs']); |
|
106 | + // } |
|
107 | + // return ''; |
|
108 | + //}, $svg); |
|
109 | + |
|
110 | + // https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/xlink:href |
|
111 | + $svg = preg_replace('/^.*?<svg|\s*(<\/svg>)(?!.*\1).*$|xlink:|\s(?:(?:version|xmlns)|(?:[a-z\-]+\:[a-z\-]+))="[^"]*"/s', '', $svg); // cleanup |
|
112 | + |
|
113 | + // https://developer.mozilla.org/en-US/docs/Web/SVG/Element/svg#attributes |
|
114 | + $svg = preg_replace_callback('/([^>]*)\s*(?=>)/s', function (array $match) use (&$attr): string { |
|
115 | + if (false === preg_match_all('/(?!\s)(?<attr>[\w\-]+)="\s*(?<value>[^"]+)\s*"/', $match[1], $matches)) { |
|
116 | + return $match[0]; |
|
117 | + } |
|
118 | + foreach ($matches['attr'] as $index => $attribute) { |
|
119 | + switch ($attribute) { |
|
120 | + case 'id': |
|
121 | + case 'width': |
|
122 | + case 'height': |
|
123 | + unset($matches[0][$index]); |
|
124 | + break; |
|
125 | + |
|
126 | + case 'viewBox': |
|
127 | + if (false !== preg_match('/\S+\s\S+\s\+?(?<width>[\d\.]+)\s\+?(?<height>[\d\.]+)/', $matches['value'][$index], $match)) { |
|
128 | + $attr[] = sprintf('%s="0 0 %s %s"', $attribute, $match['width'], $match['height']); // save! |
|
129 | + } |
|
130 | + } |
|
131 | + } |
|
132 | + |
|
133 | + return implode(' ', $matches[0]); |
|
134 | + }, $svg, 1); |
|
135 | + |
|
136 | + if ($attr) { // TODO; beautify |
|
137 | + $this->svgs[] = sprintf('id="%s" %s', $this->convertFilePath($path), $svg); // prepend ID |
|
138 | + |
|
139 | + return ['attr' => implode(' ', $attr), 'hash' => $hash]; |
|
140 | + } |
|
141 | + |
|
142 | + return null; |
|
143 | + } |
|
144 | + |
|
145 | + private function populateCache(): bool |
|
146 | + { |
|
147 | + $storageArr = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\StorageRepository::class)->findAll(); |
|
148 | + foreach ($storageArr as $storage) { |
|
149 | + if ('relative' == $storage->getConfiguration()['pathType']) { |
|
150 | + $storageArr[$storage->getUid()] = rtrim($storage->getConfiguration()['basePath'], '/'); // [^/]$ |
|
151 | + } |
|
152 | + } |
|
153 | + unset($storageArr[0]); // keep! |
|
154 | + |
|
155 | + $svgFileArr = GeneralUtility::makeInstance(\HTML\Sourceopt\Resource\SvgFileRepository::class)->findAllByStorageUids(\array_keys($storageArr)); |
|
156 | + foreach ($svgFileArr as $index => $row) { |
|
157 | + if (!$this->svgFileArr[($row['path'] = '/'.$storageArr[$row['storage']].$row['identifier'])] = $this->addFileToSpriteArr($row['sha1'], $row['path'])) { // ^[/] |
|
158 | + unset($this->svgFileArr[$row['path']]); |
|
159 | + } |
|
160 | + } |
|
161 | + unset($storageArr, $svgFileArr); // save MEM |
|
162 | + |
|
163 | + $svg = preg_replace_callback( |
|
164 | + '/<use(?<pre>.*?)(?:xlink:)?href="(?<href>\/.+?\.svg)#[^"]+"(?<post>.*?)[\s\/]*>(?:<\/use>)?/s', |
|
165 | + function (array $match): string { |
|
166 | + if (!isset($this->svgFileArr[$match['href']])) { // check usage |
|
167 | + return $match[0]; |
|
168 | + } |
|
169 | + |
|
170 | + return sprintf('<use%s href="#%s"/>', $match['pre'].$match['post'], $this->convertFilePath($match['href'])); |
|
171 | + }, |
|
172 | + '<svg xmlns="http://www.w3.org/2000/svg">' |
|
173 | + //."\n<style>\n".implode("\n", $this->styl)."\n</style>" |
|
174 | + //."\n<defs>\n".implode("\n", $this->defs)."\n</defs>" |
|
175 | + ."\n<symbol ".implode("</symbol>\n<symbol ", $this->svgs)."</symbol>\n" |
|
176 | + .'</svg>' |
|
177 | + ); |
|
178 | + |
|
179 | + //unset($this->styl); // save MEM |
|
180 | + //unset($this->defs); // save MEM |
|
181 | + unset($this->svgs); // save MEM |
|
182 | + |
|
183 | + if ($GLOBALS['TSFE']->config['config']['sourceopt.']['formatHtml'] ?? false) { |
|
184 | + $svg = preg_replace('/[\n\r\t\v\0]|\s{2,}/', '', $svg); |
|
185 | + } |
|
186 | + |
|
187 | + $svg = preg_replace('/<([a-z]+)\s*(\/|>\s*<\/\1)>\s*/i', '', $svg); // remove emtpy |
|
188 | + $svg = preg_replace('/<((circle|ellipse|line|path|polygon|polyline|rect|stop|use)\s[^>]+?)\s*>\s*<\/\2>/', '<$1/>', $svg); // shorten/minify |
|
189 | + |
|
190 | + if (!is_dir($this->sitePath.$this->outputDir)) { |
|
191 | + GeneralUtility::mkdir_deep($this->sitePath.$this->outputDir); |
|
192 | + } |
|
193 | + |
|
194 | + $this->spritePath = $this->outputDir.hash('sha1', serialize($this->svgFileArr)).'.svg'; |
|
195 | + if (false === file_put_contents($this->sitePath.$this->spritePath, $svg)) { |
|
196 | + return false; |
|
197 | + } |
|
198 | + |
|
199 | + $this->svgCache->set('spritePath', $this->spritePath); |
|
200 | + $this->svgCache->set('svgFileArr', $this->svgFileArr); |
|
201 | + |
|
202 | + return true; |
|
203 | + } |
|
204 | 204 | } |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | } |
51 | 51 | |
52 | 52 | // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attributes |
53 | - $dom['body'] = preg_replace_callback('/<img(?<pre>[^>]*)src="(?<src>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?!\s*<\/picture>)/s', function (array $match): string { // ^[/] |
|
53 | + $dom['body'] = preg_replace_callback('/<img(?<pre>[^>]*)src="(?<src>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?!\s*<\/picture>)/s', function(array $match): string { // ^[/] |
|
54 | 54 | if (!isset($this->svgFileArr[$match['src']])) { // check usage |
55 | 55 | return $match[0]; |
56 | 56 | } |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | }, $dom['body']); |
61 | 61 | |
62 | 62 | // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/object#attributes |
63 | - $dom['body'] = preg_replace_callback('/<object(?<pre>[^>]*)data="(?<data>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?:<\/object>)/s', function (array $match): string { // ^[/] |
|
63 | + $dom['body'] = preg_replace_callback('/<object(?<pre>[^>]*)data="(?<data>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?:<\/object>)/s', function(array $match): string { // ^[/] |
|
64 | 64 | if (!isset($this->svgFileArr[$match['data']])) { // check usage |
65 | 65 | return $match[0]; |
66 | 66 | } |
@@ -111,7 +111,7 @@ discard block |
||
111 | 111 | $svg = preg_replace('/^.*?<svg|\s*(<\/svg>)(?!.*\1).*$|xlink:|\s(?:(?:version|xmlns)|(?:[a-z\-]+\:[a-z\-]+))="[^"]*"/s', '', $svg); // cleanup |
112 | 112 | |
113 | 113 | // https://developer.mozilla.org/en-US/docs/Web/SVG/Element/svg#attributes |
114 | - $svg = preg_replace_callback('/([^>]*)\s*(?=>)/s', function (array $match) use (&$attr): string { |
|
114 | + $svg = preg_replace_callback('/([^>]*)\s*(?=>)/s', function(array $match) use (&$attr): string { |
|
115 | 115 | if (false === preg_match_all('/(?!\s)(?<attr>[\w\-]+)="\s*(?<value>[^"]+)\s*"/', $match[1], $matches)) { |
116 | 116 | return $match[0]; |
117 | 117 | } |
@@ -162,7 +162,7 @@ discard block |
||
162 | 162 | |
163 | 163 | $svg = preg_replace_callback( |
164 | 164 | '/<use(?<pre>.*?)(?:xlink:)?href="(?<href>\/.+?\.svg)#[^"]+"(?<post>.*?)[\s\/]*>(?:<\/use>)?/s', |
165 | - function (array $match): string { |
|
165 | + function(array $match): string { |
|
166 | 166 | if (!isset($this->svgFileArr[$match['href']])) { // check usage |
167 | 167 | return $match[0]; |
168 | 168 | } |
@@ -18,27 +18,27 @@ |
||
18 | 18 | */ |
19 | 19 | class SvgStoreMiddleware implements MiddlewareInterface |
20 | 20 | { |
21 | - /** |
|
22 | - * Search/Extract/Merge SVGs @ HTML output. |
|
23 | - */ |
|
24 | - public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
25 | - { |
|
26 | - $response = $handler->handle($request); |
|
21 | + /** |
|
22 | + * Search/Extract/Merge SVGs @ HTML output. |
|
23 | + */ |
|
24 | + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
25 | + { |
|
26 | + $response = $handler->handle($request); |
|
27 | 27 | |
28 | - if (!($response instanceof NullResponse) |
|
29 | - && $GLOBALS['TSFE'] instanceof TypoScriptFrontendController |
|
30 | - && $GLOBALS['TSFE']->config['config']['svgstore.']['enabled'] ?? false |
|
31 | - && 'text/html' == substr($response->getHeaderLine('Content-Type'), 0, 9) |
|
32 | - ) { |
|
33 | - $processedHtml = GeneralUtility::makeInstance(\HTML\Sourceopt\Service\SvgStoreService::class) |
|
34 | - ->process($response->getBody()->__toString()) |
|
35 | - ; |
|
28 | + if (!($response instanceof NullResponse) |
|
29 | + && $GLOBALS['TSFE'] instanceof TypoScriptFrontendController |
|
30 | + && $GLOBALS['TSFE']->config['config']['svgstore.']['enabled'] ?? false |
|
31 | + && 'text/html' == substr($response->getHeaderLine('Content-Type'), 0, 9) |
|
32 | + ) { |
|
33 | + $processedHtml = GeneralUtility::makeInstance(\HTML\Sourceopt\Service\SvgStoreService::class) |
|
34 | + ->process($response->getBody()->__toString()) |
|
35 | + ; |
|
36 | 36 | |
37 | - $responseBody = new Stream('php://temp', 'rw'); |
|
38 | - $responseBody->write($processedHtml); |
|
39 | - $response = $response->withBody($responseBody); |
|
40 | - } |
|
37 | + $responseBody = new Stream('php://temp', 'rw'); |
|
38 | + $responseBody->write($processedHtml); |
|
39 | + $response = $response->withBody($responseBody); |
|
40 | + } |
|
41 | 41 | |
42 | - return $response; |
|
43 | - } |
|
42 | + return $response; |
|
43 | + } |
|
44 | 44 | } |
@@ -19,39 +19,39 @@ |
||
19 | 19 | */ |
20 | 20 | class CleanHtmlMiddleware implements MiddlewareInterface |
21 | 21 | { |
22 | - /** |
|
23 | - * @var CleanHtmlService |
|
24 | - */ |
|
25 | - protected $cleanHtmlService; |
|
26 | - |
|
27 | - public function __construct() |
|
28 | - { |
|
29 | - $this->cleanHtmlService = GeneralUtility::makeInstance(CleanHtmlService::class); |
|
30 | - } |
|
31 | - |
|
32 | - /** |
|
33 | - * Clean the HTML output. |
|
34 | - */ |
|
35 | - public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
36 | - { |
|
37 | - $response = $handler->handle($request); |
|
38 | - |
|
39 | - if (!($response instanceof NullResponse) |
|
40 | - && $GLOBALS['TSFE'] instanceof TypoScriptFrontendController |
|
41 | - && $GLOBALS['TSFE']->config['config']['sourceopt.']['enabled'] ?? false |
|
42 | - && 'text/html' == substr($response->getHeaderLine('Content-Type'), 0, 9) |
|
43 | - ) { |
|
44 | - $processedHtml = $this->cleanHtmlService->clean( |
|
45 | - $response->getBody()->__toString(), |
|
46 | - $GLOBALS['TSFE']->config['config']['sourceopt.'] |
|
47 | - ); |
|
48 | - |
|
49 | - // Replace old body with $processedHtml |
|
50 | - $responseBody = new Stream('php://temp', 'rw'); |
|
51 | - $responseBody->write($processedHtml); |
|
52 | - $response = $response->withBody($responseBody); |
|
53 | - } |
|
54 | - |
|
55 | - return $response; |
|
56 | - } |
|
22 | + /** |
|
23 | + * @var CleanHtmlService |
|
24 | + */ |
|
25 | + protected $cleanHtmlService; |
|
26 | + |
|
27 | + public function __construct() |
|
28 | + { |
|
29 | + $this->cleanHtmlService = GeneralUtility::makeInstance(CleanHtmlService::class); |
|
30 | + } |
|
31 | + |
|
32 | + /** |
|
33 | + * Clean the HTML output. |
|
34 | + */ |
|
35 | + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
36 | + { |
|
37 | + $response = $handler->handle($request); |
|
38 | + |
|
39 | + if (!($response instanceof NullResponse) |
|
40 | + && $GLOBALS['TSFE'] instanceof TypoScriptFrontendController |
|
41 | + && $GLOBALS['TSFE']->config['config']['sourceopt.']['enabled'] ?? false |
|
42 | + && 'text/html' == substr($response->getHeaderLine('Content-Type'), 0, 9) |
|
43 | + ) { |
|
44 | + $processedHtml = $this->cleanHtmlService->clean( |
|
45 | + $response->getBody()->__toString(), |
|
46 | + $GLOBALS['TSFE']->config['config']['sourceopt.'] |
|
47 | + ); |
|
48 | + |
|
49 | + // Replace old body with $processedHtml |
|
50 | + $responseBody = new Stream('php://temp', 'rw'); |
|
51 | + $responseBody->write($processedHtml); |
|
52 | + $response = $response->withBody($responseBody); |
|
53 | + } |
|
54 | + |
|
55 | + return $response; |
|
56 | + } |
|
57 | 57 | } |