Same name and namespace in other branches
  1. 5.0.x advagg_mod/src/Asset/DeferCss.php \Drupal\advagg_mod\Asset\DeferCss::defer() 1 commentaire
  2. 8.x-3.x advagg_mod/src/Asset/DeferCss.php \Drupal\advagg_mod\Asset\DeferCss::defer() 1 commentaire
  3. 8.x-4.x advagg_mod/src/Asset/DeferCss.php \Drupal\advagg_mod\Asset\DeferCss::defer() 1 commentaire

Replace stylesheet links with preload & noscript links.

Paramètres

string $content: The response content.

Return value

string Updated content.

Fichier

advagg_mod/src/Asset/DeferCss.php, line 66

Classe

DeferCss
Modify stylesheet links to defer them. May lead to Flash of unstyled content.

Namespace

Drupal\advagg_mod\Asset

Code

public function defer($content) {
    if ($this->external) {
        $pattern = '/<link rel=["\']stylesheet["\'](.*)(href="[a-zA-Z0-9\\/_\\.\\-\\?\\:]*")(.*)\\/\\>/';
    }
    else {
        $pattern = '/<link rel=["\']stylesheet["\'](.*)(href="\\/[a-zA-Z0-9][a-zA-Z0-9\\/_\\.\\-\\?]*")(.*)\\/\\>/';
    }
    $content = preg_replace_callback($pattern, [
        $this,
        'callback',
    ], $content);
    // Put JS inline if configured.
    if ($this->deferMethod === 0) {
        $path = $this->moduleExtensionList
            ->getPath('advagg_mod') . '/js/loadCSS.js';
        if (!strpos($content, $path)) {
            $path = Crypt::hashBase64($path . $this->counter);
        }
        else {
            $path = str_replace('/', '\\/', $path);
        }
        $path = preg_quote($path, '/');
        $pattern = "/<script src=['\"]\\/(.*{$path}.*)\\?.*['\"]>/";
        $content = preg_replace_callback($pattern, [
            $this,
            'inlineScript',
        ], $content);
    }
    return $content;
}