Merges all background properties

@version 1.0

@todo full CSS 3 compliance

Parameters

array $input_css:

Return value

array

See also

dissolve_short_bg()

1 call to csstidy_optimise::merge_bg()
csstidy_optimise::postparse in advagg_css_compress/csstidy/class.csstidy_optimise.inc
Optimises $css after parsing @access public @version 1.0

File

advagg_css_compress/csstidy/class.csstidy_optimise.inc, line 759

Class

csstidy_optimise
CSS Optimising Class

Code

function merge_bg($input_css) {
    $background_prop_default =& $GLOBALS['csstidy']['background_prop_default'];
    // Max number of background images. CSS3 not yet fully implemented
    $number_of_values = @max(count(csstidy_optimise::explode_ws(',', $input_css['background-image'])), count(csstidy_optimise::explode_ws(',', $input_css['background-color'])), 1);
    // Array with background images to check if BG image exists
    $bg_img_array = @csstidy_optimise::explode_ws(',', csstidy::gvw_important($input_css['background-image']));
    $new_bg_value = '';
    $important = '';
    // if background properties is here and not empty, don't try anything
    if (isset($input_css['background']) and $input_css['background']) {
        return $input_css;
    }
    for ($i = 0; $i < $number_of_values; $i++) {
        foreach ($background_prop_default as $bg_property => $default_value) {
            // Skip if property does not exist
            if (!isset($input_css[$bg_property])) {
                continue;
            }
            $cur_value = $input_css[$bg_property];
            // skip all optimisation if gradient() somewhere
            if (stripos($cur_value, "gradient(") !== FALSE) {
                return $input_css;
            }
            // Skip some properties if there is no background image
            if ((!isset($bg_img_array[$i]) || $bg_img_array[$i] === 'none') && ($bg_property === 'background-size' || $bg_property === 'background-position' || $bg_property === 'background-attachment' || $bg_property === 'background-repeat')) {
                continue;
            }
            // Remove !important
            if (csstidy::is_important($cur_value)) {
                $important = ' !important';
                $cur_value = csstidy::gvw_important($cur_value);
            }
            // Do not add default values
            if ($cur_value === $default_value) {
                continue;
            }
            $temp = csstidy_optimise::explode_ws(',', $cur_value);
            if (isset($temp[$i])) {
                if ($bg_property === 'background-size') {
                    $new_bg_value .= '(' . $temp[$i] . ') ';
                }
                else {
                    $new_bg_value .= $temp[$i] . ' ';
                }
            }
        }
        $new_bg_value = trim($new_bg_value);
        if ($i != $number_of_values - 1) {
            $new_bg_value .= ',';
        }
    }
    // Delete all background-properties
    foreach ($background_prop_default as $bg_property => $default_value) {
        unset($input_css[$bg_property]);
    }
    // Add new background property
    if ($new_bg_value !== '') {
        $input_css['background'] = $new_bg_value . $important;
    }
    elseif (isset($input_css['background'])) {
        $input_css['background'] = 'none';
    }
    return $input_css;
}