views:

58

answers:

2

I need some help writing an awesome class to take a style sheet, detect browser specific CSS3 rules, and add support for all compatible browsers. This way, we can just write our styles sheets for one browser and then process the CSS files when we are ready for production.

Here's my thoughts on the class so far:

class CssRewriter {

    public function reformCss($file) {
        // Get the CSS as a string
        $cssString = file_get_contents($file);

        // Use regex to capture all styles delimited by {...}

        // Use regex to determine if the any of the captured styles are browser
        // specific (starts with -moz, -webkit, etc)

        // Determine which CSS3 rules are not present and add them to the style
        // (so if you have -moz-linear-gradient, automatically add the webkit
        // version)
    }

}
+3  A: 

Yikes. CSS parsers are not as easy as you imagine, man. Depending on regular expressions is just asking for one typo to be totally misinterpreted.

Not the answer you were looking for, but quite possibly a better one: have you considered using Sass and mixins? You're not the first to hit the issue of the repetitive nature of CSS, so someone else has already faced the challenge of a CSS pre-processor for you.

Matchu
A: 

Your best bet would be to modify existing CSS parser like CSS Tidy and add in a additional logic to output backwards-compatible CSS.

pygorex1