$value = trim($value);
if (in_array(strtolower($value), array('inherit'))) {
return true;
- } else if (preg_match('/^#([a-fA-F0-9]{1,6})$/', $value)) {
+ } else if (preg_match('/^#([a-fA-F0-9]{1,3}|[a-fA-F0-9]{6})$/', $value)) {
return true;
} else if (in_array(strtolower($value), array_keys(css_optimiser::$htmlcolours))) {
return true;
* 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()
+ * @var array
*/
protected $errors = array();
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class css_writer {
+
/**
* The current indent level
* @var int
}
/**
+ * Returns a CSS string for the provided styles
*
* @param array $styles Array of css_style objects
- * @return type
+ * @return string
*/
public static function styles(array $styles) {
$bits = array();
/**
* Sets the last error message.
*
- * @param type $message
+ * @param string $message
*/
protected function set_error($message) {
$this->error = true;
/**
* Cleans incoming values for typical things that can be optimised.
*
- * @param mixed $value
+ * @param mixed $value Cleans the provided value optimising it if possible
* @return string
*/
protected function clean_value($value) {
* Doing this allows us to associate identical colours being specified in
* different ways. e.g. Red, red, #F00, and #F00000
*
- * @param mixed $value
+ * @param mixed $value Cleans the provided value optimising it if possible
* @return string
*/
protected function clean_value($value) {
$value = trim($value);
- if (preg_match('/#([a-fA-F0-9]{6})/', $value, $matches)) {
- $value = '#'.strtoupper($matches[1]);
- } else if (preg_match('/#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/', $value, $matches)) {
- $value = $matches[1] . $matches[1] . $matches[2] . $matches[2] . $matches[3] . $matches[3];
- $value = '#'.strtoupper($value);
- } else if (array_key_exists(strtolower($value), css_optimiser::$htmlcolours)) {
- $value = css_optimiser::$htmlcolours[strtolower($value)];
+ if (css_is_colour($value)) {
+ if (preg_match('/#([a-fA-F0-9]{6})/', $value, $matches)) {
+ $value = '#'.strtoupper($matches[1]);
+ } else if (preg_match('/#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/', $value, $matches)) {
+ $value = $matches[1] . $matches[1] . $matches[2] . $matches[2] . $matches[3] . $matches[3];
+ $value = '#'.strtoupper($value);
+ } else if (array_key_exists(strtolower($value), css_optimiser::$htmlcolours)) {
+ $value = css_optimiser::$htmlcolours[strtolower($value)];
+ }
}
return $value;
}
* #123 instead of #112233
* #F00 instead of red
*
- * @param string $overridevalue
+ * @param string $overridevalue If provided then this value will be used instead
+ * of the styles current value.
* @return string
*/
public function out($overridevalue = null) {
/**
* Shrinks the colour value is possible.
*
- * @param string $value
+ * @param string $value Shrinks the current value to an optimial form if possible
* @return string
*/
public static function shrink_value($value) {
/**
* Cleans the provided value
*
- * @param mixed $value
+ * @param mixed $value Cleans the provided value optimising it if possible
* @return string
*/
protected function clean_value($value) {
/**
* Initialises a new width style
*
- * @param type $value
+ * @param mixed $value The value this style has
* @return css_style_width
*/
public static function init($value) {
* we can properly condense overrides and then reconsolidate them later into
* an optimal form.
*
- * @param string $value
+ * @param string $value The value the style has
* @return array An array of margin values that can later be consolidated
*/
public static function init($value) {
/**
* A simple init, just a single style
*
- * @param string $value
+ * @param string $value The value the style has
* @return css_style_margintop
*/
public static function init($value) {
/**
* A simple init, just a single style
*
- * @param string $value
+ * @param string $value The value the style has
* @return css_style_margintop
*/
public static function init($value) {
/**
* A simple init, just a single style
*
- * @param string $value
+ * @param string $value The value the style has
* @return css_style_margintop
*/
public static function init($value) {
/**
* A simple init, just a single style
*
- * @param string $value
+ * @param string $value The value the style has
* @return css_style_margintop
*/
public static function init($value) {
/**
* Initalises the border style into an array of individual style compontents
*
- * @param string $value
+ * @param string $value The value the style has
* @return css_style_bordercolor
*/
public static function init($value) {
/**
* Consolidates all border styles into a single style
*
- * @param array $styles
- * @return array
+ * @param array $styles An array of border styles
+ * @return array An optimised array of border styles
*/
public static function consolidate(array $styles) {
* Based upon the colour style
*
* @param mixed $value
- * @return css_style_bordercolor
+ * @return Array of css_style_bordercolor
*/
public static function init($value) {
$value = preg_replace('#\s+#', ' ', $value);
/**
* Cleans the value
*
- * @param string $value
+ * @param string $value Cleans the provided value optimising it if possible
* @return string
*/
protected function clean_value($value) {
* Initialises the border left style into individual components
*
* @param string $value
- * @return css_style_borderleftwidth|css_style_borderleftstyle|css_style_borderleftcolor
+ * @return array Array of css_style_borderleftwidth|css_style_borderleftstyle|css_style_borderleftcolor
*/
public static function init($value) {
$value = preg_replace('#\s+#', ' ', $value);
/**
* Initialises the border right style into individual components
*
- * @param string $value
- * @return css_style_borderrightwidth|css_style_borderrightstyle|css_style_borderrightcolor
+ * @param string $value The value of the style
+ * @return array Array of css_style_borderrightwidth|css_style_borderrightstyle|css_style_borderrightcolor
*/
public static function init($value) {
$value = preg_replace('#\s+#', ' ', $value);
/**
* Initialises the border top style into individual components
*
- * @param string $value
- * @return css_style_bordertopwidth|css_style_bordertopstyle|css_style_bordertopcolor
+ * @param string $value The value of the style
+ * @return array Array of css_style_bordertopwidth|css_style_bordertopstyle|css_style_bordertopcolor
*/
public static function init($value) {
$value = preg_replace('#\s+#', ' ', $value);
/**
* Initialises the border bottom style into individual components
*
- * @param string $value
- * @return css_style_borderbottomwidth|css_style_borderbottomstyle|css_style_borderbottomcolor
+ * @param string $value The value of the style
+ * @return array Array of css_style_borderbottomwidth|css_style_borderbottomstyle|css_style_borderbottomcolor
*/
public static function init($value) {
$value = preg_replace('#\s+#', ' ', $value);
*
* Based upon the colour style
*
- * @param mixed $value
- * @return css_style_borderwidth
+ * @param string $value The value of the style
+ * @return array Array of css_style_border*width
*/
public static function init($value) {
$value = preg_replace('#\s+#', ' ', $value);
*
* Based upon the colour style
*
- * @param mixed $value
- * @return css_style_borderstyle
+ * @param string $value The value of the style
+ * @return array Array of css_style_border*style
*/
public static function init($value) {
$value = preg_replace('#\s+#', ' ', $value);
/**
* Initialises this style object
*
- * @param string $value
+ * @param string $value The value of the style
* @return css_style_bordertopcolor
*/
public static function init($value) {
/**
* Initialises this style object
*
- * @param string $value
+ * @param string $value The value of the style
* @return css_style_borderleftcolor
*/
public static function init($value) {
/**
* Initialises this style object
*
- * @param string $value
+ * @param string $value The value of the style
* @return css_style_borderrightcolor
*/
public static function init($value) {
/**
* Initialises this style object
*
- * @param string $value
+ * @param string $value The value of the style
* @return css_style_borderbottomcolor
*/
public static function init($value) {
/**
* Initialises this style object
*
- * @param string $value
+ * @param string $value The value of the style
* @return css_style_bordertopwidth
*/
public static function init($value) {
/**
* Initialises this style object
*
- * @param string $value
+ * @param string $value The value of the style
* @return css_style_borderleftwidth
*/
public static function init($value) {
/**
* Initialises this style object
*
- * @param string $value
+ * @param string $value The value of the style
* @return css_style_borderrightwidth
*/
public static function init($value) {
/**
* Initialises this style object
*
- * @param string $value
+ * @param string $value The value of the style
* @return css_style_borderbottomwidth
*/
public static function init($value) {
/**
* Initialises this style object
*
- * @param string $value
+ * @param string $value The value of the style
* @return css_style_bordertopstyle
*/
public static function init($value) {
/**
* Initialises this style object
*
- * @param string $value
+ * @param string $value The value of the style
* @return css_style_borderleftstyle
*/
public static function init($value) {
/**
* Initialises this style object
*
- * @param string $value
+ * @param string $value The value of the style
* @return css_style_borderrightstyle
*/
public static function init($value) {
/**
* Initialises a background style
*
- * @param type $value
+ * @param type $value The value of the style
* @return array An array of background component.
*/
public static function init($value) {
/**
* Consolidates background styles into a single background style
*
- * @param array $styles
- * @return array
+ * @param array $styles Consolidates the provided array of background styles
+ * @return array Consolidated optimised background styles
*/
public static function consolidate(array $styles) {
/**
* Creates a new background colour style
*
- * @param mixed $value
+ * @param string $value The value of the style
* @return css_style_backgroundcolor
*/
public static function init($value) {
/**
* css_style_backgroundcolor consolidates to css_style_background
+ *
* @return string
*/
public function consolidate_to() {
/**
* Creates a new background colour style
*
- * @param mixed $value
+ * @param string $value The value of the style
* @return css_style_backgroundimage
*/
public static function init($value) {
/**
* Creates a new background colour style
*
- * @param mixed $value
+ * @param string $value The value of the style
* @return css_style_backgroundrepeat
*/
public static function init($value) {
/**
* Consolidates this style into a single background style
*
- * @return type
+ * @return string
*/
public function consolidate_to() {
return 'background';
/**
* Creates a new background colour style
*
- * @param mixed $value
+ * @param string $value The value of the style
* @return css_style_backgroundattachment
*/
public static function init($value) {
/**
* Creates a new background colour style
*
- * @param mixed $value
+ * @param string $value The value of the style
* @return css_style_backgroundposition
*/
public static function init($value) {
/**
* Initialises this padding style into several individual padding styles
*
- * @param string $value
- * @return array
+ * @param string $value The value fo the style
+ * @return array An array of padding styles
*/
public static function init($value) {
$important = '';
/**
* Consolidates several padding styles into a single style.
*
- * @param array $styles
- * @return array
+ * @param array $styles Array of padding styles
+ * @return array Optimised+consolidated array of padding styles
*/
public static function consolidate(array $styles) {
if (count($styles) != 4) {
/**
* Initialises this style
*
- * @param string $value
+ * @param string $value The value of the style
* @return css_style_paddingtop
*/
public static function init($value) {
/**
* Initialises this style
*
- * @param string $value
+ * @param string $value The value of the style
* @return css_style_paddingright
*/
public static function init($value) {
/**
* Initialises this style
*
- * @param string $value
+ * @param string $value The value of the style
* @return css_style_paddingbottom
*/
public static function init($value) {
/**
* Initialises this style
*
- * @param string $value
+ * @param string $value The value of the style
* @return css_style_paddingleft
*/
public static function init($value) {
$this->check_colors($optimiser);
$this->check_margins($optimiser);
$this->check_padding($optimiser);
+ $this->check_widths($optimiser);
$this->try_broken_css_found_in_moodle($optimiser);
$this->try_invalid_css_handling($optimiser);
$css = '#some .css[type=blah]{color:#123456;}';
$this->assertEqual($css, $optimiser->process($css));
- $cssin = '.css {width:0}';
- $cssout = '.css{width:0;}';
- $this->assertEqual($cssout, $optimiser->process($cssin));
-
- $cssin = '.css {width:0px}';
- $cssout = '.css{width:0;}';
- $this->assertEqual($cssout, $optimiser->process($cssin));
-
- $cssin = '.css {width:100px}';
- $cssout = '.css{width:100px;}';
- $this->assertEqual($cssout, $optimiser->process($cssin));
-
$cssin = '.one {color:red;} .two {color:#F00;}';
$cssout = ".one, .two{color:#F00;}";
$this->assertEqual($cssout, $optimiser->process($cssin));
$cssin = '.one {color:hsla(120,65%,75%,0.5)}';
$cssout = '.one{color:hsla(120,65%,75%,0.5);}';
$this->assertEqual($cssout, $optimiser->process($cssin));
+
+ // Try some invalid colours to make sure we don't mangle them.
+ $css = 'div#some{color:#1;}';
+ $this->assertEqual($css, $optimiser->process($css));
+
+ $css = 'div#some{color:#12;}';
+ $this->assertEqual($css, $optimiser->process($css));
+
+ $css = 'div#some{color:#1234;}';
+ $this->assertEqual($css, $optimiser->process($css));
+
+ $css = 'div#some{color:#12345;}';
+ $this->assertEqual($css, $optimiser->process($css));
+ }
+
+ protected function check_widths(css_optimiser $optimiser) {
+ $cssin = '.css {width:0}';
+ $cssout = '.css{width:0;}';
+ $this->assertEqual($cssout, $optimiser->process($cssin));
+
+ $cssin = '.css {width:0px}';
+ $cssout = '.css{width:0;}';
+ $this->assertEqual($cssout, $optimiser->process($cssin));
+
+ $cssin = '.css {width:0em}';
+ $cssout = '.css{width:0;}';
+ $this->assertEqual($cssout, $optimiser->process($cssin));
+
+ $cssin = '.css {width:0pt}';
+ $cssout = '.css{width:0;}';
+ $this->assertEqual($cssout, $optimiser->process($cssin));
+
+ $cssin = '.css {width:0mm}';
+ $cssout = '.css{width:0;}';
+ $this->assertEqual($cssout, $optimiser->process($cssin));
+
+ $cssin = '.css {width:100px}';
+ $cssout = '.css{width:100px;}';
+ $this->assertEqual($cssout, $optimiser->process($cssin));
}
/**
$this->assertTrue(css_is_colour('#aBc'));
$this->assertTrue(css_is_colour('#1a2Bc3'));
$this->assertTrue(css_is_colour('#1Ac'));
+
// Note the following two colour's arn't really colours but browsers process
// them still.
$this->assertTrue(css_is_colour('#A'));
$this->assertTrue(css_is_colour('#12'));
+ // Having four or five characters however are not valid colours and
+ // browsers don't parse them. They need to fail so that broken CSS
+ // stays broken after optimisation.
+ $this->assertFalse(css_is_colour('#1234'));
+ $this->assertFalse(css_is_colour('#12345'));
$this->assertFalse(css_is_colour('#BCDEFG'));
$this->assertFalse(css_is_colour('#'));