MyIntervals / 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
http://www.sabberworm.com/blog/2010/6/10/php-css-parser
MIT License
1.73k stars 146 forks source link

Support CSS3 media queries #75

Open Jeedey opened 10 years ago

Jeedey commented 10 years ago

I try to parse a last Boostrap Css but i got an error to Sabberworm/CSS/Parser.php at line 171. Maybe it's can parse this line : @media (min-width: 768px) {.lead {font-size: 21px;}} Any idea?

Thanks

sabberworm commented 10 years ago

You’re right, complex media queries is one of the last missing CSS3 features. I don’t think I’ll use this any time soon so if you really need it, you should code it yourself and send a pull request.

Jeedey commented 10 years ago

Finally I used some regex for parse this css, this css parser works great but it's not really good for website performance... Thank you anyway.

sabberworm commented 10 years ago

I think I’ll keep this issue open in case I do get around to implementing this.

Remo commented 10 years ago

@Jeedey would you mind sharing what you've got so far? Maybe I can help to find a fast solution based on your work.

Mipme commented 9 years ago

Also had problems with media queries, but as they are per sé not important in my case i just clean the file of any media queries. Here are my cleaning approaches – maybe that helps others too:

$cssContent = preg_replace("/@media(\s)*\([^)]+\)(\s)*\{/i", "", $cssContent);
$cssContent = preg_replace("/\}(\s)*\}/i", "", $cssContent);
rafasashi commented 8 years ago

For your information this function parses css media queries:

https://github.com/recuweb-source/php-dom-scraper/blob/master/functions_dom_parser.php#L200

Mipme commented 8 years ago

Since more and more css contains animation keyframes i had to update my media query replacement from above:

$code = preg_replace("/@media\s*\(([^)]+)\)\s*\{([^\{]+\{[^\}]+\})*\}/mi", "/* @media-begin($1) */\n$2\n/* @media-end($1) */\n", $code);

This wraps the media query with comments /* media-begin([condition]) */ and the corresponding end tag.

ghost commented 7 years ago

Any (partial?)progress with this 3 year old issue? I'd like to parse Foundation and "wrap" it under specific selector but it uses media queries a lot.

-

Maybe this could be of some help https://github.com/crossjoin/Css/tree/master/src/Crossjoin/Css/Format/Rule/AtMedia

sabberworm commented 7 years ago

@ivanjaros I’m not currently actively working on new features for the project. But I’m very much maintaining it so if you want to work on this and open a pull-request I’ll do my best to get it merged.