From f7d780b9b82a664525120a90a8b1cb25be57d0e0 Mon Sep 17 00:00:00 2001 From: Gabriel Birke Date: Sun, 9 Jan 2011 13:18:19 +0100 Subject: Preserve @import statements in CSS --- lib/exe/css.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'lib/exe/css.php') diff --git a/lib/exe/css.php b/lib/exe/css.php index e64ebc22a..eb2d96513 100644 --- a/lib/exe/css.php +++ b/lib/exe/css.php @@ -122,6 +122,9 @@ function css_out(){ // apply style replacements $css = css_applystyle($css,$tplinc); + + // place all @import statements at the top of the file + $css = css_moveimports($css); // compress whitespace and comments if($conf['compress']){ @@ -265,6 +268,7 @@ function css_loadfile($file,$location=''){ if(!$location) return $css; $css = preg_replace('#(url\([ \'"]*)((?!/|http://|https://| |\'|"))#','\\1'.$location.'\\3',$css); + $css = preg_replace('#(@import\s+[\'"])((?!/|http://|https://))#', '\\1'.$location.'\\2"', $css); return $css; } @@ -296,6 +300,29 @@ function css_pluginstyles($mode='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