Skip to content

Commit

Permalink
Merge pull request #146 from xerc/master
Browse files Browse the repository at this point in the history
[IMP] speed & viewBox regex
  • Loading branch information
lochmueller authored Oct 21, 2024
2 parents e310f63 + dde145f commit 3077333
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Classes/Service/CleanHtmlService.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function clean(string $html, array $config = [])
if (!isset($GLOBALS['TSFE']->config['config']['doctype'])
|| 'x' !== substr($GLOBALS['TSFE']->config['config']['doctype'], 0, 1)) {
$html = preg_replace(
'/<((?:area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)\s[^>\\\\]+?)\s?\/>/',
'/<((?:area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)\s[^>]+?)\s*\\\?\/>/',
'<$1>',
$html
);
Expand Down
28 changes: 18 additions & 10 deletions Classes/Service/SvgStoreService.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ public function __construct()

public function process(string $html): string
{
if (empty($this->svgFileArr)) {
return $html;
}

if ($GLOBALS['TSFE']->config['config']['disableAllHeaderCode'] ?? false) {
$dom = ['head' => '', 'body' => $html];
} elseif (!preg_match('/(?<head>.+?<\/head>)(?<body>.+)/s', $html, $dom)) {
Expand All @@ -101,7 +105,7 @@ public function process(string $html): string
}
$attr = preg_replace('/\s(?:alt|ismap|loading|title|sizes|srcset|usemap|crossorigin|decoding|fetchpriority|referrerpolicy)="[^"]*"/', '', $match['pre'].$match['post']); // cleanup

return sprintf('<svg %s %s><use href="%s#%s"/></svg>', $this->svgFileArr[$match['src']]['attr'], trim($attr), $this->spritePath, $this->convertFilePath($match['src']));
return \sprintf('<svg %s %s><use href="%s#%s"/></svg>', $this->svgFileArr[$match['src']]['attr'], trim($attr), $this->spritePath, $this->convertFilePath($match['src']));
}, $dom['body']);

// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/object#attributes
Expand All @@ -111,7 +115,7 @@ public function process(string $html): string
}
$attr = preg_replace('/\s(?:form|name|type|usemap)="[^"]*"/', '', $match['pre'].$match['post']); // cleanup

return sprintf('<svg %s %s><use href="%s#%s"/></svg>', $this->svgFileArr[$match['data']]['attr'], trim($attr), $this->spritePath, $this->convertFilePath($match['data']));
return \sprintf('<svg %s %s><use href="%s#%s"/></svg>', $this->svgFileArr[$match['data']]['attr'], trim($attr), $this->spritePath, $this->convertFilePath($match['data']));
}, $dom['body']);

return $dom['head'].$dom['body'];
Expand Down Expand Up @@ -159,7 +163,7 @@ private function addFileToSpriteArr(string $hash, string $path, array $attr = []

// https://developer.mozilla.org/en-US/docs/Web/SVG/Element/svg#attributes
$svg = preg_replace_callback('/([^>]*)\s*(?=>)/s', function (array $match) use (&$attr): string {
if (false === preg_match_all('/(?!\s)(?<attr>[\w\-]+)="\s*(?<value>[^"]+)\s*"/', $match[1], $matches)) {
if (false === preg_match_all('/(?!\s)(?<attr>[a-z\-]+)="\s*(?<value>[^"]+)\s*"/i', $match[1], $matches)) {
return $match[0];
}
foreach ($matches['attr'] as $index => $attribute) {
Expand All @@ -171,8 +175,8 @@ private function addFileToSpriteArr(string $hash, string $path, array $attr = []
break;

case 'viewBox':
if (false !== preg_match('/\S+\s\S+\s\+?(?<width>[\d\.]+)\s\+?(?<height>[\d\.]+)/', $matches['value'][$index], $match)) {
$attr[] = sprintf('%s="0 0 %s %s"', $attribute, $match['width'], $match['height']); // save!
if (false !== preg_match('/(?<minX>[-+]?[\d\.]+)\s(?<minY>[-+]?[\d\.]+)\s\+?(?<width>[\d\.]+)\s\+?(?<height>[\d\.]+)/', $matches['value'][$index], $match)) {
$attr[] = \sprintf('%s="%s %s %s %s"', $attribute, $match['minX'], $match['minY'], $match['width'], $match['height']); // save!
}
}
}
Expand All @@ -184,7 +188,7 @@ private function addFileToSpriteArr(string $hash, string $path, array $attr = []
return null;
}

$this->svgs[] = sprintf('id="%s" %s', $this->convertFilePath($path), $svg); // prepend ID
$this->svgs[] = \sprintf('id="%s" %s', $this->convertFilePath($path), $svg); // prepend ID

return ['attr' => implode(' ', $attr), 'hash' => $hash];
}
Expand All @@ -194,8 +198,8 @@ private function populateCache(): bool
$storageArr = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\StorageRepository::class)->findByStorageType('Local');
foreach ($storageArr as $storage) {
$storageConfig = $storage->getConfiguration();
if (!is_array($storageConfig) || !isset($storageConfig['pathType'], $storageConfig['basePath'])) {
continue;
if (!\is_array($storageConfig) || !isset($storageConfig['pathType'], $storageConfig['basePath'])) {
continue;
}
if ('relative' == $storageConfig['pathType']) {
$storageArr[$storage->getUid()] = rtrim($storageConfig['basePath'], '/'); // [^/]$
Expand All @@ -214,14 +218,18 @@ private function populateCache(): bool
}
unset($storageArr, $storage, $fileArr, $file); // save MEM

if (empty($this->svgFileArr)) {
return true;
}

$svg = preg_replace_callback(
'/<use(?<pre>.*?)(?:xlink:)?href="(?<href>\/.+?\.svg)(?:#[^"]*?)?"(?<post>.*?)[\s\/]*>(?:<\/use>)?/s',
function (array $match): string {
if (!isset($this->svgFileArr[$match['href']])) { // check usage
return $match[0];
}

return sprintf('<use%s href="#%s"/>', $match['pre'].$match['post'], $this->convertFilePath($match['href']));
return \sprintf('<use%s href="#%s"/>', $match['pre'].$match['post'], $this->convertFilePath($match['href']));
},
'<svg xmlns="http://www.w3.org/2000/svg">'
// ."\n<style>\n".implode("\n", $this->styl)."\n</style>"
Expand All @@ -240,7 +248,7 @@ function (array $match): string {
$svg = preg_replace('/\s{2,}/', ' ', $svg); // shrink whitespace
}

$svg = preg_replace('/<([a-z]+)\s*(\/|>\s*<\/\1)>\s*|\s+(?=\/>)/i', '', $svg); // remove emtpy TAGs & shorten endings
$svg = preg_replace('/<([a-z\-]+)\s*(\/|>\s*<\/\1)>\s*|\s+(?=\/>)/i', '', $svg); // remove emtpy TAGs & shorten endings
$svg = preg_replace('/<((circle|ellipse|line|path|polygon|polyline|rect|stop|use)\s[^>]+?)\s*>\s*<\/\2>/', '<$1/>', $svg); // shorten/minify TAG syntax

if (!is_dir($this->sitePath.$this->outputDir)) {
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

## Installation

https://github.com/lochmueller/sourceopt/blob/2346673ee51d2b64308e1ddb1433cea2f37eafcb/composer.json#L19-L20
https://github.com/lochmueller/sourceopt/blob/e310f6344a29b2dcaf7e2642724188b8f5d0059a/composer.json#L8-L9

```bash
composer require lochmueller/sourceopt
Expand Down

0 comments on commit 3077333

Please sign in to comment.