xref: /dokuwiki/lib/exe/css.php (revision 3d2fd76a6a87ddf4a45c05091799c09500265307)
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;
3309edb711SAndreas Gohr
346c47a78cSAnika Henke    if (isset($_REQUEST['s']) && ($_REQUEST['s'] == 'feed')) {
356c47a78cSAnika Henke        $mediatypes = array('feed');
366c47a78cSAnika Henke        $type = 'feed';
376c47a78cSAnika Henke    } else {
386c47a78cSAnika Henke        $mediatypes = array('screen', 'all', 'print');
396c47a78cSAnika Henke        $type = '';
40615960feSTom N Harris    }
4178a6aeb1SAndreas Gohr
42f7589b08SChris Smith    $tpl = trim(preg_replace('/[^\w-]+/','',$_REQUEST['t']));
43124af657SAndreas Gohr    if($tpl){
44124af657SAndreas Gohr        $tplinc = DOKU_INC.'lib/tpl/'.$tpl.'/';
45124af657SAndreas Gohr        $tpldir = DOKU_BASE.'lib/tpl/'.$tpl.'/';
46124af657SAndreas Gohr    }else{
4781aca18eSAndreas Gohr        $tplinc = tpl_incdir();
4881aca18eSAndreas Gohr        $tpldir = tpl_basedir();
49124af657SAndreas Gohr    }
50124af657SAndreas Gohr
5178a6aeb1SAndreas Gohr    // The generated script depends on some dynamic options
526c47a78cSAnika Henke    $cache = new cache('styles'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'].DOKU_BASE.$tplinc.$type,'.css');
5378a6aeb1SAndreas Gohr
54519b3173SAndreas Gohr    // load template styles
55519b3173SAndreas Gohr    $tplstyles = array();
56124af657SAndreas Gohr    if(@file_exists($tplinc.'style.ini')){
57124af657SAndreas Gohr        $ini = parse_ini_file($tplinc.'style.ini',true);
58519b3173SAndreas Gohr        foreach($ini['stylesheets'] as $file => $mode){
59124af657SAndreas Gohr            $tplstyles[$mode][$tplinc.$file] = $tpldir;
60519b3173SAndreas Gohr        }
61519b3173SAndreas Gohr    }
62519b3173SAndreas Gohr
636c47a78cSAnika Henke    // start output buffering
646c47a78cSAnika Henke    ob_start();
656c47a78cSAnika Henke
666c47a78cSAnika Henke    foreach($mediatypes as $mediatype) {
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();
70318cd03eSAnika Henke        // load core styles
71318cd03eSAnika Henke        $files[DOKU_INC.'lib/styles/'.$mediatype.'.css'] = DOKU_BASE.'lib/styles/';
7243576758SAndreas Gohr        // load jQuery-UI theme
736c47a78cSAnika Henke        if ($mediatype == 'screen') {
74*3d2fd76aSAnika Henke            $files[DOKU_INC.'lib/scripts/jquery/jquery-ui-theme/smoothness.css'] = DOKU_BASE.'lib/scripts/jquery/jquery-ui-theme/';
756c47a78cSAnika Henke        }
76318cd03eSAnika Henke        // load plugin styles
77318cd03eSAnika Henke        $files = array_merge($files, css_pluginstyles($mediatype));
78318cd03eSAnika Henke        // load template styles
79318cd03eSAnika Henke        if (isset($tplstyles[$mediatype])) {
80318cd03eSAnika Henke            $files = array_merge($files, $tplstyles[$mediatype]);
8109edb711SAndreas Gohr        }
82318cd03eSAnika Henke        // if old 'default' userstyle setting exists, make it 'screen' userstyle for backwards compatibility
83318cd03eSAnika Henke        if (isset($config_cascade['userstyle']['default'])) {
84318cd03eSAnika Henke            $config_cascade['userstyle']['screen'] = $config_cascade['userstyle']['default'];
85318cd03eSAnika Henke        }
86318cd03eSAnika Henke        // load user styles
87318cd03eSAnika Henke        if(isset($config_cascade['userstyle'][$mediatype])){
88318cd03eSAnika Henke            $files[$config_cascade['userstyle'][$mediatype]] = DOKU_BASE;
89318cd03eSAnika Henke        }
90318cd03eSAnika Henke        // load rtl styles
916c47a78cSAnika Henke        // note: this adds the rtl styles only to the 'screen' media type
926c47a78cSAnika Henke        // @deprecated 2012-04-09: rtl will cease to be a mode of its own,
936c47a78cSAnika Henke        //     please use "[dir=rtl]" in any css file in all, screen or print mode instead
94318cd03eSAnika Henke        if ($mediatype=='screen') {
9578a6aeb1SAndreas Gohr            if($lang['direction'] == 'rtl'){
961f5663fdSchris                if (isset($tplstyles['rtl'])) $files = array_merge($files, $tplstyles['rtl']);
9778a6aeb1SAndreas Gohr            }
9878a6aeb1SAndreas Gohr        }
9978a6aeb1SAndreas Gohr
1006619f42eSAdrian Lang        $cache_files = array_merge(array_keys($files), getConfigFiles('main'));
1016619f42eSAdrian Lang        $cache_files[] = $tplinc.'style.ini';
1026619f42eSAdrian Lang        $cache_files[] = __FILE__;
1036619f42eSAdrian Lang
10438f56bffSBen Coburn        // check cache age & handle conditional request
1056619f42eSAdrian Lang        // This may exit if a cache can be used
1066619f42eSAdrian Lang        http_cached($cache->cache,
1076619f42eSAdrian Lang                    $cache->useCache(array('files' => $cache_files)));
10878a6aeb1SAndreas Gohr
1096c47a78cSAnika Henke        // build the stylesheet
11078a6aeb1SAndreas Gohr
111d15166e5SAndreas Gohr        // print the default classes for interwiki links and file downloads
1126c47a78cSAnika Henke        if ($mediatype == 'screen') {
1131c2d1019SAndreas Gohr            css_interwiki();
114d15166e5SAndreas Gohr            css_filetypes();
11578a6aeb1SAndreas Gohr        }
11678a6aeb1SAndreas Gohr
1176c47a78cSAnika Henke        // load files
1186c47a78cSAnika Henke        $css_content = '';
1196c47a78cSAnika Henke        foreach($files as $file => $location){
1206c47a78cSAnika Henke            $css_content .= css_loadfile($file, $location);
1216c47a78cSAnika Henke        }
1226c47a78cSAnika Henke        switch ($mediatype) {
1236c47a78cSAnika Henke            case 'screen':
1246c47a78cSAnika Henke                print NL.'@media screen { /* START screen styles */'.NL.$css_content.NL.'} /* /@media END screen styles */'.NL;
1256c47a78cSAnika Henke                break;
1266c47a78cSAnika Henke            case 'print':
1276c47a78cSAnika Henke                print NL.'@media print { /* START print styles */'.NL.$css_content.NL.'} /* /@media END print styles */'.NL;
1286c47a78cSAnika Henke                break;
1296c47a78cSAnika Henke            case 'all':
1306c47a78cSAnika Henke            case 'feed':
1316c47a78cSAnika Henke            default:
1326c47a78cSAnika Henke                print NL.'/* START rest styles */ '.NL.$css_content.NL.'/* END rest styles */'.NL;
1336c47a78cSAnika Henke                break;
1346c47a78cSAnika Henke        }
1356c47a78cSAnika Henke    }
13678a6aeb1SAndreas Gohr    // end output buffering and get contents
13778a6aeb1SAndreas Gohr    $css = ob_get_contents();
13878a6aeb1SAndreas Gohr    ob_end_clean();
13978a6aeb1SAndreas Gohr
1406e69c1baSAndreas Gohr    // apply style replacements
141124af657SAndreas Gohr    $css = css_applystyle($css,$tplinc);
1426e69c1baSAndreas Gohr
143f7d780b9SGabriel Birke    // place all @import statements at the top of the file
144f7d780b9SGabriel Birke    $css = css_moveimports($css);
145f7d780b9SGabriel Birke
14678a6aeb1SAndreas Gohr    // compress whitespace and comments
14778a6aeb1SAndreas Gohr    if($conf['compress']){
14878a6aeb1SAndreas Gohr        $css = css_compress($css);
14978a6aeb1SAndreas Gohr    }
15078a6aeb1SAndreas Gohr
151809d3ba5SAndreas Gohr    // embed small images right into the stylesheet
152809d3ba5SAndreas Gohr    if($conf['cssdatauri']){
153809d3ba5SAndreas Gohr        $base = preg_quote(DOKU_BASE,'#');
154809d3ba5SAndreas Gohr        $css = preg_replace_callback('#(url\([ \'"]*)('.$base.')(.*?(?:\.(png|gif)))#i','css_datauri',$css);
155809d3ba5SAndreas Gohr    }
156809d3ba5SAndreas Gohr
1576619f42eSAdrian Lang    http_cached_finish($cache->cache, $css);
15878a6aeb1SAndreas Gohr}
15978a6aeb1SAndreas Gohr
16078a6aeb1SAndreas Gohr/**
1616e69c1baSAndreas Gohr * Does placeholder replacements in the style according to
1626e69c1baSAndreas Gohr * the ones defined in a templates style.ini file
1636e69c1baSAndreas Gohr *
1646e69c1baSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
1656e69c1baSAndreas Gohr */
166124af657SAndreas Gohrfunction css_applystyle($css,$tplinc){
167124af657SAndreas Gohr    if(@file_exists($tplinc.'style.ini')){
168124af657SAndreas Gohr        $ini = parse_ini_file($tplinc.'style.ini',true);
169519b3173SAndreas Gohr        $css = strtr($css,$ini['replacements']);
1706e69c1baSAndreas Gohr    }
1716e69c1baSAndreas Gohr    return $css;
1726e69c1baSAndreas Gohr}
1736e69c1baSAndreas Gohr
1746e69c1baSAndreas Gohr/**
1751c2d1019SAndreas Gohr * Prints classes for interwikilinks
1761c2d1019SAndreas Gohr *
1771c2d1019SAndreas Gohr * Interwiki links have two classes: 'interwiki' and 'iw_$name>' where
1781c2d1019SAndreas Gohr * $name is the identifier given in the config. All Interwiki links get
1791c2d1019SAndreas Gohr * an default style with a default icon. If a special icon is available
1801c2d1019SAndreas Gohr * for an interwiki URL it is set in it's own class. Both classes can be
1811c2d1019SAndreas Gohr * overwritten in the template or userstyles.
1821c2d1019SAndreas Gohr *
1831c2d1019SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
1841c2d1019SAndreas Gohr */
1851c2d1019SAndreas Gohrfunction css_interwiki(){
1861c2d1019SAndreas Gohr
1871c2d1019SAndreas Gohr    // default style
1881c2d1019SAndreas Gohr    echo 'a.interwiki {';
1891c2d1019SAndreas Gohr    echo ' background: transparent url('.DOKU_BASE.'lib/images/interwiki.png) 0px 1px no-repeat;';
1907b4ea081Smarklundeberg    echo ' padding: 1px 0px 1px 16px;';
1911c2d1019SAndreas Gohr    echo '}';
1921c2d1019SAndreas Gohr
1931c2d1019SAndreas Gohr    // additional styles when icon available
1941c2d1019SAndreas Gohr    $iwlinks = getInterwiki();
1951c2d1019SAndreas Gohr    foreach(array_keys($iwlinks) as $iw){
1969d2ddea4SAndreas Gohr        $class = preg_replace('/[^_\-a-z0-9]+/i','_',$iw);
1971c2d1019SAndreas Gohr        if(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.png')){
1989d2ddea4SAndreas Gohr            echo "a.iw_$class {";
1991c2d1019SAndreas Gohr            echo '  background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.png)';
2001c2d1019SAndreas Gohr            echo '}';
2011c2d1019SAndreas Gohr        }elseif(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.gif')){
2029d2ddea4SAndreas Gohr            echo "a.iw_$class {";
2031c2d1019SAndreas Gohr            echo '  background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.gif)';
2041c2d1019SAndreas Gohr            echo '}';
2051c2d1019SAndreas Gohr        }
2061c2d1019SAndreas Gohr    }
207d15166e5SAndreas Gohr}
2081c2d1019SAndreas Gohr
209d15166e5SAndreas Gohr/**
210d15166e5SAndreas Gohr * Prints classes for file download links
211d15166e5SAndreas Gohr *
212d15166e5SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
213d15166e5SAndreas Gohr */
214d15166e5SAndreas Gohrfunction css_filetypes(){
215d15166e5SAndreas Gohr
216d15166e5SAndreas Gohr    // default style
217035e07f1SKate Arzamastseva    echo '.mediafile {';
218d15166e5SAndreas Gohr    echo ' background: transparent url('.DOKU_BASE.'lib/images/fileicons/file.png) 0px 1px no-repeat;';
2195b77caf4SAndreas Gohr    echo ' padding-left: 18px;';
2205b77caf4SAndreas Gohr    echo ' padding-bottom: 1px;';
221d15166e5SAndreas Gohr    echo '}';
222d15166e5SAndreas Gohr
223d15166e5SAndreas Gohr    // additional styles when icon available
22427bf7924STom N Harris    // scan directory for all icons
22527bf7924STom N Harris    $exts = array();
22627bf7924STom N Harris    if($dh = opendir(DOKU_INC.'lib/images/fileicons')){
22727bf7924STom N Harris        while(false !== ($file = readdir($dh))){
22827bf7924STom N Harris            if(preg_match('/([_\-a-z0-9]+(?:\.[_\-a-z0-9]+)*?)\.(png|gif)/i',$file,$match)){
22927bf7924STom N Harris                $ext = strtolower($match[1]);
23027bf7924STom N Harris                $type = '.'.strtolower($match[2]);
23127bf7924STom N Harris                if($ext!='file' && (!isset($exts[$ext]) || $type=='.png')){
23227bf7924STom N Harris                    $exts[$ext] = $type;
233d15166e5SAndreas Gohr                }
234d15166e5SAndreas Gohr            }
2351c2d1019SAndreas Gohr        }
23627bf7924STom N Harris        closedir($dh);
23727bf7924STom N Harris    }
23827bf7924STom N Harris    foreach($exts as $ext=>$type){
23927bf7924STom N Harris        $class = preg_replace('/[^_\-a-z0-9]+/','_',$ext);
240035e07f1SKate Arzamastseva        echo ".mf_$class {";
24127bf7924STom N Harris        echo '  background-image: url('.DOKU_BASE.'lib/images/fileicons/'.$ext.$type.')';
24227bf7924STom N Harris        echo '}';
24327bf7924STom N Harris    }
24427bf7924STom N Harris}
2451c2d1019SAndreas Gohr
2461c2d1019SAndreas Gohr/**
24778a6aeb1SAndreas Gohr * Loads a given file and fixes relative URLs with the
24878a6aeb1SAndreas Gohr * given location prefix
24978a6aeb1SAndreas Gohr */
25078a6aeb1SAndreas Gohrfunction css_loadfile($file,$location=''){
25178a6aeb1SAndreas Gohr    if(!@file_exists($file)) return '';
25278a6aeb1SAndreas Gohr    $css = io_readFile($file);
25378a6aeb1SAndreas Gohr    if(!$location) return $css;
25478a6aeb1SAndreas Gohr
255809d3ba5SAndreas Gohr    $css = preg_replace('#(url\([ \'"]*)(?!/|data:|http://|https://| |\'|")#','\\1'.$location,$css);
256809d3ba5SAndreas Gohr    $css = preg_replace('#(@import\s+[\'"])(?!/|data:|http://|https://)#', '\\1'.$location, $css);
257809d3ba5SAndreas Gohr
25878a6aeb1SAndreas Gohr    return $css;
25978a6aeb1SAndreas Gohr}
26078a6aeb1SAndreas Gohr
261809d3ba5SAndreas Gohr/**
262809d3ba5SAndreas Gohr * Converte local image URLs to data URLs if the filesize is small
263809d3ba5SAndreas Gohr *
264809d3ba5SAndreas Gohr * Callback for preg_replace_callback
265809d3ba5SAndreas Gohr */
266809d3ba5SAndreas Gohrfunction css_datauri($match){
26728f4004cSAndreas Gohr    global $conf;
26828f4004cSAndreas Gohr
269809d3ba5SAndreas Gohr    $pre   = unslash($match[1]);
270809d3ba5SAndreas Gohr    $base  = unslash($match[2]);
271809d3ba5SAndreas Gohr    $url   = unslash($match[3]);
272809d3ba5SAndreas Gohr    $ext   = unslash($match[4]);
273809d3ba5SAndreas Gohr
274809d3ba5SAndreas Gohr    $local = DOKU_INC.$url;
275809d3ba5SAndreas Gohr    $size  = @filesize($local);
27628f4004cSAndreas Gohr    if($size && $size < $conf['cssdatauri']){
277809d3ba5SAndreas Gohr        $data = base64_encode(file_get_contents($local));
278809d3ba5SAndreas Gohr    }
279809d3ba5SAndreas Gohr    if($data){
280809d3ba5SAndreas Gohr        $url = 'data:image/'.$ext.';base64,'.$data;
281809d3ba5SAndreas Gohr    }else{
282809d3ba5SAndreas Gohr        $url = $base.$url;
283809d3ba5SAndreas Gohr    }
284809d3ba5SAndreas Gohr    return $pre.$url;
285809d3ba5SAndreas Gohr}
286809d3ba5SAndreas Gohr
28715c394afSAndreas Gohr
28878a6aeb1SAndreas Gohr/**
28978a6aeb1SAndreas Gohr * Returns a list of possible Plugin Styles (no existance check here)
29078a6aeb1SAndreas Gohr *
29178a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
29278a6aeb1SAndreas Gohr */
293318cd03eSAnika Henkefunction css_pluginstyles($mediatype='screen'){
294208c0215SAndreas Gohr    global $lang;
29578a6aeb1SAndreas Gohr    $list = array();
29678a6aeb1SAndreas Gohr    $plugins = plugin_list();
29778a6aeb1SAndreas Gohr    foreach ($plugins as $p){
298318cd03eSAnika Henke        $list[DOKU_PLUGIN."$p/$mediatype.css"]  = DOKU_BASE."lib/plugins/$p/";
299318cd03eSAnika Henke        // alternative for screen.css
300318cd03eSAnika Henke        if ($mediatype=='screen') {
30178a6aeb1SAndreas Gohr            $list[DOKU_PLUGIN."$p/style.css"]  = DOKU_BASE."lib/plugins/$p/";
30278a6aeb1SAndreas Gohr        }
3036c47a78cSAnika Henke        // @deprecated 2012-04-09: rtl will cease to be a mode of its own,
3046c47a78cSAnika Henke        //     please use "[dir=rtl]" in any css file in all, screen or print mode instead
305208c0215SAndreas Gohr        if($lang['direction'] == 'rtl'){
306208c0215SAndreas Gohr            $list[DOKU_PLUGIN."$p/rtl.css"] = DOKU_BASE."lib/plugins/$p/";
307208c0215SAndreas Gohr        }
30878a6aeb1SAndreas Gohr    }
30978a6aeb1SAndreas Gohr    return $list;
31078a6aeb1SAndreas Gohr}
31178a6aeb1SAndreas Gohr
31278a6aeb1SAndreas Gohr/**
313f7d780b9SGabriel Birke * Move all @import statements in a combined stylesheet to the top so they
314f7d780b9SGabriel Birke * aren't ignored by the browser.
315f7d780b9SGabriel Birke *
316f7d780b9SGabriel Birke * @author Gabriel Birke <birke@d-scribe.de>
317f7d780b9SGabriel Birke */
318f7d780b9SGabriel Birkefunction css_moveimports($css)
319f7d780b9SGabriel Birke{
320f7d780b9SGabriel Birke    if(!preg_match_all('/@import\s+(?:url\([^)]+\)|"[^"]+")\s*[^;]*;\s*/', $css, $matches, PREG_OFFSET_CAPTURE)) {
321f7d780b9SGabriel Birke        return $css;
322f7d780b9SGabriel Birke    }
323f7d780b9SGabriel Birke    $newCss  = "";
324f7d780b9SGabriel Birke    $imports = "";
325f7d780b9SGabriel Birke    $offset  = 0;
326f7d780b9SGabriel Birke    foreach($matches[0] as $match) {
327f7d780b9SGabriel Birke        $newCss  .= substr($css, $offset, $match[1] - $offset);
328f7d780b9SGabriel Birke        $imports .= $match[0];
329f7d780b9SGabriel Birke        $offset   = $match[1] + strlen($match[0]);
330f7d780b9SGabriel Birke    }
331f7d780b9SGabriel Birke    $newCss .= substr($css, $offset);
332f7d780b9SGabriel Birke    return $imports.$newCss;
333f7d780b9SGabriel Birke}
334f7d780b9SGabriel Birke
335f7d780b9SGabriel Birke/**
33678a6aeb1SAndreas Gohr * Very simple CSS optimizer
33778a6aeb1SAndreas Gohr *
33878a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
33978a6aeb1SAndreas Gohr */
34078a6aeb1SAndreas Gohrfunction css_compress($css){
341fd7c2db0SAndreas Gohr    //strip comments through a callback
342fd7c2db0SAndreas Gohr    $css = preg_replace_callback('#(/\*)(.*?)(\*/)#s','css_comment_cb',$css);
343fd7c2db0SAndreas Gohr
344247c1c5dSAndreas Gohr    //strip (incorrect but common) one line comments
345fd7c2db0SAndreas Gohr    $css = preg_replace('/(?<!:)\/\/.*$/m','',$css);
346247c1c5dSAndreas Gohr
34778a6aeb1SAndreas Gohr    // strip whitespaces
34878a6aeb1SAndreas Gohr    $css = preg_replace('![\r\n\t ]+!',' ',$css);
349f5379589SChristopher Smith    $css = preg_replace('/ ?([;,{}\/]) ?/','\\1',$css);
350f5379589SChristopher Smith    $css = preg_replace('/ ?: /',':',$css);
35178a6aeb1SAndreas Gohr
35278a6aeb1SAndreas Gohr    // shorten colors
35378a6aeb1SAndreas 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);
35478a6aeb1SAndreas Gohr
35578a6aeb1SAndreas Gohr    return $css;
35678a6aeb1SAndreas Gohr}
35778a6aeb1SAndreas Gohr
358c00aef76SAndreas Gohr/**
359c00aef76SAndreas Gohr * Callback for css_compress()
360c00aef76SAndreas Gohr *
361c00aef76SAndreas Gohr * Keeps short comments (< 5 chars) to maintain typical browser hacks
362c00aef76SAndreas Gohr *
363c00aef76SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
364c00aef76SAndreas Gohr */
365c00aef76SAndreas Gohrfunction css_comment_cb($matches){
366c00aef76SAndreas Gohr    if(strlen($matches[2]) > 4) return '';
367c00aef76SAndreas Gohr    return $matches[0];
368c00aef76SAndreas Gohr}
36978a6aeb1SAndreas Gohr
370e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
371