sabberworm / PHP-CSS-Parser

A Parser for CSS Files written in PHP. Allows extraction of CSS files into a data structure, manipulation of said structure and output as (optimized) CSS

Home Page:http://www.sabberworm.com/blog/2010/6/10/php-css-parser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Exceptions or return codes

rnakielski opened this issue · comments

Is there any example how I can get exception messages?
How can I detect in my code that the parsing did not work because of wrong css?

I use the first Example - neded exactly this - but with wrong css it gives me an empty result but no error.

any hints?

It’s not currently possible to do what you want. You can:

  • Either use lenient mode (default), in which errors are silently ignored
  • Or use strict parsing, which throws a UnexpectedTokenException on the first error it encounters (but stops parsing afterwards, since that’s how exceptions in PHP work).

I agree, though, that a modus operandi that logs errors but does not choke on them could be useful for certain scenarios.

right - this would be nice
now I help myself counting the Declaration Blocks like this:

        $oParser = new \Sabberworm\CSS\Parser($css);
        $oCss = $oParser->parse();
        $decBlocks = $oCss->getAllDeclarationBlocks();
        if(count($decBlocks) > 0){ 
                         OK - go n
                    }  

should work...

BTW: thanks for sharing this software

Adding to the exception handling, I think it would be nice to provide the selector that the error occurred in for the exception message. For instance, on UnexpectedTokenExceptions you get something like:

Identifier expected. Got “/\00a” 

Which can be difficult to debug to a user.