You are here

function csstidy_optimise::merge_selectors in Advanced CSS/JS Aggregation 7

Same name and namespace in other branches
  1. 6 advagg_css_compress/csstidy/class.csstidy_optimise.inc \csstidy_optimise::merge_selectors()

Merges selectors with same properties. Example: a{color:red} b{color:red} -> a,b{color:red} Very basic and has at least one bug. Hopefully there is a replacement soon.

@access public @version 1.2

Parameters

array $array:

Return value

array

1 call to csstidy_optimise::merge_selectors()
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 457

Class

csstidy_optimise
CSS Optimising Class

Code

function merge_selectors(&$array) {
  $css = $array;
  foreach ($css as $key => $value) {
    if (!isset($css[$key])) {
      continue;
    }
    $newsel = '';

    // Check if properties also exist in another selector
    $keys = array();

    // PHP bug (?) without $css = $array; here
    foreach ($css as $selector => $vali) {
      if ($selector == $key) {
        continue;
      }
      if ($css[$key] === $vali) {
        $keys[] = $selector;
      }
    }
    if (!empty($keys)) {
      $newsel = $key;
      unset($css[$key]);
      foreach ($keys as $selector) {
        unset($css[$selector]);
        $newsel .= ',' . $selector;
      }
      $css[$newsel] = $value;
    }
  }
  $array = $css;
}