Minimizes and organizes your CSS

Posted By Venu Thomas

Barry van Oudtshoorn, has developed a compactor CSS also allows us to alphabetize our CSS attributes.
It has therefore followed the specifications proposed in Google Payload Size Minimize making our CSS is smaller and, consequently, less late in coming to the end user.

What do you do?

Using a basic CSS file are applied to a series of transformations to obtain a new CSS file supplement packed.

  • Eliminate all comments
  • Sort all the properties for each selector alphabetically
  • Sort all values of each property alphabetically
  • Eliminate all unnecessary space.

Comparison with YUI Compressor

CSS_Compress_090909

The compaction is equal to that offered by YUI Compressor, but only the above (and little) in cases with gzipped CSS.
YUI_Comparison_090909

PHP Version

Taking advantage of this holiday and I’ll be okay for a project I’m developing, I ported to PHP function to use it without using Java.

<?php
class cssCompressor {
 private $cssFileName;
 private $selectors = array();
 
 function cssConstruct($cssContent = '', $deleteComments = true){
 $this->cssFileName = $cssContent;
 
 // Delete comments
 if ($deleteComments)
 $this->cssFileName = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $this->cssFileName);
 
 $selectors = array();
 // We get all the selectors
 $parties = explode("}", $this->cssFileName);
 
 foreach($parties as $part) {
 if (!empty($part))
 array_push($this->selectors, new SelectorCSS($part."}"));
 }
 }
 
 function get($cleanSpaces = true){
 $output = '';
 foreach($this->selectors as $selector) {
 $output .= $selector->get($cleanSpaces)."\n";
 }
 // Delete line breaks, spaces, ...
 if ($cleanSpaces)
 $output = str_replace(array("\t", "\n", "\r"), '', $output);
 
 return $output;
 }
}
 
/*
 SelectorCSS
*/
class SelectorCSS{
 private $properties = array();
 private $selector;
 
 function cssConstruct($selector = ''){
 $parts = explode("{", $selector);
 if (count($parts) < 2) {
 die("Selector " + selector +" incomplete");
 }
 $this->selector = trim($parts[0]);
 $contents = trim($parts[1]);
 if (strlen($contents) == 0) {
 die("Selector " + selector + "empty");
 }
 
 if (substr($contents,strlen($contents) -1 ,1) != '}'){
 die("Selector "+selector+" is wrong");
 }
 
 $contents = substr($contents, 0, $contents - 2);
 
 $this->properties = explode(';', $contents);
 }
 
 function get(){
 $str = $this->selector.'{';
 natcasesort($this->properties);
 foreach($this->properties as $property) {
 if (!empty($property))
 $str .= $property.";";
 }
 $str .= '}';
 return $str;
 }
 
}
?>

To use is simple, we simply must indicate the file’s contents. Css we want to compact and manage their properties. Upon receiving the contents of the file we can send one or more file concatenating the contents thereof.

// StyleSheet.css file content
 $fileName = file_get_contents('StyleSheet.css');
 
 // We create an object that we pass the contents of parameter
 $styleSheet = new cssCompressor($fileName);
 /* Parameters:
     1: (String) content of the file
     2: (Bool) Clear comments (default: true)
 */
 // We get the CSS content
 echo $styleSheet->get(false);
 /*    Parameters:
      1: (Bool) Delete lines (default: true)
 */

The object has a method get() that returns the contents compacted and orderly, this content can cache in a static file or display it directly.

Tags: , , ,

  •  
  • Share with others:

    Thanks for visiting us! If you enjoyed these icons please feel free to share them! Or if you want to know what's going on with WiseCodes.com, follow us!

    4 Responses to “Minimizes and organizes your CSS”

    1. This is a really good read for me. Must admit that you are one of the coolest bloggers I ever saw. Thanks for posting this informative article.

    2. Watch the human centipede online your flag on August 16th, 2010 at 9:16 AM
    3. I have been visiting variousblogs for my dissertation study. I have found your blog to be fairlybeneficial. Keep updating your blog with usefulinformation… Regards

    4. Blueair 402 Air Purifier your flag on August 20th, 2010 at 9:19 AM
    5. Excellent post. I want to thank you for this informative read, I really appreciate sharing this great post. Keep up your work. . . .

    6. Biozone Air Purifier your flag on August 26th, 2010 at 6:14 PM
    7. I love the blog. Great post. It is very true, people must learn how to learn before they can learn. lol i know it sounds funny but its very true. . .

    8. Vibram FiveFingers KSO Multisport Shoes Men’s your flag on August 30th, 2010 at 3:31 PM

    Leave a Reply

    Anti-Spam Protection by WP-SpamFree

    Locations of visitors to this page eXTReMe Tracker