You are here

private function CSSmin::processDataUrls in Advanced CSS/JS Aggregation 7.2

Searches & replaces all data urls with tokens before we start compressing, to avoid performance issues running some of the subsequent regexes against large string chunks.

Parameters

string $css:

Return value

string

1 call to CSSmin::processDataUrls()
CSSmin::run in advagg_css_compress/yui/CSSMin.inc
Minifies a string of CSS

File

advagg_css_compress/yui/CSSMin.inc, line 574

Class

CSSmin

Code

private function processDataUrls($css) {

  // Leave data urls alone to increase parse performance.
  $maxIndex = strlen($css) - 1;
  $appenIndex = $index = $lastIndex = $offset = 0;
  $sb = array();
  $pattern = '/url\\(\\s*(["\']?)data:/i';

  // Since we need to account for non-base64 data urls, we need to handle
  // ' and ) being part of the data string. Hence switching to indexOf,
  // to determine whether or not we have matching string terminators and
  // handling sb appends directly, instead of using matcher.append* methods.
  while (preg_match($pattern, $css, $m, 0, $offset)) {
    $index = $this
      ->indexOf($css, $m[0], $offset);
    $lastIndex = $index + strlen($m[0]);
    $startIndex = $index + 4;

    // "url(".length()
    $endIndex = $lastIndex - 1;
    $terminator = $m[1];

    // ', " or empty (not quoted)
    $terminatorFound = false;
    if (strlen($terminator) === 0) {
      $terminator = ')';
    }
    while ($terminatorFound === false && $endIndex + 1 <= $maxIndex) {
      $endIndex = $this
        ->indexOf($css, $terminator, $endIndex + 1);

      // endIndex == 0 doesn't really apply here
      if ($endIndex > 0 && substr($css, $endIndex - 1, 1) !== '\\') {
        $terminatorFound = true;
        if (')' !== $terminator) {
          $endIndex = $this
            ->indexOf($css, ')', $endIndex);
        }
      }
    }

    // Enough searching, start moving stuff over to the buffer
    $sb[] = $this
      ->strSlice($css, $appenIndex, $index);
    if ($terminatorFound) {
      $token = $this
        ->strSlice($css, $startIndex, $endIndex);

      // Remove all spaces only for base64 encoded URLs.
      $token = preg_replace_callback('/.+base64,.+/s', array(
        $this,
        'removeSpacesFromDataUrls',
      ), trim($token));
      $preservedTokenPlaceholder = $this
        ->registerPreservedToken($token);
      $sb[] = 'url(' . $preservedTokenPlaceholder . ')';
      $appenIndex = $endIndex + 1;
    }
    else {

      // No end terminator found, re-add the whole match. Should we throw/warn here?
      $sb[] = $this
        ->strSlice($css, $index, $lastIndex);
      $appenIndex = $lastIndex;
    }
    $offset = $lastIndex;
  }
  $sb[] = $this
    ->strSlice($css, $appenIndex);
  return implode('', $sb);
}