Skip to content

Commit

Permalink
PHP CS Fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
lochmueller committed Nov 4, 2022
1 parent 9001b93 commit 8b398d3
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 70 deletions.
2 changes: 1 addition & 1 deletion Classes/Resource/SvgFileRepository.php
Expand Up @@ -50,6 +50,6 @@ public function findAllByStorageUids(array $storageUids): array
->addOrderBy('sys_file.identifier')
->execute()
->fetchAll() // TODO; use stdClass
;
;
}
}
92 changes: 45 additions & 47 deletions Classes/Service/CleanHtmlService.php
Expand Up @@ -65,11 +65,11 @@ class CleanHtmlService implements SingletonInterface
public function setVariables(array $config): void
{
if (!empty($config)) {
if ($config['formatHtml'] && \is_numeric($config['formatHtml'])) {
if ($config['formatHtml'] && is_numeric($config['formatHtml'])) {
$this->formatType = (int) $config['formatHtml'];
}

if ($config['formatHtml.']['tabSize'] && \is_numeric($config['formatHtml.']['tabSize'])) {
if ($config['formatHtml.']['tabSize'] && is_numeric($config['formatHtml.']['tabSize'])) {
$this->tab = str_pad('', (int) $config['formatHtml.']['tabSize'], ' ');
}

Expand Down Expand Up @@ -223,54 +223,54 @@ protected function formatHtml($html)
if ('<?xml' == substr($htmlArrayBefore, 0, 5)) {
$newline = true;
} elseif (2 == $this->formatType && ( // minimalistic line break
// this element has a line break before itself
preg_match(
'/<'.$structureBoxLikeElements.'(.*)>/Usi',
$htmlArrayCurrent
) || preg_match(
'/<'.$structureBoxLikeElements.'(.*) \/>/Usi',
$htmlArrayCurrent
) // one element before is a element that has a line break after
|| preg_match(
'/<\/'.$structureBoxLikeElements.'(.*)>/Usi',
$htmlArrayBefore
) || '<!--' == substr(
$htmlArrayBefore,
0,
4
) || preg_match('/<'.$structureBoxLikeElements.'(.*) \/>/Usi', $htmlArrayBefore))
// this element has a line break before itself
preg_match(
'/<'.$structureBoxLikeElements.'(.*)>/Usi',
$htmlArrayCurrent
) || preg_match(
'/<'.$structureBoxLikeElements.'(.*) \/>/Usi',
$htmlArrayCurrent
) // one element before is a element that has a line break after
|| preg_match(
'/<\/'.$structureBoxLikeElements.'(.*)>/Usi',
$htmlArrayBefore
) || '<!--' == substr(
$htmlArrayBefore,
0,
4
) || preg_match('/<'.$structureBoxLikeElements.'(.*) \/>/Usi', $htmlArrayBefore))
) {
$newline = true;
} elseif (3 == $this->formatType && ( // aestetic line break
// this element has a line break before itself
preg_match(
'/<'.$esteticBoxLikeElements.'(.*)>/Usi',
$htmlArrayCurrent
) || preg_match(
'/<'.$esteticBoxLikeElements.'(.*) \/>/Usi',
$htmlArrayCurrent
) // one element before is a element that has a line break after
|| preg_match('/<\/'.$esteticBoxLikeElements.'(.*)>/Usi', $htmlArrayBefore) || '<!--' == substr(
$htmlArrayBefore,
0,
4
) || preg_match('/<'.$esteticBoxLikeElements.'(.*) \/>/Usi', $htmlArrayBefore))
// this element has a line break before itself
preg_match(
'/<'.$esteticBoxLikeElements.'(.*)>/Usi',
$htmlArrayCurrent
) || preg_match(
'/<'.$esteticBoxLikeElements.'(.*) \/>/Usi',
$htmlArrayCurrent
) // one element before is a element that has a line break after
|| preg_match('/<\/'.$esteticBoxLikeElements.'(.*)>/Usi', $htmlArrayBefore) || '<!--' == substr(
$htmlArrayBefore,
0,
4
) || preg_match('/<'.$esteticBoxLikeElements.'(.*) \/>/Usi', $htmlArrayBefore))
) {
$newline = true;
} elseif ($this->formatType >= 4 && ( // logical line break
// this element has a line break before itself
preg_match(
'/<'.$allBoxLikeElements.'(.*)>/Usi',
$htmlArrayCurrent
) || preg_match(
'/<'.$allBoxLikeElements.'(.*) \/>/Usi',
$htmlArrayCurrent
) // one element before is a element that has a line break after
|| preg_match('/<\/'.$allBoxLikeElements.'(.*)>/Usi', $htmlArrayBefore) || '<!--' == substr(
$htmlArrayBefore,
0,
4
) || preg_match('/<'.$allBoxLikeElements.'(.*) \/>/Usi', $htmlArrayBefore))
// this element has a line break before itself
preg_match(
'/<'.$allBoxLikeElements.'(.*)>/Usi',
$htmlArrayCurrent
) || preg_match(
'/<'.$allBoxLikeElements.'(.*) \/>/Usi',
$htmlArrayCurrent
) // one element before is a element that has a line break after
|| preg_match('/<\/'.$allBoxLikeElements.'(.*)>/Usi', $htmlArrayBefore) || '<!--' == substr(
$htmlArrayBefore,
0,
4
) || preg_match('/<'.$allBoxLikeElements.'(.*) \/>/Usi', $htmlArrayBefore))
) {
$newline = true;
}
Expand Down Expand Up @@ -351,7 +351,7 @@ protected function killLineBreaks($html)
$html = str_replace($this->newline, '', $html);

return preg_replace('/\s\s+/u', ' ', $html);
//? return preg_replace('/\n|\s+(\s)/u', '$1', $html);
// ? return preg_replace('/\n|\s+(\s)/u', '$1', $html);
}

/**
Expand Down Expand Up @@ -421,8 +421,6 @@ protected function removeEmptyLines(&$html): void

/**
* Include configured header comment in HTML content block.
*
* @param $html
*/
public function includeHeaderComment(&$html): void
{
Expand Down
43 changes: 21 additions & 22 deletions Classes/Service/SvgStoreService.php
Expand Up @@ -22,8 +22,8 @@ class SvgStoreService implements \TYPO3\CMS\Core\SingletonInterface

public function __construct()
{
//$this->styl = []; # https://stackoverflow.com/questions/39583880/external-svg-fails-to-apply-internal-css
//$this->defs = []; # https://bugs.chromium.org/p/chromium/issues/detail?id=751733#c14
// $this->styl = []; # https://stackoverflow.com/questions/39583880/external-svg-fails-to-apply-internal-css
// $this->defs = []; # https://bugs.chromium.org/p/chromium/issues/detail?id=751733#c14
$this->svgs = [];

$this->sitePath = \TYPO3\CMS\Core\Core\Environment::getPublicPath(); // [^/]$
Expand Down Expand Up @@ -94,11 +94,10 @@ private function addFileToSpriteArr(string $hash, string $path): ?array
// https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/xlink:href
$svg = preg_replace('/^.*?<svg|\s*(<\/svg>)(?!.*\1).*$|xlink:|\s(?:(?:version|xmlns)|(?:[a-z\-]+\:[a-z\-]+))="[^"]*"/s', '', $svg); // cleanup

// $svg = preg_replace('/(?<=(?:id|class)=")/', $hash.'__', $svg); // extend IDs
// $svg = preg_replace('/(?<=href="|url\()#/', $hash.'__', $svg); // recover IDs

//$svg = preg_replace('/(?<=(?:id|class)=")/', $hash.'__', $svg); // extend IDs
//$svg = preg_replace('/(?<=href="|url\()#/', $hash.'__', $svg); // recover IDs

//$svg = preg_replace_callback('/<style[^>]*>(?<styl>.+?)<\/style>|<defs[^>]*>(?<defs>.+?)<\/defs>/s', function(array $match) use($hash): string {
// $svg = preg_replace_callback('/<style[^>]*>(?<styl>.+?)<\/style>|<defs[^>]*>(?<defs>.+?)<\/defs>/s', function(array $match) use($hash): string {
//
// if(isset($match['styl']))
// {
Expand All @@ -109,7 +108,7 @@ private function addFileToSpriteArr(string $hash, string $path): ?array
// $this->defs[] = trim($match['defs']);
// }
// return '';
//}, $svg);
// }, $svg);

// https://developer.mozilla.org/en-US/docs/Web/SVG/Element/svg#attributes
$svg = preg_replace_callback('/([^>]*)\s*(?=>)/s', function (array $match) use (&$attr): string {
Expand All @@ -118,16 +117,16 @@ private function addFileToSpriteArr(string $hash, string $path): ?array
}
foreach ($matches['attr'] as $index => $attribute) {
switch ($attribute) {
case 'id':
case 'width':
case 'height':
unset($matches[0][$index]);
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!
}
case 'id':
case 'width':
case 'height':
unset($matches[0][$index]);
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!
}
}
}

Expand All @@ -153,7 +152,7 @@ private function populateCache(): bool
}
unset($storageArr[0]); // keep!

$fileArr = GeneralUtility::makeInstance(\HTML\Sourceopt\Resource\SvgFileRepository::class)->findAllByStorageUids(\array_keys($storageArr));
$fileArr = GeneralUtility::makeInstance(\HTML\Sourceopt\Resource\SvgFileRepository::class)->findAllByStorageUids(array_keys($storageArr));
foreach ($fileArr as $file) {
$file['path'] = '/'.$storageArr[$file['storage']].$file['identifier']; // ^[/]
$file['defs'] = $this->addFileToSpriteArr($file['sha1'], $file['path']);
Expand All @@ -174,14 +173,14 @@ function (array $match): string {
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>"
//."\n<defs>\n".implode("\n", $this->defs)."\n</defs>"
// ."\n<style>\n".implode("\n", $this->styl)."\n</style>"
// ."\n<defs>\n".implode("\n", $this->defs)."\n</defs>"
."\n<symbol ".implode("</symbol>\n<symbol ", $this->svgs)."</symbol>\n"
.'</svg>'
);

//unset($this->styl); // save MEM
//unset($this->defs); // save MEM
// unset($this->styl); // save MEM
// unset($this->defs); // save MEM
unset($this->svgs); // save MEM

if ($GLOBALS['TSFE']->config['config']['sourceopt.']['formatHtml'] ?? false) {
Expand Down
1 change: 1 addition & 0 deletions Tests/Unit/Manipulation/RemoveGeneratorTest.php
Expand Up @@ -15,6 +15,7 @@
* RemoveGeneratorTest.
*
* @internal
*
* @coversNothing
*/
class RemoveGeneratorTest extends \PHPUnit\Framework\TestCase
Expand Down
1 change: 1 addition & 0 deletions Tests/Unit/Service/CleanHtmlServiceTest.php
Expand Up @@ -6,6 +6,7 @@

/**
* @internal
*
* @coversNothing
*/
class CleanHtmlServiceTest extends \PHPUnit\Framework\TestCase
Expand Down

0 comments on commit 8b398d3

Please sign in to comment.