Skip to content
Browse files

Added Collection Classes

  • Loading branch information...
1 parent 3186af6 commit 8d8cef480852f1615fb4dcd2da1ab164905be731 @kaviththiranga committed Dec 10, 2012
View
533 libraries/joomla/media/combiner.php → libraries/joomla/media/collection.php
@@ -1,50 +1,60 @@
-<?php
-/**
- * @package Joomla.Platform
- * @subpackage Media
- *
- * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
- * @license GNU General Public License version 2 or later; see LICENSE
- */
-
-defined('JPATH_PLATFORM') or die;
-
-/**
- * Interface for Javascript/CSS combiner classes.
- *
- * @package Joomla.Platform
- * @subpackage Media
- * @since 12.1
- */
-abstract class JMediaCombiner
-{
- public $sources = array();
-
- public $sourceCount = 0;
-
- protected $_combined = null;
-
- protected $_options = array();
-
+<?php
+/**
+ * @package Joomla.Platform
+ * @subpackage Media
+ *
+ * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
+ * @license GNU General Public License version 2 or later; see LICENSE
+ */
+
+defined('JPATH_PLATFORM') or die;
+
+/**
+ * SuperClass for Javascript/CSS combiner classes.
+ *
+ * @package Joomla.Platform
+ * @subpackage Media
+ * @since 12.1
+ */
+abstract class JMediaCollection
+{
+ public $sources = array();
+
+ public $sourceCount = 0;
+
+ protected $combined = null;
+
+ protected $options = array();
+
/**
* @var array JMediaCombiner instances container.
* @since 11.1
- */
- protected static $instances = array();
-
- /**
- * Constructor
- *
- * @param Array $options options for the combiner
- *
- * @since 12.1
- */
- public function __construct($options = array())
- {
- // Merge user defined options with default options
- $this->_options = array_merge($this->_options, $options);
- }
-
+ */
+ protected static $instances = array();
+
+ /**
+ * Constructor
+ *
+ * @param Array $options options for the combiner
+ *
+ * @since 12.1
+ */
+ public function __construct($options = array())
+ {
+ // Merge user defined options with default options
+ $this->options = array_merge($this->options, $options);
+ }
+
+ /**
+ * Method to combine a set of files and save to single file.
+ *
+ * @return Void
+ *
+ * @since 12.1
+ */
+ public abstract function combine();
+
+
/**
* Method to set combiner options.
*
@@ -53,238 +63,177 @@ public function __construct($options = array())
* @return void
*
* @since 12.1
- */
- public function setOptions($options)
- {
- // Merge user defined options with default options
- $this->_options = array_merge($this->_options, $options);
- }
-
- /**
- * Method to set source files to combine
- *
- * @param array $files array of source files
- *
- * @throws RuntimeException
- *
- * @return void
- *
- * @since 12.1
- */
- public function setSources($files =array())
- {
- // Get combiner object type
- $type = $this->_options['type'];
-
- foreach ($files as $file)
- {
- // Check file ext for compability
- if (JFile::getExt($file) == $type)
- {
- // Check whether file already registered
- if (!in_array($file, $this->sources))
- {
- $this->sources[] = $file;
- $this->sourceCount++;
- }
- }
- else
- {
- throw new RuntimeException(JText::sprintf('JMEDIA_COMBINE_ERROR_MULTIPLE_FILE_TYPES'), $type);
- }
-
- }
- }
-
- /**
- * Method to get source files
- *
- * @return array Source File
- *
- * @since 12.1
- */
- public function getSources()
- {
- return $this->sources;
- }
-
- /**
- * Method to get combined string
- *
- * @return String Combined String
- */
- public function getCombined()
- {
- return $this->_combined;
- }
-
+ */
+ public function setOptions($options)
+ {
+ // Merge user defined options with default options
+ $this->options = array_merge($this->options, $options);
+ }
+
+ /**
+ * Method to set source files to combine
+ *
+ * @param array $files array of source files
+ *
+ * @throws RuntimeException
+ *
+ * @return void
+ *
+ * @since 12.1
+ */
+ public function addFiles($files =array())
+ {
+ // Get combiner object type
+ $type = $this->options['type'];
+
+ foreach ($files as $file)
+ {
+ // Check file ext for compatibility
+ if (JFile::getExt($file) == $type)
+ {
+ // Check whether file already registered
+ if (!in_array($file, $this->sources))
+ {
+ $this->sources[] = $file;
+ $this->sourceCount++;
+ }
+ }
+ else
+ {
+ throw new RuntimeException(sprintf("Multiple File types detected in files array."), $type);
+ }
+
+ }
+ }
+
/**
- * Get a list of available combiners
+ * Method to get source files
+ *
+ * @return array Source File
+ *
+ * @since 12.1
+ */
+ public function getSources()
+ {
+ return $this->sources;
+ }
+
+ /**
+ * Method to get combined string
+ *
+ * @return String Combined String
+ */
+ public function getCombined()
+ {
+ if ($this->combined == null)
+ {
+ $this->combine();
+ }
+ return $this->combined;
+ }
+
+ /**
+ * Get a list of available collection classes
+ *
+ * @return array An array of available collection classes
*
- * @return array An array of available combiners
+ * @since 12.1
+ */
+ public static function getCollectionTypes()
+ {
+ // Instantiate variables.
+ $combiners = array();
+
+ // Get a list of types.
+ $types = JFolder::files(__DIR__ . '/collection');
+
+ // Loop through the types and find the ones that are available.
+ foreach ($types as $type)
+ {
+ // Ignore some files.
+ if ($type == 'index.html')
+ {
+ continue;
+ }
+
+ // Derive the class name from the type.
+ $class = str_ireplace('.php', '', trim($type));
+
+ // If the class doesn't exist we have nothing left to do but look at the next type. We did our best.
+ if (!class_exists('JMediaCollection' . ucfirst($class)))
+ {
+ continue;
+ }
+
+ // Combiner names should not have file extensions.
+ $combiners[] = $class;
+
+ }
+
+ return $combiners;
+ }
+
+ /**
+ * Method to get options
+ *
+ * @return array Options for the collection object
*
* @since 12.1
- */
- public static function getCombiners()
- {
- // Instantiate variables.
- $combiners = array();
-
- // Get a list of types.
- $types = JFolder::files(__DIR__ . '/combiner');
-
- // Loop through the types and find the ones that are available.
- foreach ($types as $type)
- {
- // Ignore some files.
- if ($type == 'index.html')
- {
- continue;
- }
-
- // Derive the class name from the type.
- $class = str_ireplace('.php', '', trim($type));
-
- // If the class doesn't exist we have nothing left to do but look at the next type. We did our best.
- if (!class_exists('JMediaCombiner' . ucfirst($class)))
- {
- continue;
- }
-
- // Combiner names should not have file extensions.
- $combiners[] = $class;
-
- }
-
- return $combiners;
- }
-
+ */
+ public function getOptions()
+ {
+ return $this->options;
+ }
+
/**
- * Method to get combiner options
+ * Gives a collection object for CSS/JS
+ *
+ * @param array $options options for the compressor
*
- * @return array Options for the combinor
+ * @return JMediaCollection Returns a JMediaCollection object
*
* @since 12.1
- */
- public function getOptions()
- {
- return $this->_options;
- }
-
- /**
- * Gives a combiner object for CSS/JS
- *
- * @param array $options options for the compressor
- *
- * @return JMediaCombiner Returns a JMediaCombiner object
- *
- * @since 12.1
- */
- public static function getInstance( $options = array())
- {
-
- // Get the options signature for the database connector.
- $signature = md5(serialize($options));
-
- // If we already have a database connector instance for these options then just use that.
- if (empty(self::$instances[$signature]))
- {
- // Derive the class name from the type.
- $class = 'JMediaCombiner' . ucfirst(strtolower($options['type']));
-
- // If the class still doesn't exist we have nothing left to do but throw an exception. We did our best.
- if (!class_exists($class))
- {
- throw new RuntimeException(JText::sprintf('JMEDIA_ERROR_LOAD_COMBINER', $options['type']));
- }
-
- // Create our new JMediaCompressor class based on the options given.
- try
- {
- $instance = new $class($options);
- }
- catch (RuntimeException $e)
- {
- throw new RuntimeException(JText::sprintf('JLIB_DATABASE_ERROR_CONNECT_DATABASE', $e->getMessage()));
- }
-
- // Set the new combinerr to the global instances based on signature.
- self::$instances[$signature] = $instance;
- }
- else
- {
- $instance = self::$instances[$signature];
- $instance->clear();
- }
-
- return self::$instances[$signature];
- }
-
- /**
- * Static method to get a set of files combined
- *
- * @param array $files Set of source files
- * @param array $options Options for combiner
- * @param string $destination Destination file
- *
- * @return boolean True on success
- *
- * @since 12.1
- */
- public static function combineFiles($files, $options = array(), $destination = null)
- {
- // Detect file type
- $type = JFile::getExt($files[0]);
-
- if (!self::isSupported($files[0]))
- {
- throw new RuntimeException(JText::sprintf('JMEDIA_ERROR_FILE_TYPE_NOT_SUPPORTED'));
- }
-
- // Checks for the destination
- if ($destination === null)
- {
- $type = $extension = pathinfo($files[0], PATHINFO_EXTENSION);
-
- // Check for the file prefix in options, assign default prefix if not dound
- if (array_key_exists('PREFIX', $options) && !empty($options['PREFIX']))
- {
- $destination = str_ireplace('.' . $type, '.' . $options['PREFIX'] . '.' . $type, $files[0]);
- }
- else
- {
- $destination = str_ireplace('.' . $type, '.combined.' . $type, $files[0]);
- }
- }
-
- $options['type'] ? $options['type']: $type;
-
- $combiner = self::getInstance($options);
-
- $combiner->setSources($files);
-
- $combiner->combine();
-
- if (!empty($combiner->_combined))
- {
- $force = array_key_exists('overwrite', $options) && !empty($options['overwrite']) ? $options['overwrite'] : false;
-
- if (!JFile::exists($destination) || (JFile::exists($destination) && $force))
- {
- JFile::write($destination, $combiner->getCombined());
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
-
+ */
+ public static function getInstance( $options = array())
+ {
+
+ // Get the options signature for the database connector.
+ $signature = md5(serialize($options));
+
+ // If we already have a database connector instance for these options then just use that.
+ if (empty(self::$instances[$signature]))
+ {
+ // Derive the class name from the type.
+ $class = 'JMediaCollection' . ucfirst(strtolower($options['type']));
+
+ // If the class still doesn't exist we have nothing left to do but throw an exception. We did our best.
+ if (!class_exists($class))
+ {
+ throw new RuntimeException(sprintf("Error Loading Collection class for %s file type", $options['type']));
+ }
+
+ // Create our new JMediaCompressor class based on the options given.
+ try
+ {
+ $instance = new $class($options);
+ }
+ catch (RuntimeException $e)
+ {
+ throw new RuntimeException(sprintf("Error Loading Collection class for %s file type", $e->getMessage()));
+ }
+
+ // Set the new combiner to the global instances based on signature.
+ self::$instances[$signature] = $instance;
+ }
+ else
+ {
+ $instance = self::$instances[$signature];
+ $instance->clear();
+ }
+
+ return self::$instances[$signature];
+ }
+
/**
* Method to test if supported
*
@@ -293,33 +242,33 @@ public static function combineFiles($files, $options = array(), $destination = n
* @return boolean true or false
*
* @since 12.1
- */
- public static function isSupported($sourceFile)
- {
- $combiners = self::getCombiners();
-
- foreach ($combiners as $class)
- {
- if (strtolower(str_ireplace('JMediaCombiner', '', $class)) === strtolower(JFile::getExt($sourceFile)))
- {
- return true;
- }
- }
-
- return true;
- }
-
+ */
+ public static function isSupported($sourceFile)
+ {
+ $combiners = self::getCollectionTypes();
+
+ foreach ($combiners as $class)
+ {
+ if (strtolower(str_ireplace('JMediaCollection', '', $class)) === strtolower(JFile::getExt($sourceFile)))
+ {
+ return true;
+ }
+ }
+
+ return true;
+ }
+
/**
* Method to clear combiner data
*
* @return void
*
* @since 12.1
- */
- public function clear()
- {
- $this->sources = array();
- $this->sourceCount = 0;
- $this->_combined = null;
- }
-}
+ */
+ public function clear()
+ {
+ $this->sources = array();
+ $this->sourceCount = 0;
+ $this->combined = null;
+ }
+}
View
72 libraries/joomla/media/collection/css.php
@@ -0,0 +1,72 @@
+<?php
+/**
+ * @package Joomla.Platform
+ * @subpackage Media
+ *
+ * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
+ * @license GNU General Public License version 2 or later; see LICENSE
+ */
+
+defined('JPATH_PLATFORM') or die;
+
+/**
+ * CSS combiner classes.
+ *
+ * @package Joomla.Platform
+ * @subpackage Media
+ * @since 12.1
+ */
+class JMediaCollectionCss extends JMediaCollection
+{
+ /**
+ * Constructor
+ *
+ * @param Array $options options
+ *
+ * @since 12.1
+ */
+ public function __construct($options = array())
+ {
+ $this->_options = array('COMPRESS' => false, 'FILE_COMMENTS' => true, 'COMPRESS_OPTIONS' => array());
+ parent::__construct($options);
+ }
+
+ /**
+ * Method to combine a set of files and save to single file.
+ *
+ * @return Void
+ *
+ * @since 12.1
+ */
+ public function combine()
+ {
+ $this->combined = '';
+
+ foreach ($this->sources as $file)
+ {
+ if ($this->_options['FILE_COMMENTS'])
+ {
+ $this->combined .= '/** File : ' . JFile::getName($file) . ' : Start **/' . "\n\n";
+ }
+
+ if ($this->_options['COMPRESS'])
+ {
+ $this->_options['COMPRESS_OPTIONS']['type'] = 'css';
+
+ $this->combined .= JMediaCompressor::compressString(JFile::read($file), $this->_options['COMPRESS_OPTIONS']) . "\n\n";
+ }
+ else
+ {
+ $this->combined .= JFile::read($file) . "\n\n";
+ }
+
+ if ($this->_options['FILE_COMMENTS'])
+ {
+ $this->combined .= '/** File : ' . JFile::getName($file) . ' : End **/' . "\n\n";
+ }
+ }
+
+ $this->combined .= '/** ' . $this->sourceCount . ' css files are combined **/';
+
+ }
+}
View
72 libraries/joomla/media/collection/js.php
@@ -0,0 +1,72 @@
+<?php
+/**
+ * @package Joomla.Platform
+ * @subpackage Media
+ *
+ * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
+ * @license GNU General Public License version 2 or later; see LICENSE
+ */
+
+defined('JPATH_PLATFORM') or die;
+
+/**
+ * Javascript combiner class.
+ *
+ * @package Joomla.Platform
+ * @subpackage Compress
+ * @since 12.1
+ */
+class JMediaCollectionJs extends JMediaCollection
+{
+
+ /**
+ * Constructor
+ *
+ * @param Array $options options
+ *
+ * @since 12.1
+ */
+ public function __construct($options = array())
+ {
+ $this->_options = array('COMPRESS' => false, 'FILE_COMMENTS' => true, 'COMPRESS_OPTIONS' => array());
+ parent::__construct($options);
+ }
+
+ /**
+ * Method to combine a set of files and save to single file.
+ *
+ * @since 12.1
+ *
+ * @return void
+ */
+ public function combine()
+ {
+ $this->combined = '';
+
+ foreach ($this->sources as $file)
+ {
+ if ($this->_options['FILE_COMMENTS'])
+ {
+ $this->combined .= '/** File : ' . JFile::getName($file) . ' : Start **/' . "\n\n";
+ }
+
+ if ($this->_options['COMPRESS'])
+ {
+ $this->_options['COMPRESS_OPTIONS']['type'] = 'js';
+
+ $this->combined .= JMediaCompressor::compressString(JFile::read($file), $this->_options['COMPRESS_OPTIONS']) . "\n\n";
+ }
+ else
+ {
+ $this->combined .= JFile::read($file) . "\n\n";
+ }
+
+ if ($this->_options['FILE_COMMENTS'])
+ {
+ $this->combined .= '/** File : ' . JFile::getName($file) . ' : End **/' . "\n\n";
+ }
+ }
+
+ $this->combined .= '/** ' . $this->sourceCount . ' js files are combined **/';
+ }
+}
View
795 libraries/joomla/media/compressor.php
@@ -1,393 +1,402 @@
-<?php
-/**
- * @package Joomla.Platform
- * @subpackage Media
- *
- * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
- * @license GNU General Public License version 2 or later; see LICENSE
- */
-
-defined('JPATH_PLATFORM') or die;
-
-/**
- * Javascript and CSS Compressor Class.
- *
- * @package Joomla.Platform
- * @subpackage Media
- * @since 12.1
- */
-abstract class JMediaCompressor
-{
- /**
- * @var String To hold uncompressed Code.
- * @since 12.1
- */
- public $_uncompressed = null;
-
- /**
- * @var int size of uncompressed Code.
- * @since 12.1
- */
- public $_uncompressedSize = null;
-
- /**
- * @var String To hold compressed Code.
- * @since 12.1
- */
- protected $_compressed = null;
-
- /**
- * @var int size of compressed Code.
- * @since 12.1
- */
- public $_compressedSize = null;
-
- /**
- * @var Array Compression options for CSS Minifier.
- * @since 12.1
- */
- protected $_options = array();
-
- /**
- * @var array JMediaCompressor instances container.
- * @since 11.1
- */
- protected static $instances = array();
-
- /**
- * Method to set uncompressed code.
- *
- * @param string $uncompressed Uncomressed Code.
- *
- * @return void
- *
- * @since 12.1
- */
- public function setUncompressed($uncompressed)
- {
- $this->_uncompressed = $uncompressed;
- $this->_uncompressedSize = strlen($this->_uncompressed);
- }
-
- /**
- * Method to get uncompressed code.
- *
- * @return String uncompressed code.
- *
- * @since 12.1
- */
- public function getUncompressed()
- {
- return $this->_uncompressed;
- }
-
- /**
- * Method to set uncompressed code.
- *
- * @param string $compressed compressed Code.
- *
- * @return void
- *
- * @since 12.1
- */
- public function setCompressed($compressed)
- {
- $this->_compressed = $compressed;
- $this->_compressedSize = strlen($this->_compressed);
- }
-
- /**
- * Method to get compressed code.
- *
- * @return String compressed code.
- *
- * @since 12.1
- */
- public function getCompressed()
- {
- return $this->_compressed;
- }
-
- /**
- * Method to set compression options.
- *
- * @param Array $options options to compress.
- *
- * @return void
- *
- * @since 12.1
- */
- public function setOptions($options)
- {
- // Merge user defined options with default options
- $this->_options = array_merge($this->_options, $options);
- }
-
- /**
- * Object Constructor takes two parameters.
- *
- * @param Array $options Compression options for Minifier.
- *
- * @since 12.1
- */
- public function __construct($options = array())
- {
- // Merge user defined options with default options
- $this->_options = array_merge($this->_options, $options);
- }
-
- /**
- * Method to get compressed ratio.
- *
- * @return double Compressed ratio.
- *
- * @since 12.1
- */
- public function getRatio()
- {
- return round(($this->_compressedSize / $this->_uncompressedSize * 100), 2);
- }
-
- /**
- * Get a list of available compressors
- *
- * @return array An array of available compressors
- *
- * @since 11.1
- */
- public static function getCompressors()
- {
- // Instantiate variables.
- $compressors = array();
-
- // Get a list of types.
- $types = JFolder::files(__DIR__ . '/compressor');
-
- // Loop through the types and find the ones that are available.
- foreach ($types as $type)
- {
- // Ignore some files.
- if ($type == 'index.html')
- {
- continue;
- }
-
- // Derive the class name from the type.
- $class = str_ireplace('.php', '', trim($type));
-
- // If the class doesn't exist we have nothing left to do but look at the next type. We did our best.
- if (!class_exists('JMediaCompressor' . ucfirst($class)))
- {
- continue;
- }
-
- // Compressor names should not have file extensions.
- $compressors[] = $class;
-
- }
-
- return $compressors;
- }
-
- /**
- * Method to get compressor options
- *
- * @return array Options for the compressor
- *
- * @since 12.1
- */
- public function getOptions()
- {
- return $this->_options;
- }
-
- /**
- * Compress a CSS/JS file with given options
- *
- * @param string $uncompressed The String to be compressed
- * @param array $options An asssociative array with options. Eg: type, force overwirte, prefix for minified files
- *
- * @return string compressed string
- *
- * @since 12.1
- */
- public static function compressString( $uncompressed, $options)
- {
- if (!array_key_exists('type', $options))
- {
- throw new RuntimeException(JText::sprintf('JMEDIA_ERROR_COMPRESSOR_TYPE_NOT_DEFINED'));
- }
- $compressor = self::getInstance($options);
- $compressor->clear();
- $compressor->setUncompressed($uncompressed);
-
- try
- {
- $compressor->compress();
- }
- catch (Exception $e)
- {
- return false;
- }
- return $compressor->getCompressed();
- }
-
- /**
- * Compress a CSS/JS file with given options
- *
- * @param string $sourcefile The full file path of the source file.
- * @param array $options An asssociative array with options. Eg: type, force overwirte, prefix for minified files
- * @param string $destination The full file path of the destination file. If left empty the compressed file will be returned as string
- *
- * @return boolean false on failure.
- *
- * @since 12.1
- */
- public static function compressFile( $sourcefile, $options = array(), $destination = null )
- {
- $options['type'] = strtolower(JFile::getExt($sourcefile));
-
- if (!self::isSupported($sourcefile))
- {
- throw new RuntimeException(JText::sprintf('JMEDIA_ERROR_FILE_TYPE_NOT_SUPPORTED'));
- }
- $compressor = self::getInstance($options);
- $uncompressed = JFile::read($sourcefile);
-
- if ($destination === null)
- {
- $type = $extension = pathinfo($sourcefile, PATHINFO_EXTENSION);
- if (array_key_exists('PREFIX', $options) && !empty($options['PREFIX']))
- {
- $destination = str_ireplace('.' . $type, '.' . $options['PREFIX'] . '.' . $type, $sourcefile);
- }
- else
- {
- $destination = str_ireplace('.' . $type, '.min.' . $type, $sourcefile);
- }
- }
-
- if (!$uncompressed)
- {
- throw new JMediaException("Error reading the file (" . $sourcefile . ") contents");
- }
-
- $compressor->setUncompressed($uncompressed);
-
- try
- {
- $compressor->compress();
- }
- catch (Exception $e)
- {
- return false;
- }
-
- // Sets force overwrite option
- $force = array_key_exists('overwrite', $options) && !empty($options['overwrite']) ? $options['overwrite'] : false;
-
- if (!JFile::exists($destination) || (JFile::exists($destination) && $force))
- {
- if (JFile::write($destination, $compressor->getCompressed()))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
-
- /**
- * Gives a compressor object for CSS/JS
- *
- * @param array $options options for the compressor
- *
- * @return JMediaCompressor Returns a JMediaCompressor object
- *
- * @since 12.1
- */
- public static function getInstance($options = array())
- {
-
- // Get the options signature for the compressor.
- $signature = md5(serialize($options));
-
- // If we already have a compressor instance for these options then just use that.
- if (empty(self::$instances[$signature]))
- {
- // Derive the class name from the type.
- $class = 'JMediaCompressor' . ucfirst(strtolower($options['type']));
-
- // If the class still doesn't exist we have nothing left to do but throw an exception. We did our best.
- if (!class_exists($class))
- {
- throw new RuntimeException(JText::sprintf('JMEDIA_ERROR_LOAD_COMPRESSOR', $options['type']));
- }
-
- // Create our new JMediaCompressor class based on the options given.
- try
- {
- $instance = new $class($options);
- }
- catch (RuntimeException $e)
- {
- throw new RuntimeException(JText::sprintf('JLIB_DATABASE_ERROR_CONNECT_DATABASE', $e->getMessage()));
- }
-
- // Set the new connector to the global instances based on signature.
- self::$instances[$signature] = $instance;
- }
- else
- {
- $instance = self::$instances[$signature];
- $instance->clear();
- }
-
- return self::$instances[$signature];
- }
-
- /**
- * Method to test if supported
- *
- * @param string $sourceFile file to test
- *
- * @return boolean true or false
- *
- * @since 12.1
- */
- public static function isSupported($sourceFile)
- {
- $compressors = self::getCompressors();
-
- foreach ($compressors as $class)
- {
- if (strtolower(str_ireplace('JMediaCompressor', '', $class)) === strtolower(JFile::getExt($sourceFile)))
- {
- return true;
- }
- }
-
- return false;
- }
-
- /**
- * Method to clear compressor data
- *
- * @return void
- *
- * @since 12.1
- */
- public function clear()
- {
- $this->_compressed = null;
- $this->_compressedSize = null;
- $this->_uncompressed = null;
- $this->_uncompressedSize = null;
- }
-}
+<?php
+/**
+ * @package Joomla.Platform
+ * @subpackage Media
+ *
+ * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
+ * @license GNU General Public License version 2 or later; see LICENSE
+ */
+
+defined('JPATH_PLATFORM') or die;
+
+/**
+ * Javascript and CSS Compressor Class.
+ *
+ * @package Joomla.Platform
+ * @subpackage Media
+ * @since 12.1
+ */
+abstract class JMediaCompressor
+{
+ /**
+ * @var String To hold uncompressed Code.
+ * @since 12.1
+ */
+ public $uncompressed = null;
+
+ /**
+ * @var int size of uncompressed Code.
+ * @since 12.1
+ */
+ public $uncompressedSize = null;
+
+ /**
+ * @var String To hold compressed Code.
+ * @since 12.1
+ */
+ protected $compressed = null;
+
+ /**
+ * @var int size of compressed Code.
+ * @since 12.1
+ */
+ public $compressedSize = null;
+
+ /**
+ * @var Array Compression options for CSS Minifier.
+ * @since 12.1
+ */
+ protected $options = array();
+
+ /**
+ * @var array JMediaCompressor instances container.
+ * @since 11.1
+ */
+ protected static $instances = array();
+
+ /**
+ * Object Constructor takes two parameters.
+ *
+ * @param Array $options Compression options for Minifier.
+ *
+ * @since 12.1
+ */
+ public function __construct($options = array())
+ {
+ // Merge user defined options with default options
+ $this->options = array_merge($this->options, $options);
+ }
+
+ /**
+ * Method to compress the code.
+ *
+ * @return Void
+ *
+ * @since 12.1
+ */
+ public abstract function compress();
+
+ /**
+ * Method to set uncompressed code.
+ *
+ * @param string $uncompressed Uncomressed Code.
+ *
+ * @return void
+ *
+ * @since 12.1
+ */
+ public function setUncompressed($uncompressed)
+ {
+ $this->uncompressed = $uncompressed;
+ $this->uncompressedSize = strlen($this->uncompressed);
+ }
+
+ /**
+ * Method to get uncompressed code.
+ *
+ * @return String uncompressed code.
+ *
+ * @since 12.1
+ */
+ public function getUncompressed()
+ {
+ return $this->uncompressed;
+ }
+
+ /**
+ * Method to set uncompressed code.
+ *
+ * @param string $compressed compressed Code.
+ *
+ * @return void
+ *
+ * @since 12.1
+ */
+ public function setCompressed($compressed)
+ {
+ $this->compressed = $compressed;
+ $this->compressedSize = strlen($this->compressed);
+ }
+
+ /**
+ * Method to get compressed code.
+ *
+ * @return String compressed code.
+ *
+ * @since 12.1
+ */
+ public function getCompressed()
+ {
+ return $this->compressed;
+ }
+
+ /**
+ * Method to set compression options.
+ *
+ * @param Array $options options to compress.
+ *
+ * @return void
+ *
+ * @since 12.1
+ */
+ public function setOptions($options)
+ {
+ // Merge user defined options with default options
+ $this->options = array_merge($this->options, $options);
+ }
+
+ /**
+ * Method to get compressed ratio.
+ *
+ * @return double Compressed ratio.
+ *
+ * @since 12.1
+ */
+ public function getRatio()
+ {
+ return round(($this->compressedSize / $this->uncompressedSize * 100), 2);
+ }
+
+ /**
+ * Get a list of available compressors
+ *
+ * @return array An array of available compressors
+ *
+ * @since 11.1
+ */
+ public static function getCompressors()
+ {
+ // Instantiate variables.
+ $compressors = array();
+
+ // Get a list of types.
+ $types = JFolder::files(__DIR__ . '/compressor');
+
+ // Loop through the types and find the ones that are available.
+ foreach ($types as $type)
+ {
+ // Ignore some files.
+ if ($type == 'index.html')
+ {
+ continue;
+ }
+
+ // Derive the class name from the type.
+ $class = str_ireplace('.php', '', trim($type));
+
+ // If the class doesn't exist we have nothing left to do but look at the next type. We did our best.
+ if (!class_exists('JMediaCompressor' . ucfirst($class)))
+ {
+ continue;
+ }
+
+ // Compressor names should not have file extensions.
+ $compressors[] = $class;
+
+ }
+
+ return $compressors;
+ }
+
+ /**
+ * Method to get compressor options
+ *
+ * @return array Options for the compressor
+ *
+ * @since 12.1
+ */
+ public function getOptions()
+ {
+ return $this->options;
+ }
+
+ /**
+ * Compress a CSS/JS file with given options
+ *
+ * @param string $uncompressed The String to be compressed
+ * @param array $options An asssociative array with options. Eg: type, force overwirte, prefix for minified files
+ *
+ * @return string compressed string
+ *
+ * @since 12.1
+ */
+ public static function compressString( $uncompressed, $options)
+ {
+ if (!array_key_exists('type', $options))
+ {
+ throw new RuntimeException(sprintf("File Type is not defined in options array"));
+ }
+ $compressor = self::getInstance($options);
+ $compressor->clear();
+ $compressor->setUncompressed($uncompressed);
+
+ try
+ {
+ $compressor->compress();
+ }
+ catch (Exception $e)
+ {
+ return false;
+ }
+ return $compressor->getCompressed();
+ }
+
+ /**
+ * Compress a CSS/JS file with given options
+ *
+ * @param string $sourcefile The full file path of the source file.
+ * @param array $options An asssociative array with options. Eg: type, force overwirte, prefix for minified files
+ * @param string $destination The full file path of the destination file. If left empty the compressed file will be returned as string
+ *
+ * @return boolean false on failure.
+ *
+ * @since 12.1
+ */
+ public static function compressFile( $sourcefile, $options = array(), $destination = null )
+ {
+ $options['type'] = strtolower(JFile::getExt($sourcefile));
+
+ if (!self::isSupported($sourcefile))
+ {
+ throw new RuntimeException(sprintf("The file type of the source file is not supported by the Compressors"));
+ }
+ $compressor = self::getInstance($options);
+ $uncompressed = JFile::read($sourcefile);
+
+ if ($destination === null)
+ {
+ $type = $extension = pathinfo($sourcefile, PATHINFO_EXTENSION);
+ if (array_key_exists('PREFIX', $options) && !empty($options['PREFIX']))
+ {
+ $destination = str_ireplace('.' . $type, '.' . $options['PREFIX'] . '.' . $type, $sourcefile);
+ }
+ else
+ {
+ $destination = str_ireplace('.' . $type, '.min.' . $type, $sourcefile);
+ }
+ }
+
+ if (!$uncompressed)
+ {
+ throw new Exception("Error reading the file (" . $sourcefile . ") contents");
+ }
+
+ $compressor->setUncompressed($uncompressed);
+
+ try
+ {
+ $compressor->compress();
+ }
+ catch (Exception $e)
+ {
+ return false;
+ }
+
+ // Sets force overwrite option
+ $force = array_key_exists('overwrite', $options) && !empty($options['overwrite']) ? $options['overwrite'] : false;
+
+ if (!JFile::exists($destination) || (JFile::exists($destination) && $force))
+ {
+ if (JFile::write($destination, $compressor->getCompressed()))
+ {
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+ }
+ else
+ {
+ return false;
+ }
+ }
+
+ /**
+ * Gives a compressor object for CSS/JS
+ *
+ * @param array $options options for the compressor
+ *
+ * @return JMediaCompressor Returns a JMediaCompressor object
+ *
+ * @since 12.1
+ */
+ public static function getInstance($options = array())
+ {
+
+ // Get the options signature for the compressor.
+ $signature = md5(serialize($options));
+
+ // If we already have a compressor instance for these options then just use that.
+ if (empty(self::$instances[$signature]))
+ {
+ // Derive the class name from the type.
+ $class = 'JMediaCompressor' . ucfirst(strtolower($options['type']));
+
+ // If the class still doesn't exist we have nothing left to do but throw an exception. We did our best.
+ if (!class_exists($class))
+ {
+ throw new RuntimeException(sprintf("Error Loading Compressor class for %s file type", $options['type']));
+ }
+
+ // Create our new JMediaCompressor class based on the options given.
+ try
+ {
+ $instance = new $class($options);
+ }
+ catch (RuntimeException $e)
+ {
+ throw new RuntimeException(sprintf("Error Loading Collection class for %s file type", $e->getMessage()));
+ }
+
+ // Set the new connector to the global instances based on signature.
+ self::$instances[$signature] = $instance;
+ }
+ else
+ {
+ $instance = self::$instances[$signature];
+ $instance->clear();
+ }
+
+ return self::$instances[$signature];
+ }
+
+ /**
+ * Method to test if supported
+ *
+ * @param string $sourceFile file to test
+ *
+ * @return boolean true or false
+ *
+ * @since 12.1
+ */
+ public static function isSupported($sourceFile)
+ {
+ $compressors = self::getCompressors();
+
+ foreach ($compressors as $class)
+ {
+ if (strtolower(str_ireplace('JMediaCompressor', '', $class)) === strtolower(JFile::getExt($sourceFile)))
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Method to clear compressor data
+ *
+ * @return void
+ *
+ * @since 12.1
+ */
+ public function clear()
+ {
+ $this->compressed = null;
+ $this->compressedSize = null;
+ $this->uncompressed = null;
+ $this->uncompressedSize = null;
+ }
+}
View
530 libraries/joomla/media/compressor/css.php
@@ -1,271 +1,271 @@
-<?php
-/**
- * @package Joomla.Platform
- * @subpackage Media
- *
- * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
- * @license GNU General Public License version 2 or later; see LICENSE
- */
-
-defined('JPATH_PLATFORM') or die;
-
-/**
- * CSS Compressor Class.
- *
- * @package Joomla.Platform
- * @subpackage Media
- * @since 12.1
- */
-class JMediaCompressorCss extends JMediaCompressor
-{
- /**
- * Object constructor
- *
- * @param Array $options Compression options for CSS Minifier.
- *
- * @since 12.1
- */
- public function __construct($options = array())
- {
- $this->_options = array('REMOVE_COMMENTS' => true, 'MIN_COLOR_CODES' => true, 'LIMIT_LINE_LENGTH' => true);
- parent::__construct($options);
- }
-
- /**
- * Method to compress the code.
- *
- * @return Void
- *
- * @since 12.1
- */
- public function compress()
- {
- if ($this->_uncompressed === null)
- {
- throw new RuntimeException(JText::sprintf('JMEDIA_CSS_COMPRESSION_ERROR_UNCOMPRESSED_NOTSET'));
- }
-
- $this->_compressed = str_replace("\r\n", "\n", $this->_uncompressed);
-
- $this->_compressed = $this->_preServe($this->_compressed);
-
- /* Process all valid comments and apply call back, handleComments() function will return relavent replacements
- * Second argument is to tell call $this->_handleComments() method and get replacement patterns for matches
- * Delimiter '~' is used because using '/' will make this regex pattern ambigious
- */
- $this->_compressed = preg_replace_callback('~\\s*/\\*([\\s\\S]*?)\\*/\\s*~', array($this,'_handleComments'), $this->_compressed);
-
- $this->_compressed = $this->_removeWS($this->_compressed);
-
- // Handle selectors - match a start of a selector and pass them to $this->_handleSelectors() to get replacements
- // /x is used turn on free-spacing mode in regex patterns
- $this->_compressed = preg_replace_callback('/(?:\\s*[^~>+,\\s]+\\s*[,>+~])+\\s*[^~>+,\\s]+{/', array($this,'_handleSelectors'), $this->_compressed);
-
- if ($this->_options['MIN_COLOR_CODES'])
- {
- $this->_compressed = $this->_minColorCodes($this->_compressed);
- }
-
- if ($this->_options['LIMIT_LINE_LENGTH'])
- {
- $this->_compressed = $this->_breakInToLines($this->_compressed);
- }
-
- $this->_compressed = preg_replace('/:first-l(etter|ine)\\{/', ':first-l$1 {', $this->_compressed);
-
- $this->_compressed = trim($this->_compressed);
-
- $this->_compressedSize = strlen($this->_compressed);
- }
-
- /**
- * Method to preserve special browser hacks - Will add 'keep' word infront of comments
- *
- * @param string $source source css code
- *
- * @return string modified css code
- *
- * @since 12.1
- */
- private function _preServe($source)
- {
- // Preserve empty comment after '>'
- $patterns [] = '~>/\\*\\s*\\*/~';
- $replacements[] = '>/*keep*/';
-
- // Preserve empty comment between property and value
- $patterns [] = '~/\\*\\s*\\*/\\s*:~';
- $replacements[] = '/*keep*/:';
-
- $patterns [] = '~:\\s*/\\*\\s*\\*/~';
- $replacements[] = ':/*keep*/';
-
- return preg_replace($patterns, $replacements, $source);
- }
-
- /**
- * Method to detect which replacement patterne to use for identified comments
- *
- * @param Array $matches bacreferences from preg_replace_callback()
- *
- * @return string replacements for comments
- *
- * @since 12.1
- */
- private function _handleComments($matches)
- {
- // Do not replace the preserved and need to keep comments
- if ($matches[1] === 'keep')
- {
- return '/**/';
- }
-
- // Replacement for css mid pass filters
- if ($matches[1] === '" "')
- {
- return '/*" "*/';
- }
-
- // Replacement for css mid pass filters
- if (preg_match('@";\\}\\s*\\}/\\*\\s+@', $matches[1]))
- {
- return '/*";}}/* */';
- }
-
- // Keep any surrounding white space
- if ((trim($matches[0]) !== $matches[1]))
- {
- return ' ';
- }
-
- return '';
- }
-
- /**
- * Method to process css selectors and identify replacements
- *
- * @param array $matches bacreferences from preg_replace_callback()
- *
- * @return String replacements for selectors
- *
- * @since 12.1
- */
- private function _handleSelectors($matches)
- {
- // Remove space around combinators
- return preg_replace('/\\s*([,>+~])\\s*/', '$1', $matches[0]);
- }
-
- /**
- * Method to remove unnecessary white spaces
- *
- * @param string $source source css code
- *
- * @return string white space removed css code
- *
- * @since 12.1
- */
- private function _removeWS($source)
- {
- // Remove spaces around ;
- $patterns[] = '/\\s*;\\s*/';
-
- $replacements[] = ';';
-
- // Remove spaces around {} and final ; inside {}
- $patterns[] = '/\\s*{\\s*/';
-
- $replacements[] = '{';
-
- $patterns[] = '/;?\\s*}\\s*/';
-
- $replacements[] = '}';
-
- // Remove spaces around urls
- // X is used turn on free-spacing mode in regex patterns
- $patterns[] = '/url\\( # url(
- \\s*
- ([^\\)]+?) # match 1 is url
- \\s*
- \\) # )
- /x';
-
- $replacements[] = 'url($1)';
-
- $patterns[] = '/@import\\s+url/';
-
- $replacements[] = '@import url';
-
- // Remove tabs and spaces around a new line
-
- $patterns[] = '/[ \\t]*\\n+\\s*/';
-
- $replacements[] = "\n";
-
- // Remove spaces around css rules and colons
- // X is used turn on free-spacing mode in regex patterns
- $patterns[] = '/\\s* ([{;]) # match 1 = start of block
- \\s*
- ([\\*_]?[\\w\\-]+) # match 2 = property
- \\s* : \\s*
- (\\b|[#\'"-]) # match 3 = start of value
- /x';
-
- // Using backreferences 1, 2 and 3
- $replacements[] = '$1$2:$3';
-
- $tmp = preg_replace($patterns, $replacements, $source);
-
- return $tmp;
- }
-
- /**
- * Method to minimize colour codes
- *
- * @param string $source Source css code
- *
- * @return string modified css code
- *
- * @since 12.1
- */
- private function _minColorCodes($source)
- {
- return preg_replace('/([^=])#([a-f\\d])\\2([a-f\\d])\\3([a-f\\d])\\4([\\s;\\}])/i', '$1#$2$3$4$5', $source);
- }
-
- /**
- * Method to break minified code in to new lines to limit line lengths (optional)
- *
- * @param string $source Source css code
- *
- * @return string modified css code
- *
- * @since 12.1
- */
- private function _breakInToLines($source)
- {
- // Insert a newline between desendant selectors
- $source = preg_replace('/([\\w#\\.\\*]+)\\s+([\\w#\\.\\*]+){/', "$1\n$2{", $source);
-
- // Insert a new line after 1st numeric value found within a padding, margin, border or outline property
- $source = preg_replace('/
- ((?:padding|margin|border|outline):\\d+(?:px|em)?) # match 1 = 1st numeric string (eg: 10px)
- \\s+
- /x', "$1\n", $source
- );
- return $source;
- }
-
+<?php
+/**
+ * @package Joomla.Platform
+ * @subpackage Media
+ *
+ * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
+ * @license GNU General Public License version 2 or later; see LICENSE
+ */
+
+defined('JPATH_PLATFORM') or die;
+
+/**
+ * CSS Compressor Class.
+ *
+ * @package Joomla.Platform
+ * @subpackage Media
+ * @since 12.1
+ */
+class JMediaCompressorCss extends JMediaCompressor
+{
+ /**
+ * Object constructor
+ *
+ * @param Array $options Compression options for CSS Minifier.
+ *
+ * @since 12.1
+ */
+ public function __construct($options = array())
+ {
+ $this->_options = array('REMOVE_COMMENTS' => true, 'MIN_COLOR_CODES' => true, 'LIMIT_LINE_LENGTH' => true);
+ parent::__construct($options);
+ }
+
+ /**
+ * Method to compress the code.
+ *
+ * @return Void
+ *
+ * @since 12.1
+ */
+ public function compress()
+ {
+ if ($this->uncompressed === null)
+ {
+ throw new RuntimeException(JText::sprintf('JMEDIA_CSS_COMPRESSION_ERROR_UNCOMPRESSED_NOTSET'));
+ }
+
+ $this->compressed = str_replace("\r\n", "\n", $this->uncompressed);
+
+ $this->compressed = $this->_preServe($this->compressed);
+
+ /* Process all valid comments and apply call back, handleComments() function will return relavent replacements
+ * Second argument is to tell call $this->_handleComments() method and get replacement patterns for matches
+ * Delimiter '~' is used because using '/' will make this regex pattern ambigious
+ */
+ $this->compressed = preg_replace_callback('~\\s*/\\*([\\s\\S]*?)\\*/\\s*~', array($this,'_handleComments'), $this->compressed);
+
+ $this->compressed = $this->_removeWS($this->compressed);
+
+ // Handle selectors - match a start of a selector and pass them to $this->_handleSelectors() to get replacements
+ // /x is used turn on free-spacing mode in regex patterns
+ $this->compressed = preg_replace_callback('/(?:\\s*[^~>+,\\s]+\\s*[,>+~])+\\s*[^~>+,\\s]+{/', array($this,'_handleSelectors'), $this->compressed);
+
+ if ($this->_options['MIN_COLOR_CODES'])
+ {
+ $this->compressed = $this->_minColorCodes($this->compressed);
+ }
+
+ if ($this->_options['LIMIT_LINE_LENGTH'])
+ {
+ $this->compressed = $this->_breakInToLines($this->compressed);
+ }
+
+ $this->compressed = preg_replace('/:first-l(etter|ine)\\{/', ':first-l$1 {', $this->compressed);
+
+ $this->compressed = trim($this->compressed);
+
+ $this->compressedSize = strlen($this->compressed);
+ }
+
+ /**
+ * Method to preserve special browser hacks - Will add 'keep' word infront of comments
+ *
+ * @param string $source source css code
+ *
+ * @return string modified css code
+ *
+ * @since 12.1
+ */
+ private function _preServe($source)
+ {
+ // Preserve empty comment after '>'
+ $patterns [] = '~>/\\*\\s*\\*/~';
+ $replacements[] = '>/*keep*/';
+
+ // Preserve empty comment between property and value
+ $patterns [] = '~/\\*\\s*\\*/\\s*:~';
+ $replacements[] = '/*keep*/:';
+
+ $patterns [] = '~:\\s*/\\*\\s*\\*/~';
+ $replacements[] = ':/*keep*/';
+
+ return preg_replace($patterns, $replacements, $source);
+ }
+
+ /**
+ * Method to detect which replacement patterne to use for identified comments
+ *
+ * @param Array $matches bacreferences from preg_replace_callback()
+ *
+ * @return string replacements for comments
+ *
+ * @since 12.1
+ */
+ private function _handleComments($matches)
+ {
+ // Do not replace the preserved and need to keep comments
+ if ($matches[1] === 'keep')
+ {
+ return '/**/';
+ }
+
+ // Replacement for css mid pass filters
+ if ($matches[1] === '" "')
+ {
+ return '/*" "*/';
+ }
+
+ // Replacement for css mid pass filters
+ if (preg_match('@";\\}\\s*\\}/\\*\\s+@', $matches[1]))
+ {
+ return '/*";}}/* */';
+ }
+
+ // Keep any surrounding white space
+ if ((trim($matches[0]) !== $matches[1]))
+ {
+ return ' ';
+ }
+
+ return '';
+ }
+
+ /**
+ * Method to process css selectors and identify replacements
+ *
+ * @param array $matches bacreferences from preg_replace_callback()
+ *
+ * @return String replacements for selectors
+ *
+ * @since 12.1
+ */
+ private function _handleSelectors($matches)
+ {
+ // Remove space around combinators
+ return preg_replace('/\\s*([,>+~])\\s*/', '$1', $matches[0]);
+ }
+
+ /**
+ * Method to remove unnecessary white spaces
+ *
+ * @param string $source source css code
+ *
+ * @return string white space removed css code
+ *
+ * @since 12.1
+ */
+ private function _removeWS($source)
+ {
+ // Remove spaces around ;
+ $patterns[] = '/\\s*;\\s*/';
+
+ $replacements[] = ';';
+
+ // Remove spaces around {} and final ; inside {}
+ $patterns[] = '/\\s*{\\s*/';
+
+ $replacements[] = '{';
+
+ $patterns[] = '/;?\\s*}\\s*/';
+
+ $replacements[] = '}';
+
+ // Remove spaces around urls
+ // X is used turn on free-spacing mode in regex patterns
+ $patterns[] = '/url\\( # url(
+ \\s*
+ ([^\\)]+?) # match 1 is url
+ \\s*
+ \\) # )
+ /x';
+
+ $replacements[] = 'url($1)';
+
+ $patterns[] = '/@import\\s+url/';
+
+ $replacements[] = '@import url';
+
+ // Remove tabs and spaces around a new line
+
+ $patterns[] = '/[ \\t]*\\n+\\s*/';
+
+ $replacements[] = "\n";
+
+ // Remove spaces around css rules and colons
+ // X is used turn on free-spacing mode in regex patterns
+ $patterns[] = '/\\s* ([{;]) # match 1 = start of block
+ \\s*
+ ([\\*_]?[\\w\\-]+) # match 2 = property
+ \\s* : \\s*
+ (\\b|[#\'"-]) # match 3 = start of value
+ /x';
+
+ // Using backreferences 1, 2 and 3
+ $replacements[] = '$1$2:$3';
+
+ $tmp = preg_replace($patterns, $replacements, $source);
+
+ return $tmp;
+ }
+
+ /**
+ * Method to minimize colour codes
+ *
+ * @param string $source Source css code
+ *
+ * @return string modified css code
+ *
+ * @since 12.1
+ */
+ private function _minColorCodes($source)
+ {
+ return preg_replace('/([^=])#([a-f\\d])\\2([a-f\\d])\\3([a-f\\d])\\4([\\s;\\}])/i', '$1#$2$3$4$5', $source);
+ }
+
+ /**
+ * Method to break minified code in to new lines to limit line lengths (optional)
+ *
+ * @param string $source Source css code
+ *
+ * @return string modified css code
+ *
+ * @since 12.1
+ */
+ private function _breakInToLines($source)
+ {
+ // Insert a newline between desendant selectors
+ $source = preg_replace('/([\\w#\\.\\*]+)\\s+([\\w#\\.\\*]+){/', "$1\n$2{", $source);
+
+ // Insert a new line after 1st numeric value found within a padding, margin, border or outline property
+ $source = preg_replace('/
+ ((?:padding|margin|border|outline):\\d+(?:px|em)?) # match 1 = 1st numeric string (eg: 10px)
+ \\s+
+ /x', "$1\n", $source
+ );
+ return $source;
+ }
+
/**
* Method to clear compressor data
*
* @return void
*
* @since 12.1
- */
- public function clear()
- {
- parent::clear();
- }
-
-}
+ */
+ public function clear()
+ {
+ parent::clear();
+ }
+
+}
View
968 libraries/joomla/media/compressor/js.php
@@ -1,477 +1,477 @@
-<?php
-/**
- * @package Joomla.Platform
- * @subpackage Media
- *
- * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
- * @license GNU General Public License version 2 or later; see LICENSE
- */
-
-defined('JPATH_PLATFORM') or die;
-
-/**
- * Javascript Compressor Class.
- *
- * @package Joomla.Platform
- * @subpackage Media
- *
- * @since 12.1
- */
-class JMediaCompressorJs extends JMediaCompressor
-{
-
- /**
- * Used to track the index
- *
- * @var String
- * @since 12.1
- */
- private $_a = "\n";
-
- /**
- * Used to track the index.
- *
- *@var String
- *@since 12.1
- */
- private $_b = '';
-
- /**
- * Next available Index to process.
- *
- * @var int
- * @since 12.1
- */
- private $_nextIndex = 0;
-
- /**
- * length of uncompressed code after CR and LF chars are replaced with CR
- *
- * @var int
- * @since 12.1
- */
- private $_startLength = 0;
-
- /**
- * to hold a preloaded char to peek nextindex
- *
- * @var char
- * @since 12.1
- */
- private $_preLoaded = null;
-
- /**
- * last preocessed char used to identify keywords
- *
- * @var char
- * @since 12.1
- */
- private $_previousChar = null;
-
- /**
- * Object Constructor one parameters.
- *
- * @param Array $options Compression options for CSS Minifier.
- *
- * @since 12.1
- */
- public function __construct($options = array())
- {
- $this->_options = array('REMOVE_COMMENTS' => true, 'CHANGE_ENCODING' => true);
-
- parent::__construct($options);
- }
-
- /**
- * Method to compress the code.
- *
- * @return void
- *
- * @since 12.1
- */
- public function compress()
- {
- if ($this->_uncompressed === null)
- {
- throw new RuntimeException(JText::sprintf('JMEDIA_JS_COMPRESSION_ERROR_UNCOMPRESSED_NOTSET'));
- }
- $encoding = $this->_changeCharEncoding();
-
- $this->_uncompressed = str_replace("\r\n", "\n", $this->_uncompressed);
- $this->_startLength = strlen($this->_uncompressed);
-
- /* Commands to determine start point of switch in _executeCommand()
- * Command 1 : Keep A
- * Command 2 : Delete A
- * Command 3 : Delete A to B
- */
- $this->_executeCommand(3);
-
- while ($this->_a !== null)
- {
- $cmd = 1;
-
- if ($this->_a === ' ')
- {
- if (($this->_previousChar === '+' || $this->_previousChar === '-') && ($this->_b === $this->_previousChar))
- {
- // Do nothing
- }
- elseif (!$this->_checkAlphaNum($this->_b))
- {
- $cmd = 2;
- }
- }
- elseif ($this->_a === "\n")
- {
- if ($this->_b === ' ')
- {
- $cmd = 3;
- }
- elseif ( $this->_b === null || (strpos('{[(+-', $this->_b) === false && !$this->_checkAlphaNum($this->_b)))
- {
- $cmd = 2;
- }
- }
- elseif (!$this->_checkAlphaNum($this->_a))
- {
- if ($this->_b === ' ' || ($this->_b === "\n" && (false === strpos('}])+-"\'', $this->_a))))
- {
- $cmd = 3;
- }
- }
-
- $this->_executeCommand($cmd);
-
- }// End While
-
- // Resets multibyte encoding type
- if ($encoding !== null)
- {
- mb_internal_encoding($encoding);
- }
-
- $this->_compressed = trim($this->_compressed);
- $this->_compressedSize = strlen($this->_compressed);
- }
-
- /**
- * Method to execute commands
- *
- * @param int $cmd command number to execute
- *
- * @return void
- *
- * @throws JMediaException
- *
- * @since 12.1
- */
- private function _executeCommand($cmd)
- {
- // Prevent + + or - - becomes ++ or --
- if ($cmd === 3 && ($this->_a === '+' || $this->_a === '-') && $this->_b === ' ' )
- {
- if ($this->_uncompressed[$this->_nextIndex] === $this->_a)
- {
- $cmd = 1;
- }
- }
-
- switch ($cmd)
- {
- case 1 :
- $this->_compressed .= $this->_a;
- $this->_previousChar = $this->_a;
-
- case 2 :
- $this->_a = $this->_b;
-
- if ($this->_a === "'" || $this->_a === '"')
- {
- while (true)
- {
- $this->_compressed .= $this->_a;
- $this->_previousChar = $this->_a;
-
- $this->_a = $this->_next();
-
- if ($this->_a === $this->_b)
- {
- break;
- }
-
- if ($this->_a === '\n')
- {
- throw new JMediaException("Unterminated string at index " . $this->_nextIndex);
- }
-
- if ($this->_a === '\\')
- {
- $this->_compressed .= $this->_a;
- $this->_previousChar = $this->_a;
-
- $this->_a = $this->_next();
- }
- }
- }
-
- case 3 :
- $this->_b = $this->_getB();
-
- if ($this->_b === '/' && $this->_checkRegExp())
- {
- $this->_compressed .= $this->_a . $this->_b;
-
- while (true)
- {
- $this->_a = $this->_next();
-
- if ($this->_a === '/')
- {
- break;
- }
- elseif ($this->_a === '\\')
- {
- $this->_compressed .= $this->_a;
- $this->_a = $this->_next();
- }
- elseif (ord($this->_a) <= 10)
- {
- throw new JMediaException("Unterminated Regular expression at index" . $this->_nextIndex);
- }
-
- $this->_compressed .= $this->_a;
- $this->_previousChar = $this->_a;
- }
-
- $this->_b = $this->_getB();
- }
- }// End switch
- }
-
- /**
- * Method to check whether a char is alpha numeric
- *
- * @param char $char char to be checked
- *
- * @return boolean true if char is alpha numeric
- *
- * @since 12.1
- */
- private function _checkAlphaNum($char)
- {
-