Permalink
Browse files

Property sorter plugin now supports legacy vendor prefixes (for linti…

…ng).

Removed support for the deprecated var() variable notation.
Some internal refactoring.
  • Loading branch information...
1 parent 49c7de1 commit 7b1c3fc0f56098e1726d3096836047d65c4cbeeb @peteboere committed Sep 12, 2012
Showing with 104 additions and 97 deletions.
  1. +10 −8 README.md
  2. +2 −15 lib/Core.php
  3. +3 −7 lib/Regex.php
  4. +28 −34 lib/Rule.php
  5. +61 −33 plugins/property-sorter.php
View
@@ -13,14 +13,16 @@ http://the-echoplex.net/csscrush
Quick start
===================================
- <?php
-
- require_once 'CssCrush/CssCrush.php';
- $global_css = csscrush_file( '/css/global.css' );
-
- ?>
-
- <link rel="stylesheet" type="text/css" href="<?php echo $global_css; ?>" media="all" />
+```php
+<?php
+
+require_once 'CssCrush/CssCrush.php';
+$global_css = csscrush_file( '/css/global.css' );
+
+?>
+
+<link rel="stylesheet" href="<?php echo $global_css; ?>" media="all" />
+```
Submitting bugs
View
@@ -1210,23 +1210,10 @@ protected static function cb_extractVariables ( $match ) {
protected static function cb_placeVariables ( $match ) {
- $before_char = $match[1];
-
- // Check for dollar shorthand
- if ( empty( $match[2] ) && isset( $match[3] ) && strpos( $match[0], '$' ) !== false ) {
-
- $variable_name = $match[3];
- }
- else {
- $variable_name = $match[2];
- }
+ $variable_name = $match[1];
if ( isset( self::$storage->variables[ $variable_name ] ) ) {
-
- return $before_char . self::$storage->variables[ $variable_name ];
- }
- else {
- return $before_char;
+ return self::$storage->variables[ $variable_name ];
}
}
View
@@ -59,12 +59,7 @@ public static function init () {
$patt->argToken = '!___arg(\d+)___!';
// Functions
- $patt->varFunction = '!(?:
- ([^a-z0-9_-])
- var\(\s*([a-z0-9_-]+)\s*\)
- |
- \$\(\s*([a-z0-9_-]+)\s*\) # Dollar syntax
- )!ixS';
+ $patt->varFunction = '!\$\(\s*([a-z0-9_-]+)\s*\)!iS';
$patt->function = '!(^|[^a-z0-9_-])([a-z_-]+)(___p\d+___)!i';
// Specific functions
@@ -73,7 +68,8 @@ public static function init () {
$patt->thisFunction = csscrush_regex::createFunctionMatchPatt( array( 'this' ) );
// Misc.
- $patt->vendorPrefix = '!^-([a-z]+)-([a-z-]+)!';
+ $patt->vendorPrefix = '!^-([a-z]+)-([a-z-]+)!iS';
+ $patt->mixinExtend = '!^(?:(@include|mixin)|(@?extends?))[\s\:]+!iS';
$patt->absoluteUrl = '!^https?://!';
$patt->argListSplit = '!\s*[,\s]\s*!S';
$patt->mathBlacklist = '![^\.0-9\*\/\+\-\(\)]!S';
View
@@ -126,7 +126,7 @@ public function __construct ( $selector_string = null, $declarations_string ) {
$declaration = csscrush_util::stripComments( $declaration );
// Accept several different syntaxes for mixin and extends.
- if ( preg_match( '!^(?:(@include|mixin)|(@?extends?))[\s\:]+!iS', $declaration, $m ) ) {
+ if ( preg_match( $regex->mixinExtend, $declaration, $m ) ) {
$prop = isset( $m[2] ) ? 'extends' : 'mixin';
$value = substr( $declaration, strlen( $m[0] ) );
@@ -253,8 +253,6 @@ public function cssQueryFunction ( $input, $fn_name, $call_property ) {
}
$default = isset( $args[0] ) ? $args[0] : null;
- // csscrush::log( array( $name, $property, $default ), 'query args' );
-
// Try to match a abstract rule first
if ( preg_match( csscrush_regex::$patt->name, $name ) ) {
@@ -330,30 +328,27 @@ public function addPropertyAliases () {
// There are aliases for the current property
foreach ( $aliasedProperties[ $prop ] as $prop_alias ) {
+ // If an aliased version already exists to not create one.
if ( $this->propertyCount( $prop_alias ) ) {
continue;
}
- // If the aliased property hasn't been set manually, we create it
+ // Create the aliased declaration.
$copy = clone $declaration;
- $copy->family = $copy->property;
$copy->property = $prop_alias;
- // Remembering to set the vendor property
+ // Set the aliased declaration vendor property.
$copy->vendor = null;
-
- // Increment the property count
- $this->addProperty( $prop_alias );
if ( preg_match( $regex->vendorPrefix, $prop_alias, $vendor ) ) {
$copy->vendor = $vendor[1];
}
$new_set[] = $copy;
}
}
- // Un-aliased property or a property alias that has been manually set
+ // Un-aliased property or a property alias that has been manually set.
$new_set[] = $declaration;
}
- // Re-assign
+ // Re-assign.
$this->declarations = $new_set;
}
@@ -368,8 +363,8 @@ public function addFunctionAliases () {
$new_set = array();
- // Keep track of the function aliases we apply and to which property 'family'
- // they belong, so we can avoid un-unecessary duplications
+ // Keep track of the function aliases we apply and to which property
+ // they belong, so we can avoid un-unecessary duplications.
$used_fn_aliases = array();
// Shim in aliased functions
@@ -383,14 +378,15 @@ public function addFunctionAliases () {
$new_set[] = $declaration;
continue;
}
- // Get list of functions used in declaration that are alias-able, if none skip
+
+ // Get list of functions used in declaration that are alias-able, if none skip.
$intersect = array_intersect( $declaration->functions, $aliased_functions );
if ( empty( $intersect ) ) {
$new_set[] = $declaration;
continue;
}
- // csscrush::log($intersect);
- // Loop the aliasable functions
+
+ // Loop the aliasable functions.
foreach ( $intersect as $fn_name ) {
if ( $declaration->vendor ) {
@@ -404,7 +400,7 @@ public function addFunctionAliases () {
'${1}' . $fn_search,
$declaration->value
);
- $used_fn_aliases[ $declaration->family ][] = $fn_search;
+ $used_fn_aliases[ $declaration->canonicalProperty ][] = $fn_search;
}
}
else {
@@ -413,8 +409,8 @@ public function addFunctionAliases () {
foreach ( $function_aliases[ $fn_name ] as $fn_alias ) {
if (
- isset( $used_fn_aliases[ $declaration->family ] ) &&
- in_array( $fn_alias, $used_fn_aliases[ $declaration->family ] )
+ isset( $used_fn_aliases[ $declaration->canonicalProperty ] ) &&
+ in_array( $fn_alias, $used_fn_aliases[ $declaration->canonicalProperty ] )
) {
// If the function alias has already been applied in a vendor property
// for the same declaration property assume all is good
@@ -427,8 +423,6 @@ public function addFunctionAliases () {
$copy->value
);
$new_set[] = $copy;
- // Increment the property count
- $this->addProperty( $copy->property );
}
}
}
@@ -705,7 +699,7 @@ public static function get ( $token ) {
class csscrush_declaration {
public $property;
- public $family;
+ public $canonicalProperty;
public $vendor;
public $functions;
public $value;
@@ -734,15 +728,15 @@ public function __construct ( $prop, $value, $contextIndex = 0 ) {
$prop = substr( $prop, 1 );
}
- // Store the property family
- // Store the vendor id, if one is present
+ // Store the canonical property name.
+ // Store the vendor mark if one is present.
if ( preg_match( $regex->vendorPrefix, $prop, $vendor ) ) {
- $family = $vendor[2];
+ $canonical_property = $vendor[2];
$vendor = $vendor[1];
}
else {
$vendor = null;
- $family = $prop;
+ $canonical_property = $prop;
}
// Check for !important keywords
@@ -779,14 +773,14 @@ public function __construct ( $prop, $value, $contextIndex = 0 ) {
$functions = array();
}
- $this->property = $prop;
- $this->family = $family;
- $this->vendor = $vendor;
- $this->functions = $functions;
- $this->index = $contextIndex;
- $this->value = $value;
- $this->skip = $skip;
- $this->important = $important;
+ $this->property = $prop;
+ $this->canonicalProperty = $canonical_property;
+ $this->vendor = $vendor;
+ $this->functions = $functions;
+ $this->index = $contextIndex;
+ $this->value = $value;
+ $this->skip = $skip;
+ $this->important = $important;
}
public function getFullValue () {
@@ -2,21 +2,21 @@
/**
* Property sorter.
* Order CSS properties according to a sorting table.
- *
+ *
* Examples use the predefined sorting table.
- *
+ *
* To define a custom sorting table globally define $CSSCRUSH_PROPERTY_SORT_ORDER.
* Assign an empty array to create an alphabetical sort:
- *
+ *
* $CSSCRUSH_PROPERTY_SORT_ORDER = array( 'color', ... );
- *
+ *
*
* @before
* color: red;
* background: #000;
* opacity: .5;
* display: block;
- *
+ *
* @after
* display: block;
* opacity: .5;
@@ -48,35 +48,77 @@ function csscrush__property_sorter ( csscrush_rule $rule ) {
function _csscrush__property_sorter_callback ( $a, $b ) {
$map =& _csscrush__property_sorter_get_table();
- $a_prop =& $a->property;
- $b_prop =& $b->property;
+ $a_prop =& $a->canonicalProperty;
+ $b_prop =& $b->canonicalProperty;
$a_listed = isset( $map[ $a_prop ] );
$b_listed = isset( $map[ $b_prop ] );
- // If both properties are listed do a table comparison.
+ // If the properties are identical we need to flag for an index comparison.
+ $compare_indexes = false;
+
+ // If the 'canonical' properties are identical we need to flag for a vendor comparison.
+ $compare_vendor = false;
+
+ // If both properties are listed.
if ( $a_listed && $b_listed ) {
if ( $a_prop === $b_prop ) {
- return $a->index > $b->index ? 1 : -1;
+ if ( $a->vendor || $b->vendor ) {
+ $compare_vendor = true;
+ }
+ else {
+ $compare_indexes = true;
+ }
+ }
+ else {
+ // Table comparison.
+ return $map[ $a_prop ] > $map[ $b_prop ] ? 1 : -1;
}
- return $map[ $a_prop ] > $map[ $b_prop ] ? 1 : -1;
}
- // Listed properties always come before un-listed.
- if ( $a_listed && ! $b_listed ) {
+ // If one property is listed it always takes higher priority.
+ elseif ( $a_listed && ! $b_listed ) {
return -1;
}
- if ( $b_listed && ! $a_listed ) {
+ elseif ( $b_listed && ! $a_listed ) {
return 1;
}
- // If propertes are the same compare declaration indexes.
- if ( $a_prop === $b_prop ) {
+ // If neither property is listed.
+ else {
+
+ if ( $a_prop === $b_prop ) {
+ if ( $a->vendor || $b->vendor ) {
+ $compare_vendor = true;
+ }
+ else {
+ $compare_indexes = true;
+ }
+ }
+ else {
+ // Regular sort.
+ return $a_prop > $b_prop ? 1 : -1;
+ }
+ }
+
+ // Comparing by index.
+ if ( $compare_indexes ) {
return $a->index > $b->index ? 1 : -1;
}
- // If neither property is listed do a regular sort.
- return $a_prop > $b_prop ? 1 : -1;
+ // Comparing by vendor mark.
+ if ( $compare_vendor ) {
+ if ( ! $a->vendor && $b->vendor ) {
+ return 1;
+ }
+ elseif ( $a->vendor && ! $b->vendor ) {
+ return -1;
+ }
+ else {
+ // If both have a vendor mark compare vendor name length.
+ return strlen( $b->vendor ) > strlen( $a->vendor ) ? 1 : -1;
+ }
+ }
}
@@ -115,22 +157,8 @@ function &_csscrush__property_sorter_get_table () {
}
}
- // Add in prefixed properties based on the aliases file
- $collated_table = array();
- $property_aliases =& csscrush::$config->aliases[ 'properties' ];
- $priority = 0;
-
- foreach ( $table as $property ) {
- if ( isset( $property_aliases[ $property ] ) ) {
- foreach ( $property_aliases[ $property ] as &$property_alias ) {
- $collated_table[ $property_alias ] = ++$priority;
- }
- }
- $collated_table[ $property ] = ++$priority;
- }
-
- // Cache the collated table
- $GLOBALS[ 'CSSCRUSH_PROPERTY_SORT_ORDER_CACHE' ] = $collated_table;
+ // Cache the table (and flip it).
+ $GLOBALS[ 'CSSCRUSH_PROPERTY_SORT_ORDER_CACHE' ] = array_flip( $table );
return $GLOBALS[ 'CSSCRUSH_PROPERTY_SORT_ORDER_CACHE' ];
}

0 comments on commit 7b1c3fc

Please sign in to comment.