Skip to content

Commit

Permalink
[FIX] config.disableAllHeaderCode lochmueller#95
Browse files Browse the repository at this point in the history
  • Loading branch information
xerc committed Jul 9, 2022
1 parent f4f8a2c commit 7a2b71b
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions Classes/Service/SvgStoreService.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,33 @@ public function process(string $html): string
throw new \Exception('file does not exists: '.$this->sitePath.$this->spritePath);
}

if (!preg_match('/(?<head>.+?<\/head>)(?<body>.+)/s', $html, $html) && 5 == \count($html)) {
throw new \Exception('fix HTML!');
if ($GLOBALS['TSFE']->config['config']['disableAllHeaderCode'] ?? false) {
$dom = ['head' => '', 'body' => $html];
} elseif (!preg_match('/(?<head>.+?<\/head>)(?<body>.+)/s', $html, $dom) && 5 == \count($dom)) {
return $html;
}

// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attributes
$html['body'] = preg_replace_callback('/<img(?<pre>[^>]*)src="(?<src>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?!\s*<\/picture>)/s', function (array $match): string { // ^[/]
$dom['body'] = preg_replace_callback('/<img(?<pre>[^>]*)src="(?<src>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?!\s*<\/picture>)/s', function (array $match): string { // ^[/]
if (!isset($this->svgFileArr[$match['src']])) { // check usage
return $match[0];
}
$attr = preg_replace('/\s(?:alt|ismap|loading|title|sizes|srcset|usemap|crossorigin|decoding|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']));
}, $html['body']);
}, $dom['body']);

// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/object#attributes
$html['body'] = preg_replace_callback('/<object(?<pre>[^>]*)data="(?<data>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?:<\/object>)/s', function (array $match): string { // ^[/]
$dom['body'] = preg_replace_callback('/<object(?<pre>[^>]*)data="(?<data>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?:<\/object>)/s', function (array $match): string { // ^[/]
if (!isset($this->svgFileArr[$match['data']])) { // check usage
return $match[0];
}
$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']));
}, $html['body']);
}, $dom['body']);

return $html['head'].$html['body'];
return $dom['head'].$dom['body'];
}

private function convertFilePath(string $path): string
Expand Down

0 comments on commit 7a2b71b

Please sign in to comment.