Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/theme_assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,19 @@ public function setCacheBuster(string $cache_buster): theme_assets
return $this;
}

public function setCss(string $key, string $data, string $media = 'all', array $attributes = []): theme_assets
public function setCss(string $key, string $data, string $media = '', array $attributes = [], bool $noscript = false): theme_assets
{
$attributes['media'] = $media;
// If the media attribute is not set, do not default to 'all', because it is considered render-blocking
// Even an empty media="" can be render-blocking, so we only set it if a media type is given
// https://developer.chrome.com/docs/lighthouse/performance/render-blocking-resources?hl=de
if($media !== '') {
$attributes['media'] = $media;
}

$this->css[$key] = [
'data' => $data,
'attributes' => $attributes,
'noscript' => $noscript,
];

return $this;
Expand Down Expand Up @@ -163,7 +169,7 @@ public function getCss(): string

if (!$return) {
foreach ($this->css as $css_key => $css) {
$return .= $this->getLinkTag($this->id.'--'.$css_key, $css['data'], $css['attributes'], $this->cache_buster);
$return .= $this->getLinkTag($this->id.'--'.$css_key, $css['data'], $css['attributes'], $this->cache_buster, $css['noscript'] ?? false);
}
}

Expand Down
10 changes: 7 additions & 3 deletions lib/theme_assets_trait.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,22 @@ private function getScriptTag(string $key, string $file, array $attributes, stri
*
* @return string
*/
private function getLinkTag(string $key, string $file, array $attributes, string $cache_buster = ''): string
private function getLinkTag(string $key, string $file, array $attributes, string $cache_buster = '', bool $noscript = false): string
{
if ($this->isAdmin()) {
$attributes['data-key'] = 'style--'.$key;
}

$noscript_tag = $noscript ? '<noscript><link rel="stylesheet" href="'.$this->stripDots($file).$this->getCacheBuster($file, $cache_buster).'"></noscript>' : '';
$onload = '';
if($noscript) {
$onload = ' onload="this.onload=null;this.rel=\'stylesheet\'"'; // rex_string::buildAttributes() bypass wegen Sanitizer der Hochkommata
}
$attributes['href'] = $this->stripDots($file).$this->getCacheBuster($file, $cache_buster);

$attributes['rel'] = $attributes['rel'] ?? 'stylesheet';
$attributes['type'] = $attributes['type'] ?? 'text/css';

return '<link'.rex_string::buildAttributes($attributes).' />'.PHP_EOL;
return '<link'.rex_string::buildAttributes($attributes).$onload.' />'.$noscript_tag.PHP_EOL;
Copy link

Copilot AI Jul 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider inserting a newline or space before $noscript_tag so that the <noscript> element is rendered on its own line for improved readability of the generated HTML.

Suggested change
return '<link'.rex_string::buildAttributes($attributes).$onload.' />'.$noscript_tag.PHP_EOL;
return '<link'.rex_string::buildAttributes($attributes).$onload.' />'.PHP_EOL.$noscript_tag.PHP_EOL;

Copilot uses AI. Check for mistakes.
}

/**
Expand Down