You are here

function advagg_mod_inline_defer in Advanced CSS/JS Aggregation 7.2

Defer inline js by using setTimeout.

Parameters

array $js: JS array.

bool $jquery_deferred: TRUE if jquery is deferred.

1 call to advagg_mod_inline_defer()
advagg_mod_js_post_alter in advagg_mod/advagg_mod.module
Alter the js array.

File

advagg_mod/advagg_mod.module, line 2628
Advanced aggregation modifier module.

Code

function advagg_mod_inline_defer(array &$js, $jquery_deferred) {
  if ($jquery_deferred) {
    $bootstrap_rev = strrev('/bootstrap.js');
    $bootstrap_min_rev = strrev('/bootstrap.min.js');
    foreach ($js as &$values) {

      // Defer bootstrap if jquery is deferred.
      if (is_string($values['data']) && (stripos(strrev($values['data']), $bootstrap_rev) === 0 || stripos(strrev($values['data']), $bootstrap_min_rev) === 0)) {
        $values['defer'] = TRUE;
        continue;
      }

      // Only do inline.
      if ($values['type'] === 'inline') {

        // Skip if advagg has already wrapped this inline code.
        if (strpos($values['data'], 'advagg_mod_') !== FALSE) {
          continue;
        }
        if (!empty($values['no_defer'])) {
          continue;
        }

        // Do not wrap inline js if it contains a named function definition.
        $pattern = '/\\s*function\\s+((?:[a-z][a-z0-9_]*))\\s*\\(.*\\)\\s*\\{/smix';
        $match = preg_match($pattern, $values['data']);
        if (!$match) {

          // Defer inline scripts by wrapping the code in setTimeout callback.
          $matches[2] = $matches[0] = $values['data'];
          $values['data'] = advagg_mod_wrap_inline_js($matches);
        }
        elseif (stripos($values['data'], 'jQuery.') !== FALSE || stripos($values['data'], '(jQuery)') !== FALSE) {

          // Inline js has a named function that uses jQuery;
          // do not defer jQuery.js.
          $no_jquery_defer = TRUE;
        }
      }
    }
    unset($values);
  }
  elseif (variable_get('advagg_mod_js_defer_inline_alter', ADVAGG_MOD_JS_DEFER_INLINE_ALTER)) {
    foreach ($js as &$values) {

      // Skip if not inline.
      if ($values['type'] !== 'inline') {
        continue;
      }

      // Skip if advagg has already wrapped this inline code.
      if (strpos($values['data'], 'advagg_mod_') !== FALSE) {
        continue;
      }
      if (!empty($values['no_defer'])) {
        continue;
      }

      // Do not wrap inline js if it contains a named function definition.
      $pattern = '/\\s*function\\s+((?:[a-z][a-z0-9_]*))\\s*\\(.*\\)\\s*\\{/smix';
      $match = preg_match($pattern, $values['data']);
      if (!$match) {

        // Defer the inline script by wrapping the code in setTimeout callback.
        $values['data'] = advagg_mod_defer_inline_js($values['data']);
      }
    }
    unset($values);
  }
  if (!empty($no_jquery_defer)) {
    $jquery_rev = strrev('/jquery.js');
    $jquery_min_rev = strrev('/jquery.min.js');
    foreach ($js as $name => &$values) {

      // Skip if not a file or external.
      if ($values['type'] !== 'file' && $values['type'] !== 'external') {
        continue;
      }

      // Special handling for jQuery.
      if (stripos(strrev($name), $jquery_rev) === 0 || stripos(strrev($name), $jquery_min_rev) === 0) {
        $values['defer'] = FALSE;
      }
    }
  }
}