function testEmptySelectors() { $src = '.test { font-weight: bold; }' . "\n" . '.empty { }' . "\n" . '.whatever { color: red }'; $css = new CssFile('somefile.css'); $css->setSource($src); $this->assertTrue(strpos($css->getSource(), '.empty') == 0); $this->assertTrue(strpos($css->getSource(), '.whatever') > 0); }
function compileCssList($list, $minify = false, $version = false, $images_version = false) { $compiled_css = ''; foreach ($list as $source) { $css = file_get_contents($source); $c = new CssFile($css, $version, $images_version); $compiled_css .= $c->render($minify); } return $compiled_css; }
public function saveCss() { $params = json_decode($this->request->get('result'), true); $theme = $params['theme']; if (!$theme) { $theme = 'barebone'; } // save custom CSS $css = new CssFile('upload/css/' . $theme . '.css'); $css->setSource($params['css']); $css->save(); // deleted rules foreach ($params['deletedRules'] as $file => $selectors) { $css = CssFile::getInstanceFromUrl($file, $theme); foreach ($selectors as $selector) { $css->deleteSelector($selector); } $css->save(); } // deleted properties foreach ($params['deletedProperties'] as $file => $selectors) { $css = CssFile::getInstanceFromUrl($file, $theme); foreach ($selectors as $selector => $properties) { foreach ($properties as $property => $value) { $css->deleteProperty($selector, $property); } } $css->save(); } }
/** * ... * * @param array $params * @param Smarty $smarty * @return string * * @package application.helper.smarty * @author Integry Systems */ function smarty_function_includeCss($params, LiveCartSmarty $smarty) { $fileName = $params['file']; $filePath = substr($fileName, 0, 1) != '/' ? ClassLoader::getRealPath('public.stylesheet.') . $fileName : ClassLoader::getRealPath('public') . $fileName; // fix slashes $filePath = str_replace('\\', DIRECTORY_SEPARATOR, $filePath); $filePath = str_replace('/', DIRECTORY_SEPARATOR, $filePath); if (!is_file($filePath) && !isset($params['external']) || substr($filePath, -4) != '.css') { return; } $css = CssFile::getInstanceFromPath($filePath, $smarty->getApplication()->getTheme()); $origFileName = $fileName; if ($css->isPatched()) { $filePath = $css->getPatchedFilePath(); $fileName = $css->getPatchedFileRelativePath(); } if (isset($params['inline']) && $params['inline'] == 'true') { $path = 'stylesheet/' . str_replace(DIRECTORY_SEPARATOR, '/', $fileName) . '?' . filemtime($filePath); return '<link href="' . $path . '" media="screen" rel="Stylesheet" type="text/css" />' . "\n"; } else { if (isset($params['external'])) { $smarty->_smarty_vars['INCLUDED_STYLESHEET_FILES_EXTERNAL'][] = $fileName; } else { $includedStylesheetTimestamp = $smarty->_smarty_vars['INCLUDED_STYLESHEET_TIMESTAMP']; if (!($includedStylesheetFiles = $smarty->_smarty_vars['INCLUDED_STYLESHEET_FILES'])) { $includedStylesheetFiles = array(); } if (in_array($filePath, $includedStylesheetFiles)) { if (isset($params['front'])) { unset($includedStylesheetFiles[array_search($filePath, $includedStylesheetFiles)]); } else { return; } } $fileMTime = filemtime($filePath); if ($fileMTime > (int) $includedStylesheetTimestamp) { $smarty->_smarty_vars['INCLUDED_STYLESHEET_TIMESTAMP'] = $fileMTime; } if (isset($params['front'])) { array_unshift($includedStylesheetFiles, $filePath); } else { if (isset($params['last'])) { $includedStylesheetFiles['x' . (count($includedStylesheetFiles) + 200) * (int) $params['last']] = $filePath; } else { array_push($includedStylesheetFiles, $filePath); } } $smarty->_smarty_vars['INCLUDED_STYLESHEET_FILES'] = $includedStylesheetFiles; } } foreach ($smarty->getApplication()->getConfigContainer()->getFilesByRelativePath('public/stylesheet/' . $origFileName, true) as $file) { if (realpath($file) == realpath($filePath)) { continue; } $file = substr($file, strlen(ClassLoader::getRealPath('public'))); $params['file'] = $file; smarty_function_includeCss($params, $smarty); } }
/** * Generate the build files for css and scripts. * * @return void */ protected function _buildFiles() { if (!empty($this->_buildFiles['css'])) { $Css = new CssFile(); foreach ($this->_buildFiles['css'] as $target => $contents) { if (strpos($target, ':hash') === 0) { $target = md5(implode('_', $contents)); } $this->out('Saving CSS file for ' . $target); $compress = $Css->process($contents); $Css->cache($target . '.css', $compress); } } if (!empty($this->_buildFiles['script'])) { $Js = new JsFile(); foreach ($this->_buildFiles['script'] as $target => $contents) { if (strpos($target, ':hash') === 0) { $target = md5(implode('_', $contents)); } $this->out('Saving Javascript file for ' . $target); $compress = $Js->process($contents); $Js->cache($target . '.js', $compress); } } }
/** * ... * * @param array $params * @param Smarty $smarty * @return string * * @package application.helper.smarty * @author Integry Systems */ function smarty_function_compiledCss($params, Smarty_Internal_Template $smarty) { $app = $smarty->getApplication(); if (!$app->isBackend()) { if (!function_exists('smarty_function_includeCss')) { include_once 'function.includeCss.php'; } $last = 1000; $files = array('common.css'); $theme = new Theme($smarty->getApplication()->getTheme(), $app); foreach ($theme->getAllParentThemes() as $parentTheme) { $files[] = CssFile::getTheme($parentTheme) . '.css'; } $files[] = CssFile::getTheme($smarty->getApplication()->getTheme()) . '.css'; foreach ($files as $file) { smarty_function_includeCss(array('file' => '/upload/css/' . $file, 'last' => ++$last), $smarty); } } $includedStylesheetTimestamp = $smarty->getGlobal("INCLUDED_STYLESHEET_TIMESTAMP"); $includedStylesheetFiles = $smarty->getGlobal("INCLUDED_STYLESHEET_FILES"); if ($includedStylesheetFiles) { uksort($includedStylesheetFiles, 'strnatcasecmp'); } $out = ''; if (isset($params['glue']) && $params['glue'] == true && !$smarty->getApplication()->isDevMode() && (!$smarty->getApplication()->isCustomizationMode() || $app->isBackend())) { $request = $smarty->getApplication()->getRequest(); if (isset($params['nameMethod']) && 'hash' == $params['nameMethod']) { $names = array_values((array) $includedStylesheetFiles); sort($names); $compiledFileName = md5(implode("\n", $names)) . '.css'; } else { $compiledFileName = $request->getControllerName() . '-' . $request->getActionName() . '.css'; } $compiledFilePath = ClassLoader::getRealPath('public.cache.stylesheet.') . $compiledFileName; $baseDir = ClassLoader::getRealPath('public.stylesheet.'); $publicDir = ClassLoader::getRealPath('public.'); $compiledFileTimestamp = 0; if (!is_file($compiledFilePath) || filemtime($compiledFilePath) < $includedStylesheetTimestamp) { if (!is_dir(ClassLoader::getRealPath('public.cache.stylesheet'))) { mkdir(ClassLoader::getRealPath('public.cache.stylesheet'), 0777, true); } // compile $compiledFileContent = ""; foreach ($includedStylesheetFiles as $key => $cssFile) { $relPath = str_replace($publicDir, '', $cssFile); $relPath = str_replace('\\', '/', $relPath); $compiledFileContent .= "\n\n\n/***************************************************\n"; $compiledFileContent .= " * " . $relPath . "\n"; $compiledFileContent .= " ***************************************************/\n\n"; $content = file_get_contents($cssFile); $pre = array('..', 'http', '/'); foreach (array("'", '"', '') as $quote) { foreach ($pre as $i) { $content = str_ireplace('url(' . $quote . $i, 'url__(' . $quote . $i, $content); } $content = str_replace('url(' . $quote, 'url(' . $quote . dirname($relPath) . '/', $content); foreach ($pre as $i) { $content = str_replace('url__(' . $quote . $i, 'url(' . $quote . $i, $content); } } $content = str_replace('url(..', 'url(' . dirname($relPath) . '/..', $content); $content = str_replace('url(\'..', 'url(\'' . dirname($relPath) . '/..', $content); $content = str_replace('url("..', 'url("' . dirname($relPath) . '/..', $content); $content = str_replace('upload/css/"../../', '"', $content); $compiledFileContent .= $content; } $compiledFileContent = preg_replace('/\\.(jpg|png|gif|bmp)/', '.$1?' . time(), $compiledFileContent); $compiledFileContent = preg_replace('/-moz-border-radius\\:([ \\.a-zA-Z0-9]+);/', '-moz-border-radius: $1; -khtml-border-radius: $1; border-radius: $1; ', $compiledFileContent); file_put_contents($compiledFilePath, $compiledFileContent); if (function_exists('gzencode')) { file_put_contents($compiledFilePath . '.gz', gzencode($compiledFileContent, 9)); } } $compiledFileTimestamp = filemtime($compiledFilePath); $out = '<link href="' . $app->getPublicUrl('gzip.php') . '?file=' . $compiledFileName . '&time=' . $compiledFileTimestamp . '" rel="Stylesheet" type="text/css"/>'; } else { if ($includedStylesheetFiles) { $includeString = ""; $publicPath = ClassLoader::getRealPath('public.'); foreach ($includedStylesheetFiles as $cssFile) { $urlPath = str_replace('\\', '/', str_replace($publicPath, '', $cssFile)); $includeString .= '<link href="' . $app->getPublicUrl($urlPath) . '?' . filemtime($cssFile) . '" rel="Stylesheet" type="text/css"/>' . "\n"; } $out = $includeString; } } if ($externalFiles = $smarty->getGlobal("INCLUDED_STYLESHEET_FILES_EXTERNAL")) { foreach ($externalFiles as $cssFile) { $out .= '<link href="' . $cssFile . '" rel="Stylesheet" type="text/css"/>' . "\n"; } } return $out; }