/lib/csslib.php
PHP | 4781 lines | 2406 code | 405 blank | 1970 comment | 482 complexity | 81f7fa13994cb0ad9399fcde3ec855bb MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-3.0, Apache-2.0, BSD-3-Clause
Large files files are truncated, but you can click here to view the full file
- <?php
- // This file is part of Moodle - http://moodle.org/
- //
- // Moodle is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // Moodle is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
- /**
- * This file contains CSS related class, and function for the CSS optimiser
- *
- * Please see the {@link css_optimiser} class for greater detail.
- *
- * @package core
- * @category css
- * @copyright 2012 Sam Hemelryk
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
- // NOTE: do not verify MOODLE_INTERNAL here, this is used from themes too
- /**
- * Stores CSS in a file at the given path.
- *
- * This function either succeeds or throws an exception.
- *
- * @param theme_config $theme The theme that the CSS belongs to.
- * @param string $csspath The path to store the CSS at.
- * @param array $cssfiles The CSS files to store.
- */
- function css_store_css(theme_config $theme, $csspath, array $cssfiles) {
- global $CFG;
- // Check if both the CSS optimiser is enabled and the theme supports it.
- if (!empty($CFG->enablecssoptimiser) && $theme->supportscssoptimisation) {
- // This is an experimental feature introduced in Moodle 2.3
- // The CSS optimiser organises the CSS in order to reduce the overall number
- // of rules and styles being sent to the client. It does this by collating
- // the CSS before it is cached removing excess styles and rules and stripping
- // out any extraneous content such as comments and empty rules.
- $optimiser = new css_optimiser;
- $css = '';
- foreach ($cssfiles as $file) {
- $css .= file_get_contents($file)."\n";
- }
- $css = $theme->post_process($css);
- $css = $optimiser->process($css);
- // If cssoptimisestats is set then stats from the optimisation are collected
- // and output at the beginning of the CSS
- if (!empty($CFG->cssoptimiserstats)) {
- $css = $optimiser->output_stats_css().$css;
- }
- } else {
- // This is the default behaviour.
- // The cssoptimise setting was introduced in Moodle 2.3 and will hopefully
- // in the future be changed from an experimental setting to the default.
- // The css_minify_css will method will use the Minify library remove
- // comments, additional whitespace and other minor measures to reduce the
- // the overall CSS being sent.
- // However it has the distinct disadvantage of having to minify the CSS
- // before running the post process functions. Potentially things may break
- // here if theme designers try to push things with CSS post processing.
- $css = $theme->post_process(css_minify_css($cssfiles));
- }
- clearstatcache();
- if (!file_exists(dirname($csspath))) {
- @mkdir(dirname($csspath), $CFG->directorypermissions, true);
- }
- // Prevent serving of incomplete file from concurrent request,
- // the rename() should be more atomic than fwrite().
- ignore_user_abort(true);
- if ($fp = fopen($csspath.'.tmp', 'xb')) {
- fwrite($fp, $css);
- fclose($fp);
- rename($csspath.'.tmp', $csspath);
- @chmod($csspath, $CFG->filepermissions);
- @unlink($csspath.'.tmp'); // just in case anything fails
- }
- ignore_user_abort(false);
- if (connection_aborted()) {
- die;
- }
- }
- /**
- * Sends IE specific CSS
- *
- * In writing the CSS parser I have a theory that we could optimise the CSS
- * then split it based upon the number of selectors to ensure we dont' break IE
- * and that we include only as many sub-stylesheets as we require.
- * Of course just a theory but may be fun to code.
- *
- * @param string $themename The name of the theme we are sending CSS for.
- * @param string $rev The revision to ensure we utilise the cache.
- * @param string $etag The revision to ensure we utilise the cache.
- * @param bool $slasharguments
- */
- function css_send_ie_css($themename, $rev, $etag, $slasharguments) {
- global $CFG;
- $lifetime = 60*60*24*60; // 60 days only - the revision may get incremented quite often
- $relroot = preg_replace('|^http.?://[^/]+|', '', $CFG->wwwroot);
- $css = "/** Unfortunately IE6/7 does not support more than 4096 selectors in one CSS file, which means we have to use some ugly hacks :-( **/";
- if ($slasharguments) {
- $css .= "\n@import url($relroot/styles.php/$themename/$rev/plugins);";
- $css .= "\n@import url($relroot/styles.php/$themename/$rev/parents);";
- $css .= "\n@import url($relroot/styles.php/$themename/$rev/theme);";
- } else {
- $css .= "\n@import url($relroot/styles.php?theme=$themename&rev=$rev&type=plugins);";
- $css .= "\n@import url($relroot/styles.php?theme=$themename&rev=$rev&type=parents);";
- $css .= "\n@import url($relroot/styles.php?theme=$themename&rev=$rev&type=theme);";
- }
- header('Etag: '.$etag);
- header('Content-Disposition: inline; filename="styles.php"');
- header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
- header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
- header('Pragma: ');
- header('Cache-Control: public, max-age='.$lifetime);
- header('Accept-Ranges: none');
- header('Content-Type: text/css; charset=utf-8');
- header('Content-Length: '.strlen($css));
- echo $css;
- die;
- }
- /**
- * Sends a cached CSS file
- *
- * This function sends the cached CSS file. Remember it is generated on the first
- * request, then optimised/minified, and finally cached for serving.
- *
- * @param string $csspath The path to the CSS file we want to serve.
- * @param string $etag The revision to make sure we utilise any caches.
- */
- function css_send_cached_css($csspath, $etag) {
- $lifetime = 60*60*24*60; // 60 days only - the revision may get incremented quite often
- header('Etag: '.$etag);
- header('Content-Disposition: inline; filename="styles.php"');
- header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($csspath)) .' GMT');
- header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
- header('Pragma: ');
- header('Cache-Control: public, max-age='.$lifetime);
- header('Accept-Ranges: none');
- header('Content-Type: text/css; charset=utf-8');
- if (!min_enable_zlib_compression()) {
- header('Content-Length: '.filesize($csspath));
- }
- readfile($csspath);
- die;
- }
- /**
- * Sends CSS directly without caching it.
- *
- * This function takes a raw CSS string, optimises it if required, and then
- * serves it.
- * Turning both themedesignermode and CSS optimiser on at the same time is aweful
- * for performance because of the optimiser running here. However it was done so
- * that theme designers could utilise the optimised output during development to
- * help them optimise their CSS... not that they should write lazy CSS.
- *
- * @param string $css
- */
- function css_send_uncached_css($css, $themesupportsoptimisation = true) {
- global $CFG;
- header('Content-Disposition: inline; filename="styles_debug.php"');
- header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
- header('Expires: '. gmdate('D, d M Y H:i:s', time() + THEME_DESIGNER_CACHE_LIFETIME) .' GMT');
- header('Pragma: ');
- header('Accept-Ranges: none');
- header('Content-Type: text/css; charset=utf-8');
- if (is_array($css)) {
- $css = implode("\n\n", $css);
- }
- echo $css;
- die;
- }
- /**
- * Send file not modified headers
- * @param int $lastmodified
- * @param string $etag
- */
- function css_send_unmodified($lastmodified, $etag) {
- $lifetime = 60*60*24*60; // 60 days only - the revision may get incremented quite often
- header('HTTP/1.1 304 Not Modified');
- header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
- header('Cache-Control: public, max-age='.$lifetime);
- header('Content-Type: text/css; charset=utf-8');
- header('Etag: '.$etag);
- if ($lastmodified) {
- header('Last-Modified: '. gmdate('D, d M Y H:i:s', $lastmodified) .' GMT');
- }
- die;
- }
- /**
- * Sends a 404 message about CSS not being found.
- */
- function css_send_css_not_found() {
- header('HTTP/1.0 404 not found');
- die('CSS was not found, sorry.');
- }
- /**
- * Uses the minify library to compress CSS.
- *
- * This is used if $CFG->enablecssoptimiser has been turned off. This was
- * the original CSS optimisation library.
- * It removes whitespace and shrinks things but does no apparent optimisation.
- * Note the minify library is still being used for JavaScript.
- *
- * @param array $files An array of files to minify
- * @return string The minified CSS
- */
- function css_minify_css($files) {
- global $CFG;
- if (empty($files)) {
- return '';
- }
- set_include_path($CFG->libdir . '/minify/lib' . PATH_SEPARATOR . get_include_path());
- require_once('Minify.php');
- if (0 === stripos(PHP_OS, 'win')) {
- Minify::setDocRoot(); // IIS may need help
- }
- // disable all caching, we do it in moodle
- Minify::setCache(null, false);
- $options = array(
- 'bubbleCssImports' => false,
- // Don't gzip content we just want text for storage
- 'encodeOutput' => false,
- // Maximum age to cache, not used but required
- 'maxAge' => (60*60*24*20),
- // The files to minify
- 'files' => $files,
- // Turn orr URI rewriting
- 'rewriteCssUris' => false,
- // This returns the CSS rather than echoing it for display
- 'quiet' => true
- );
- $error = 'unknown';
- try {
- $result = Minify::serve('Files', $options);
- if ($result['success']) {
- return $result['content'];
- }
- } catch (Exception $e) {
- $error = $e->getMessage();
- $error = str_replace("\r", ' ', $error);
- $error = str_replace("\n", ' ', $error);
- }
- // minification failed - try to inform the theme developer and include the non-minified version
- $css = <<<EOD
- /* Error: $error */
- /* Problem detected during theme CSS minimisation, please review the following code */
- /* ================================================================================ */
- EOD;
- foreach ($files as $cssfile) {
- $css .= file_get_contents($cssfile)."\n";
- }
- return $css;
- }
- /**
- * Determines if the given value is a valid CSS colour.
- *
- * A CSS colour can be one of the following:
- * - Hex colour: #AA66BB
- * - RGB colour: rgb(0-255, 0-255, 0-255)
- * - RGBA colour: rgba(0-255, 0-255, 0-255, 0-1)
- * - HSL colour: hsl(0-360, 0-100%, 0-100%)
- * - HSLA colour: hsla(0-360, 0-100%, 0-100%, 0-1)
- *
- * Or a recognised browser colour mapping {@link css_optimiser::$htmlcolours}
- *
- * @param string $value The colour value to check
- * @return bool
- */
- function css_is_colour($value) {
- $value = trim($value);
- $hex = '/^#([a-fA-F0-9]{1,3}|[a-fA-F0-9]{6})$/';
- $rgb = '#^rgb\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$#i';
- $rgba = '#^rgba\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1}(\.\d+)?)\s*\)$#i';
- $hsl = '#^hsl\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})\%\s*,\s*(\d{1,3})\%\s*\)$#i';
- $hsla = '#^hsla\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})\%\s*,\s*(\d{1,3})\%\s*,\s*(\d{1}(\.\d+)?)\s*\)$#i';
- if (in_array(strtolower($value), array('inherit'))) {
- return true;
- } else if (preg_match($hex, $value)) {
- return true;
- } else if (in_array(strtolower($value), array_keys(css_optimiser::$htmlcolours))) {
- return true;
- } else if (preg_match($rgb, $value, $m) && $m[1] < 256 && $m[2] < 256 && $m[3] < 256) {
- // It is an RGB colour
- return true;
- } else if (preg_match($rgba, $value, $m) && $m[1] < 256 && $m[2] < 256 && $m[3] < 256) {
- // It is an RGBA colour
- return true;
- } else if (preg_match($hsl, $value, $m) && $m[1] <= 360 && $m[2] <= 100 && $m[3] <= 100) {
- // It is an HSL colour
- return true;
- } else if (preg_match($hsla, $value, $m) && $m[1] <= 360 && $m[2] <= 100 && $m[3] <= 100) {
- // It is an HSLA colour
- return true;
- }
- // Doesn't look like a colour.
- return false;
- }
- /**
- * Returns true is the passed value looks like a CSS width.
- * In order to pass this test the value must be purely numerical or end with a
- * valid CSS unit term.
- *
- * @param string|int $value
- * @return boolean
- */
- function css_is_width($value) {
- $value = trim($value);
- if (in_array(strtolower($value), array('auto', 'inherit'))) {
- return true;
- }
- if ((string)$value === '0' || preg_match('#^(\-\s*)?(\d*\.)?(\d+)\s*(em|px|pt|\%|in|cm|mm|ex|pc)$#i', $value)) {
- return true;
- }
- return false;
- }
- /**
- * A simple sorting function to sort two array values on the number of items they contain
- *
- * @param array $a
- * @param array $b
- * @return int
- */
- function css_sort_by_count(array $a, array $b) {
- $a = count($a);
- $b = count($b);
- if ($a == $b) {
- return 0;
- }
- return ($a > $b) ? -1 : 1;
- }
- /**
- * A basic CSS optimiser that strips out unwanted things and then processing the
- * CSS organising styles and moving duplicates and useless CSS.
- *
- * This CSS optimiser works by reading through a CSS string one character at a
- * time and building an object structure of the CSS.
- * As part of that processing styles are expanded out as much as they can be to
- * ensure we collect all mappings, at the end of the processing those styles are
- * then combined into an optimised form to keep them as short as possible.
- *
- * @package core
- * @category css
- * @copyright 2012 Sam Hemelryk
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
- class css_optimiser {
- /**
- * Used when the processor is about to start processing.
- * Processing states. Used internally.
- */
- const PROCESSING_START = 0;
- /**
- * Used when the processor is currently processing a selector.
- * Processing states. Used internally.
- */
- const PROCESSING_SELECTORS = 0;
- /**
- * Used when the processor is currently processing a style.
- * Processing states. Used internally.
- */
- const PROCESSING_STYLES = 1;
- /**
- * Used when the processor is currently processing a comment.
- * Processing states. Used internally.
- */
- const PROCESSING_COMMENT = 2;
- /**
- * Used when the processor is currently processing an @ rule.
- * Processing states. Used internally.
- */
- const PROCESSING_ATRULE = 3;
- /**
- * The raw string length before optimisation.
- * Stats variables set during and after processing
- * @var int
- */
- protected $rawstrlen = 0;
- /**
- * The number of comments that were removed during optimisation.
- * Stats variables set during and after processing
- * @var int
- */
- protected $commentsincss = 0;
- /**
- * The number of rules in the CSS before optimisation.
- * Stats variables set during and after processing
- * @var int
- */
- protected $rawrules = 0;
- /**
- * The number of selectors using in CSS rules before optimisation.
- * Stats variables set during and after processing
- * @var int
- */
- protected $rawselectors = 0;
- /**
- * The string length after optimisation.
- * Stats variables set during and after processing
- * @var int
- */
- protected $optimisedstrlen = 0;
- /**
- * The number of rules after optimisation.
- * Stats variables set during and after processing
- * @var int
- */
- protected $optimisedrules = 0;
- /**
- * The number of selectors used in rules after optimisation.
- * Stats variables set during and after processing
- * @var int
- */
- protected $optimisedselectors = 0;
- /**
- * The start time of the optimisation.
- * Stats variables set during and after processing
- * @var int
- */
- protected $timestart = 0;
- /**
- * The end time of the optimisation.
- * Stats variables set during and after processing
- * @var int
- */
- protected $timecomplete = 0;
- /**
- * Will be set to any errors that may have occured during processing.
- * This is updated only at the end of processing NOT during.
- *
- * @var array
- */
- protected $errors = array();
- /**
- * Processes incoming CSS optimising it and then returning it.
- *
- * @param string $css The raw CSS to optimise
- * @return string The optimised CSS
- */
- public function process($css) {
- global $CFG;
- // Easiest win there is
- $css = trim($css);
- $this->reset_stats();
- $this->timestart = microtime(true);
- $this->rawstrlen = strlen($css);
- // Don't try to process files with no content... it just doesn't make sense.
- // But we should produce an error for them, an empty CSS file will lead to a
- // useless request for those running theme designer mode.
- if ($this->rawstrlen === 0) {
- $this->errors[] = 'Skipping file as it has no content.';
- return '';
- }
- // First up we need to remove all line breaks - this allows us to instantly
- // reduce our processing requirements and as we will process everything
- // into a new structure there's really nothing lost.
- $css = preg_replace('#\r?\n#', ' ', $css);
- // Next remove the comments... no need to them in an optimised world and
- // knowing they're all gone allows us to REALLY make our processing simpler
- $css = preg_replace('#/\*(.*?)\*/#m', '', $css, -1, $this->commentsincss);
- $medias = array(
- 'all' => new css_media()
- );
- $imports = array();
- $charset = false;
- // Keyframes are used for CSS animation they will be processed right at the very end.
- $keyframes = array();
- $currentprocess = self::PROCESSING_START;
- $currentrule = css_rule::init();
- $currentselector = css_selector::init();
- $inquotes = false; // ' or "
- $inbraces = false; // {
- $inbrackets = false; // [
- $inparenthesis = false; // (
- $currentmedia = $medias['all'];
- $currentatrule = null;
- $suspectatrule = false;
- $buffer = '';
- $char = null;
- // Next we are going to iterate over every single character in $css.
- // This is why we removed line breaks and comments!
- for ($i = 0; $i < $this->rawstrlen; $i++) {
- $lastchar = $char;
- $char = substr($css, $i, 1);
- if ($char == '@' && $buffer == '') {
- $suspectatrule = true;
- }
- switch ($currentprocess) {
- // Start processing an @ rule e.g. @media, @page, @keyframes
- case self::PROCESSING_ATRULE:
- switch ($char) {
- case ';':
- if (!$inbraces) {
- $buffer .= $char;
- if ($currentatrule == 'import') {
- $imports[] = $buffer;
- $currentprocess = self::PROCESSING_SELECTORS;
- } else if ($currentatrule == 'charset') {
- $charset = $buffer;
- $currentprocess = self::PROCESSING_SELECTORS;
- }
- }
- if ($currentatrule !== 'media') {
- $buffer = '';
- $currentatrule = false;
- }
- // continue 1: The switch processing chars
- // continue 2: The switch processing the state
- // continue 3: The for loop
- continue 3;
- case '{':
- if ($currentatrule == 'media' && preg_match('#\s*@media\s*([a-zA-Z0-9]+(\s*,\s*[a-zA-Z0-9]+)*)\s*{#', $buffer, $matches)) {
- // Basic media declaration
- $mediatypes = str_replace(' ', '', $matches[1]);
- if (!array_key_exists($mediatypes, $medias)) {
- $medias[$mediatypes] = new css_media($mediatypes);
- }
- $currentmedia = $medias[$mediatypes];
- $currentprocess = self::PROCESSING_SELECTORS;
- $buffer = '';
- } else if ($currentatrule == 'media' && preg_match('#\s*@media\s*([^{]+)#', $buffer, $matches)) {
- // Advanced media query declaration http://www.w3.org/TR/css3-mediaqueries/
- $mediatypes = $matches[1];
- $hash = md5($mediatypes);
- $medias[$hash] = new css_media($mediatypes);
- $currentmedia = $medias[$hash];
- $currentprocess = self::PROCESSING_SELECTORS;
- $buffer = '';
- } else if ($currentatrule == 'keyframes' && preg_match('#@((\-moz\-|\-webkit\-)?keyframes)\s*([^\s]+)#', $buffer, $matches)) {
- // Keyframes declaration, we treat it exactly like a @media declaration except we don't allow
- // them to be overridden to ensure we don't mess anything up. (means we keep everything in order)
- $keyframefor = $matches[1];
- $keyframename = $matches[3];
- $keyframe = new css_keyframe($keyframefor, $keyframename);
- $keyframes[] = $keyframe;
- $currentmedia = $keyframe;
- $currentprocess = self::PROCESSING_SELECTORS;
- $buffer = '';
- }
- // continue 1: The switch processing chars
- // continue 2: The switch processing the state
- // continue 3: The for loop
- continue 3;
- }
- break;
- // Start processing selectors
- case self::PROCESSING_START:
- case self::PROCESSING_SELECTORS:
- switch ($char) {
- case '[':
- $inbrackets ++;
- $buffer .= $char;
- // continue 1: The switch processing chars
- // continue 2: The switch processing the state
- // continue 3: The for loop
- continue 3;
- case ']':
- $inbrackets --;
- $buffer .= $char;
- // continue 1: The switch processing chars
- // continue 2: The switch processing the state
- // continue 3: The for loop
- continue 3;
- case ' ':
- if ($inbrackets) {
- // continue 1: The switch processing chars
- // continue 2: The switch processing the state
- // continue 3: The for loop
- continue 3;
- }
- if (!empty($buffer)) {
- // Check for known @ rules
- if ($suspectatrule && preg_match('#@(media|import|charset|(\-moz\-|\-webkit\-)?(keyframes))\s*#', $buffer, $matches)) {
- $currentatrule = (!empty($matches[3]))?$matches[3]:$matches[1];
- $currentprocess = self::PROCESSING_ATRULE;
- $buffer .= $char;
- } else {
- $currentselector->add($buffer);
- $buffer = '';
- }
- }
- $suspectatrule = false;
- // continue 1: The switch processing chars
- // continue 2: The switch processing the state
- // continue 3: The for loop
- continue 3;
- case '{':
- if ($inbrackets) {
- // continue 1: The switch processing chars
- // continue 2: The switch processing the state
- // continue 3: The for loop
- continue 3;
- }
- if ($buffer !== '') {
- $currentselector->add($buffer);
- }
- $currentrule->add_selector($currentselector);
- $currentselector = css_selector::init();
- $currentprocess = self::PROCESSING_STYLES;
- $buffer = '';
- // continue 1: The switch processing chars
- // continue 2: The switch processing the state
- // continue 3: The for loop
- continue 3;
- case '}':
- if ($inbrackets) {
- // continue 1: The switch processing chars
- // continue 2: The switch processing the state
- // continue 3: The for loop
- continue 3;
- }
- if ($currentatrule == 'media') {
- $currentmedia = $medias['all'];
- $currentatrule = false;
- $buffer = '';
- } else if (strpos($currentatrule, 'keyframes') !== false) {
- $currentmedia = $medias['all'];
- $currentatrule = false;
- $buffer = '';
- }
- // continue 1: The switch processing chars
- // continue 2: The switch processing the state
- // continue 3: The for loop
- continue 3;
- case ',':
- if ($inbrackets) {
- // continue 1: The switch processing chars
- // continue 2: The switch processing the state
- // continue 3: The for loop
- continue 3;
- }
- $currentselector->add($buffer);
- $currentrule->add_selector($currentselector);
- $currentselector = css_selector::init();
- $buffer = '';
- // continue 1: The switch processing chars
- // continue 2: The switch processing the state
- // continue 3: The for loop
- continue 3;
- }
- break;
- // Start processing styles
- case self::PROCESSING_STYLES:
- if ($char == '"' || $char == "'") {
- if ($inquotes === false) {
- $inquotes = $char;
- }
- if ($inquotes === $char && $lastchar !== '\\') {
- $inquotes = false;
- }
- }
- if ($inquotes) {
- $buffer .= $char;
- continue 2;
- }
- switch ($char) {
- case ';':
- if ($inparenthesis) {
- $buffer .= $char;
- // continue 1: The switch processing chars
- // continue 2: The switch processing the state
- // continue 3: The for loop
- continue 3;
- }
- $currentrule->add_style($buffer);
- $buffer = '';
- $inquotes = false;
- // continue 1: The switch processing chars
- // continue 2: The switch processing the state
- // continue 3: The for loop
- continue 3;
- case '}':
- $currentrule->add_style($buffer);
- $this->rawselectors += $currentrule->get_selector_count();
- $currentmedia->add_rule($currentrule);
- $currentrule = css_rule::init();
- $currentprocess = self::PROCESSING_SELECTORS;
- $this->rawrules++;
- $buffer = '';
- $inquotes = false;
- $inparenthesis = false;
- // continue 1: The switch processing chars
- // continue 2: The switch processing the state
- // continue 3: The for loop
- continue 3;
- case '(':
- $inparenthesis = true;
- $buffer .= $char;
- // continue 1: The switch processing chars
- // continue 2: The switch processing the state
- // continue 3: The for loop
- continue 3;
- case ')':
- $inparenthesis = false;
- $buffer .= $char;
- // continue 1: The switch processing chars
- // continue 2: The switch processing the state
- // continue 3: The for loop
- continue 3;
- }
- break;
- }
- $buffer .= $char;
- }
- foreach ($medias as $media) {
- $this->optimise($media);
- }
- $css = $this->produce_css($charset, $imports, $medias, $keyframes);
- $this->timecomplete = microtime(true);
- return trim($css);
- }
- /**
- * Produces CSS for the given charset, imports, media, and keyframes
- * @param string $charset
- * @param array $imports
- * @param array $medias
- * @param array $keyframes
- * @return string
- */
- protected function produce_css($charset, array $imports, array $medias, array $keyframes) {
- $css = '';
- if (!empty($charset)) {
- $imports[] = $charset;
- }
- if (!empty($imports)) {
- $css .= implode("\n", $imports);
- $css .= "\n\n";
- }
- $cssreset = array();
- $cssstandard = array();
- $csskeyframes = array();
- // Process each media declaration individually
- foreach ($medias as $media) {
- // If this declaration applies to all media types
- if (in_array('all', $media->get_types())) {
- // Collect all rules that represet reset rules and remove them from the media object at the same time.
- // We do this because we prioritise reset rules to the top of a CSS output. This ensures that they
- // can't end up out of order because of optimisation.
- $resetrules = $media->get_reset_rules(true);
- if (!empty($resetrules)) {
- $cssreset[] = css_writer::media('all', $resetrules);
- }
- }
- // Get the standard cSS
- $cssstandard[] = $media->out();
- }
- // Finally if there are any keyframe declarations process them now.
- if (count($keyframes) > 0) {
- foreach ($keyframes as $keyframe) {
- $this->optimisedrules += $keyframe->count_rules();
- $this->optimisedselectors += $keyframe->count_selectors();
- if ($keyframe->has_errors()) {
- $this->errors += $keyframe->get_errors();
- }
- $csskeyframes[] = $keyframe->out();
- }
- }
- // Join it all together
- $css .= join('', $cssreset);
- $css .= join('', $cssstandard);
- $css .= join('', $csskeyframes);
- // Record the strlenght of the now optimised CSS.
- $this->optimisedstrlen = strlen($css);
- // Return the now produced CSS
- return $css;
- }
- /**
- * Optimises the CSS rules within a rule collection of one form or another
- *
- * @param css_rule_collection $media
- * @return void This function acts in reference
- */
- protected function optimise(css_rule_collection $media) {
- $media->organise_rules_by_selectors();
- $this->optimisedrules += $media->count_rules();
- $this->optimisedselectors += $media->count_selectors();
- if ($media->has_errors()) {
- $this->errors += $media->get_errors();
- }
- }
- /**
- * Returns an array of stats from the last processing run
- * @return string
- */
- public function get_stats() {
- $stats = array(
- 'timestart' => $this->timestart,
- 'timecomplete' => $this->timecomplete,
- 'timetaken' => round($this->timecomplete - $this->timestart, 4),
- 'commentsincss' => $this->commentsincss,
- 'rawstrlen' => $this->rawstrlen,
- 'rawselectors' => $this->rawselectors,
- 'rawrules' => $this->rawrules,
- 'optimisedstrlen' => $this->optimisedstrlen,
- 'optimisedrules' => $this->optimisedrules,
- 'optimisedselectors' => $this->optimisedselectors,
- 'improvementstrlen' => '-',
- 'improvementrules' => '-',
- 'improvementselectors' => '-',
- );
- // Avoid division by 0 errors by checking we have valid raw values
- if ($this->rawstrlen > 0) {
- $stats['improvementstrlen'] = round(100 - ($this->optimisedstrlen / $this->rawstrlen) * 100, 1).'%';
- }
- if ($this->rawrules > 0) {
- $stats['improvementrules'] = round(100 - ($this->optimisedrules / $this->rawrules) * 100, 1).'%';
- }
- if ($this->rawselectors > 0) {
- $stats['improvementselectors'] = round(100 - ($this->optimisedselectors / $this->rawselectors) * 100, 1).'%';
- }
- return $stats;
- }
- /**
- * Returns true if any errors have occured during processing
- *
- * @return bool
- */
- public function has_errors() {
- return !empty($this->errors);
- }
- /**
- * Returns an array of errors that have occured
- *
- * @param bool $clear If set to true the errors will be cleared after being returned.
- * @return array
- */
- public function get_errors($clear = false) {
- $errors = $this->errors;
- if ($clear) {
- // Reset the error array
- $this->errors = array();
- }
- return $errors;
- }
- /**
- * Returns any errors as a string that can be included in CSS.
- *
- * @return string
- */
- public function output_errors_css() {
- $computedcss = "/****************************************\n";
- $computedcss .= " *--- Errors found during processing ----\n";
- foreach ($this->errors as $error) {
- $computedcss .= preg_replace('#^#m', '* ', $error);
- }
- $computedcss .= " ****************************************/\n\n";
- return $computedcss;
- }
- /**
- * Returns a string to display stats about the last generation within CSS output
- *
- * @return string
- */
- public function output_stats_css() {
- $computedcss = "/****************************************\n";
- $computedcss .= " *------- CSS Optimisation stats --------\n";
- if ($this->rawstrlen === 0) {
- $computedcss .= " File not processed as it has no content /\n\n";
- $computedcss .= " ****************************************/\n\n";
- return $computedcss;
- } else if ($this->rawrules === 0) {
- $computedcss .= " File contained no rules to be processed /\n\n";
- $computedcss .= " ****************************************/\n\n";
- return $computedcss;
- }
- $stats = $this->get_stats();
- $computedcss .= " * ".date('r')."\n";
- $computedcss .= " * {$stats['commentsincss']} \t comments removed\n";
- $computedcss .= " * Optimisation took {$stats['timetaken']} seconds\n";
- $computedcss .= " *--------------- before ----------------\n";
- $computedcss .= " * {$stats['rawstrlen']} \t chars read in\n";
- $computedcss .= " * {$stats['rawrules']} \t rules read in\n";
- $computedcss .= " * {$stats['rawselectors']} \t total selectors\n";
- $computedcss .= " *---------------- after ----------------\n";
- $computedcss .= " * {$stats['optimisedstrlen']} \t chars once optimized\n";
- $computedcss .= " * {$stats['optimisedrules']} \t optimized rules\n";
- $computedcss .= " * {$stats['optimisedselectors']} \t total selectors once optimized\n";
- $computedcss .= " *---------------- stats ----------------\n";
- $computedcss .= " * {$stats['improvementstrlen']} \t reduction in chars\n";
- $computedcss .= " * {$stats['improvementrules']} \t reduction in rules\n";
- $computedcss .= " * {$stats['improvementselectors']} \t reduction in selectors\n";
- $computedcss .= " ****************************************/\n\n";
- return $computedcss;
- }
- /**
- * Resets the stats ready for another fresh processing
- */
- public function reset_stats() {
- $this->commentsincss = 0;
- $this->optimisedrules = 0;
- $this->optimisedselectors = 0;
- $this->optimisedstrlen = 0;
- $this->rawrules = 0;
- $this->rawselectors = 0;
- $this->rawstrlen = 0;
- $this->timecomplete = 0;
- $this->timestart = 0;
- }
- /**
- * An array of the common HTML colours that are supported by most browsers.
- *
- * This reference table is used to allow us to unify colours, and will aid
- * us in identifying buggy CSS using unsupported colours.
- *
- * @staticvar array
- * @var array
- */
- public static $htmlcolours = array(
- 'aliceblue' => '#F0F8FF',
- 'antiquewhite' => '#FAEBD7',
- 'aqua' => '#00FFFF',
- 'aquamarine' => '#7FFFD4',
- 'azure' => '#F0FFFF',
- 'beige' => '#F5F5DC',
- 'bisque' => '#FFE4C4',
- 'black' => '#000000',
- 'blanchedalmond' => '#FFEBCD',
- 'blue' => '#0000FF',
- 'blueviolet' => '#8A2BE2',
- 'brown' => '#A52A2A',
- 'burlywood' => '#DEB887',
- 'cadetblue' => '#5F9EA0',
- 'chartreuse' => '#7FFF00',
- 'chocolate' => '#D2691E',
- 'coral' => '#FF7F50',
- 'cornflowerblue' => '#6495ED',
- 'cornsilk' => '#FFF8DC',
- 'crimson' => '#DC143C',
- 'cyan' => '#00FFFF',
- 'darkblue' => '#00008B',
- 'darkcyan' => '#008B8B',
- 'darkgoldenrod' => '#B8860B',
- 'darkgray' => '#A9A9A9',
- 'darkgrey' => '#A9A9A9',
- 'darkgreen' => '#006400',
- 'darkKhaki' => '#BDB76B',
- 'darkmagenta' => '#8B008B',
- 'darkolivegreen' => '#556B2F',
- 'arkorange' => '#FF8C00',
- 'darkorchid' => '#9932CC',
- 'darkred' => '#8B0000',
- 'darksalmon' => '#E9967A',
- 'darkseagreen' => '#8FBC8F',
- 'darkslateblue' => '#483D8B',
- 'darkslategray' => '#2F4F4F',
- 'darkslategrey' => '#2F4F4F',
- 'darkturquoise' => '#00CED1',
- 'darkviolet' => '#9400D3',
- 'deeppink' => '#FF1493',
- 'deepskyblue' => '#00BFFF',
- 'dimgray' => '#696969',
- 'dimgrey' => '#696969',
- 'dodgerblue' => '#1E90FF',
- 'firebrick' => '#B22222',
- 'floralwhite' => '#FFFAF0',
- 'forestgreen' => '#228B22',
- 'fuchsia' => '#FF00FF',
- 'gainsboro' => '#DCDCDC',
- 'ghostwhite' => '#F8F8FF',
- 'gold' => '#FFD700',
- 'goldenrod' => '#DAA520',
- 'gray' => '#808080',
- 'grey' => '#808080',
- 'green' => '#008000',
- 'greenyellow' => '#ADFF2F',
- 'honeydew' => '#F0FFF0',
- 'hotpink' => '#FF69B4',
- 'indianred ' => '#CD5C5C',
- 'indigo ' => '#4B0082',
- 'ivory' => '#FFFFF0',
- 'khaki' => '#F0E68C',
- 'lavender' => '#E6E6FA',
- 'lavenderblush' => '#FFF0F5',
- 'lawngreen' => '#7CFC00',
- 'lemonchiffon' => '#FFFACD',
- 'lightblue' => '#ADD8E6',
- 'lightcoral' => '#F08080',
- 'lightcyan' => '#E0FFFF',
- 'lightgoldenrodyellow' => '#FAFAD2',
- 'lightgray' => '#D3D3D3',
- 'lightgrey' => '#D3D3D3',
- 'lightgreen' => '#90EE90',
- 'lightpink' => '#FFB6C1',
- 'lightsalmon' => '#FFA07A',
- 'lightseagreen' => '#20B2AA',
- 'lightskyblue' => '#87CEFA',
- 'lightslategray' => '#778899',
- 'lightslategrey' => '#778899',
- 'lightsteelblue' => '#B0C4DE',
- 'lightyellow' => '#FFFFE0',
- 'lime' => '#00FF00',
- 'limegreen' => '#32CD32',
- 'linen' => '#FAF0E6',
- 'magenta' => '#FF00FF',
- 'maroon' => '#800000',
- 'mediumaquamarine' => '#66CDAA',
- 'mediumblue' => '#0000CD',
- 'mediumorchid' => '#BA55D3',
- 'mediumpurple' => '#9370D8',
- 'mediumseagreen' => '#3CB371',
- 'mediumslateblue' => '#7B68EE',
- 'mediumspringgreen' => '#00FA9A',
- 'mediumturquoise' => '#48D1CC',
- 'mediumvioletred' => '#C71585',
- 'midnightblue' => '#191970',
- 'mintcream' => '#F5FFFA',
- 'mistyrose' => '#FFE4E1',
- 'moccasin' => '#FFE4B5',
- 'navajowhite' => '#FFDEAD',
- 'navy' => '#000080',
- 'oldlace' => '#FDF5E6',
- 'olive' => '#808000',
- 'olivedrab' => '#6B8E23',
- 'orange' => '#FFA500',
- 'orangered' => '#FF4500',
- 'orchid' => '#DA70D6',
- 'palegoldenrod' => '#EEE8AA',
- 'palegreen' => '#98FB98',
- 'paleturquoise' => '#AFEEEE',
- 'palevioletred' => '#D87093',
- 'papayawhip' => '#FFEFD5',
- 'peachpuff' => '#FFDAB9',
- 'peru' => '#CD853F',
- 'pink' => '#FFC0CB',
- 'plum' => '#DDA0DD',
- 'powderblue' => '#B0E0E6',
- 'purple' => '#800080',
- 'red' => '#FF0000',
- 'rosybrown' => '#BC8F8F',
- 'royalblue' => '#4169E1',
- 'saddlebrown' => '#8B4513',
- 'salmon' => '#FA8072',
- 'sandybrown' => '#F4A460',
- 'seagreen' => '#2E8B57',
- 'seashell' => '#FFF5EE',
- 'sienna' => '#A0522D',
- 'silver' => '#C0C0C0',
- 'skyblue' => '#87CEEB',
- 'slateblue' => '#6A5ACD',
- 'slategray' => '#708090',
- 'slategrey' => '#708090',
- 'snow' => '#FFFAFA',
- 'springgreen' => '#00FF7F',
- 'steelblue' => '#4682B4',
- 'tan' => '#D2B48C',
- 'teal' => '#008080',
- 'thistle' => '#D8BFD8',
- 'tomato' => '#FF6347',
- 'transparent' => 'transparent',
- 'turquoise' => '#40E0D0',
- 'violet' => '#EE82EE',
- 'wheat' => '#F5DEB3',
- 'white' => '#FFFFFF',
- 'whitesmoke' => '#F5F5F5',
- 'yellow' => '#FFFF00',
- 'yellowgreen' => '#9ACD32'
- );
- }
- /**
- * Used to prepare CSS strings
- *
- * @package core
- * @category css
- * @copyright 2012 Sam Hemelryk
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
- abstract class css_writer {
- /**
- * The current indent level
- * @var int
- */
- protected static $indent = 0;
- /**
- * Returns true if the output should still maintain minimum formatting.
- * @return bool
- */
- protected static function is_pretty() {
- global $CFG;
- return (!empty($CFG->cssoptimiserpretty));
- }
- /**
- * Returns the indenting char to use for indenting things nicely.
- * @return string
- */
- protected static function get_indent() {
- if (self::is_pretty()) {
- return str_repeat(" ", self::$indent);
- }
- return '';
- }
- /**
- * Increases the current indent
- */
- protected static function increase_indent() {
- self::$indent++;
- }
- /**
- * Decreases the current indent
- */
- protected static function decrease_indent() {
- self::$indent--;
- }
- /**
- * Returns the string to use as a separator
- * @return string
- */
- protected static function get_separator() {
- return (self::is_pretty())?"\n":' ';
- }
- /**
- * Returns CSS for media
- *
- * @param string $typestring
- * @param array $rules An array of css_rule objects
- * @return string
- */
- public static function media($typestring, array &$rules) {
- $nl = self::get_separator();
- $output = '';
- if ($typestring !== 'all') {
- $output .= "\n@media {$typestring} {".$nl;
- self::increase_indent();
- }
- foreach ($rules as $rule) {
- $output .= $rule->out().$nl;
- }
- if ($typestring !== 'all') {
- self::decrease_indent();
- $output .= '}';
- }
- return $output;
- }
- /**
- * Returns CSS for a keyframe
- *
- * @param string $for The desired declaration. e.g. keyframes, -moz-keyframes, -webkit-keyframes
- * @param string $name The name for the keyframe
- * @param array $rules An array of rules belonging to the keyframe
- * @return string
- */
- public static function keyframe($for, $name, array &$rules) {
- $nl = self::get_separator();
- $output = "\n@{$for} {$name} {";
- foreach ($rules as $rule) {
- $output .= $rule->out();
- }
- $output .= '}';
- return $output;
- }
- /**
- * Returns CSS for a rule
- *
- * @param string $selector
- * @param string $styles
- * @return string
- */
- public static function rule($selector, $styles) {
- $css = self::get_indent()."{$selector}{{$styles}}";
- return $css;
- }
- /**
- * Returns CSS for the selectors of a rule
- *
- * @param array $selectors Array of css_selector objects
- * @return string
- */
- public static function selectors(array $selectors) {
- $nl = self::get_separator();
- $selectorstrings = array();
- foreach ($selectors as $selector) {
- $selectorstrings[] = $selector->out();
- }
- return join(','.$nl, $selectorstrings);
- }
- /**
- * Returns a selector given the components that make it up.
- *
- * @param array $components
- * @return string
- */
- public static function selector(array $components) {
- return trim(join(' ', $components));
- }
- /**
- * Returns a CSS string for the provided styles
- *
- * @param array $styles Array of css_style objects
- * @return string
- */
- public static function styles(array $styles) {
- $bits = array();
- foreach ($styles as $style) {
- // Check if the style is an array. If it is then we are outputing an advanced style.
- // An advanced style is a style with one or more values, and can occur in situations like background-image
- // where browse specific values are being used.
- if (is_array($style)) {
- foreach ($style as $advstyle) {
- $bits[] = $advstyle->out();
- }
- continue;
- }
- $bits[] = $style->out();
- }
- return join('', $bits);
- }
- /**
- * Returns a style CSS
- *
- * @param string $name
- * @param string $value
- * @param bool $important
- * @return string
- */
- public static function style($name, $value, $important = false) {
- $value = trim($value);
- if ($important && strpos($value, '!important') === false) {
- $value .= ' !important';
- }
- return "{$name}:{$value};";
- }
- }
- /**
- * A structure to represent a CSS selector.
- *
- * The selector is the classes, id, elements, and psuedo bits that make up a CSS
- * rule.
- *
- * @package core
- * @category css
- * @copyright 2012 Sam Hemelryk
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
- class css_selector {
- /**
- * An array of selector bits
- * @var array
- */
- …
Large files files are truncated, but you can click here to view the full file