You are here

private function CSSCompression_Combine_MarginPadding::expand in Advanced CSS/JS Aggregation 7

Same name and namespace in other branches
  1. 6 advagg_css_compress/css-compressor-3.x/src/lib/Combine/MarginPadding.inc \CSSCompression_Combine_MarginPadding::expand()

Explodes shorthanded margin/padding properties for later combination

Parameters

(string) val: Rule set:

1 call to CSSCompression_Combine_MarginPadding::expand()
CSSCompression_Combine_MarginPadding::combine in advagg_css_compress/css-compressor-3.x/src/lib/Combine/MarginPadding.inc
Combines multiple directional properties of margin/padding into single definition.

File

advagg_css_compress/css-compressor-3.x/src/lib/Combine/MarginPadding.inc, line 118

Class

CSSCompression_Combine_MarginPadding
CSS Compressor [VERSION] [DATE] Corey Hart @ http://www.codenothing.com

Code

private function expand($val) {
  $pos = 0;
  while (preg_match($this->rmpbase, $val, $match, PREG_OFFSET_CAPTURE, $pos)) {
    $replace = '';
    $prop = $match[2][0];
    $value = preg_split($this->rspace, trim($match[3][0]));
    $positions = array(
      'top' => 0,
      'right' => 0,
      'bottom' => 0,
      'left' => 0,
    );

    // Skip uncombinables
    if ($this->Combine
      ->checkUncombinables($value)) {
      $pos = $match[0][1] + strlen($match[0][0]);
      continue;
    }

    // Each position needs a value
    switch (count($value)) {
      case 1:
        $positions['top'] = $positions['right'] = $positions['bottom'] = $positions['left'] = $value[0];
        break;
      case 2:
        $positions['top'] = $positions['bottom'] = $value[0];
        $positions['right'] = $positions['left'] = $value[1];
        break;
      case 3:
        $positions['top'] = $value[0];
        $positions['right'] = $positions['left'] = $value[1];
        $positions['bottom'] = $value[2];
        break;
      case 4:
        $positions['top'] = $value[0];
        $positions['right'] = $value[1];
        $positions['bottom'] = $value[2];
        $positions['left'] = $value[3];
        break;
      default:
        continue;
    }

    // Build the replacement
    foreach ($positions as $p => $v) {
      $replace .= "{$prop}-{$p}:{$v};";
    }
    $colon = strlen($match[1][0]);
    $val = substr_replace($val, $replace, $match[0][1] + $colon, strlen($match[0][0]) - $colon);
    $pos = $match[0][1] + strlen($replace) - $colon - 1;
  }
  return $val;
}