Permalink
Browse files

Added values aliases, dynamic 'runtime' variables and RGBA fallback m…

…acro. Some cleaning up and commenting.
  • Loading branch information...
1 parent 93c0fd3 commit 52a98bbbd646e16221c9c5215f59ec18c89a8936 @peteboere committed Aug 31, 2011
Showing with 131 additions and 40 deletions.
  1. +8 −0 CssCrush/CssCrush.aliases
  2. +51 −25 CssCrush/CssCrush.macros.php
  3. +72 −15 CssCrush/CssCrush.php
@@ -112,6 +112,14 @@
;----------------------------------------------------------------------------
+;-- Value aliases
+
+[values]
+ display:box[] = -webkit-box
+ display:box[] = -moz-box
+
+
+;----------------------------------------------------------------------------
;-- Function aliases
[functions]
@@ -1,25 +1,34 @@
-<?php
+<?php
################################################################################################
-######## IE Legacy
+# Comment out/in as required
-CssCrush::addRuleMacro( 'csscrush_display_inlineblock' );
+// IE 6 shims
CssCrush::addRuleMacro( 'csscrush_minheight' );
+
+// IE 6/7 shims
CssCrush::addRuleMacro( 'csscrush_clip' );
+CssCrush::addRuleMacro( 'csscrush_display_inlineblock' );
+
+// IE filter
CssCrush::addRuleMacro( 'csscrush_filter' );
-########
+// RGBA fallback
+CssCrush::addRuleMacro( 'csscrush_rgba' );
+
+
+################################################################################################
/**
- * Fix inline-block in IE < 8
+ * Simulate inline-block in IE < 8
*
* Before:
* display: inline-block;
*
* After:
* display: inline-block;
- * display: inline;
- * zoom: 1;
+ * *display: inline;
+ * *zoom: 1;
*/
function csscrush_display_inlineblock ( $rule ) {
if ( $rule->propertyCount( 'display' ) < 1 ) {
@@ -33,9 +42,7 @@ function csscrush_display_inlineblock ( $rule ) {
continue;
}
$new_set[] = $rule->createDeclaration( '*display', 'inline' );
- if ( !$rule->propertyCount( '*zoom' ) ) {
- $new_set[] = $rule->createDeclaration( '*zoom', 1 );
- }
+ $new_set[] = $rule->createDeclaration( '*zoom', 1 );
}
$rule->declarations = $new_set;
}
@@ -126,30 +133,49 @@ function csscrush_filter ( $rule ) {
$rule->declarations = $new_set;
}
-
-################################################################################################
-######## Display:box
-
-CssCrush::addRuleMacro( 'csscrush_display_box' );
-
-########
-
/**
- * Prefixed values on `display:box`
+ * RGBA fallback
+ * Only works with background shorthand IE < 8
+ * (http://css-tricks.com/2151-rgba-browser-support/)
+ *
+ * Before:
+ * background: rgba(0,0,0,.5);
+ *
+ * After:
+ * background: rgb(0,0,0);
+ * background: rgba(0,0,0,.5);
*/
-function csscrush_display_box ( $rule ) {
- if ( $rule->propertyCount( 'display' ) < 1 ) {
+function csscrush_rgba ( $rule ) {
+ $props = array_keys( $rule->properties );
+
+ // Determine which properties apply
+ $rgba_props = array();
+ foreach ( $props as $prop ) {
+ if ( $prop === 'background' or strpos( $prop, 'color' ) !== false ) {
+ $rgba_props[] = $prop;
+ }
+ }
+ if ( empty( $rgba_props ) ) {
return;
}
+
$new_set = array();
foreach ( $rule as $declaration ) {
- $is_display = $declaration->property === 'display';
- if ( !$is_display or ( $is_display and $declaration->value !== 'box' ) ) {
+ $is_viable = in_array( $declaration->property, $rgba_props );
+ if (
+ !$is_viable or
+ $is_viable and !preg_match( '!^rgba___p\d+___$!', $declaration->value )
+ ) {
$new_set[] = $declaration;
continue;
}
- $new_set[] = $rule->createDeclaration( 'display', '-webkit-box' );
- $new_set[] = $rule->createDeclaration( 'display', '-moz-box' );
+ // Create rgb value from rgba
+ $raw_value = $rule->getDeclarationValue( $declaration );
+ $raw_value = substr( $raw_value, 5, strlen( $raw_value ) - 1 );
+ list( $r, $g, $b, $a ) = explode( ',', $raw_value );
+
+ // Add rgb value to the stack, followed by rgba
+ $new_set[] = $rule->createDeclaration( $declaration->property, "rgb($r,$g,$b)" );
$new_set[] = $declaration;
}
$rule->declarations = $new_set;
View
@@ -18,10 +18,17 @@
*/
class CssCrush {
+ // Path information, global settings
public static $config;
+
+ // The path of this script
public static $location;
- public static $aliases;
- public static $macros;
+
+ // Aliases from the aliases file
+ public static $aliases = array();
+
+ // Macro function names
+ public static $macros = array();
public static $COMPILE_SUFFIX = '.crush.css';
protected static $assetsLoaded = false;
@@ -32,7 +39,7 @@ class CssCrush {
protected static $compileName;
protected static $tokenUID;
- // Pattern matching
+ // Regular expressions
public static $regex = array(
'import' => '!
@import\s+ # import at-rule
@@ -87,7 +94,7 @@ public static function init () {
}
$config->docRoot = rtrim( str_replace( '\\', '/', $docRoot ), '/' );
- // Convert to objects for ease of use
+ // Casting to objects for ease of use
self::$regex = (object) self::$regex;
self::$regex->token = (object) self::$regex->token;
self::$regex->function = (object) self::$regex->function;
@@ -101,6 +108,17 @@ protected static function loadAssets () {
if ( file_exists( $aliases_file ) ) {
if ( $result = parse_ini_file( $aliases_file, true ) ) {
self::$aliases = $result;
+
+ // Value aliases require a little preprocessing
+ if ( isset( self::$aliases[ 'values' ] ) ) {
+ $store = array();
+ foreach ( self::$aliases[ 'values' ] as $prop_val => $aliases ) {
+ list( $prop, $value ) = array_map( 'trim', explode( ':', $prop_val ) );
+ $store[ $prop ][ $value ] = $aliases;
+ }
+ self::$aliases[ 'values' ] = $store;
+ self::log( $store );
+ }
}
else {
trigger_error( __METHOD__ . ": Aliases file was not parsed correctly (syntax error).\n", E_USER_NOTICE );
@@ -111,7 +129,6 @@ protected static function loadAssets () {
}
// Load macros file if it exists
- self::$macros = array();
$macros_file = self::$location . '/' . __CLASS__ . '.macros.php';
if ( file_exists( $macros_file ) ) {
require_once $macros_file;
@@ -375,9 +392,14 @@ protected static function getBoilerplate () {
protected static function parseOptions ( $options ) {
// Create default options for those not set
$option_defaults = array(
+ // Minify. Set true for formatting and comments
'debug' => false,
+ // Append 'checksum' to output file name
'versioning' => true,
+ // Use the template boilerplate
'boilerplate' => true,
+ // Variables passed in at runtime
+ 'vars' => array(),
);
self::$options = is_array( $options ) ?
array_merge( $option_defaults, $options ) : $option_defaults;
@@ -399,7 +421,11 @@ protected static function compile ( $hostfile ) {
// Parse variables
$output = preg_replace_callback( $regex->variables, array( 'self', 'cb_extractVariables' ), $output );
- // self::log( self::$storage->variables );
+ // Overriding variables
+ if ( !empty( self::$options[ 'vars' ] ) ) {
+ self::$storage->variables = array_merge(
+ self::$storage->variables, self::$options[ 'vars' ] );
+ }
// Place variables
$output = preg_replace_callback( $regex->function->var, array( 'self', 'cb_placeVariables' ), $output );
@@ -493,8 +519,8 @@ protected static function validateCache ( $hostfile ) {
$existingfile = new stdClass;
$existingfile->name = $filename;
- $existingfile->path = "{$config->baseDir}/{$existingfile->name}";
- $existingfile->URL = "{$config->baseURL}/{$existingfile->name}";
+ $existingfile->path = "$config->baseDir/$existingfile->name";
+ $existingfile->URL = "$config->baseURL/$existingfile->name";
// Start off with the host file then add imported files
$all_files = array( $hostfile->mtime );
@@ -525,7 +551,7 @@ protected static function validateCache ( $hostfile ) {
) {
// Files have not been modified and config is the same: return the old file
self::log( "Files have not been modified, returning existing
- file '{$existingfile->URL}'" );
+ file '$existingfile->URL'" );
return $existingfile->URL . ( self::$options[ 'versioning' ] !== false ? "?{$existing_datesum}" : '' );
}
else {
@@ -847,6 +873,7 @@ protected static function cb_extractRules ( $match ) {
if ( !empty( self::$aliases ) ) {
$rule->addPropertyAliases();
$rule->addFunctionAliases();
+ $rule->addValueAliases();
}
$rule->applyMacros();
$rule->expandSelectors();
@@ -1106,8 +1133,9 @@ public function __construct ( $selector_string = null, $declarations_string ) {
if ( !empty( $selector_string ) ) {
$selector_adjustments = array(
- // 'Hocus' pseudo class shorthand
+ // 'hocus' and 'pocus' pseudo class shorthand
'!:hocus([^a-z0-9_-])!' => ':any(:hover,:focus)$1',
+ '!:pocus([^a-z0-9_-])!' => ':any(:hover,:focus,:active)$1',
// Reduce double colon syntax for backwards compatability
'!::(after|before|first-letter|first-line)!' => ':$1',
);
@@ -1208,7 +1236,7 @@ public function addPropertyAliases () {
// First test for the existence of any aliased properties
$intersect = array_intersect( array_keys( $aliasedProperties ), array_keys( $this->properties ) );
if ( empty( $intersect ) ) {
- return $this;
+ return;
}
// Shim in aliased properties
@@ -1240,8 +1268,6 @@ public function addPropertyAliases () {
}
// Re-assign
$this->declarations = $new_set;
-
- return $this;
}
public function addFunctionAliases () {
@@ -1250,7 +1276,7 @@ public function addFunctionAliases () {
$aliased_functions = array_keys( $function_aliases );
if ( empty( $aliased_functions ) ) {
- return $this;
+ return;
}
$new_set = array();
@@ -1320,8 +1346,39 @@ public function addFunctionAliases () {
// Re-assign
$this->declarations = $new_set;
+ }
+
+ public function addValueAliases () {
+
+ $aliasedValues =& CssCrush::$aliases[ 'values' ];
+
+ // First test for the existence of any aliased properties
+ $intersect = array_intersect( array_keys( $aliasedValues ), array_keys( $this->properties ) );
+
+ if ( empty( $intersect ) ) {
+ return;
+ }
- return $this;
+ $new_set = array();
+ foreach ( $this->declarations as $declaration ) {
+ foreach ( $aliasedValues as $value_prop => $value_aliases ) {
+ if ( $this->propertyCount( $value_prop ) < 1 ) {
+ continue;
+ }
+ foreach ( $value_aliases as $value => $aliases ) {
+ if ( $declaration->value === $value ) {
+ foreach ( $aliases as $alias ) {
+ $copy = clone $declaration;
+ $copy->value = $alias;
+ $new_set[] = $copy;
+ }
+ }
+ }
+ }
+ $new_set[] = $declaration;
+ }
+ // Re-assign
+ $this->declarations = $new_set;
}
public function applyMacros () {

0 comments on commit 52a98bb

Please sign in to comment.