From 88833bac87e7fb295c0479a8260d1d63051bca8d Mon Sep 17 00:00:00 2001 From: flammy Date: Mon, 24 Jun 2013 15:05:59 +0300 Subject: Fixes validation problems with base64 encoded images in CSS. --- lib/exe/css.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/exe/css.php') diff --git a/lib/exe/css.php b/lib/exe/css.php index 1e662c64a..768c8eda4 100644 --- a/lib/exe/css.php +++ b/lib/exe/css.php @@ -314,7 +314,7 @@ function css_datauri($match){ $data = base64_encode(file_get_contents($local)); } if($data){ - $url = 'data:image/'.$ext.';base64,'.$data; + $url = '\'data:image/'.$ext.';base64,'.$data.'\''; }else{ $url = $base.$url; } -- cgit v1.2.3 From d4a1ece8f011dd69db05b83d2f98e3207f1a7e48 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Mon, 29 Jul 2013 20:06:01 +0200 Subject: add LESS support still needs testing --- lib/exe/css.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'lib/exe/css.php') diff --git a/lib/exe/css.php b/lib/exe/css.php index 1e662c64a..4a9c825c1 100644 --- a/lib/exe/css.php +++ b/lib/exe/css.php @@ -154,7 +154,11 @@ function css_out(){ // apply style replacements $css = css_applystyle($css,$tplinc); - // place all @import statements at the top of the file + // parse LESS + $less = new lessc(); + $css = $less->compile($css); + + // place all remaining @import statements at the top of the file $css = css_moveimports($css); // compress whitespace and comments @@ -175,6 +179,9 @@ function css_out(){ * Does placeholder replacements in the style according to * the ones defined in a templates style.ini file * + * This also adds the ini defined placeholders as less variables + * (sans the surrounding __ and with a ini_ prefix) + * * @author Andreas Gohr */ function css_applystyle($css,$tplinc){ @@ -182,6 +189,13 @@ function css_applystyle($css,$tplinc){ if($styleini){ $css = strtr($css,$styleini['replacements']); + + $less = ''; + foreach($styleini as $key => $value){ + $key = trim($key, '_'); + $key = '@ini_'.$key; + $less .= "$key: $value\n"; + } } return $css; } @@ -333,14 +347,17 @@ function css_pluginstyles($mediatype='screen'){ $plugins = plugin_list(); foreach ($plugins as $p){ $list[DOKU_PLUGIN."$p/$mediatype.css"] = DOKU_BASE."lib/plugins/$p/"; + $list[DOKU_PLUGIN."$p/$mediatype.less"] = DOKU_BASE."lib/plugins/$p/"; // alternative for screen.css if ($mediatype=='screen') { $list[DOKU_PLUGIN."$p/style.css"] = DOKU_BASE."lib/plugins/$p/"; + $list[DOKU_PLUGIN."$p/style.less"] = 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/"; + $list[DOKU_PLUGIN."$p/rtl.less"] = DOKU_BASE."lib/plugins/$p/"; } } return $list; -- cgit v1.2.3 From c51b334ee77310dc0055311092f224be0342cd65 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Tue, 30 Jul 2013 12:07:15 +0200 Subject: fixed ini replacement to less variables stuff --- lib/exe/css.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'lib/exe/css.php') diff --git a/lib/exe/css.php b/lib/exe/css.php index 4a9c825c1..04516e8ba 100644 --- a/lib/exe/css.php +++ b/lib/exe/css.php @@ -191,11 +191,13 @@ function css_applystyle($css,$tplinc){ $css = strtr($css,$styleini['replacements']); $less = ''; - foreach($styleini as $key => $value){ + foreach($styleini['replacements'] as $key => $value){ $key = trim($key, '_'); $key = '@ini_'.$key; - $less .= "$key: $value\n"; + $less .= "$key: $value;\n"; } + + $css = $less.$css; } return $css; } -- cgit v1.2.3 From cbe37079b2519e6a79696ab6525a61498ab3c3a6 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Tue, 30 Jul 2013 15:56:29 +0200 Subject: convert ini replacements to less vars first This makes it possible to safely overwrite ini replacements from within any less file --- lib/exe/css.php | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'lib/exe/css.php') diff --git a/lib/exe/css.php b/lib/exe/css.php index 04516e8ba..0157e6a40 100644 --- a/lib/exe/css.php +++ b/lib/exe/css.php @@ -154,6 +154,9 @@ function css_out(){ // apply style replacements $css = css_applystyle($css,$tplinc); + print $css; + + // parse LESS $less = new lessc(); $css = $less->compile($css); @@ -188,15 +191,21 @@ function css_applystyle($css,$tplinc){ $styleini = css_styleini($tplinc); if($styleini){ - $css = strtr($css,$styleini['replacements']); - + // we convert ini replacements to LESS variable names + // and build a list of variable: value; pairs $less = ''; foreach($styleini['replacements'] as $key => $value){ - $key = trim($key, '_'); - $key = '@ini_'.$key; - $less .= "$key: $value;\n"; + $lkey = trim($key, '_'); + $lkey = '@ini_'.$lkey; + $less .= "$lkey: $value;\n"; + + $styleini['replacements'][$key] = $lkey; } + // we now replace all old ini replacements with LESS variables + $css = strtr($css, $styleini['replacements']); + + // now prepend the list of LESS variables as the very first thing $css = $less.$css; } return $css; -- cgit v1.2.3 From fbaa87779929e23ede8a79fd3890ef493fdb1d81 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Tue, 30 Jul 2013 16:00:28 +0200 Subject: removed debug statement. sorry --- lib/exe/css.php | 3 --- 1 file changed, 3 deletions(-) (limited to 'lib/exe/css.php') diff --git a/lib/exe/css.php b/lib/exe/css.php index 0157e6a40..cd531ef35 100644 --- a/lib/exe/css.php +++ b/lib/exe/css.php @@ -154,9 +154,6 @@ function css_out(){ // apply style replacements $css = css_applystyle($css,$tplinc); - print $css; - - // parse LESS $less = new lessc(); $css = $less->compile($css); -- cgit v1.2.3 From fd975da73e627193ca68a681ea6b2556c8327f63 Mon Sep 17 00:00:00 2001 From: Anika Henke Date: Wed, 31 Jul 2013 11:58:47 +0100 Subject: removed possibility to have rtl.less files in plugins --- lib/exe/css.php | 1 - 1 file changed, 1 deletion(-) (limited to 'lib/exe/css.php') diff --git a/lib/exe/css.php b/lib/exe/css.php index cd531ef35..5c966db82 100644 --- a/lib/exe/css.php +++ b/lib/exe/css.php @@ -365,7 +365,6 @@ function css_pluginstyles($mediatype='screen'){ // 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/"; - $list[DOKU_PLUGIN."$p/rtl.less"] = DOKU_BASE."lib/plugins/$p/"; } } return $list; -- cgit v1.2.3 From 72a66eb76026e69cd4554035a010693d6ae017ad Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Fri, 2 Aug 2013 18:12:46 +0200 Subject: check less compilation for errors This now gives proper files and line numbers for errors --- lib/exe/css.php | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 3 deletions(-) (limited to 'lib/exe/css.php') diff --git a/lib/exe/css.php b/lib/exe/css.php index 5c966db82..91fc11d66 100644 --- a/lib/exe/css.php +++ b/lib/exe/css.php @@ -131,6 +131,8 @@ function css_out(){ // load files $css_content = ''; foreach($files[$mediatype] as $file => $location){ + $display = str_replace(fullpath(DOKU_INC), '', fullpath($file)); + $css_content .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n"; $css_content .= css_loadfile($file, $location); } switch ($mediatype) { @@ -154,9 +156,8 @@ function css_out(){ // apply style replacements $css = css_applystyle($css,$tplinc); - // parse LESS - $less = new lessc(); - $css = $less->compile($css); + // parse less + $css = css_parseless($css); // place all remaining @import statements at the top of the file $css = css_moveimports($css); @@ -175,6 +176,59 @@ function css_out(){ http_cached_finish($cache->cache, $css); } +/** + * Uses phpless to parse LESS in our CSS + * + * most of this function is error handling to show a nice useful error when + * LESS compilation fails + * + * @param $css + * @return string + */ +function css_parseless($css) { + $less = new lessc(); + try { + return $less->compile($css); + } catch(Exception $e) { + // get exception message + $msg = str_replace(array("\n", "\r", "'"), array(), $e->getMessage()); + + // try to use line number to find affected file + if(preg_match('/line: (\d+)$/', $msg, $m)){ + $msg = substr($msg, 0, -1* strlen($m[0])); //remove useless linenumber + $lno = $m[1]; + + // walk upwards to last include + $lines = explode("\n", $css); + $count = count($lines); + for($i=$lno-1; $i>=0; $i--){ + if(preg_match('/\/(\* XXXXXXXXX )(.*?)( XXXXXXXXX \*)\//', $lines[$i], $m)){ + // we found it, add info to message + $msg .= ' in '.$m[2].' at line '.($lno-$i); + break; + } + } + } + + // something went wrong + $error = 'A fatal error occured during compilation of the CSS files. '. + 'If you recently installed a new plugin or template it '. + 'might be broken and you should try disabling it again. ['.$msg.']'; + + echo ".dokuwiki:before { + content: '$error'; + background-color: red; + display: block; + background-color: #fcc; + border-color: #ebb; + color: #000; + padding: 0.5em; + }"; + + exit; + } +} + /** * Does placeholder replacements in the style according to * the ones defined in a templates style.ini file -- cgit v1.2.3 From afb2c08218345dc3604024c829a5c408e3f39277 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sat, 3 Aug 2013 14:42:05 +0200 Subject: allow for a style.ini in conf//style.ini This is another go at what pull request #227 tried to do. This removes support for a style.local.ini in the template file in preference of a style.ini in the conf folder --- lib/exe/css.php | 139 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 79 insertions(+), 60 deletions(-) (limited to 'lib/exe/css.php') diff --git a/lib/exe/css.php b/lib/exe/css.php index 83972d407..ce6a83fea 100644 --- a/lib/exe/css.php +++ b/lib/exe/css.php @@ -40,43 +40,33 @@ function css_out(){ $type = ''; } + // decide from where to get the template $tpl = trim(preg_replace('/[^\w-]+/','',$INPUT->str('t'))); - if($tpl){ - $tplinc = DOKU_INC.'lib/tpl/'.$tpl.'/'; - $tpldir = DOKU_BASE.'lib/tpl/'.$tpl.'/'; - }else{ - $tplinc = tpl_incdir(); - $tpldir = tpl_basedir(); - } - - // used style.ini file - $styleini = css_styleini($tplinc); + if(!$tpl) $tpl = $conf['template']; // The generated script depends on some dynamic options - $cache = new cache('styles'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'].DOKU_BASE.$tplinc.$type,'.css'); + $cache = new cache('styles'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'].DOKU_BASE.$tpl.$type,'.css'); - // load template styles - $tplstyles = array(); - if ($styleini) { - foreach($styleini['stylesheets'] as $file => $mode) { - $tplstyles[$mode][$tplinc.$file] = $tpldir; - } - } + // load styl.ini + $styleini = css_styleini($tpl); + + print_r($styleini); // 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']; } - // Array of needed files and their web locations, the latter ones - // are needed to fix relative paths in the stylesheets - $files = array(); - + // cache influencers + $tplinc = tpl_basedir($tpl); $cache_files = getConfigFiles('main'); $cache_files[] = $tplinc.'style.ini'; - $cache_files[] = $tplinc.'style.local.ini'; + $cache_files[] = DOKU_CONF."tpl/$tpl/style.ini"; $cache_files[] = __FILE__; + // Array of needed files and their web locations, the latter ones + // are needed to fix relative paths in the stylesheets + $files = array(); foreach($mediatypes as $mediatype) { $files[$mediatype] = array(); // load core styles @@ -88,8 +78,8 @@ function css_out(){ // load plugin styles $files[$mediatype] = array_merge($files[$mediatype], css_pluginstyles($mediatype)); // load template styles - if (isset($tplstyles[$mediatype])) { - $files[$mediatype] = array_merge($files[$mediatype], $tplstyles[$mediatype]); + if (isset($styleini['stylesheets'][$mediatype])) { + $files[$mediatype] = array_merge($files[$mediatype], $styleini['stylesheets'][$mediatype]); } // load user styles if(isset($config_cascade['userstyle'][$mediatype])){ @@ -101,7 +91,7 @@ function css_out(){ // 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[$mediatype] = array_merge($files[$mediatype], $tplstyles['rtl']); + if (isset($styleini['stylesheets']['rtl'])) $files[$mediatype] = array_merge($files[$mediatype], $styleini['stylesheets']['rtl']); if (isset($config_cascade['userstyle']['rtl'])) $files[$mediatype][$config_cascade['userstyle']['rtl']] = DOKU_BASE; } } @@ -154,7 +144,7 @@ function css_out(){ ob_end_clean(); // apply style replacements - $css = css_applystyle($css,$tplinc); + $css = css_applystyle($css, $styleini['replacements']); // parse less $css = css_parseless($css); @@ -200,7 +190,6 @@ function css_parseless($css) { // walk upwards to last include $lines = explode("\n", $css); - $count = count($lines); for($i=$lno-1; $i>=0; $i--){ if(preg_match('/\/(\* XXXXXXXXX )(.*?)( XXXXXXXXX \*)\//', $lines[$i], $m)){ // we found it, add info to message @@ -238,50 +227,80 @@ function css_parseless($css) { * * @author Andreas Gohr */ -function css_applystyle($css,$tplinc){ - $styleini = css_styleini($tplinc); - - if($styleini){ - // we convert ini replacements to LESS variable names - // and build a list of variable: value; pairs - $less = ''; - foreach($styleini['replacements'] as $key => $value){ - $lkey = trim($key, '_'); - $lkey = '@ini_'.$lkey; - $less .= "$lkey: $value;\n"; - - $styleini['replacements'][$key] = $lkey; - } +function css_applystyle($css, $replacements) { + // we convert ini replacements to LESS variable names + // and build a list of variable: value; pairs + $less = ''; + foreach((array) $replacements as $key => $value) { + $lkey = trim($key, '_'); + $lkey = '@ini_'.$lkey; + $less .= "$lkey: $value;\n"; + + $replacements[$key] = $lkey; + } - // we now replace all old ini replacements with LESS variables - $css = strtr($css, $styleini['replacements']); + // we now replace all old ini replacements with LESS variables + $css = strtr($css, $replacements); - // now prepend the list of LESS variables as the very first thing - $css = $less.$css; - } + // now prepend the list of LESS variables as the very first thing + $css = $less.$css; return $css; } /** - * Get contents of merged style.ini and style.local.ini as an array. + * Load style ini contents + * + * Loads and merges style.ini files from template and config and prepares + * the stylesheet modes * - * @author Anika Henke + * @author Andreas Gohr + * @param string $tpl the used template + * @return array with keys 'stylesheets' and 'replacements' */ -function css_styleini($tplinc) { - $styleini = array(); +function css_styleini($tpl) { + $stylesheets = array(); // mode, file => base + $replacements = array(); // placeholder => value + + // load template's style.ini + $incbase = tpl_incdir($tpl); + $webbase = tpl_basedir($tpl); + $ini = $incbase.'style.ini'; + if(file_exists($ini)){ + $data = parse_ini_file($ini, true); + + // stylesheets + if(is_array($data['stylesheets'])) foreach($data['stylesheets'] as $file => $mode){ + $stylesheets[$mode][$incbase.$file] = $webbase; + } - foreach (array($tplinc.'style.ini', $tplinc.'style.local.ini') as $ini) { - $tmp = (@file_exists($ini)) ? parse_ini_file($ini, true) : array(); + // replacements + if(is_array($data['replacements'])){ + $replacements = array_merge($replacements, $data['replacements']); + } + } - foreach($tmp as $key => $value) { - if(array_key_exists($key, $styleini) && is_array($value)) { - $styleini[$key] = array_merge($styleini[$key], $tmp[$key]); - } else { - $styleini[$key] = $value; - } + // load configs's style.ini + $incbase = dirname($ini).'/'; + $webbase = DOKU_BASE; + $ini = DOKU_CONF."/tpl/$tpl/style.ini"; + if(file_exists($ini)){ + $data = parse_ini_file($ini, true); + + // stylesheets + if(is_array($data['stylesheets'])) foreach($data['stylesheets'] as $file => $mode){ + $stylesheets[$mode][$incbase.$file] = $webbase; + } + + // replacements + if(is_array($data['replacements'])){ + $replacements = array_merge($replacements, $data['replacements']); } } - return $styleini; + + return array( + 'stylesheets' => $stylesheets, + 'replacements' => $replacements + ); } /** -- cgit v1.2.3 From 568ffd7ef769ecacdf91ac7bdf01dbcedaf8643a Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sat, 3 Aug 2013 16:25:02 +0200 Subject: Readded style.local.ini and deprecated it --- lib/exe/css.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'lib/exe/css.php') diff --git a/lib/exe/css.php b/lib/exe/css.php index ce6a83fea..aa82ba9a2 100644 --- a/lib/exe/css.php +++ b/lib/exe/css.php @@ -279,6 +279,23 @@ function css_styleini($tpl) { } } + // load template's style.local.ini + // @deprecated 2013-08-03 + $ini = $incbase.'style.local.ini'; + if(file_exists($ini)){ + $data = parse_ini_file($ini, true); + + // stylesheets + if(is_array($data['stylesheets'])) foreach($data['stylesheets'] as $file => $mode){ + $stylesheets[$mode][$incbase.$file] = $webbase; + } + + // replacements + if(is_array($data['replacements'])){ + $replacements = array_merge($replacements, $data['replacements']); + } + } + // load configs's style.ini $incbase = dirname($ini).'/'; $webbase = DOKU_BASE; -- cgit v1.2.3 From 90e793b91a348ae2876ed14c8b0d0000192d55e5 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sat, 3 Aug 2013 22:52:10 +0200 Subject: removed debug statement --- lib/exe/css.php | 2 -- 1 file changed, 2 deletions(-) (limited to 'lib/exe/css.php') diff --git a/lib/exe/css.php b/lib/exe/css.php index aa82ba9a2..299e1ebcf 100644 --- a/lib/exe/css.php +++ b/lib/exe/css.php @@ -50,8 +50,6 @@ function css_out(){ // load styl.ini $styleini = css_styleini($tpl); - print_r($styleini); - // 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']; -- cgit v1.2.3 From 8c5aad7bcd74959cceb191697de486a5f6e98fd7 Mon Sep 17 00:00:00 2001 From: Anika Henke Date: Sat, 3 Aug 2013 22:16:08 +0100 Subject: fixed conf style.ini not working due to wrong incbase path --- lib/exe/css.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/exe/css.php') diff --git a/lib/exe/css.php b/lib/exe/css.php index aa82ba9a2..48304b2b6 100644 --- a/lib/exe/css.php +++ b/lib/exe/css.php @@ -297,9 +297,9 @@ function css_styleini($tpl) { } // load configs's style.ini - $incbase = dirname($ini).'/'; $webbase = DOKU_BASE; - $ini = DOKU_CONF."/tpl/$tpl/style.ini"; + $ini = DOKU_CONF."tpl/$tpl/style.ini"; + $incbase = dirname($ini).'/'; if(file_exists($ini)){ $data = parse_ini_file($ini, true); -- cgit v1.2.3 From 724f299965bd5272d45b883477424bd693c9ce95 Mon Sep 17 00:00:00 2001 From: Anika Henke Date: Sat, 3 Aug 2013 22:23:37 +0100 Subject: added style.local.ini to cache files --- lib/exe/css.php | 1 + 1 file changed, 1 insertion(+) (limited to 'lib/exe/css.php') diff --git a/lib/exe/css.php b/lib/exe/css.php index 08dc4d5dd..9e1e22e1a 100644 --- a/lib/exe/css.php +++ b/lib/exe/css.php @@ -59,6 +59,7 @@ function css_out(){ $tplinc = tpl_basedir($tpl); $cache_files = getConfigFiles('main'); $cache_files[] = $tplinc.'style.ini'; + $cache_files[] = $tplinc.'style.local.ini'; // @deprecated $cache_files[] = DOKU_CONF."tpl/$tpl/style.ini"; $cache_files[] = __FILE__; -- cgit v1.2.3 From d91ab76f52f59d264301e18c28a0d3bae996fab8 Mon Sep 17 00:00:00 2001 From: Matt Perry Date: Tue, 27 Aug 2013 01:18:10 -0700 Subject: Fix CodeSniffer violations Fix violations for the following sniff DokuWiki.Functions.OpeningFunctionBrace Also removed an extraneous semicolon. --- lib/exe/css.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'lib/exe/css.php') diff --git a/lib/exe/css.php b/lib/exe/css.php index 9e1e22e1a..60e17ae82 100644 --- a/lib/exe/css.php +++ b/lib/exe/css.php @@ -465,8 +465,7 @@ function css_pluginstyles($mediatype='screen'){ * * @author Gabriel Birke */ -function css_moveimports($css) -{ +function css_moveimports($css) { if(!preg_match_all('/@import\s+(?:url\([^)]+\)|"[^"]+")\s*[^;]*;\s*/', $css, $matches, PREG_OFFSET_CAPTURE)) { return $css; } -- cgit v1.2.3 From 9c7a681b5de7bebb468569a1a416000935b3b3d7 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sat, 12 Oct 2013 18:56:40 +0200 Subject: removed css_moveimports() this is now done by lessphp --- lib/exe/css.php | 25 ------------------------- 1 file changed, 25 deletions(-) (limited to 'lib/exe/css.php') diff --git a/lib/exe/css.php b/lib/exe/css.php index 60e17ae82..afba5fc02 100644 --- a/lib/exe/css.php +++ b/lib/exe/css.php @@ -148,9 +148,6 @@ function css_out(){ // parse less $css = css_parseless($css); - // place all remaining @import statements at the top of the file - $css = css_moveimports($css); - // compress whitespace and comments if($conf['compress']){ $css = css_compress($css); @@ -459,28 +456,6 @@ function css_pluginstyles($mediatype='screen'){ return $list; } -/** - * Move all @import statements in a combined stylesheet to the top so they - * aren't ignored by the browser. - * - * @author Gabriel Birke - */ -function css_moveimports($css) { - if(!preg_match_all('/@import\s+(?:url\([^)]+\)|"[^"]+")\s*[^;]*;\s*/', $css, $matches, PREG_OFFSET_CAPTURE)) { - return $css; - } - $newCss = ""; - $imports = ""; - $offset = 0; - foreach($matches[0] as $match) { - $newCss .= substr($css, $offset, $match[1] - $offset); - $imports .= $match[0]; - $offset = $match[1] + strlen($match[0]); - } - $newCss .= substr($css, $offset); - return $imports.$newCss; -} - /** * Very simple CSS optimizer * -- cgit v1.2.3 From 205907a73eaf15c53de98f153e90890706e79cbe Mon Sep 17 00:00:00 2001 From: furun Date: Tue, 15 Oct 2013 11:34:31 +0200 Subject: compact some CSS styles to their shorthand syntax FS#2509 --- lib/exe/css.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'lib/exe/css.php') diff --git a/lib/exe/css.php b/lib/exe/css.php index afba5fc02..6dfdf06e8 100644 --- a/lib/exe/css.php +++ b/lib/exe/css.php @@ -473,8 +473,19 @@ function css_compress($css){ $css = preg_replace('/ ?([;,{}\/]) ?/','\\1',$css); $css = preg_replace('/ ?: /',':',$css); + // number compression + $css = preg_replace('/([: ])0+(\.\d+?)0*((?:pt|pc|in|mm|cm|em|ex|px)\b|%)(?=[^\{]*[;\}])/', '$1$2$3', $css); // "0.1em" to ".1em", "1.10em" to "1.1em" + $css = preg_replace('/([: ])\.(0)+((?:pt|pc|in|mm|cm|em|ex|px)\b|%)(?=[^\{]*[;\}])/', '$1$2', $css); // ".0em" to "0" + $css = preg_replace('/([: ]0)0*(\.0*)?((?:pt|pc|in|mm|cm|em|ex|px)(?=[^\{]*[;\}])\b|%)/', '$1', $css); // "0.0em" to "0" + $css = preg_replace('/([: ]\d+)(\.0*)((?:pt|pc|in|mm|cm|em|ex|px)(?=[^\{]*[;\}])\b|%)/', '$1$3', $css); // "1.0em" to "1em" + $css = preg_replace('/([: ])0+(\d+|\d*\.\d+)((?:pt|pc|in|mm|cm|em|ex|px)(?=[^\{]*[;\}])\b|%)/', '$1$2$3', $css); // "001em" to "1em" + + // shorten attributes (1em 1em 1em 1em -> 1em) + $css = preg_replace('/(? Date: Sun, 3 Nov 2013 20:49:30 +0000 Subject: Revert "Fixes validation problems with base64 encoded images in CSS." This reverts commit 88833bac87e7fb295c0479a8260d1d63051bca8d. and fixes FS#2874 --- lib/exe/css.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/exe/css.php') diff --git a/lib/exe/css.php b/lib/exe/css.php index 6dfdf06e8..f0bd24b43 100644 --- a/lib/exe/css.php +++ b/lib/exe/css.php @@ -422,7 +422,7 @@ function css_datauri($match){ $data = base64_encode(file_get_contents($local)); } if($data){ - $url = '\'data:image/'.$ext.';base64,'.$data.'\''; + $url = 'data:image/'.$ext.';base64,'.$data; }else{ $url = $base.$url; } -- cgit v1.2.3 From 8c867678811e7f91159175c41ef4722a2fc5308c Mon Sep 17 00:00:00 2001 From: Anika Henke Date: Mon, 4 Nov 2013 01:17:09 +0000 Subject: removed loading of deprecated RTL styles --- lib/exe/css.php | 15 --------------- 1 file changed, 15 deletions(-) (limited to 'lib/exe/css.php') diff --git a/lib/exe/css.php b/lib/exe/css.php index f0bd24b43..87fb779eb 100644 --- a/lib/exe/css.php +++ b/lib/exe/css.php @@ -84,16 +84,6 @@ function css_out(){ if(isset($config_cascade['userstyle'][$mediatype])){ $files[$mediatype][$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($styleini['stylesheets']['rtl'])) $files[$mediatype] = array_merge($files[$mediatype], $styleini['stylesheets']['rtl']); - if (isset($config_cascade['userstyle']['rtl'])) $files[$mediatype][$config_cascade['userstyle']['rtl']] = DOKU_BASE; - } - } $cache_files = array_merge($cache_files, array_keys($files[$mediatype])); } @@ -447,11 +437,6 @@ function css_pluginstyles($mediatype='screen'){ $list[DOKU_PLUGIN."$p/style.css"] = DOKU_BASE."lib/plugins/$p/"; $list[DOKU_PLUGIN."$p/style.less"] = 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/"; - } } return $list; } -- cgit v1.2.3 From 30f686eb67205a1da8765c992e2f9ee1a158c712 Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Tue, 5 Nov 2013 20:15:00 +0000 Subject: add DOKU_INC to less import directories --- lib/exe/css.php | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'lib/exe/css.php') diff --git a/lib/exe/css.php b/lib/exe/css.php index f0bd24b43..bc0645400 100644 --- a/lib/exe/css.php +++ b/lib/exe/css.php @@ -173,6 +173,12 @@ function css_out(){ */ function css_parseless($css) { $less = new lessc(); + $less->importDir[] = DOKU_INC; + + if (defined('DOKU_UNITTEST')){ + $less->importDir[] = TMP_DIR; + } + try { return $less->compile($css); } catch(Exception $e) { -- cgit v1.2.3 From de737055c55e54246de5000d601a089328c1af1c Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Tue, 5 Nov 2013 20:15:39 +0000 Subject: update url/file rewriting in css_loadfile() to support @import of less files --- lib/exe/css.php | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) (limited to 'lib/exe/css.php') diff --git a/lib/exe/css.php b/lib/exe/css.php index bc0645400..3929b2d4b 100644 --- a/lib/exe/css.php +++ b/lib/exe/css.php @@ -403,12 +403,38 @@ function css_loadfile($file,$location=''){ $css = io_readFile($file); if(!$location) return $css; - $css = preg_replace('#(url\([ \'"]*)(?!/|data:|http://|https://| |\'|")#','\\1'.$location,$css); - $css = preg_replace('#(@import\s+[\'"])(?!/|data:|http://|https://)#', '\\1'.$location, $css); + global $css_location; + global $css_current_dir; + $css_current_dir = preg_replace('#^('.DOKU_INC.(defined('DOKU_UNITTEST')?'|'.realpath(TMP_DIR) : '').')#','',dirname($file)).'/'; + $css_location = $location; + + $css = preg_replace_callback('#(url\( *)([\'"]?)(.*?)(\2)( *\))#','css_loadfile_callback',$css); + $css = preg_replace_callback('#(@import\s+)([\'"])(.*?)(\2)#','css_loadfile_callback',$css); +# $css = preg_replace_callback('#(url\([ \'"]*)(?!/|data:|http://|https://| |\'|")#','\\1'.$location,$css); +# $css = preg_replace_callback('#(@import\s+[\'"])(?!/|data:|http://|https://)#', '\\1'.$location, $css); return $css; } +function css_loadfile_callback($match){ + global $css_location; + global $css_current_dir; + + if (preg_match('#^(/|data:|https?://)#',$match[3])) { + return $match[0]; + } + else if (substr($match[3],-5) == '.less') { + if ($match[3]{0} != '/') { + $match[3] = $css_current_dir . $match[3]; + } + } + else { + $match[3] = $css_location . $match[3]; + } + + return join('',array_slice($match,1)); +} + /** * Converte local image URLs to data URLs if the filesize is small * -- cgit v1.2.3 From 4eb5f931edaaabdd436f4c2802d0d293f8ef76cd Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Mon, 11 Nov 2013 22:03:58 +0000 Subject: fix sp. in comment --- lib/exe/css.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/exe/css.php') diff --git a/lib/exe/css.php b/lib/exe/css.php index 3929b2d4b..948251440 100644 --- a/lib/exe/css.php +++ b/lib/exe/css.php @@ -436,7 +436,7 @@ function css_loadfile_callback($match){ } /** - * Converte local image URLs to data URLs if the filesize is small + * Convert local image URLs to data URLs if the filesize is small * * Callback for preg_replace_callback */ -- cgit v1.2.3 From 12ffbbc324be1f06ccfcff580f512990dca929b8 Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Mon, 11 Nov 2013 22:31:28 +0000 Subject: refactor to improve elegance --- lib/exe/css.php | 75 ++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 25 deletions(-) (limited to 'lib/exe/css.php') diff --git a/lib/exe/css.php b/lib/exe/css.php index 948251440..9cde09545 100644 --- a/lib/exe/css.php +++ b/lib/exe/css.php @@ -399,40 +399,65 @@ function css_filetypes(){ * given location prefix */ function css_loadfile($file,$location=''){ - if(!@file_exists($file)) return ''; - $css = io_readFile($file); - if(!$location) return $css; + $css_file = new DokuCssFile($file); + return $css_file->load($location); +} - global $css_location; - global $css_current_dir; - $css_current_dir = preg_replace('#^('.DOKU_INC.(defined('DOKU_UNITTEST')?'|'.realpath(TMP_DIR) : '').')#','',dirname($file)).'/'; - $css_location = $location; +class DokuCssFile { - $css = preg_replace_callback('#(url\( *)([\'"]?)(.*?)(\2)( *\))#','css_loadfile_callback',$css); - $css = preg_replace_callback('#(@import\s+)([\'"])(.*?)(\2)#','css_loadfile_callback',$css); -# $css = preg_replace_callback('#(url\([ \'"]*)(?!/|data:|http://|https://| |\'|")#','\\1'.$location,$css); -# $css = preg_replace_callback('#(@import\s+[\'"])(?!/|data:|http://|https://)#', '\\1'.$location, $css); + protected $filepath; + protected $location; + private $relative_path = null; - return $css; -} + public function __construct($file) { + $this->filepath = $file; + } + + public function load($location='') { + if (!@file_exists($this->filepath)) return ''; -function css_loadfile_callback($match){ - global $css_location; - global $css_current_dir; + $css = io_readFile($this->filepath); + if (!$location) return $css; - if (preg_match('#^(/|data:|https?://)#',$match[3])) { - return $match[0]; + $this->location = $location; + + $css = preg_replace_callback('#(url\( *)([\'"]?)(.*?)(\2)( *\))#',array($this,'replacements'),$css); + $css = preg_replace_callback('#(@import\s+)([\'"])(.*?)(\2)#',array($this,'replacements'),$css); + + return $css; } - else if (substr($match[3],-5) == '.less') { - if ($match[3]{0} != '/') { - $match[3] = $css_current_dir . $match[3]; + + private function getRelativePath(){ + + if (is_null($this->relative_path)) { + $basedir = array(DOKU_INC); + if (defined('DOKU_UNITTEST')) { + $basedir[] = realpath(TMP_DIR); + } + $regex = '#^('.join('|',$basedir).')#'; + + $this->relative_path = preg_replace($regex, '', dirname($this->filepath)); } + + return $this->relative_path; } - else { - $match[3] = $css_location . $match[3]; - } - return join('',array_slice($match,1)); + public function replacements($match) { + + if (preg_match('#^(/|data:|https?://)#',$match[3])) { + return $match[0]; + } + else if (substr($match[3],-5) == '.less') { + if ($match[3]{0} != '/') { + $match[3] = $this->getRelativePath() . '/' . $match[3]; + } + } + else { + $match[3] = $this->location . $match[3]; + } + + return join('',array_slice($match,1)); + } } /** -- cgit v1.2.3 From 47f862d1e038979f4d2dd5bb0c3eaaa9d1ee8fee Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Tue, 12 Nov 2013 23:37:38 +0000 Subject: Fix an issue with style.ini replacements values not having relative locations corrected, when those values are "url(...)". Explanation: In the change to the less css extension, variable replacements now happen after the less/css files are processed for correction of relative locations. --- lib/exe/css.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'lib/exe/css.php') diff --git a/lib/exe/css.php b/lib/exe/css.php index f0bd24b43..af7f9e4f1 100644 --- a/lib/exe/css.php +++ b/lib/exe/css.php @@ -271,7 +271,7 @@ function css_styleini($tpl) { // replacements if(is_array($data['replacements'])){ - $replacements = array_merge($replacements, $data['replacements']); + $replacements = array_merge($replacements, css_fixreplacementurls($data['replacements'],$webbase)); } } @@ -288,7 +288,7 @@ function css_styleini($tpl) { // replacements if(is_array($data['replacements'])){ - $replacements = array_merge($replacements, $data['replacements']); + $replacements = array_merge($replacements, css_fixreplacementurls($data['replacements'],$webbase)); } } @@ -306,7 +306,7 @@ function css_styleini($tpl) { // replacements if(is_array($data['replacements'])){ - $replacements = array_merge($replacements, $data['replacements']); + $replacements = array_merge($replacements, css_fixreplacementurls($data['replacements'],$webbase)); } } @@ -316,6 +316,13 @@ function css_styleini($tpl) { ); } +function css_fixreplacementurls($replacements, $location) { + foreach($replacements as $key => $value) { + $replacements[$key] = preg_replace('#(url\([ \'"]*)(?!/|data:|http://|https://| |\'|")#','\\1'.$location,$value); + } + return $replacements; +} + /** * Prints classes for interwikilinks * -- cgit v1.2.3