I love the css optimized files but if we are optimizing why not use :

.clear-block {display:block}

instead of :

.clear-block {display:block;}

to fix this I added to my:

includes/common.inc

in function drupal_build_css_cache

i added :
// further optimize CSS file
$data = str_replace(';}','}',$data);

on line 1897 and 1898

How do I get this in to drupal?

Comments

ainigma32’s picture

Version: 6.10 » 7.x-dev

See here http://drupal.org/patch

Basically it's: create a patch, attach it to this issue, set the issue to patch (code needs review) and brace yourself ;-)

As mentioned in one of the pages on patching (I think) all new features are implemented in HEAD first. When applied the patch can be back ported to D6.

- Arie

marcus7777’s picture

Assigned: Unassigned » marcus7777
Status: Active » Fixed

okay thanks I'm setting up the cvs and I'll make a patch thanks again Arie

Marcus

marcus7777’s picture

Status: Fixed » Needs review
StatusFileSize
new724 bytes

patch :

mr.baileys’s picture

Status: Needs review » Needs work
Issue tags: +Performance, +CSS, +CSS aggregation

Thanks for the patch!

Some comments after review:

  1. Make sure to read through the coding standards: Comments should start with a capitalized letter and end with a period, and spaces should be added between commas and each parameter.
  2. I think the comment "further optimize CSS file" doesn't really add any extra information. Maybe something like "Remove semi-colons on the ending declaration for a rule."?
  3. Performance-wise, I'm wondering if it wouldn't make more sense to change the existing regex pattern instead of a new replace instruction. I wonder if we can somehow add your performance improvement to the following code-block:
        if ($_optimize) {
          // Perform some safe CSS optimizations.
          $contents = preg_replace('<
            \s*([@{}:;,]|\)\s|\s\()\s* |  # Remove whitespace around separators, but keep space around parentheses.
            /\*([^*\\\\]|\*(?!/))+\*/ |   # Remove comments that are not CSS hacks.
            [\n\r]                        # Remove line breaks.
            >x', '\1', $contents);
        }
    

Setting back to CNW for #1 and #2.

Out of curiosity, have you compared file size before and after the patch? I'd be interested to know how much the CSS files shrink after this change...

mr.baileys’s picture

one more:

  1. your change should probably go into drupal_load_stylesheet (where other optimization is happening) instead of drupal_build_css_cache.
marcus7777’s picture

Thanks you very much. Mr. baileys

I'll read the coding standards and look in to the preg_replace and see if I can move it to drupal_load_stylesheet

I save about 300 - 500 bites on my sites (a bite per css command), so not a lot, but I like to have the smallest output I can.

Marcus

marcus7777’s picture

I am new it using reg exp what am i doing wrong?

got this working

<?php
print preg_replace('/[;]+(?=})/' , '\1' , 'fieldset {margin-bottom: 1em;  padding: .5em;}form { margin: 0;  padding: 0;}hr {height: 1px;  border: 1px solid gray;}');
?>

but how do I add it to the code in drupal_load_stylesheet?
Here is my attempt below (my line with <<<) got no errors just dus not 'Remove semi-colons on the ending declaration for a rule.'

<?php
print preg_replace( '<
\s*([@{}:;,]|\)\s|\s\()\s* |  # Remove whitespace around separators, but keep space around parentheses.
   /\*([^*\\\\]|\*(?!/))+\*/ |   # Remove comments that are not CSS hacks.
 [\n\r]|                     # Remove line breaks.
 [;]+(?=})                   #<<< Remove semi-colons on the ending declaration for a rule.
>x', '\1', 
/*
**Added defaults.css to test.
*/
'/* $Id: defaults.css,v 1.5 2007/10/02 12:10:40 dries Exp $ */

/*
** HTML elements
*/
fieldset {  margin-bottom: 1em;  padding: .5em;}form {  margin: 0;  padding: 0;}hr {  height: 1px;  border: 1px solid gray;}
img {  border: 0;}
table {  border-collapse: collapse;}
th {text-align: left; /* LTR */ padding-right: 1em; /* LTR */ border-bottom: 3px solid #ccc;}

/*
** Markup free clearing
** Details: http://www.positioniseverything.net/easyclearing.html
*/
.clear-block:after {
  content: ".";
  display: block;
  height: 0;
  clear: both;
  visibility: hidden;
}

.clear-block {
  display: inline-block;
}

/* Hides from IE-mac \*/
* html .clear-block {
  height: 1%;
}
.clear-block {
  display: block;
}
/* End hide from IE-mac */
');
?>
marcus7777’s picture

Title: further optimize CSS file » further optimize CSS file (need help with reg exp)
Category: feature » support

Updating to support Request as I need help. thanks

ainigma32’s picture

Couldn't you just do '/;(\})/' ?

- Arie

marcus7777’s picture

thanks Arie
I can't get that to work as part of preg_replace that's already there,

I could do:

<?php
    if ($_optimize) {
      // Perform some safe CSS optimizations.
      $contents = preg_replace('<
        \s*([@{}:;,]|\)\s|\s\()\s* |  # Remove whitespace around separators, but keep space around parentheses.
        /\*([^*\\\\]|\*(?!/))+\*/ |   # Remove comments that are not CSS hacks.
        [\n\r]                        # Remove line breaks.
        >x', '\1', $contents);
        $contents = preg_replace('/[;]+(?=})/' , '' , $contents); // Remove semi-colons on the ending declaration for a rule.
    }
?>

but if it is speed we need :

<?php
    if ($_optimize) {
      // Perform some safe CSS optimizations.
      $contents = preg_replace('<
        \s*([@{}:;,]|\)\s|\s\()\s* |  # Remove whitespace around separators, but keep space around parentheses.
        /\*([^*\\\\]|\*(?!/))+\*/ |   # Remove comments that are not CSS hacks.
        [\n\r]                        # Remove line breaks.
        >x', '\1', $contents);
        $contents = str_replace(';}' , '}' , $contents); // Remove semi-colons on the ending declaration for a rule.
    }
?>

I think is faster as it is simple. I was hoping to add it to the preg_replace function, but it hard for me not knowing about Regular Expressions, but learning slowly.

marcus7777’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new700 bytes
marcus7777’s picture

hi
Looking in to the bytes save it is 2 bytes per css command so on testing 10 sites I saves about 1000 bytes per site. see how much you can save ;-}

Marcus

mr.baileys’s picture

Category: support » feature
Status: Reviewed & tested by the community » Needs review

Setting back to feature request and CNR. This change will still need 2 positive reviews before it's ready to be committed...

marcus7777’s picture

opps, yes I agree, thanks

Status: Needs review » Needs work

The last submitted patch failed testing.

philbar’s picture

Seems like a lot of work going into relatively small performance gain. Perhaps the CSSTidy module will be a good start for issues like these.

http://drupal.org/project/csstidy

wim leers’s picture

Title: further optimize CSS file (need help with reg exp) » Further optimize CSS files
Priority: Normal » Minor
Issue tags: +Page loading performance

Very simple change that is indeed a tiny performance improvement.

If you can get the tests passing, I'll mark it RTBC.

StevenWill’s picture

StatusFileSize
new558 bytes

Try the attached patch.

StevenWill’s picture

Status: Needs work » Needs review

Updating status.

Status: Needs review » Needs work

The last submitted patch, common-remove-semicolon.patch, failed testing.

philbar’s picture

Instead of spending time work on this, I'd appreciate help getting CSSTidy ready for core as css-optimize.inc file.

With GZIP compression, changes like removing semi-colons aren't significant.

r13ose’s picture

Issue summary: View changes

Since CSSTidy is no longer being maintained, move to Advanced CSS/JS Aggregation.

There should be an update to see what marcus7777 is asking is either in the core or the module above.

Status: Needs work » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.