xref: /dokuwiki/lib/exe/css.php (revision 14977bd2edded61cecdc10116826eef384777e82)
178a6aeb1SAndreas Gohr<?php
278a6aeb1SAndreas Gohr/**
378a6aeb1SAndreas Gohr * DokuWiki StyleSheet creator
478a6aeb1SAndreas Gohr *
578a6aeb1SAndreas Gohr * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
678a6aeb1SAndreas Gohr * @author     Andreas Gohr <andi@splitbrain.org>
778a6aeb1SAndreas Gohr */
878a6aeb1SAndreas Gohr
9d0a27cb0SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/../../');
101c2d1019SAndreas Gohrif(!defined('NOSESSION')) define('NOSESSION',true); // we do not use a session or authentication here (better caching)
1198bda4fdSAndreas Gohrif(!defined('DOKU_DISABLE_GZIP_OUTPUT')) define('DOKU_DISABLE_GZIP_OUTPUT',1); // we gzip ourself here
126c47a78cSAnika Henkeif(!defined('NL')) define('NL',"\n");
1378a6aeb1SAndreas Gohrrequire_once(DOKU_INC.'inc/init.php');
1478a6aeb1SAndreas Gohr
1578a6aeb1SAndreas Gohr// Main (don't run when UNIT test)
1678a6aeb1SAndreas Gohrif(!defined('SIMPLE_TEST')){
1778a6aeb1SAndreas Gohr    header('Content-Type: text/css; charset=utf-8');
1878a6aeb1SAndreas Gohr    css_out();
1978a6aeb1SAndreas Gohr}
2078a6aeb1SAndreas Gohr
2178a6aeb1SAndreas Gohr
2278a6aeb1SAndreas Gohr// ---------------------- functions ------------------------------
2378a6aeb1SAndreas Gohr
2478a6aeb1SAndreas Gohr/**
2578a6aeb1SAndreas Gohr * Output all needed Styles
2678a6aeb1SAndreas Gohr *
2778a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
2878a6aeb1SAndreas Gohr */
2978a6aeb1SAndreas Gohrfunction css_out(){
3078a6aeb1SAndreas Gohr    global $conf;
3178a6aeb1SAndreas Gohr    global $lang;
3209edb711SAndreas Gohr    global $config_cascade;
33bfd0f597STom N Harris    global $INPUT;
3409edb711SAndreas Gohr
3529f2dfdcSAndreas Gohr    if ($INPUT->str('s') == 'feed') {
366c47a78cSAnika Henke        $mediatypes = array('feed');
376c47a78cSAnika Henke        $type = 'feed';
386c47a78cSAnika Henke    } else {
396c47a78cSAnika Henke        $mediatypes = array('screen', 'all', 'print');
406c47a78cSAnika Henke        $type = '';
41615960feSTom N Harris    }
4278a6aeb1SAndreas Gohr
43bfd0f597STom N Harris    $tpl = trim(preg_replace('/[^\w-]+/','',$INPUT->str('t')));
44124af657SAndreas Gohr    if($tpl){
45124af657SAndreas Gohr        $tplinc = DOKU_INC.'lib/tpl/'.$tpl.'/';
46124af657SAndreas Gohr        $tpldir = DOKU_BASE.'lib/tpl/'.$tpl.'/';
47124af657SAndreas Gohr    }else{
4881aca18eSAndreas Gohr        $tplinc = tpl_incdir();
4981aca18eSAndreas Gohr        $tpldir = tpl_basedir();
50124af657SAndreas Gohr    }
51124af657SAndreas Gohr
5278a6aeb1SAndreas Gohr    // The generated script depends on some dynamic options
536c47a78cSAnika Henke    $cache = new cache('styles'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'].DOKU_BASE.$tplinc.$type,'.css');
5478a6aeb1SAndreas Gohr
55519b3173SAndreas Gohr    // load template styles
56519b3173SAndreas Gohr    $tplstyles = array();
57124af657SAndreas Gohr    if(@file_exists($tplinc.'style.ini')){
58124af657SAndreas Gohr        $ini = parse_ini_file($tplinc.'style.ini',true);
59519b3173SAndreas Gohr        foreach($ini['stylesheets'] as $file => $mode){
60124af657SAndreas Gohr            $tplstyles[$mode][$tplinc.$file] = $tpldir;
61519b3173SAndreas Gohr        }
62519b3173SAndreas Gohr    }
63519b3173SAndreas Gohr
646c47a78cSAnika Henke    // start output buffering
656c47a78cSAnika Henke    ob_start();
666c47a78cSAnika Henke
6778a6aeb1SAndreas Gohr    // Array of needed files and their web locations, the latter ones
6878a6aeb1SAndreas Gohr    // are needed to fix relative paths in the stylesheets
6978a6aeb1SAndreas Gohr    $files = array();
70*14977bd2SMichael Hamann
71*14977bd2SMichael Hamann    $cache_files = getConfigFiles('main');
72*14977bd2SMichael Hamann    $cache_files[] = $tplinc.'style.ini';
73*14977bd2SMichael Hamann    $cache_files[] = __FILE__;
74*14977bd2SMichael Hamann
75*14977bd2SMichael Hamann    foreach($mediatypes as $mediatype) {
76*14977bd2SMichael Hamann        $files[$mediatype] = array();
77318cd03eSAnika Henke        // load core styles
78*14977bd2SMichael Hamann        $files[$mediatype][DOKU_INC.'lib/styles/'.$mediatype.'.css'] = DOKU_BASE.'lib/styles/';
7943576758SAndreas Gohr        // load jQuery-UI theme
806c47a78cSAnika Henke        if ($mediatype == 'screen') {
81*14977bd2SMichael Hamann            $files[$mediatype][DOKU_INC.'lib/scripts/jquery/jquery-ui-theme/smoothness.css'] = DOKU_BASE.'lib/scripts/jquery/jquery-ui-theme/';
826c47a78cSAnika Henke        }
83318cd03eSAnika Henke        // load plugin styles
84*14977bd2SMichael Hamann        $files[$mediatype] = array_merge($files[$mediatype], css_pluginstyles($mediatype));
85318cd03eSAnika Henke        // load template styles
86318cd03eSAnika Henke        if (isset($tplstyles[$mediatype])) {
87*14977bd2SMichael Hamann            $files[$mediatype] = array_merge($files[$mediatype], $tplstyles[$mediatype]);
8809edb711SAndreas Gohr        }
89318cd03eSAnika Henke        // if old 'default' userstyle setting exists, make it 'screen' userstyle for backwards compatibility
90318cd03eSAnika Henke        if (isset($config_cascade['userstyle']['default'])) {
91318cd03eSAnika Henke            $config_cascade['userstyle']['screen'] = $config_cascade['userstyle']['default'];
92318cd03eSAnika Henke        }
93318cd03eSAnika Henke        // load user styles
94318cd03eSAnika Henke        if(isset($config_cascade['userstyle'][$mediatype])){
95*14977bd2SMichael Hamann            $files[$mediatype][$config_cascade['userstyle'][$mediatype]] = DOKU_BASE;
96318cd03eSAnika Henke        }
97318cd03eSAnika Henke        // load rtl styles
986c47a78cSAnika Henke        // note: this adds the rtl styles only to the 'screen' media type
996c47a78cSAnika Henke        // @deprecated 2012-04-09: rtl will cease to be a mode of its own,
1006c47a78cSAnika Henke        //     please use "[dir=rtl]" in any css file in all, screen or print mode instead
101318cd03eSAnika Henke        if ($mediatype=='screen') {
10278a6aeb1SAndreas Gohr            if($lang['direction'] == 'rtl'){
103*14977bd2SMichael Hamann                if (isset($tplstyles['rtl'])) $files[$mediatype] = array_merge($files[$mediatype], $tplstyles['rtl']);
10478a6aeb1SAndreas Gohr            }
10578a6aeb1SAndreas Gohr        }
10678a6aeb1SAndreas Gohr
107*14977bd2SMichael Hamann        $cache_files = array_merge($cache_files, array_keys($files[$mediatype]));
108*14977bd2SMichael Hamann    }
1096619f42eSAdrian Lang
11038f56bffSBen Coburn    // check cache age & handle conditional request
1116619f42eSAdrian Lang    // This may exit if a cache can be used
1126619f42eSAdrian Lang    http_cached($cache->cache,
1136619f42eSAdrian Lang                $cache->useCache(array('files' => $cache_files)));
11478a6aeb1SAndreas Gohr
1156c47a78cSAnika Henke    // build the stylesheet
116*14977bd2SMichael Hamann    foreach ($mediatypes as $mediatype) {
11778a6aeb1SAndreas Gohr
118d15166e5SAndreas Gohr        // print the default classes for interwiki links and file downloads
1196c47a78cSAnika Henke        if ($mediatype == 'screen') {
1201c2d1019SAndreas Gohr            css_interwiki();
121d15166e5SAndreas Gohr            css_filetypes();
12278a6aeb1SAndreas Gohr        }
12378a6aeb1SAndreas Gohr
1246c47a78cSAnika Henke        // load files
1256c47a78cSAnika Henke        $css_content = '';
126*14977bd2SMichael Hamann        foreach($files[$mediatype] as $file => $location){
1276c47a78cSAnika Henke            $css_content .= css_loadfile($file, $location);
1286c47a78cSAnika Henke        }
1296c47a78cSAnika Henke        switch ($mediatype) {
1306c47a78cSAnika Henke            case 'screen':
1316c47a78cSAnika Henke                print NL.'@media screen { /* START screen styles */'.NL.$css_content.NL.'} /* /@media END screen styles */'.NL;
1326c47a78cSAnika Henke                break;
1336c47a78cSAnika Henke            case 'print':
1346c47a78cSAnika Henke                print NL.'@media print { /* START print styles */'.NL.$css_content.NL.'} /* /@media END print styles */'.NL;
1356c47a78cSAnika Henke                break;
1366c47a78cSAnika Henke            case 'all':
1376c47a78cSAnika Henke            case 'feed':
1386c47a78cSAnika Henke            default:
1396c47a78cSAnika Henke                print NL.'/* START rest styles */ '.NL.$css_content.NL.'/* END rest styles */'.NL;
1406c47a78cSAnika Henke                break;
1416c47a78cSAnika Henke        }
1426c47a78cSAnika Henke    }
14378a6aeb1SAndreas Gohr    // end output buffering and get contents
14478a6aeb1SAndreas Gohr    $css = ob_get_contents();
14578a6aeb1SAndreas Gohr    ob_end_clean();
14678a6aeb1SAndreas Gohr
1476e69c1baSAndreas Gohr    // apply style replacements
148124af657SAndreas Gohr    $css = css_applystyle($css,$tplinc);
1496e69c1baSAndreas Gohr
150f7d780b9SGabriel Birke    // place all @import statements at the top of the file
151f7d780b9SGabriel Birke    $css = css_moveimports($css);
152f7d780b9SGabriel Birke
15378a6aeb1SAndreas Gohr    // compress whitespace and comments
15478a6aeb1SAndreas Gohr    if($conf['compress']){
15578a6aeb1SAndreas Gohr        $css = css_compress($css);
15678a6aeb1SAndreas Gohr    }
15778a6aeb1SAndreas Gohr
158809d3ba5SAndreas Gohr    // embed small images right into the stylesheet
159809d3ba5SAndreas Gohr    if($conf['cssdatauri']){
160809d3ba5SAndreas Gohr        $base = preg_quote(DOKU_BASE,'#');
161809d3ba5SAndreas Gohr        $css = preg_replace_callback('#(url\([ \'"]*)('.$base.')(.*?(?:\.(png|gif)))#i','css_datauri',$css);
162809d3ba5SAndreas Gohr    }
163809d3ba5SAndreas Gohr
1646619f42eSAdrian Lang    http_cached_finish($cache->cache, $css);
16578a6aeb1SAndreas Gohr}
16678a6aeb1SAndreas Gohr
16778a6aeb1SAndreas Gohr/**
1686e69c1baSAndreas Gohr * Does placeholder replacements in the style according to
1696e69c1baSAndreas Gohr * the ones defined in a templates style.ini file
1706e69c1baSAndreas Gohr *
1716e69c1baSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
1726e69c1baSAndreas Gohr */
173124af657SAndreas Gohrfunction css_applystyle($css,$tplinc){
174124af657SAndreas Gohr    if(@file_exists($tplinc.'style.ini')){
175124af657SAndreas Gohr        $ini = parse_ini_file($tplinc.'style.ini',true);
176519b3173SAndreas Gohr        $css = strtr($css,$ini['replacements']);
1776e69c1baSAndreas Gohr    }
1786e69c1baSAndreas Gohr    return $css;
1796e69c1baSAndreas Gohr}
1806e69c1baSAndreas Gohr
1816e69c1baSAndreas Gohr/**
1821c2d1019SAndreas Gohr * Prints classes for interwikilinks
1831c2d1019SAndreas Gohr *
1841c2d1019SAndreas Gohr * Interwiki links have two classes: 'interwiki' and 'iw_$name>' where
1851c2d1019SAndreas Gohr * $name is the identifier given in the config. All Interwiki links get
1861c2d1019SAndreas Gohr * an default style with a default icon. If a special icon is available
1871c2d1019SAndreas Gohr * for an interwiki URL it is set in it's own class. Both classes can be
1881c2d1019SAndreas Gohr * overwritten in the template or userstyles.
1891c2d1019SAndreas Gohr *
1901c2d1019SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
1911c2d1019SAndreas Gohr */
1921c2d1019SAndreas Gohrfunction css_interwiki(){
1931c2d1019SAndreas Gohr
1941c2d1019SAndreas Gohr    // default style
1951c2d1019SAndreas Gohr    echo 'a.interwiki {';
1961c2d1019SAndreas Gohr    echo ' background: transparent url('.DOKU_BASE.'lib/images/interwiki.png) 0px 1px no-repeat;';
1977b4ea081Smarklundeberg    echo ' padding: 1px 0px 1px 16px;';
1981c2d1019SAndreas Gohr    echo '}';
1991c2d1019SAndreas Gohr
2001c2d1019SAndreas Gohr    // additional styles when icon available
2011c2d1019SAndreas Gohr    $iwlinks = getInterwiki();
2021c2d1019SAndreas Gohr    foreach(array_keys($iwlinks) as $iw){
2039d2ddea4SAndreas Gohr        $class = preg_replace('/[^_\-a-z0-9]+/i','_',$iw);
2041c2d1019SAndreas Gohr        if(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.png')){
2059d2ddea4SAndreas Gohr            echo "a.iw_$class {";
2061c2d1019SAndreas Gohr            echo '  background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.png)';
2071c2d1019SAndreas Gohr            echo '}';
2081c2d1019SAndreas Gohr        }elseif(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.gif')){
2099d2ddea4SAndreas Gohr            echo "a.iw_$class {";
2101c2d1019SAndreas Gohr            echo '  background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.gif)';
2111c2d1019SAndreas Gohr            echo '}';
2121c2d1019SAndreas Gohr        }
2131c2d1019SAndreas Gohr    }
214d15166e5SAndreas Gohr}
2151c2d1019SAndreas Gohr
216d15166e5SAndreas Gohr/**
217d15166e5SAndreas Gohr * Prints classes for file download links
218d15166e5SAndreas Gohr *
219d15166e5SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
220d15166e5SAndreas Gohr */
221d15166e5SAndreas Gohrfunction css_filetypes(){
222d15166e5SAndreas Gohr
223d15166e5SAndreas Gohr    // default style
224035e07f1SKate Arzamastseva    echo '.mediafile {';
225d15166e5SAndreas Gohr    echo ' background: transparent url('.DOKU_BASE.'lib/images/fileicons/file.png) 0px 1px no-repeat;';
2265b77caf4SAndreas Gohr    echo ' padding-left: 18px;';
2275b77caf4SAndreas Gohr    echo ' padding-bottom: 1px;';
228d15166e5SAndreas Gohr    echo '}';
229d15166e5SAndreas Gohr
230d15166e5SAndreas Gohr    // additional styles when icon available
23127bf7924STom N Harris    // scan directory for all icons
23227bf7924STom N Harris    $exts = array();
23327bf7924STom N Harris    if($dh = opendir(DOKU_INC.'lib/images/fileicons')){
23427bf7924STom N Harris        while(false !== ($file = readdir($dh))){
23527bf7924STom N Harris            if(preg_match('/([_\-a-z0-9]+(?:\.[_\-a-z0-9]+)*?)\.(png|gif)/i',$file,$match)){
23627bf7924STom N Harris                $ext = strtolower($match[1]);
23727bf7924STom N Harris                $type = '.'.strtolower($match[2]);
23827bf7924STom N Harris                if($ext!='file' && (!isset($exts[$ext]) || $type=='.png')){
23927bf7924STom N Harris                    $exts[$ext] = $type;
240d15166e5SAndreas Gohr                }
241d15166e5SAndreas Gohr            }
2421c2d1019SAndreas Gohr        }
24327bf7924STom N Harris        closedir($dh);
24427bf7924STom N Harris    }
24527bf7924STom N Harris    foreach($exts as $ext=>$type){
24627bf7924STom N Harris        $class = preg_replace('/[^_\-a-z0-9]+/','_',$ext);
247035e07f1SKate Arzamastseva        echo ".mf_$class {";
24827bf7924STom N Harris        echo '  background-image: url('.DOKU_BASE.'lib/images/fileicons/'.$ext.$type.')';
24927bf7924STom N Harris        echo '}';
25027bf7924STom N Harris    }
25127bf7924STom N Harris}
2521c2d1019SAndreas Gohr
2531c2d1019SAndreas Gohr/**
25478a6aeb1SAndreas Gohr * Loads a given file and fixes relative URLs with the
25578a6aeb1SAndreas Gohr * given location prefix
25678a6aeb1SAndreas Gohr */
25778a6aeb1SAndreas Gohrfunction css_loadfile($file,$location=''){
25878a6aeb1SAndreas Gohr    if(!@file_exists($file)) return '';
25978a6aeb1SAndreas Gohr    $css = io_readFile($file);
26078a6aeb1SAndreas Gohr    if(!$location) return $css;
26178a6aeb1SAndreas Gohr
262809d3ba5SAndreas Gohr    $css = preg_replace('#(url\([ \'"]*)(?!/|data:|http://|https://| |\'|")#','\\1'.$location,$css);
263809d3ba5SAndreas Gohr    $css = preg_replace('#(@import\s+[\'"])(?!/|data:|http://|https://)#', '\\1'.$location, $css);
264809d3ba5SAndreas Gohr
26578a6aeb1SAndreas Gohr    return $css;
26678a6aeb1SAndreas Gohr}
26778a6aeb1SAndreas Gohr
268809d3ba5SAndreas Gohr/**
269809d3ba5SAndreas Gohr * Converte local image URLs to data URLs if the filesize is small
270809d3ba5SAndreas Gohr *
271809d3ba5SAndreas Gohr * Callback for preg_replace_callback
272809d3ba5SAndreas Gohr */
273809d3ba5SAndreas Gohrfunction css_datauri($match){
27428f4004cSAndreas Gohr    global $conf;
27528f4004cSAndreas Gohr
276809d3ba5SAndreas Gohr    $pre   = unslash($match[1]);
277809d3ba5SAndreas Gohr    $base  = unslash($match[2]);
278809d3ba5SAndreas Gohr    $url   = unslash($match[3]);
279809d3ba5SAndreas Gohr    $ext   = unslash($match[4]);
280809d3ba5SAndreas Gohr
281809d3ba5SAndreas Gohr    $local = DOKU_INC.$url;
282809d3ba5SAndreas Gohr    $size  = @filesize($local);
28328f4004cSAndreas Gohr    if($size && $size < $conf['cssdatauri']){
284809d3ba5SAndreas Gohr        $data = base64_encode(file_get_contents($local));
285809d3ba5SAndreas Gohr    }
286809d3ba5SAndreas Gohr    if($data){
287809d3ba5SAndreas Gohr        $url = 'data:image/'.$ext.';base64,'.$data;
288809d3ba5SAndreas Gohr    }else{
289809d3ba5SAndreas Gohr        $url = $base.$url;
290809d3ba5SAndreas Gohr    }
291809d3ba5SAndreas Gohr    return $pre.$url;
292809d3ba5SAndreas Gohr}
293809d3ba5SAndreas Gohr
29415c394afSAndreas Gohr
29578a6aeb1SAndreas Gohr/**
29678a6aeb1SAndreas Gohr * Returns a list of possible Plugin Styles (no existance check here)
29778a6aeb1SAndreas Gohr *
29878a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
29978a6aeb1SAndreas Gohr */
300318cd03eSAnika Henkefunction css_pluginstyles($mediatype='screen'){
301208c0215SAndreas Gohr    global $lang;
30278a6aeb1SAndreas Gohr    $list = array();
30378a6aeb1SAndreas Gohr    $plugins = plugin_list();
30478a6aeb1SAndreas Gohr    foreach ($plugins as $p){
305318cd03eSAnika Henke        $list[DOKU_PLUGIN."$p/$mediatype.css"]  = DOKU_BASE."lib/plugins/$p/";
306318cd03eSAnika Henke        // alternative for screen.css
307318cd03eSAnika Henke        if ($mediatype=='screen') {
30878a6aeb1SAndreas Gohr            $list[DOKU_PLUGIN."$p/style.css"]  = DOKU_BASE."lib/plugins/$p/";
30978a6aeb1SAndreas Gohr        }
3106c47a78cSAnika Henke        // @deprecated 2012-04-09: rtl will cease to be a mode of its own,
3116c47a78cSAnika Henke        //     please use "[dir=rtl]" in any css file in all, screen or print mode instead
312208c0215SAndreas Gohr        if($lang['direction'] == 'rtl'){
313208c0215SAndreas Gohr            $list[DOKU_PLUGIN."$p/rtl.css"] = DOKU_BASE."lib/plugins/$p/";
314208c0215SAndreas Gohr        }
31578a6aeb1SAndreas Gohr    }
31678a6aeb1SAndreas Gohr    return $list;
31778a6aeb1SAndreas Gohr}
31878a6aeb1SAndreas Gohr
31978a6aeb1SAndreas Gohr/**
320f7d780b9SGabriel Birke * Move all @import statements in a combined stylesheet to the top so they
321f7d780b9SGabriel Birke * aren't ignored by the browser.
322f7d780b9SGabriel Birke *
323f7d780b9SGabriel Birke * @author Gabriel Birke <birke@d-scribe.de>
324f7d780b9SGabriel Birke */
325f7d780b9SGabriel Birkefunction css_moveimports($css)
326f7d780b9SGabriel Birke{
327f7d780b9SGabriel Birke    if(!preg_match_all('/@import\s+(?:url\([^)]+\)|"[^"]+")\s*[^;]*;\s*/', $css, $matches, PREG_OFFSET_CAPTURE)) {
328f7d780b9SGabriel Birke        return $css;
329f7d780b9SGabriel Birke    }
330f7d780b9SGabriel Birke    $newCss  = "";
331f7d780b9SGabriel Birke    $imports = "";
332f7d780b9SGabriel Birke    $offset  = 0;
333f7d780b9SGabriel Birke    foreach($matches[0] as $match) {
334f7d780b9SGabriel Birke        $newCss  .= substr($css, $offset, $match[1] - $offset);
335f7d780b9SGabriel Birke        $imports .= $match[0];
336f7d780b9SGabriel Birke        $offset   = $match[1] + strlen($match[0]);
337f7d780b9SGabriel Birke    }
338f7d780b9SGabriel Birke    $newCss .= substr($css, $offset);
339f7d780b9SGabriel Birke    return $imports.$newCss;
340f7d780b9SGabriel Birke}
341f7d780b9SGabriel Birke
342f7d780b9SGabriel Birke/**
34378a6aeb1SAndreas Gohr * Very simple CSS optimizer
34478a6aeb1SAndreas Gohr *
34578a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
34678a6aeb1SAndreas Gohr */
34778a6aeb1SAndreas Gohrfunction css_compress($css){
348fd7c2db0SAndreas Gohr    //strip comments through a callback
349fd7c2db0SAndreas Gohr    $css = preg_replace_callback('#(/\*)(.*?)(\*/)#s','css_comment_cb',$css);
350fd7c2db0SAndreas Gohr
351247c1c5dSAndreas Gohr    //strip (incorrect but common) one line comments
352fd7c2db0SAndreas Gohr    $css = preg_replace('/(?<!:)\/\/.*$/m','',$css);
353247c1c5dSAndreas Gohr
35478a6aeb1SAndreas Gohr    // strip whitespaces
35578a6aeb1SAndreas Gohr    $css = preg_replace('![\r\n\t ]+!',' ',$css);
356f5379589SChristopher Smith    $css = preg_replace('/ ?([;,{}\/]) ?/','\\1',$css);
357f5379589SChristopher Smith    $css = preg_replace('/ ?: /',':',$css);
35878a6aeb1SAndreas Gohr
35978a6aeb1SAndreas Gohr    // shorten colors
36078a6aeb1SAndreas Gohr    $css = preg_replace("/#([0-9a-fA-F]{1})\\1([0-9a-fA-F]{1})\\2([0-9a-fA-F]{1})\\3/", "#\\1\\2\\3",$css);
36178a6aeb1SAndreas Gohr
36278a6aeb1SAndreas Gohr    return $css;
36378a6aeb1SAndreas Gohr}
36478a6aeb1SAndreas Gohr
365c00aef76SAndreas Gohr/**
366c00aef76SAndreas Gohr * Callback for css_compress()
367c00aef76SAndreas Gohr *
368c00aef76SAndreas Gohr * Keeps short comments (< 5 chars) to maintain typical browser hacks
369c00aef76SAndreas Gohr *
370c00aef76SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
371c00aef76SAndreas Gohr */
372c00aef76SAndreas Gohrfunction css_comment_cb($matches){
373c00aef76SAndreas Gohr    if(strlen($matches[2]) > 4) return '';
374c00aef76SAndreas Gohr    return $matches[0];
375c00aef76SAndreas Gohr}
37678a6aeb1SAndreas Gohr
377e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
378