css.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. <?php
  2. /**
  3. * DokuWiki StyleSheet creator
  4. *
  5. * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
  6. * @author Andreas Gohr <andi@splitbrain.org>
  7. */
  8. use dokuwiki\Cache\Cache;
  9. use dokuwiki\Extension\Event;
  10. if(!defined('DOKU_INC')) define('DOKU_INC', __DIR__ .'/../../');
  11. if(!defined('NOSESSION')) define('NOSESSION',true); // we do not use a session or authentication here (better caching)
  12. if(!defined('DOKU_DISABLE_GZIP_OUTPUT')) define('DOKU_DISABLE_GZIP_OUTPUT',1); // we gzip ourself here
  13. if(!defined('NL')) define('NL',"\n");
  14. require_once(DOKU_INC.'inc/init.php');
  15. // Main (don't run when UNIT test)
  16. if(!defined('SIMPLE_TEST')){
  17. header('Content-Type: text/css; charset=utf-8');
  18. css_out();
  19. }
  20. // ---------------------- functions ------------------------------
  21. /**
  22. * Output all needed Styles
  23. *
  24. * @author Andreas Gohr <andi@splitbrain.org>
  25. */
  26. function css_out(){
  27. global $conf;
  28. global $lang;
  29. global $config_cascade;
  30. global $INPUT;
  31. if ($INPUT->str('s') == 'feed') {
  32. $mediatypes = array('feed');
  33. $type = 'feed';
  34. } else {
  35. $mediatypes = array('screen', 'all', 'print', 'speech');
  36. $type = '';
  37. }
  38. // decide from where to get the template
  39. $tpl = trim(preg_replace('/[^\w-]+/','',$INPUT->str('t')));
  40. if(!$tpl) $tpl = $conf['template'];
  41. // load style.ini
  42. $styleUtil = new \dokuwiki\StyleUtils($tpl, $INPUT->bool('preview'));
  43. $styleini = $styleUtil->cssStyleini();
  44. // cache influencers
  45. $tplinc = tpl_incdir($tpl);
  46. $cache_files = getConfigFiles('main');
  47. $cache_files[] = $tplinc.'style.ini';
  48. $cache_files[] = DOKU_CONF."tpl/$tpl/style.ini";
  49. $cache_files[] = __FILE__;
  50. if($INPUT->bool('preview')) $cache_files[] = $conf['cachedir'].'/preview.ini';
  51. // Array of needed files and their web locations, the latter ones
  52. // are needed to fix relative paths in the stylesheets
  53. $media_files = array();
  54. foreach($mediatypes as $mediatype) {
  55. $files = array();
  56. // load core styles
  57. $files[DOKU_INC.'lib/styles/'.$mediatype.'.css'] = DOKU_BASE.'lib/styles/';
  58. // load jQuery-UI theme
  59. if ($mediatype == 'screen') {
  60. $files[DOKU_INC.'lib/scripts/jquery/jquery-ui-theme/smoothness.css'] =
  61. DOKU_BASE.'lib/scripts/jquery/jquery-ui-theme/';
  62. }
  63. // load plugin styles
  64. $files = array_merge($files, css_pluginstyles($mediatype));
  65. // load template styles
  66. if (isset($styleini['stylesheets'][$mediatype])) {
  67. $files = array_merge($files, $styleini['stylesheets'][$mediatype]);
  68. }
  69. // load user styles
  70. if(isset($config_cascade['userstyle'][$mediatype]) and is_array($config_cascade['userstyle'][$mediatype])) {
  71. foreach($config_cascade['userstyle'][$mediatype] as $userstyle) {
  72. $files[$userstyle] = DOKU_BASE;
  73. }
  74. }
  75. // Let plugins decide to either put more styles here or to remove some
  76. $media_files[$mediatype] = css_filewrapper($mediatype, $files);
  77. $CSSEvt = new Event('CSS_STYLES_INCLUDED', $media_files[$mediatype]);
  78. // Make it preventable.
  79. if ( $CSSEvt->advise_before() ) {
  80. $cache_files = array_merge($cache_files, array_keys($media_files[$mediatype]['files']));
  81. } else {
  82. // unset if prevented. Nothing will be printed for this mediatype.
  83. unset($media_files[$mediatype]);
  84. }
  85. // finish event.
  86. $CSSEvt->advise_after();
  87. }
  88. // The generated script depends on some dynamic options
  89. $cache = new Cache(
  90. 'styles' .
  91. $_SERVER['HTTP_HOST'] .
  92. $_SERVER['SERVER_PORT'] .
  93. $INPUT->bool('preview') .
  94. DOKU_BASE .
  95. $tpl .
  96. $type,
  97. '.css'
  98. );
  99. $cache->setEvent('CSS_CACHE_USE');
  100. // check cache age & handle conditional request
  101. // This may exit if a cache can be used
  102. $cache_ok = $cache->useCache(array('files' => $cache_files));
  103. http_cached($cache->cache, $cache_ok);
  104. // start output buffering
  105. ob_start();
  106. // Fire CSS_STYLES_INCLUDED for one last time to let the
  107. // plugins decide whether to include the DW default styles.
  108. // This can be done by preventing the Default.
  109. $media_files['DW_DEFAULT'] = css_filewrapper('DW_DEFAULT');
  110. Event::createAndTrigger('CSS_STYLES_INCLUDED', $media_files['DW_DEFAULT'], 'css_defaultstyles');
  111. // build the stylesheet
  112. foreach ($mediatypes as $mediatype) {
  113. // Check if there is a wrapper set for this type.
  114. if ( !isset($media_files[$mediatype]) ) {
  115. continue;
  116. }
  117. $cssData = $media_files[$mediatype];
  118. // Print the styles.
  119. print NL;
  120. if ( $cssData['encapsulate'] === true ) print $cssData['encapsulationPrefix'] . ' {';
  121. print '/* START '.$cssData['mediatype'].' styles */'.NL;
  122. // load files
  123. foreach($cssData['files'] as $file => $location){
  124. $display = str_replace(fullpath(DOKU_INC), '', fullpath($file));
  125. print "\n/* XXXXXXXXX $display XXXXXXXXX */\n";
  126. print css_loadfile($file, $location);
  127. }
  128. print NL;
  129. if ( $cssData['encapsulate'] === true ) print '} /* /@media ';
  130. else print '/*';
  131. print ' END '.$cssData['mediatype'].' styles */'.NL;
  132. }
  133. // end output buffering and get contents
  134. $css = ob_get_contents();
  135. ob_end_clean();
  136. // strip any source maps
  137. stripsourcemaps($css);
  138. // apply style replacements
  139. $css = css_applystyle($css, $styleini['replacements']);
  140. // parse less
  141. $css = css_parseless($css);
  142. // compress whitespace and comments
  143. if($conf['compress']){
  144. $css = css_compress($css);
  145. }
  146. // embed small images right into the stylesheet
  147. if($conf['cssdatauri']){
  148. $base = preg_quote(DOKU_BASE,'#');
  149. $css = preg_replace_callback('#(url\([ \'"]*)('.$base.')(.*?(?:\.(png|gif)))#i','css_datauri',$css);
  150. }
  151. http_cached_finish($cache->cache, $css);
  152. }
  153. /**
  154. * Uses phpless to parse LESS in our CSS
  155. *
  156. * most of this function is error handling to show a nice useful error when
  157. * LESS compilation fails
  158. *
  159. * @param string $css
  160. * @return string
  161. */
  162. function css_parseless($css) {
  163. global $conf;
  164. $less = new lessc();
  165. $less->importDir = array(DOKU_INC);
  166. $less->setPreserveComments(!$conf['compress']);
  167. if (defined('DOKU_UNITTEST')){
  168. $less->importDir[] = TMP_DIR;
  169. }
  170. try {
  171. return $less->compile($css);
  172. } catch(Exception $e) {
  173. // get exception message
  174. $msg = str_replace(array("\n", "\r", "'"), array(), $e->getMessage());
  175. // try to use line number to find affected file
  176. if(preg_match('/line: (\d+)$/', $msg, $m)){
  177. $msg = substr($msg, 0, -1* strlen($m[0])); //remove useless linenumber
  178. $lno = $m[1];
  179. // walk upwards to last include
  180. $lines = explode("\n", $css);
  181. for($i=$lno-1; $i>=0; $i--){
  182. if(preg_match('/\/(\* XXXXXXXXX )(.*?)( XXXXXXXXX \*)\//', $lines[$i], $m)){
  183. // we found it, add info to message
  184. $msg .= ' in '.$m[2].' at line '.($lno-$i);
  185. break;
  186. }
  187. }
  188. }
  189. // something went wrong
  190. $error = 'A fatal error occured during compilation of the CSS files. '.
  191. 'If you recently installed a new plugin or template it '.
  192. 'might be broken and you should try disabling it again. ['.$msg.']';
  193. echo ".dokuwiki:before {
  194. content: '$error';
  195. background-color: red;
  196. display: block;
  197. background-color: #fcc;
  198. border-color: #ebb;
  199. color: #000;
  200. padding: 0.5em;
  201. }";
  202. exit;
  203. }
  204. }
  205. /**
  206. * Does placeholder replacements in the style according to
  207. * the ones defined in a templates style.ini file
  208. *
  209. * This also adds the ini defined placeholders as less variables
  210. * (sans the surrounding __ and with a ini_ prefix)
  211. *
  212. * @author Andreas Gohr <andi@splitbrain.org>
  213. *
  214. * @param string $css
  215. * @param array $replacements array(placeholder => value)
  216. * @return string
  217. */
  218. function css_applystyle($css, $replacements) {
  219. // we convert ini replacements to LESS variable names
  220. // and build a list of variable: value; pairs
  221. $less = '';
  222. foreach((array) $replacements as $key => $value) {
  223. $lkey = trim($key, '_');
  224. $lkey = '@ini_'.$lkey;
  225. $less .= "$lkey: $value;\n";
  226. $replacements[$key] = $lkey;
  227. }
  228. // we now replace all old ini replacements with LESS variables
  229. $css = strtr($css, $replacements);
  230. // now prepend the list of LESS variables as the very first thing
  231. $css = $less.$css;
  232. return $css;
  233. }
  234. /**
  235. * Wrapper for the files, content and mediatype for the event CSS_STYLES_INCLUDED
  236. *
  237. * @author Gerry Weißbach <gerry.w@gammaproduction.de>
  238. *
  239. * @param string $mediatype type ofthe current media files/content set
  240. * @param array $files set of files that define the current mediatype
  241. * @return array
  242. */
  243. function css_filewrapper($mediatype, $files=array()){
  244. return array(
  245. 'files' => $files,
  246. 'mediatype' => $mediatype,
  247. 'encapsulate' => $mediatype != 'all',
  248. 'encapsulationPrefix' => '@media '.$mediatype
  249. );
  250. }
  251. /**
  252. * Prints the @media encapsulated default styles of DokuWiki
  253. *
  254. * @author Gerry Weißbach <gerry.w@gammaproduction.de>
  255. *
  256. * This function is being called by a CSS_STYLES_INCLUDED event
  257. * The event can be distinguished by the mediatype which is:
  258. * DW_DEFAULT
  259. */
  260. function css_defaultstyles(){
  261. // print the default classes for interwiki links and file downloads
  262. print '@media screen {';
  263. css_interwiki();
  264. css_filetypes();
  265. print '}';
  266. }
  267. /**
  268. * Prints classes for interwikilinks
  269. *
  270. * Interwiki links have two classes: 'interwiki' and 'iw_$name>' where
  271. * $name is the identifier given in the config. All Interwiki links get
  272. * an default style with a default icon. If a special icon is available
  273. * for an interwiki URL it is set in it's own class. Both classes can be
  274. * overwritten in the template or userstyles.
  275. *
  276. * @author Andreas Gohr <andi@splitbrain.org>
  277. */
  278. function css_interwiki(){
  279. // default style
  280. echo 'a.interwiki {';
  281. echo ' background: transparent url('.DOKU_BASE.'lib/images/interwiki.svg) 0 0 no-repeat;';
  282. echo ' background-size: 1.2em;';
  283. echo ' padding: 0 0 0 1.4em;';
  284. echo '}';
  285. // additional styles when icon available
  286. $iwlinks = getInterwiki();
  287. foreach (array_keys($iwlinks) as $iw) {
  288. $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $iw);
  289. foreach (['svg', 'png', 'gif'] as $ext) {
  290. $file = 'lib/images/interwiki/' . $iw . '.' . $ext;
  291. if (file_exists(DOKU_INC . $file)) {
  292. echo "a.iw_$class {";
  293. echo ' background-image: url(' . DOKU_BASE . $file . ')';
  294. echo '}';
  295. break;
  296. }
  297. }
  298. }
  299. }
  300. /**
  301. * Prints classes for file download links
  302. *
  303. * @author Andreas Gohr <andi@splitbrain.org>
  304. */
  305. function css_filetypes(){
  306. // default style
  307. echo '.mediafile {';
  308. echo ' background: transparent url('.DOKU_BASE.'lib/images/fileicons/svg/file.svg) 0px 1px no-repeat;';
  309. echo ' background-size: 1.2em;';
  310. echo ' padding-left: 1.5em;';
  311. echo '}';
  312. // additional styles when icon available
  313. // scan directory for all icons
  314. $exts = array();
  315. if($dh = opendir(DOKU_INC.'lib/images/fileicons/svg')){
  316. while(false !== ($file = readdir($dh))){
  317. if(preg_match('/(.*?)\.svg$/i',$file, $match)){
  318. $exts[] = strtolower($match[1]);
  319. }
  320. }
  321. closedir($dh);
  322. }
  323. foreach($exts as $ext){
  324. $class = preg_replace('/[^_\-a-z0-9]+/','_',$ext);
  325. echo ".mf_$class {";
  326. echo ' background-image: url('.DOKU_BASE.'lib/images/fileicons/svg/'.$ext.'.svg)';
  327. echo '}';
  328. }
  329. }
  330. /**
  331. * Loads a given file and fixes relative URLs with the
  332. * given location prefix
  333. *
  334. * @param string $file file system path
  335. * @param string $location
  336. * @return string
  337. */
  338. function css_loadfile($file,$location=''){
  339. $css_file = new DokuCssFile($file);
  340. return $css_file->load($location);
  341. }
  342. /**
  343. * Helper class to abstract loading of css/less files
  344. *
  345. * @author Chris Smith <chris@jalakai.co.uk>
  346. */
  347. class DokuCssFile {
  348. protected $filepath; // file system path to the CSS/Less file
  349. protected $location; // base url location of the CSS/Less file
  350. protected $relative_path = null;
  351. public function __construct($file) {
  352. $this->filepath = $file;
  353. }
  354. /**
  355. * Load the contents of the css/less file and adjust any relative paths/urls (relative to this file) to be
  356. * relative to the dokuwiki root: the web root (DOKU_BASE) for most files; the file system root (DOKU_INC)
  357. * for less files.
  358. *
  359. * @param string $location base url for this file
  360. * @return string the CSS/Less contents of the file
  361. */
  362. public function load($location='') {
  363. if (!file_exists($this->filepath)) return '';
  364. $css = io_readFile($this->filepath);
  365. if (!$location) return $css;
  366. $this->location = $location;
  367. $css = preg_replace_callback('#(url\( *)([\'"]?)(.*?)(\2)( *\))#',array($this,'replacements'),$css);
  368. $css = preg_replace_callback('#(@import\s+)([\'"])(.*?)(\2)#',array($this,'replacements'),$css);
  369. return $css;
  370. }
  371. /**
  372. * Get the relative file system path of this file, relative to dokuwiki's root folder, DOKU_INC
  373. *
  374. * @return string relative file system path
  375. */
  376. protected function getRelativePath(){
  377. if (is_null($this->relative_path)) {
  378. $basedir = array(DOKU_INC);
  379. // during testing, files may be found relative to a second base dir, TMP_DIR
  380. if (defined('DOKU_UNITTEST')) {
  381. $basedir[] = realpath(TMP_DIR);
  382. }
  383. $basedir = array_map('preg_quote_cb', $basedir);
  384. $regex = '/^('.join('|',$basedir).')/';
  385. $this->relative_path = preg_replace($regex, '', dirname($this->filepath));
  386. }
  387. return $this->relative_path;
  388. }
  389. /**
  390. * preg_replace callback to adjust relative urls from relative to this file to relative
  391. * to the appropriate dokuwiki root location as described in the code
  392. *
  393. * @param array see http://php.net/preg_replace_callback
  394. * @return string see http://php.net/preg_replace_callback
  395. */
  396. public function replacements($match) {
  397. if (preg_match('#^(/|data:|https?://)#', $match[3])) { // not a relative url? - no adjustment required
  398. return $match[0];
  399. } elseif (substr($match[3], -5) == '.less') { // a less file import? - requires a file system location
  400. if ($match[3][0] != '/') {
  401. $match[3] = $this->getRelativePath() . '/' . $match[3];
  402. }
  403. } else { // everything else requires a url adjustment
  404. $match[3] = $this->location . $match[3];
  405. }
  406. return join('',array_slice($match,1));
  407. }
  408. }
  409. /**
  410. * Convert local image URLs to data URLs if the filesize is small
  411. *
  412. * Callback for preg_replace_callback
  413. *
  414. * @param array $match
  415. * @return string
  416. */
  417. function css_datauri($match){
  418. global $conf;
  419. $pre = unslash($match[1]);
  420. $base = unslash($match[2]);
  421. $url = unslash($match[3]);
  422. $ext = unslash($match[4]);
  423. $local = DOKU_INC.$url;
  424. $size = @filesize($local);
  425. if($size && $size < $conf['cssdatauri']){
  426. $data = base64_encode(file_get_contents($local));
  427. }
  428. if (!empty($data)){
  429. $url = 'data:image/'.$ext.';base64,'.$data;
  430. }else{
  431. $url = $base.$url;
  432. }
  433. return $pre.$url;
  434. }
  435. /**
  436. * Returns a list of possible Plugin Styles (no existance check here)
  437. *
  438. * @author Andreas Gohr <andi@splitbrain.org>
  439. *
  440. * @param string $mediatype
  441. * @return array
  442. */
  443. function css_pluginstyles($mediatype='screen'){
  444. $list = array();
  445. $plugins = plugin_list();
  446. foreach ($plugins as $p){
  447. $list[DOKU_PLUGIN."$p/$mediatype.css"] = DOKU_BASE."lib/plugins/$p/";
  448. $list[DOKU_PLUGIN."$p/$mediatype.less"] = DOKU_BASE."lib/plugins/$p/";
  449. // alternative for screen.css
  450. if ($mediatype=='screen') {
  451. $list[DOKU_PLUGIN."$p/style.css"] = DOKU_BASE."lib/plugins/$p/";
  452. $list[DOKU_PLUGIN."$p/style.less"] = DOKU_BASE."lib/plugins/$p/";
  453. }
  454. }
  455. return $list;
  456. }
  457. /**
  458. * Very simple CSS optimizer
  459. *
  460. * @author Andreas Gohr <andi@splitbrain.org>
  461. *
  462. * @param string $css
  463. * @return string
  464. */
  465. function css_compress($css){
  466. // replace quoted strings with placeholder
  467. $quote_storage = [];
  468. $quote_cb = function ($match) use (&$quote_storage) {
  469. $quote_storage[] = $match[0];
  470. return '"STR'.(count($quote_storage)-1).'"';
  471. };
  472. $css = preg_replace_callback('/(([\'"]).*?(?<!\\\\)\2)/', $quote_cb, $css);
  473. // strip comments through a callback
  474. $css = preg_replace_callback('#(/\*)(.*?)(\*/)#s','css_comment_cb',$css);
  475. // strip (incorrect but common) one line comments
  476. $css = preg_replace_callback('/^.*\/\/.*$/m','css_onelinecomment_cb',$css);
  477. // strip whitespaces
  478. $css = preg_replace('![\r\n\t ]+!',' ',$css);
  479. $css = preg_replace('/ ?([;,{}\/]) ?/','\\1',$css);
  480. $css = preg_replace('/ ?: /',':',$css);
  481. // number compression
  482. $css = preg_replace(
  483. '/([: ])0+(\.\d+?)0*((?:pt|pc|in|mm|cm|em|ex|px)\b|%)(?=[^\{]*[;\}])/',
  484. '$1$2$3',
  485. $css
  486. ); // "0.1em" to ".1em", "1.10em" to "1.1em"
  487. $css = preg_replace(
  488. '/([: ])\.(0)+((?:pt|pc|in|mm|cm|em|ex|px)\b|%)(?=[^\{]*[;\}])/',
  489. '$1$2',
  490. $css
  491. ); // ".0em" to "0"
  492. $css = preg_replace(
  493. '/([: ]0)0*(\.0*)?((?:pt|pc|in|mm|cm|em|ex|px)(?=[^\{]*[;\}])\b|%)/',
  494. '$1',
  495. $css
  496. ); // "0.0em" to "0"
  497. $css = preg_replace(
  498. '/([: ]\d+)(\.0*)((?:pt|pc|in|mm|cm|em|ex|px)(?=[^\{]*[;\}])\b|%)/',
  499. '$1$3',
  500. $css
  501. ); // "1.0em" to "1em"
  502. $css = preg_replace(
  503. '/([: ])0+(\d+|\d*\.\d+)((?:pt|pc|in|mm|cm|em|ex|px)(?=[^\{]*[;\}])\b|%)/',
  504. '$1$2$3',
  505. $css
  506. ); // "001em" to "1em"
  507. // shorten attributes (1em 1em 1em 1em -> 1em)
  508. $css = preg_replace(
  509. '/(?<![\w\-])((?:margin|padding|border|border-(?:width|radius)):)([\w\.]+)( \2)+(?=[;\}]| !)/',
  510. '$1$2',
  511. $css
  512. ); // "1em 1em 1em 1em" to "1em"
  513. $css = preg_replace(
  514. '/(?<![\w\-])((?:margin|padding|border|border-(?:width)):)([\w\.]+) ([\w\.]+) \2 \3(?=[;\}]| !)/',
  515. '$1$2 $3',
  516. $css
  517. ); // "1em 2em 1em 2em" to "1em 2em"
  518. // shorten colors
  519. $css = preg_replace(
  520. "/#([0-9a-fA-F]{1})\\1([0-9a-fA-F]{1})\\2([0-9a-fA-F]{1})\\3(?=[^\{]*[;\}])/",
  521. "#\\1\\2\\3",
  522. $css
  523. );
  524. // replace back protected strings
  525. $quote_back_cb = function ($match) use (&$quote_storage) {
  526. return $quote_storage[$match[1]];
  527. };
  528. $css = preg_replace_callback('/"STR(\d+)"/', $quote_back_cb, $css);
  529. $css = trim($css);
  530. return $css;
  531. }
  532. /**
  533. * Callback for css_compress()
  534. *
  535. * Keeps short comments (< 5 chars) to maintain typical browser hacks
  536. *
  537. * @author Andreas Gohr <andi@splitbrain.org>
  538. *
  539. * @param array $matches
  540. * @return string
  541. */
  542. function css_comment_cb($matches){
  543. if(strlen($matches[2]) > 4) return '';
  544. return $matches[0];
  545. }
  546. /**
  547. * Callback for css_compress()
  548. *
  549. * Strips one line comments but makes sure it will not destroy url() constructs with slashes
  550. *
  551. * @param array $matches
  552. * @return string
  553. */
  554. function css_onelinecomment_cb($matches) {
  555. $line = $matches[0];
  556. $i = 0;
  557. $len = strlen($line);
  558. while ($i< $len){
  559. $nextcom = strpos($line, '//', $i);
  560. $nexturl = stripos($line, 'url(', $i);
  561. if($nextcom === false) {
  562. // no more comments, we're done
  563. $i = $len;
  564. break;
  565. }
  566. if($nexturl === false || $nextcom < $nexturl) {
  567. // no url anymore, strip comment and be done
  568. $i = $nextcom;
  569. break;
  570. }
  571. // we have an upcoming url
  572. $i = strpos($line, ')', $nexturl);
  573. }
  574. return substr($line, 0, $i);
  575. }
  576. //Setup VIM: ex: et ts=4 :