tags:

views:

152

answers:

3

My custom WP theme has a text block in the theme options panel that allows the user to create and maintain a custom css block that is applied to the site template at runtime.

I would like to trim or "minify" this content before its stored in the database, but retain all the whitespace when its presented back to the user for editing.

Would this be possible?

For example, if the user has entered the following as their custom css code...

.red {color:red;}

.green {color:green;}

.blue {color:blue;}

Then I would like to store it in the database as:

.red{color:red;}.green{color:green;}.blue{color:blue;}

But still display it as it was input (ie, retain all the white space and line breaks) when the user is editing the content via my theme options panel.

+1  A: 

Google has created a PHP class to help developers minify CSS located on Google Code

MANCHUCK
+2  A: 

If you strip the whitespace from your data before storing it, that whitespace information is lost. You may therefore want to consider the following options:

  • Keep two columns in the database: Original CSS, and Minified CSS. Send the minified version to the browser, but the user would get the original version for editing in the back-end. If the user makes some changes, commit to both the original and minified fields.

  • You can also simply store just the original version in the database without any whitespace stripping. Then simply pre-processes the CSS before sending it to the browser. However, hard disk space being very cheap, you can avoid the pre-processing step by using the technique suggested above.

  • Store the minified version, as you suggested in the question, then apply some formatting logic at the back-end, such that a line break would be added after each semicolon, etc. Obviously the user might have used a different code formatting that the one generated. You can also apply the formatting Logic on the client-side instead, using JavaScript.

Daniel Vassallo
Another solution might be to do everything with minified css on the server, and use JavaScript on the client side to unminify before display...probably pretty straightforward to write as a regex...and the processing is passed to the client so there's no additional server load.
Beska
@Beska: Good point... I'll add that to my answer.
Daniel Vassallo
Daniel, good points. One option that might help would be to store the content in a css file (that's used at runtime) and also store it in the db. Use the db copy (unminified) for editing, but sync to the file (minified) upon save. I like the file approach due to css caching that's not possible with inline styles. Your thoughts?
Scott B
@Scott B: If you are ok with storing the user-generated CSS on the filesystem, that is another valid option. However note that you can still link to an external CSS sheet, by generated one with php at runtime. This script could gather the style data from the database, and then output it using the `text/css` mime-type.
Daniel Vassallo
+1  A: 

What you need is a zipping algorithm, not a replace space. Look for the zipping algos and use them to compress your css when sending over and unzip at the client-side. I think html provides some options. Will check and add comments.

pinaki
look at these - * http://developer.yahoo.com/performance/rules.html * http://stackoverflow.com/questions/48555/best-way-to-compress-html-css-js-with-mod-deflate-and-mod-gzip-disabled
pinaki
also can have a look at http://www.phpbuilder.com/manual/en/function.gzcompress.php
pinaki
Now here's an interesting option. Might get some relatively big results from this, since large css files probably compress quite well indeed.
Beska
Thanks pinaki, I will check the resources.
Scott B