From 6c47a78cc56b2c460658675c694bd178c31a1830 Mon Sep 17 00:00:00 2001
From: Anika Henke <anika@selfthinker.org>
Date: Mon, 9 Apr 2012 14:15:29 +0100
Subject: [PATCH] load only one stylesheet for all modes instead of three

Instead of three stylesheets for 'all', 'screen' and 'print' modes, they are all loaded into a single stylesheet by wrapping all screen styles in a "@media screen {}" and all print styles in a "@media print {}". The 'all' mode is not wrapped in anything.
Potential issues with existing CSS: If any of your screen or print CSS files already contain any "@media" syntax, the CSS will probably break. In that case please add any CSS with "@media" in it to the 'all' mode instead!

Also, the 'rtl' mode is hereby deprecated. Please just prefix any RTL styles within your normal CSS files with "[dir=rtl]". This also fixes that RTL styles cannot be added for 'all' or 'print' modes.
---
 inc/template.php           |   6 +-
 lib/exe/css.php            | 125 ++++++++++++++++++++++---------------
 lib/tpl/dokuwiki/style.ini |   4 +-
 3 files changed, 79 insertions(+), 56 deletions(-)

diff --git a/inc/template.php b/inc/template.php
index ab6aa925f..131f34020 100644
--- a/inc/template.php
+++ b/inc/template.php
@@ -354,12 +354,8 @@ function tpl_metaheaders($alt=true){
     }
 
     // load stylesheets
-    $head['link'][] = array('rel'=>'stylesheet', 'media'=>'screen', 'type'=>'text/css',
+    $head['link'][] = array('rel'=>'stylesheet', 'type'=>'text/css',
             'href'=>DOKU_BASE.'lib/exe/css.php?t='.$conf['template'].'&tseed='.$tseed);
-    $head['link'][] = array('rel'=>'stylesheet', 'media'=>'all', 'type'=>'text/css',
-            'href'=>DOKU_BASE.'lib/exe/css.php?s=all&t='.$conf['template'].'&tseed='.$tseed);
-    $head['link'][] = array('rel'=>'stylesheet', 'media'=>'print', 'type'=>'text/css',
-            'href'=>DOKU_BASE.'lib/exe/css.php?s=print&t='.$conf['template'].'&tseed='.$tseed);
 
     // make $INFO and other vars available to JavaScripts
     $json = new JSON();
diff --git a/lib/exe/css.php b/lib/exe/css.php
index 69b512205..5468376c6 100644
--- a/lib/exe/css.php
+++ b/lib/exe/css.php
@@ -9,6 +9,7 @@
 if(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/../../');
 if(!defined('NOSESSION')) define('NOSESSION',true); // we do not use a session or authentication here (better caching)
 if(!defined('DOKU_DISABLE_GZIP_OUTPUT')) define('DOKU_DISABLE_GZIP_OUTPUT',1); // we gzip ourself here
+if(!defined('NL')) define('NL',"\n");
 require_once(DOKU_INC.'inc/init.php');
 
 // Main (don't run when UNIT test)
@@ -30,10 +31,12 @@ function css_out(){
     global $lang;
     global $config_cascade;
 
-    $mediatype = 'screen';
-    if (isset($_REQUEST['s']) &&
-        in_array($_REQUEST['s'], array('all', 'print', 'feed'))) {
-        $mediatype = $_REQUEST['s'];
+    if (isset($_REQUEST['s']) && ($_REQUEST['s'] == 'feed')) {
+        $mediatypes = array('feed');
+        $type = 'feed';
+    } else {
+        $mediatypes = array('screen', 'all', 'print');
+        $type = '';
     }
 
     $tpl = trim(preg_replace('/[^\w-]+/','',$_REQUEST['t']));
@@ -46,7 +49,7 @@ function css_out(){
     }
 
     // The generated script depends on some dynamic options
-    $cache = new cache('styles'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'].DOKU_BASE.$tplinc.$mediatype,'.css');
+    $cache = new cache('styles'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'].DOKU_BASE.$tplinc.$type,'.css');
 
     // load template styles
     $tplstyles = array();
@@ -57,57 +60,79 @@ function css_out(){
         }
     }
 
-    // Array of needed files and their web locations, the latter ones
-    // are needed to fix relative paths in the stylesheets
-    $files   = array();
-    // load core styles
-    $files[DOKU_INC.'lib/styles/'.$mediatype.'.css'] = DOKU_BASE.'lib/styles/';
-    // load jQuery-UI theme
-    $files[DOKU_INC.'lib/scripts/jquery/jquery-ui-theme/smoothness.css'] = DOKU_BASE.'lib/scripts/jquery/jquery-ui-theme/';
-    // load plugin styles
-    $files = array_merge($files, css_pluginstyles($mediatype));
-    // load template styles
-    if (isset($tplstyles[$mediatype])) {
-        $files = array_merge($files, $tplstyles[$mediatype]);
-    }
-    // if old 'default' userstyle setting exists, make it 'screen' userstyle for backwards compatibility
-    if (isset($config_cascade['userstyle']['default'])) {
-        $config_cascade['userstyle']['screen'] = $config_cascade['userstyle']['default'];
-    }
-    // load user styles
-    if(isset($config_cascade['userstyle'][$mediatype])){
-        $files[$config_cascade['userstyle'][$mediatype]] = DOKU_BASE;
-    }
-    // load rtl styles
-    // @todo: this currently adds the rtl styles only to the 'screen' media type
-    //        but 'print' and 'all' should also be supported
-    if ($mediatype=='screen') {
-        if($lang['direction'] == 'rtl'){
-            if (isset($tplstyles['rtl'])) $files = array_merge($files, $tplstyles['rtl']);
+    // start output buffering
+    ob_start();
+
+    foreach($mediatypes as $mediatype) {
+        // Array of needed files and their web locations, the latter ones
+        // are needed to fix relative paths in the stylesheets
+        $files   = array();
+        // load core styles
+        $files[DOKU_INC.'lib/styles/'.$mediatype.'.css'] = DOKU_BASE.'lib/styles/';
+        // load jQuery-UI theme
+        if ($mediatype == 'screen') {
+            //$files[DOKU_INC.'lib/scripts/jquery/jquery-ui-theme/smoothness.css'] = DOKU_BASE.'lib/scripts/jquery/jquery-ui-theme/';
+        }
+        // load plugin styles
+        $files = array_merge($files, css_pluginstyles($mediatype));
+        // load template styles
+        if (isset($tplstyles[$mediatype])) {
+            $files = array_merge($files, $tplstyles[$mediatype]);
+        }
+        // if old 'default' userstyle setting exists, make it 'screen' userstyle for backwards compatibility
+        if (isset($config_cascade['userstyle']['default'])) {
+            $config_cascade['userstyle']['screen'] = $config_cascade['userstyle']['default'];
+        }
+        // load user styles
+        if(isset($config_cascade['userstyle'][$mediatype])){
+            $files[$config_cascade['userstyle'][$mediatype]] = DOKU_BASE;
+        }
+        // load rtl styles
+        // note: this adds the rtl styles only to the 'screen' media type
+        // @deprecated 2012-04-09: rtl will cease to be a mode of its own,
+        //     please use "[dir=rtl]" in any css file in all, screen or print mode instead
+        if ($mediatype=='screen') {
+            if($lang['direction'] == 'rtl'){
+                if (isset($tplstyles['rtl'])) $files = array_merge($files, $tplstyles['rtl']);
+            }
         }
-    }
 
-    $cache_files = array_merge(array_keys($files), getConfigFiles('main'));
-    $cache_files[] = $tplinc.'style.ini';
-    $cache_files[] = __FILE__;
+        $cache_files = array_merge(array_keys($files), getConfigFiles('main'));
+        $cache_files[] = $tplinc.'style.ini';
+        $cache_files[] = __FILE__;
 
-    // check cache age & handle conditional request
-    // This may exit if a cache can be used
-    http_cached($cache->cache,
-                $cache->useCache(array('files' => $cache_files)));
+        // check cache age & handle conditional request
+        // This may exit if a cache can be used
+        http_cached($cache->cache,
+                    $cache->useCache(array('files' => $cache_files)));
 
-    // start output buffering and build the stylesheet
-    ob_start();
+        // build the stylesheet
 
-    // print the default classes for interwiki links and file downloads
-    css_interwiki();
-    css_filetypes();
+        // print the default classes for interwiki links and file downloads
+        if ($mediatype == 'screen') {
+            css_interwiki();
+            css_filetypes();
+        }
 
-    // load files
-    foreach($files as $file => $location){
-        print css_loadfile($file, $location);
+        // load files
+        $css_content = '';
+        foreach($files as $file => $location){
+            $css_content .= css_loadfile($file, $location);
+        }
+        switch ($mediatype) {
+            case 'screen':
+                print NL.'@media screen { /* START screen styles */'.NL.$css_content.NL.'} /* /@media END screen styles */'.NL;
+                break;
+            case 'print':
+                print NL.'@media print { /* START print styles */'.NL.$css_content.NL.'} /* /@media END print styles */'.NL;
+                break;
+            case 'all':
+            case 'feed':
+            default:
+                print NL.'/* START rest styles */ '.NL.$css_content.NL.'/* END rest styles */'.NL;
+                break;
+        }
     }
-
     // end output buffering and get contents
     $css = ob_get_contents();
     ob_end_clean();
@@ -275,6 +300,8 @@ function css_pluginstyles($mediatype='screen'){
         if ($mediatype=='screen') {
             $list[DOKU_PLUGIN."$p/style.css"]  = DOKU_BASE."lib/plugins/$p/";
         }
+        // @deprecated 2012-04-09: rtl will cease to be a mode of its own,
+        //     please use "[dir=rtl]" in any css file in all, screen or print mode instead
         if($lang['direction'] == 'rtl'){
             $list[DOKU_PLUGIN."$p/rtl.css"] = DOKU_BASE."lib/plugins/$p/";
         }
diff --git a/lib/tpl/dokuwiki/style.ini b/lib/tpl/dokuwiki/style.ini
index b8e55bcc8..b52d7e652 100644
--- a/lib/tpl/dokuwiki/style.ini
+++ b/lib/tpl/dokuwiki/style.ini
@@ -29,10 +29,10 @@ css/design.css            = screen
 css/pagetools.css         = screen
 css/content.css           = screen
 css/includes.css          = screen
-css/mobile.css            = screen
 css/rtl.css               = screen
 
-css/print.css         = print
+css/mobile.css            = all
+css/print.css             = print
 
 
 ; This section is used to configure some placeholder values used in
-- 
GitLab