1

Using PHP/Codeigniter/HTMLPurifier/CSStidy like so:

require_once 'extra/htmlpurifier-4_4_0/library/HTMLPurifier.auto.php';
require_once 'extra/csstidy-1_3/class.csstidy.php';

$input_css = $this->input->post('input_css');

$config = HTMLPurifier_Config::createDefault();
$config->set('Filter.ExtractStyleBlocks', TRUE);

// Create a new purifier instance
$purifier = new HTMLPurifier($config);

// Wrap our CSS in style tags and pass to purifier. 
// we're not actually interested in the html response though
$html = $purifier->purify('<style>'.$input_css.'</style>');

// The "style" blocks are stored seperately
$output_css = $purifier->context->get('StyleBlocks');

// Get the first style block
$unClean_css = $input_css;
$clean_css = $output_css[0];

...is there a way to turn OFF whatever it is inside HTMLPurifier or CSStidy that is causing $clean_css to be forced into lowercase? I need the output to be left alone, in terms of case. I would just not use CSStidy but it is working great for security and compression, etc.. just is also hosing some (e.g. background-image) file paths on account of case-sensitivity. I am not able to fix the issue from the ground up (i.e. I cannot just force all lowercase)... so I ask the question the way I did. A config setting perhaps? (I hope) ...I have been looking but do not see one to do the trick yet.

Update: as nickb points out, there is a config option in CSStidy that might control this, so then the question becomes: How to set that option to FALSE in the context of HTMLpurifier?

Or is that lowercase_s CSSTidy configuration option even the culprit? I don't know because I am so far unable to stop the lowercasing one way or another. I want for example that input like so:

.zzzzzz {
    background:transparent url("images/tmpl_Btn1_CSSdemo.jpg") no-repeat right top;
}

...would NOT become (as it is doing now) :

.zzzzzz {
    background:transparent url("images/tmpl_btn1_cssdemo.jpg") no-repeat right top;
}
govinda
  • 1,683
  • 5
  • 20
  • 34

2 Answers2

1

You need to set the lowercase_s CSSTidy configuration option to false. Otherwise, it will lowercase all of your selectors for inclusion in XHTML.

nickb
  • 59,313
  • 13
  • 108
  • 143
  • 1
    Hmm, that's silly of CSS Tidy. Maybe we should change that default. – Edward Z. Yang Jun 01 '12 at 02:31
  • where is that? As I look, I keep getting docs showing how to run CSStidy from the command line... I just need an example for setting a CSSTidy configuration option in PHP runtime. – govinda Jun 01 '12 at 02:53
  • I found this: http://csstidy.sourceforge.net/usage.php , but it does not show how to set the CSStidy config. when *in the context of HTMLpurifier* – govinda Jun 01 '12 at 03:01
  • 1
    I found [this source](http://repo.or.cz/w/htmlpurifier.git/blob/HEAD:/library/HTMLPurifier/Filter/ExtractStyleBlocks.php) of HTMLPurifier where it is invoking CSSTidy, and I cannot determine if, where, or how it passes any configuration values over to CSSTidy. Maybe @EdwardZ.Yang could shed some light into whether or not it is possible. – nickb Jun 01 '12 at 03:07
  • @EdwardZ.Yang please do say if you can - how one can set a CSSTidy config option in the context of code like in my OP above. Nick, should I accept your answer now (as it is undoubtedly the best answer, and in the direction I had hoped), or do I wait and accept when I have actually got a definitive solution? ..and so rid of the problem which I posted? Thanks to you both for your time. – govinda Jun 01 '12 at 03:36
  • 1
    @Govinda It is your choice - I also found [this source](http://csstidy.svn.sourceforge.net/viewvc/csstidy/trunk/class.csstidy.php?revision=134&view=markup) which shows that you may be able to just edit the CSSTidy source code (class.csstidy.php) to achieve what you need. I would try changing the values of `lowercase_s` and `case_properties` inside the `$this->settings` array. Although it's not the flexible solution you're looking for, hopefully it'll work. – nickb Jun 01 '12 at 03:41
  • yes, a runtime config setting would be best, but I'll meanwhile settle for anything. I actually had already found and tried what you just suggested (though I did not think to also play with the `case_properties` setting; thanks ;-) In any case still no luck; the output is still forced to lowercase no matter what I do with those settings in this file: `/path/to/my/doc/root/extra/csstidy-1_3/class.csstidy.php` ... so something is overriding .. but where? – govinda Jun 01 '12 at 03:56
  • (If I alter the setting for `case_properties` in that file, it sticks.. just not so for `lowercase_s`.) – govinda Jun 01 '12 at 04:02
1

Actually, this is an HTML Purifier bug, not a CSS Tidy bug. I'm working on a fix.

Please apply this patch: http://repo.or.cz/w/htmlpurifier.git/commit/f38fca32a9bad0952df221f8664ee2ab13978504 (only the patches to file in library/ are really necessary)

Edward Z. Yang
  • 26,325
  • 16
  • 80
  • 110