function csscrush_rgba(CssCrush_Rule $rule)
{
    $props = array_keys($rule->properties);
    // Determine which properties apply
    $rgba_props = array();
    foreach ($props as $prop) {
        if ($prop === 'background' or strpos($prop, 'color') !== false) {
            $rgba_props[] = $prop;
        }
    }
    if (empty($rgba_props)) {
        return;
    }
    $new_set = array();
    foreach ($rule as $declaration) {
        $is_viable = in_array($declaration->property, $rgba_props);
        if ($declaration->skip or !$is_viable or $is_viable and !preg_match('!^rgba___p\\d+___$!', $declaration->value)) {
            $new_set[] = $declaration;
            continue;
        }
        // Create rgb value from rgba
        $raw_value = $rule->getDeclarationValue($declaration);
        $raw_value = substr($raw_value, 5, strlen($raw_value) - 1);
        list($r, $g, $b, $a) = explode(',', $raw_value);
        // Add rgb value to the stack, followed by rgba
        $new_set[] = $rule->createDeclaration($declaration->property, "rgb({$r},{$g},{$b})");
        $new_set[] = $declaration;
    }
    $rule->declarations = $new_set;
}
function csscrush_minheight(CssCrush_Rule $rule)
{
    if ($rule->propertyCount('min-height') < 1) {
        return;
    }
    $new_set = array();
    foreach ($rule as $declaration) {
        $new_set[] = $declaration;
        if ($declaration->skip or $declaration->property !== 'min-height') {
            continue;
        }
        $new_set[] = $rule->createDeclaration('_height', $declaration->value);
    }
    $rule->declarations = $new_set;
}
function csscrush_clip(CssCrush_Rule $rule)
{
    // Assume it's been dealt with if the property occurs more than once
    if ($rule->propertyCount('clip') !== 1) {
        return;
    }
    $new_set = array();
    foreach ($rule as $declaration) {
        $new_set[] = $declaration;
        if ($declaration->skip or $declaration->property !== 'clip') {
            continue;
        }
        $new_set[] = $rule->createDeclaration('*clip', str_replace(',', ' ', $rule->getDeclarationValue($declaration)));
    }
    $rule->declarations = $new_set;
}
function csscrush_display_inlineblock(CssCrush_Rule $rule)
{
    if ($rule->propertyCount('display') < 1) {
        return;
    }
    $new_set = array();
    foreach ($rule as $declaration) {
        $new_set[] = $declaration;
        $is_display = $declaration->property === 'display';
        if ($declaration->skip or !$is_display or $is_display and $declaration->value !== 'inline-block') {
            continue;
        }
        $new_set[] = $rule->createDeclaration('*display', 'inline');
        $new_set[] = $rule->createDeclaration('*zoom', 1);
    }
    $rule->declarations = $new_set;
}
function csscrush_filter(CssCrush_Rule $rule)
{
    if ($rule->propertyCount('-ms-filter') < 1) {
        return;
    }
    $filter_prefix = 'progid:DXImageTransform.Microsoft.';
    $new_set = array();
    foreach ($rule as $declaration) {
        if ($declaration->skip or $declaration->property !== '-ms-filter') {
            $new_set[] = $declaration;
            continue;
        }
        $list = array_map('trim', explode(',', $declaration->value));
        foreach ($list as &$item) {
            if (strpos($item, $filter_prefix) !== 0 and strpos($item, 'alpha') !== 0) {
                $item = $filter_prefix . ucfirst($item);
            }
        }
        $declaration->value = implode(',', $list);
        if (!$rule->propertyCount('zoom')) {
            // Filters need hasLayout
            $new_set[] = $rule->createDeclaration('zoom', 1);
        }
        // Quoted version for -ms-filter IE >= 8
        $new_set[] = $rule->createDeclaration('-ms-filter', "\"{$declaration->value}\"");
        // Star escaped property for IE < 8
        $new_set[] = $rule->createDeclaration('*filter', $declaration->value);
    }
    $rule->declarations = $new_set;
}
function csscrush_opacity(CssCrush_Rule $rule)
{
    if ($rule->propertyCount('opacity') < 1) {
        return;
    }
    $new_set = array();
    foreach ($rule as $declaration) {
        $new_set[] = $declaration;
        if ($declaration->skip or $declaration->property != 'opacity') {
            continue;
        }
        $opacity = (double) $declaration->value;
        $opacity = round($opacity * 100);
        if (!$rule->propertyCount('zoom')) {
            // Filters need hasLayout
            $new_set[] = $rule->createDeclaration('zoom', 1);
        }
        $value = "alpha(opacity={$opacity})";
        $new_set[] = $rule->createDeclaration('-ms-filter', "\"{$value}\"");
        $new_set[] = $rule->createDeclaration('*filter', $value);
    }
    $rule->declarations = $new_set;
}
예제 #7
0
파일: Core.php 프로젝트: handrus/css-crush
 protected static function cb_extractRules($match)
 {
     $rule = new CssCrush_Rule($match[1], $match[2]);
     // Only store rules with declarations
     if (!empty($rule->declarations)) {
         if (!empty(self::$aliases)) {
             $rule->addPropertyAliases();
             $rule->addFunctionAliases();
             $rule->addValueAliases();
         }
         $rule->applyMacros();
         $rule->expandSelectors();
         $label = self::createTokenLabel('r');
         self::$storage->tokens->rules[$label] = $rule;
         return $label . "\n";
     } else {
         return '';
     }
 }