/** * Consolidates background styles into a single background style * * @param array $styles Consolidates the provided array of background styles * @return array Consolidated optimised background styles */ public static function consolidate(array $styles) { if (count($styles) < 1) { return $styles; } $color = $image = $repeat = $attachment = $position = null; foreach ($styles as $style) { switch ($style->get_name()) { case 'background-color': $color = css_style_color::shrink_value($style->get_value()); break; case 'background-image': $image = $style->get_value(); break; case 'background-repeat': $repeat = $style->get_value(); break; case 'background-attachment': $attachment = $style->get_value(); break; case 'background-position': $position = $style->get_value(); break; } } if ((is_null($image) || is_null($position) || is_null($repeat)) && ($image != null || $position != null || $repeat != null)) { return $styles; } $value = array(); if (!is_null($color)) { $value[] .= $color; } if (!is_null($image)) { $value[] .= $image; } if (!is_null($repeat)) { $value[] .= $repeat; } if (!is_null($attachment)) { $value[] .= $attachment; } if (!is_null($position)) { $value[] .= $position; } return array(new css_style_background('background', join(' ', $value))); }
/** * Consolidates background styles into a single background style * * @param array $styles Consolidates the provided array of background styles * @return array Consolidated optimised background styles */ public static function consolidate(array $styles) { if (empty($styles)) { return $styles; } $color = null; $image = null; $repeat = null; $attachment = null; $position = null; $size = null; $origin = null; $clip = null; $someimportant = false; $allimportant = null; foreach ($styles as $style) { if ($style instanceof css_style_backgroundimage_advanced) { continue; } if ($style->is_important()) { $someimportant = true; if ($allimportant === null) { $allimportant = true; } } else { if ($allimportant === true) { $allimportant = false; } } } $organisedstyles = array(); $advancedstyles = array(); $importantstyles = array(); foreach ($styles as $style) { if ($style instanceof css_style_backgroundimage_advanced) { $advancedstyles[] = $style; continue; } if ($someimportant && !$allimportant && $style->is_important()) { $importantstyles[] = $style; continue; } $organisedstyles[$style->get_name()] = $style; switch ($style->get_name()) { case 'background-color': $color = css_style_color::shrink_value($style->get_value(false)); break; case 'background-image': $image = $style->get_value(false); break; case 'background-repeat': $repeat = $style->get_value(false); break; case 'background-attachment': $attachment = $style->get_value(false); break; case 'background-position': $position = $style->get_value(false); break; case 'background-clip': $clip = $style->get_value(); break; case 'background-origin': $origin = $style->get_value(); break; case 'background-size': $size = $style->get_value(); break; } } $consolidatetosingle = array(); if (!is_null($color) && !is_null($image) && !is_null($repeat) && !is_null($attachment) && !is_null($position)) { // We can use the shorthand background-style! if (!$organisedstyles['background-color']->is_special_empty_value()) { $consolidatetosingle[] = $color; } if (!$organisedstyles['background-image']->is_special_empty_value()) { $consolidatetosingle[] = $image; } if (!$organisedstyles['background-repeat']->is_special_empty_value()) { $consolidatetosingle[] = $repeat; } if (!$organisedstyles['background-attachment']->is_special_empty_value()) { $consolidatetosingle[] = $attachment; } if (!$organisedstyles['background-position']->is_special_empty_value()) { $consolidatetosingle[] = $position; } // Reset them all to null so we don't use them again. $color = null; $image = null; $repeat = null; $attachment = null; $position = null; } $return = array(); // Single background style needs to come first; if (count($consolidatetosingle) > 0) { $returnstyle = new css_style_background('background', join(' ', $consolidatetosingle)); if ($allimportant) { $returnstyle->set_important(); } $return[] = $returnstyle; } foreach ($styles as $style) { $value = null; switch ($style->get_name()) { case 'background-color': $value = $color; break; case 'background-image': $value = $image; break; case 'background-repeat': $value = $repeat; break; case 'background-attachment': $value = $attachment; break; case 'background-position': $value = $position; break; case 'background-clip': $value = $clip; break; case 'background-origin': $value = $origin; break; case 'background-size': $value = $size; break; } if (!is_null($value)) { $return[] = $style; } } $return = array_merge($return, $importantstyles, $advancedstyles); return $return; }