You are here

function agrcache_process_css_group in Aggregate cache 7

Aggregate and compress css groups.

File

./agrcache.module, line 353
Provides imagecache style generation of css/js aggregates.

Code

function agrcache_process_css_group($css) {

  // Build aggregate CSS file.
  $data = '';
  foreach ($css as $stylesheet) {

    // Only 'file' stylesheets can be aggregated.
    if ($stylesheet['type'] == 'file') {
      $contents = drupal_load_stylesheet($stylesheet['data'], TRUE);

      // Build the base URL of this CSS file: start with the full URL.
      $css_base_url = file_create_url($stylesheet['data']);

      // Move to the parent.
      $css_base_url = substr($css_base_url, 0, strrpos($css_base_url, '/'));

      // Simplify to a relative URL if the stylesheet URL starts with the
      // base URL of the website.
      if (substr($css_base_url, 0, strlen($GLOBALS['base_root'])) == $GLOBALS['base_root']) {
        $css_base_url = substr($css_base_url, strlen($GLOBALS['base_root']));
      }
      _drupal_build_css_path(NULL, $css_base_url . '/');

      // Anchor all paths in the CSS with its base URL, ignoring external and absolute paths.
      $data .= preg_replace_callback('/url\\(\\s*[\'"]?(?![a-z]+:|\\/+)([^\'")]+)[\'"]?\\s*\\)/i', '_drupal_build_css_path', $contents);
    }
  }

  // Per the W3C specification at http://www.w3.org/TR/REC-CSS2/cascade.html#at-import,
  // @import rules must proceed any other style, so we move those to the top.
  $regexp = '/@import[^;]+;/i';
  preg_match_all($regexp, $data, $matches);
  $data = preg_replace($regexp, '', $data);
  $data = implode('', $matches[0]) . $data;
  return $data;
}