Announcement

Collapse
No announcement yet.

CSS Compression/minification

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    CSS Compression/minification

    At the partner day Hiugh showed us this method to compress/minify css: it works in v18 and v16 but NOT in v14 and v12.

    Code:
     
     Parse error: parse error in main on line 3
    Can anyone explain what is different in v14/12 that causes this error?

    Here is the Compression php stuff:


    Code:
    This technique uses PHP and regular expressions.
    
    Put this before the CSS
    
    <actinic:block php="true">
    
    $sCSS = <<<'CSSDefinition'
    
    and this at the end:
    
    CSSDefinition;
    <actinic:block if="%3cactinic%3avariable%20name%3d%22IsCompressionEnabled%22%20%2f%3e">
     // Clean up CSS
     $sCSS = preg_replace('/\\r|\\n/', ' ', $sCSS);  // eliminate newlines
     $sCSS = preg_replace('/\s+/', ' ', $sCSS);  // convert all multiple tabs and spaces to a single space
     $sCSS = preg_replace('/\/\*.*?\*\//', '', $sCSS); // remove all comments
     $sCSS = preg_replace('/\s*(\{|\}|:|;|,|>|\/)\s*/', '${1}', $sCSS); // eliminate spaces either side of { } : ; , > /
     $sCSS = preg_replace('/([^0-9])0px/', '${1}0', $sCSS); // convert 0px to 0
     $sCSS = preg_replace('/^\s*/', '', $sCSS);  // remove spaces remaining at start of CSS
     $sCSS = preg_replace('/([^\'])#([0-9a-fA-F])\2([0-9a-fA-F])\3([0-9a-fA-F])\4/', '${1}#${2}${3}${4}', $sCSS);  // convert colours like #aabbcc to #abc
     $sCSS = preg_replace('/(padding|margin):([0-9]+(?:px|%)+) \2;/', '${1}:${2};', $sCSS); // convert margin:0 0; to margin:0;
     $sCSS = preg_replace('/(padding|margin):([0-9]+(?:px|%)+) \2 \2 \2;/', '${1}:${2};', $sCSS); // convert margin:0 0 0 0; to margin:0;
     $sCSS = str_replace(' !important', '!important', $sCSS); // remove space before !important
     $sCSS = str_replace(';}', '}', $sCSS); // get rid of trailing ; in rules
    </actinic:block>
     echo $sCSS;
    </actinic:block>
    Thank you
    Jonathan Chappell
    Website Designer
    SellerDeck Website Designer
    Actinic to SellerDeck upgrades
    Graphicz Limited - www.graphicz.co.uk

    #2
    Removing the single quotes and removing the compression blockif seems to work:

    Code:
     
     Put this before the CSS  <actinic:block php="true">  $sCSS = <<<CSSDEF  and this at the end:  CSSDEF;  // Clean up CSS  $sCSS = preg_replace('/\\r|\\n/', ' ', $sCSS);  // eliminate newlines  $sCSS = preg_replace('/\s+/', ' ', $sCSS);  // convert all multiple tabs and spaces to a single space  $sCSS = preg_replace('/\/\*.*?\*\//', '', $sCSS); // remove all comments  $sCSS = preg_replace('/\s*(\{|\}|:|;|,|>|\/)\s*/', '${1}', $sCSS); // eliminate spaces either side of { } : ; , > /  $sCSS = preg_replace('/([^0-9])0px/', '${1}0', $sCSS); // convert 0px to 0  $sCSS = preg_replace('/^\s*/', '', $sCSS);  // remove spaces remaining at start of CSS  $sCSS = preg_replace('/([^\'])#([0-9a-fA-F])\2([0-9a-fA-F])\3([0-9a-fA-F])\4/', '${1}#${2}${3}${4}', $sCSS);  // convert colours like #aabbcc to #abc  $sCSS = preg_replace('/(padding|margin):([0-9]+(?:px|%)+) \2;/', '${1}:${2};', $sCSS); // convert margin:0 0; to margin:0;  $sCSS = preg_replace('/(padding|margin):([0-9]+(?:px|%)+) \2 \2 \2;/', '${1}:${2};', $sCSS); // convert margin:0 0 0 0; to margin:0;  $sCSS = str_replace(' !important', '!important', $sCSS); // remove space before !important  $sCSS = str_replace(';}', '}', $sCSS); // get rid of trailing ; in rules  echo $sCSS; </actinic:block>
    NB The second CSSDEF; must be hard up against the left edge.
    Jonathan Chappell
    Website Designer
    SellerDeck Website Designer
    Actinic to SellerDeck upgrades
    Graphicz Limited - www.graphicz.co.uk

    Comment


      #3
      Your line
      Code:
       $sCSS = <<<'CSSDefinition'
      is using PHP's nowdoc syntax that came in with PHP 5.3.0.

      Older SellerDeck versions use an older version of PHP that doesn't support nowdoc.

      The older PHP's supports heredoc which is what you will be using if you remove the single quotes.
      Norman - www.drillpine.biz
      Edinburgh, U K / Bitez, Turkey

      Comment


        #4
        Thank you Norman, all is now clear!
        Jonathan Chappell
        Website Designer
        SellerDeck Website Designer
        Actinic to SellerDeck upgrades
        Graphicz Limited - www.graphicz.co.uk

        Comment

        Working...
        X