You are here

private function CSSmin::minify in Advanced CSS/JS Aggregation 6

Same name and namespace in other branches
  1. 8.2 advagg_css_minify/yui/CSSMin.inc \CSSmin::minify()
  2. 8.3 advagg_css_minify/yui/CSSMin.inc \CSSmin::minify()
  3. 7.2 advagg_css_compress/yui/CSSMin.inc \CSSmin::minify()

Does bulk of the minification

Parameters

string $css:

int|bool $linebreak_pos:

Return value

string

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

File

advagg_css_compress/yui/CSSMin.inc, line 216

Class

CSSmin

Code

private function minify($css, $linebreak_pos) {

  // strings are safe, now wrestle the comments
  for ($i = 0, $max = count($this->comments); $i < $max; $i++) {
    $token = $this->comments[$i];
    $placeholder = '/' . self::COMMENT . $i . '___/';

    // ! in the first position of the comment means preserve
    // so push to the preserved tokens keeping the !
    if (substr($token, 0, 1) === '!') {
      $this->preserved_tokens[] = $token;
      $token_tring = self::TOKEN . (count($this->preserved_tokens) - 1) . '___';
      $css = preg_replace($placeholder, $token_tring, $css, 1);

      // Preserve new lines for /*! important comments
      $css = preg_replace('/\\s*[\\n\\r\\f]+\\s*(\\/\\*' . $token_tring . ')/S', self::NL . '$1', $css);
      $css = preg_replace('/(' . $token_tring . '\\*\\/)\\s*[\\n\\r\\f]+\\s*/S', '$1' . self::NL, $css);
      continue;
    }

    // \ in the last position looks like hack for Mac/IE5
    // shorten that to /*\*/ and the next one to /**/
    if (substr($token, strlen($token) - 1, 1) === '\\') {
      $this->preserved_tokens[] = '\\';
      $css = preg_replace($placeholder, self::TOKEN . (count($this->preserved_tokens) - 1) . '___', $css, 1);
      $i = $i + 1;

      // attn: advancing the loop
      $this->preserved_tokens[] = '';
      $css = preg_replace('/' . self::COMMENT . $i . '___/', self::TOKEN . (count($this->preserved_tokens) - 1) . '___', $css, 1);
      continue;
    }

    // keep empty comments after child selectors (IE7 hack)
    // e.g. html >/**/ body
    if (strlen($token) === 0) {
      $start_index = $this
        ->index_of($css, $this
        ->str_slice($placeholder, 1, -1));
      if ($start_index > 2) {
        if (substr($css, $start_index - 3, 1) === '>') {
          $this->preserved_tokens[] = '';
          $css = preg_replace($placeholder, self::TOKEN . (count($this->preserved_tokens) - 1) . '___', $css, 1);
        }
      }
    }

    // in all other cases kill the comment
    $css = preg_replace('/\\/\\*' . $this
      ->str_slice($placeholder, 1, -1) . '\\*\\//', '', $css, 1);
  }

  // Normalize all whitespace strings to single spaces. Easier to work with that way.
  $css = preg_replace('/\\s+/', ' ', $css);

  // Shorten & preserve calculations calc(...) since spaces are important
  $css = preg_replace_callback('/calc(\\((?:[^\\(\\)]+|(?1))*\\))/i', array(
    $this,
    'replace_calc',
  ), $css);

  // Replace positive sign from numbers preceded by : or a white-space before the leading space is removed
  // +1.2em to 1.2em, +.8px to .8px, +2% to 2%
  $css = preg_replace('/((?<!\\\\)\\:|\\s)\\+(\\.?\\d+)/S', '$1$2', $css);

  // Remove leading zeros from integer and float numbers preceded by : or a white-space
  // 000.6 to .6, -0.8 to -.8, 0050 to 50, -01.05 to -1.05
  $css = preg_replace('/((?<!\\\\)\\:|\\s)(\\-?)0+(\\.?\\d+)/S', '$1$2$3', $css);

  // Remove trailing zeros from float numbers preceded by : or a white-space
  // -6.0100em to -6.01em, .0100 to .01, 1.200px to 1.2px
  $css = preg_replace('/((?<!\\\\)\\:|\\s)(\\-?)(\\d?\\.\\d+?)0+([^\\d])/S', '$1$2$3$4', $css);

  // Remove trailing .0 -> -9.0 to -9
  $css = preg_replace('/((?<!\\\\)\\:|\\s)(\\-?\\d+)\\.0([^\\d])/S', '$1$2$3', $css);

  // Replace 0 length numbers with 0
  $css = preg_replace('/((?<!\\\\)\\:|\\s)\\-?\\.?0+([^\\d])/S', '${1}0$2', $css);

  // Remove the spaces before the things that should not have spaces before them.
  // But, be careful not to turn "p :link {...}" into "p:link{...}"
  // Swap out any pseudo-class colons with the token, and then swap back.
  $css = preg_replace_callback('/(?:^|\\})(?:(?:[^\\{\\:])+\\:)+(?:[^\\{]*\\{)/', array(
    $this,
    'replace_colon',
  ), $css);
  $css = preg_replace('/\\s+([\\!\\{\\}\\;\\:\\>\\+\\(\\)\\]\\~\\=,])/', '$1', $css);
  $css = preg_replace('/' . self::CLASSCOLON . '/', ':', $css);

  // retain space for special IE6 cases
  $css = preg_replace('/\\:first\\-(line|letter)(\\{|,)/i', ':first-$1 $2', $css);

  // no space after the end of a preserved comment
  $css = preg_replace('/\\*\\/ /', '*/', $css);

  // Put the space back in some cases, to support stuff like
  // @media screen and (-webkit-min-device-pixel-ratio:0){
  $css = preg_replace('/\\band\\(/i', 'and (', $css);

  // Remove the spaces after the things that should not have spaces after them.
  $css = preg_replace('/([\\!\\{\\}\\:;\\>\\+\\(\\[\\~\\=,])\\s+/S', '$1', $css);

  // remove unnecessary semicolons
  $css = preg_replace('/;+\\}/', '}', $css);

  // Fix for issue: #2528146
  // Restore semicolon if the last property is prefixed with a `*` (lte IE7 hack)
  // to avoid issues on Symbian S60 3.x browsers.
  $css = preg_replace('/(\\*[a-z0-9\\-]+\\s*\\:[^;\\}]+)(\\})/', '$1;$2', $css);

  // Replace 0 length units 0(px,em,%) with 0.
  $css = preg_replace('/((?<!\\\\)\\:|\\s)\\-?0(?:em|ex|ch|rem|vw|vh|vm|vmin|cm|mm|in|px|pt|pc|%)/iS', '${1}0', $css);

  // Replace 0 0; or 0 0 0; or 0 0 0 0; with 0.
  $css = preg_replace('/\\:0(?: 0){1,3}(;|\\})/', ':0$1', $css);

  // Fix for issue: #2528142
  // Replace text-shadow:0; with text-shadow:0 0 0;
  $css = preg_replace('/(text-shadow\\:0)(;|\\})/ie', "strtolower('\$1 0 0\$2')", $css);

  // Replace background-position:0; with background-position:0 0;
  // same for transform-origin
  $css = preg_replace('/(background\\-position|(?:webkit|moz|o|ms|)\\-?transform\\-origin)\\:0(;|\\})/ieS', "strtolower('\$1:0 0\$2')", $css);

  // Shorten colors from rgb(51,102,153) to #336699, rgb(100%,0%,0%) to #ff0000 (sRGB color space)
  // Shorten colors from hsl(0, 100%, 50%) to #ff0000 (sRGB color space)
  // This makes it more likely that it'll get further compressed in the next step.
  $css = preg_replace_callback('/rgb\\s*\\(\\s*([0-9,\\s\\-\\.\\%]+)\\s*\\)(.{1})/i', array(
    $this,
    'rgb_to_hex',
  ), $css);
  $css = preg_replace_callback('/hsl\\s*\\(\\s*([0-9,\\s\\-\\.\\%]+)\\s*\\)(.{1})/i', array(
    $this,
    'hsl_to_hex',
  ), $css);

  // Shorten colors from #AABBCC to #ABC or short color name.
  $css = $this
    ->compress_hex_colors($css);

  // border: none to border:0, outline: none to outline:0
  $css = preg_replace('/(border\\-?(?:top|right|bottom|left|)|outline)\\:none(;|\\})/ieS', "strtolower('\$1:0\$2')", $css);

  // shorter opacity IE filter
  $css = preg_replace('/progid\\:DXImageTransform\\.Microsoft\\.Alpha\\(Opacity\\=/i', 'alpha(opacity=', $css);

  // Remove empty rules.
  $css = preg_replace('/[^\\};\\{\\/]+\\{\\}/S', '', $css);

  // Some source control tools don't like it when files containing lines longer
  // than, say 8000 characters, are checked in. The linebreak option is used in
  // that case to split long lines after a specific column.
  if ($linebreak_pos !== FALSE && (int) $linebreak_pos >= 0) {
    $linebreak_pos = (int) $linebreak_pos;
    $start_index = $i = 0;
    while ($i < strlen($css)) {
      $i++;
      if ($css[$i - 1] === '}' && $i - $start_index > $linebreak_pos) {
        $css = $this
          ->str_slice($css, 0, $i) . "\n" . $this
          ->str_slice($css, $i);
        $start_index = $i;
      }
    }
  }

  // Replace multiple semi-colons in a row by a single one
  // See SF bug #1980989
  $css = preg_replace('/;;+/', ';', $css);

  // Restore new lines for /*! important comments
  $css = preg_replace('/' . self::NL . '/', "\n", $css);

  // restore preserved comments and strings
  for ($i = 0, $max = count($this->preserved_tokens); $i < $max; $i++) {
    $css = preg_replace('/' . self::TOKEN . $i . '___/', $this->preserved_tokens[$i], $css, 1);
  }

  // Trim the final string (for any leading or trailing white spaces)
  return trim($css);
}