CSSParser release

So… I haven’t really been doing any work at all on the CSSParser(my own CSS Tidy project for reading and compressing CSS) I started on a couple of months ago. What happened is what always happens with my own projects. After I’ve implemented the initial idea, I start to loose interest.
But I’ve made it further with this CSSParser than I usually do, so I’ve decided to release the source code for the project. Not every function will be well documented but I’ve tried to comment as much as possible.

Download it here.

I’ll do a quick explanation of how the process, just to give an idea of how it all works.

  1. The CSSParser class reads a given CSS, using a single preg_match_all call.
  2. Now then class will start to build up an array of CSSSelector classes. This class will hold all the properties and identifiers of a CSS selector from the given CSS.
  3. Each property of the CSSSelector will be held in a CSSProperty class or a class extended the CSSProperty like CSSBorder and CSSBox. This way it’s easy to add more specific output on properties, simply by extending the CSSProperty class and making your own property class.
  4. Now all the data is structured in the CSSParser class. And by calling the printCSS() function with a CSSOut class as the argument, we can get the CSS out the way we want.

When printing the CSS a very primitive templating system is used, where the template simply is PHP that has access to the list of selectors after they’ve been run through a preProcess function optimizing the data for output.
An example can be the template1.php file (found in the downloadable zip):

1
2
3
4
5
6
7
8
9
10
11
<?php
foreach ($data as $selector) {
    echo implode(",\n", $selector->identifier);
    echo " {\n";
    foreach ($selector->properties as $property) {
        foreach ($property->getProperty($settings) as $key => $value) {
            echo "\t$key: $value;\n";
        }
    }
    echo "}\n";
}?>


In the bottom of the CSSParser.php file, there’s a small test which outputs a CSS file using 2 different templates. This little example shows you how you can easily use the parser:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
$css = new CSSParser();
$css->readCSS(file_get_contents(dirname(__FILE__) . "/data/test.css"));
 
$settings = new CSSOut();
 
echo "Readability:\n";
$settings->templateFile = "data/template1.php";
var_dump($css->printCSS($settings));
 
echo "\nSmallest:\n";
$settings->templateFile = "data/template2.php";
var_dump($css->printCSS($settings));
?>

This project is far from done (or well tested), so even though it can read and parse the simple test.css file included in the zip, it’s not ready for an actual live deployment. It’s simply a piece of code that shows how I would’ve implemented a CSS Parser if I had the time to finish it all.

So if you decide to download the project, here’s a little guide with places of interest (code pieces in the project I feel is vital for this to work or had most fun writing).

  • CSSParser->readCSS($cssString) This function is the heart of it all. It processes the CSS and sets up the selectors with the correct properties. I feel I’ve documented this function well, and since I don’t do regular expressions very often I’m particularly proud of the 2 patterns just at the start of this function!
  • CSSSelector->getHandler($property) The function that makes sure the correct CSSProperty class is used for a property. It’s actually a bit confusing, and I had to read it a couple of times again before realising what I was trying to achieve when I wrote it. It’s important to note that it’s connected with the $propertyList variable at the top of the CSSSelector.php file.
  • CSSBorder Yep the entire class. This was my first property class, then I wrote the CSSProperty class, so the CSSBorder could fit on it, and then wrote the CSSBox in between these to classes. The getBorderArray() function in this class, is also responsible for my Checking for a value speed test
Leave a comment

0 Comments.

Leave a Reply

[ Ctrl + Enter ]