Given a file info array it will split the file up.

Parameters

array $file_info: File info array from advagg_get_info_on_file().

string $file_contents: CSS file contents.

Return value

array Array with advagg_get_info_on_file data and split data.

4 calls to advagg_split_css_file()
AdvAggCascadingStylesheetsTestCase::testRenderFile in tests/advagg.test
Tests rendering the stylesheets.
advagg_advagg_get_info_on_files_alter in ./advagg.advagg.inc
Implements hook_advagg_get_info_on_files_alter().
advagg_generate_groups in ./advagg.inc
Group the CSS/JS into the biggest buckets possible.
hook_advagg_get_info_on_files_alter in ./advagg.api.php
Let other modules add/alter additional information about files passed in.

File

./advagg.inc, line 1065

Code

function advagg_split_css_file(array $file_info, $file_contents = '') {
    // Make advagg_parse_media_blocks() available.
    module_load_include('inc', 'advagg', 'advagg.missing');
    // Get the CSS file and break up by media queries.
    if (empty($file_contents)) {
        $file_contents = (string) @advagg_file_get_contents($file_info['data']);
    }
    $media_blocks = advagg_parse_media_blocks($file_contents);
    // Get the advagg_ie_css_selector_limiter_value.
    $selector_limit = (int) max(variable_get('advagg_ie_css_selector_limiter_value', ADVAGG_IE_CSS_SELECTOR_LIMITER_VALUE), 100);
    // Group media queries together.
    $part_selector_count = 0;
    $counter = 0;
    $values = array();
    foreach ($media_blocks as $media_block) {
        // Get the number of selectors.
        $selector_count = advagg_count_css_selectors($media_block);
        // This chunk is bigger than $selector_limit. It needs to be split.
        if ($selector_count > $selector_limit) {
            $inner_selector_count = 0;
            // Split css string.
            list($media_query, $split_css_strings) = advagg_split_css_string($media_block, $selector_limit);
            foreach ($split_css_strings as $split_css_strings) {
                $counter_changed = FALSE;
                if (empty($split_css_strings)) {
                    continue;
                }
                // Make sure selector count doesn't go over selector limit.
                $inner_selector_count = advagg_count_css_selectors($split_css_strings);
                $part_selector_count += $inner_selector_count;
                if ($part_selector_count > $selector_limit) {
                    if (!empty($values[$counter])) {
                        ++$counter;
                    }
                    $counter_changed = TRUE;
                    $part_selector_count = $inner_selector_count;
                }
                // Add to output array.
                if (isset($values[$counter])) {
                    if (!empty($media_query)) {
                        $values[$counter] .= "\n{$media_query} { {$split_css_strings} } ";
                    }
                    else {
                        $values[$counter] .= "{$split_css_strings}";
                    }
                }
                else {
                    if (!empty($media_query)) {
                        $values[$counter] = "{$media_query} { {$split_css_strings} } ";
                    }
                    else {
                        $values[$counter] = $split_css_strings;
                    }
                }
            }
            // Add to current selector counter and go to the next value.
            if (!$counter_changed) {
                $part_selector_count += $inner_selector_count;
            }
            continue;
        }
        $part_selector_count += $selector_count;
        if ($part_selector_count > $selector_limit) {
            if (!empty($values[$counter])) {
                ++$counter;
            }
            $values[$counter] = $media_block;
            $part_selector_count = $selector_count;
        }
        else {
            if (isset($values[$counter])) {
                $values[$counter] .= "\n{$media_block}";
            }
            else {
                $values[$counter] = $media_block;
            }
        }
    }
    // Save data.
    $parts = array();
    $overall_counter = 0;
    foreach ($values as $key => $value) {
        $last_chunk = FALSE;
        $file_info['split_last_part'] = FALSE;
        if (count($values) - 1 == $key) {
            $last_chunk = TRUE;
        }
        if ($last_chunk) {
            $file_info['split_last_part'] = TRUE;
        }
        // Get the number of selectors.
        $selector_count = advagg_count_css_selectors($value);
        $overall_counter += $selector_count;
        // Save file.
        $subfile = advagg_create_subfile($value, $overall_counter, $file_info);
        if (empty($subfile)) {
            // Something broke; do not create a subfile.
            $variables = array(
                '@info' => var_export($file_info, TRUE),
            );
            watchdog('advagg', 'Spliting up a CSS file failed. File info: <code>@info</code>', $variables);
            return array();
        }
        $parts[] = $subfile;
    }
    return $parts;
}