Example #1
0
 /**
  * Generate the old webkit syntax for the linear-gradient
  *
  * @param Stylecow\Property $property The property object with the standard syntax
  */
 public static function webkitLinearGradient(Property $property)
 {
     $newProperty = clone $property;
     $newProperty->executeFunction('linear-gradient', function ($params) {
         $point = 'top';
         if (preg_match('/(top|bottom|left|right|deg)/', $params[0])) {
             $point = array_shift($params);
         }
         switch ($point) {
             case 'to bottom':
                 $start = 'left top';
                 $end = 'left bottom';
                 break;
             case 'to top':
                 $start = 'left bottom';
                 $end = 'left top';
                 break;
             case 'to right':
                 $start = 'left top';
                 $end = 'right top';
                 break;
             case 'to left':
                 $start = 'right top';
                 $end = 'left top';
                 break;
             default:
                 if (preg_match('/^\\ddeg$/', $point)) {
                     $radius = intval($point);
                 } else {
                     $start = 'left top';
                     $end = 'left bottom';
                 }
         }
         $color_stops = array();
         $tk = count($params) - 1;
         foreach ($params as $k => $param) {
             $param = Parser::explode(' ', trim($param));
             $color = $param[0];
             $stop = isset($param[1]) ? $param[1] : null;
             if ($k === 0) {
                 $text = 'from';
             } else {
                 if ($k === $tk) {
                     $text = 'to';
                 } else {
                     $text = 'color-stop';
                 }
             }
             if ($stop) {
                 $color_stops[] = $text . '(' . $stop . ', ' . $color . ')';
             } else {
                 $color_stops[] = $text . '(' . $color . ')';
             }
         }
         if (isset($radius)) {
             return '-webkit-gradient(linear, ' . $radius . 'deg, ' . implode(', ', $color_stops) . ')';
         } else {
             return '-webkit-gradient(linear, ' . $start . ', ' . $end . ', ' . implode(', ', $color_stops) . ')';
         }
     });
     if ($property->value !== $newProperty->value) {
         $newProperty->vendor = 'webkit';
         $property->parent->addProperty($newProperty, $property->getPositionInParent());
     }
 }
Example #2
0
 /**
  * Converts the old syntax of linear-gradient to newer
  *
  * @param Stylecow\Property $property The property object with the old syntax
  */
 public static function normalizeLinearGradient($property)
 {
     return $property->executeFunction('linear-gradient', function ($params) {
         switch ($params[0]) {
             case 'center top':
             case 'top':
                 $params[0] = 'to bottom';
                 break;
             case 'center bottom':
             case 'bottom':
                 $params[0] = 'to top';
                 break;
             case 'left top':
             case 'left':
                 $params[0] = 'to right';
                 break;
             case 'right top':
             case 'right':
                 $params[0] = 'to left';
                 break;
             default:
                 if (strpos($params[0], 'to ') === 0 || strpos($params[0], 'deg') !== false) {
                     return null;
                 }
                 array_unshift($params, 'to bottom');
         }
         return 'linear-gradient(' . implode(', ', $params) . ')';
     });
 }