Skip to content

Commit

Permalink
linear-gradient functions with angles specified in degrees now conver…
Browse files Browse the repository at this point in the history
…t child vendor aliases to match (#43).

Updated changelog.
  • Loading branch information
peteboere committed Jan 30, 2013
1 parent b295c86 commit 608f580
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 32 deletions.
8 changes: 4 additions & 4 deletions CHANGELOG.md
@@ -1,8 +1,8 @@
1.9.1 (? January 2013)
1.9.1 (30th January 2013)
-----
* Resolved issue #42.
* Resolved issues #42 and #43.
* Fixed command line context option.
* Added noise/texture generating functions with the noise plugin.
* Added noise plugin (noise/texture generating functions).

1.9 (12th January 2013)
---
Expand All @@ -15,7 +15,7 @@
* Added functions API for defining custom functions inside plugins.
* Improved gradient function aliasing to handle new angle keywords (to left, at center, etc.).
* Added svg-gradients plugin for simulating CSS3 gradients with data-uris.
* Added `formatting` option for un-minified output. Possible values (custom formatters can also be defined):
* Added `formatter` option for un-minified output. Possible values (custom formatters can also be defined):
* "block" (default) - Rules are block formatted.
* "single-line" - Rules are printed in single lines.
* "padded" - Rules are printed in single lines with right padded selectors.
Expand Down
2 changes: 1 addition & 1 deletion cli.php
Expand Up @@ -263,7 +263,7 @@ function get_stdin_contents () {

// Resolve a context for URLs.
if ( ! $context ) {
$context = $input_file ? dirname( realpath( $input_file ) ) : null;
$context = $input_file ? dirname( realpath( $input_file ) ) : getcwd();
}

// If there is an import context set document root also.
Expand Down
86 changes: 60 additions & 26 deletions lib/CssCrush/PostAliasFix.php
Expand Up @@ -39,12 +39,14 @@ static public function remove ( $alias_type, $key )
}
}

function csscrush__post_alias_fix_lineargradients ( $declaration_copies, $fn_name ) {
CssCrush_PostAliasFix::init();

// Swap the new 'to' gradient syntax to the old 'from' syntax for the prefixed versions.
// 1. Create new paren tokens based on the first prefixed declaration.
// 2. Replace the new syntax with the legacy syntax.
// 3. Swap in the new tokens on all the prefixed declarations.

/**
* Convert the new angle syntax (keyword and degree) on -x-linear-gradient() functions
* to legacy equivalents.
*/
function csscrush__post_alias_fix_lineargradients ( $declaration_copies, $fn_name ) {

static $angles_new, $angles_old;
if ( ! $angles_new ) {
Expand All @@ -67,34 +69,64 @@ function csscrush__post_alias_fix_lineargradients ( $declaration_copies, $fn_nam
$angles_old = array_values( $angles );
}

// 1, 2.
$patt = '~(?<![\w-])-[a-z]+-' . $fn_name . '(\?p\d+\?)~i';
// Degree angle regex and replace callback.
static $deg_patt;
static $deg_convert_callback;
if ( ! $deg_convert_callback ) {
$deg_patt = '~(?<=[\( ])(' . CssCrush_Regex::$classes->number . ')deg\b~i';
// Legacy angles move anti-clockwise and start from East, not North.
$deg_convert_callback = create_function( '$m', '
$angle = floatval( $m[1] );
$angle = ( $angle + 90 ) - ( $angle * 2 );
return ( $angle < 0 ? $angle + 360 : $angle ) . \'deg\';
');
}

// Create new paren tokens based on the first prefixed declaration.
// Replace the new syntax with the legacy syntax.
$original_parens = array();
$replacement_parens = array();
foreach ( CssCrush_Regex::matchAll( $patt, $declaration_copies[0]->value ) as $m ) {
$fn_patt = '~(?<![\w-])-[a-z]+-' . $fn_name . '(\?p\d+\?)~i';

foreach ( CssCrush_Regex::matchAll( $fn_patt, $declaration_copies[0]->value ) as $m ) {

$original_parens[] = $m[1][0];
$replacement_parens[] = CssCrush::$process->addToken(
str_ireplace(
$angles_new,
$angles_old,
CssCrush::$process->fetchToken( $m[1][0] )
), 'p' );
$original_paren_value = CssCrush::$process->fetchToken( $m[1][0] );

// Convert keyword angle values.
$updated_paren_value = str_ireplace(
$angles_new,
$angles_old,
$original_paren_value
);

// Convert degree angle values.
$updated_paren_value = preg_replace_callback(
$deg_patt,
$deg_convert_callback,
$updated_paren_value
);

$replacement_parens[] = CssCrush::$process->addToken( $updated_paren_value, 'p' );
}

// 3.
// Swap in the new tokens on all the prefixed declarations.
foreach ( $declaration_copies as $prefixed_copy ) {
$prefixed_copy->value = str_replace( $original_parens, $replacement_parens, $prefixed_copy->value );
$prefixed_copy->value = str_replace(
$original_parens,
$replacement_parens,
$prefixed_copy->value
);
}
}

/**
* Remove the 'at' keyword from -x-radial-gradient() for legacy implementations.
*/
function csscrush__post_alias_fix_radialgradients ( $declaration_copies, $fn_name ) {

// Remove the new 'at' keyword from gradient syntax for legacy implementations.
// 1. Create new paren tokens based on the first prefixed declaration.
// 2. Replace the new syntax with the legacy syntax.
// 3. Swap in the new tokens on all the prefixed declarations.

// 1, 2.
// Create new paren tokens based on the first prefixed declaration.
// Replace the new syntax with the legacy syntax.
$patt = '~(?<![\w-])-[a-z]+-' . $fn_name . '(\?p\d+\?)~i';
$original_parens = array();
$replacement_parens = array();
Expand All @@ -108,10 +140,12 @@ function csscrush__post_alias_fix_radialgradients ( $declaration_copies, $fn_nam
), 'p' );
}

// 3.
// Swap in the new tokens on all the prefixed declarations.
foreach ( $declaration_copies as $prefixed_copy ) {
$prefixed_copy->value = str_replace( $original_parens, $replacement_parens, $prefixed_copy->value );
$prefixed_copy->value = str_replace(
$original_parens,
$replacement_parens,
$prefixed_copy->value
);
}
}

CssCrush_PostAliasFix::init();
2 changes: 1 addition & 1 deletion plugins/noise.php
Expand Up @@ -71,7 +71,7 @@
* background: turbulence();
*
* // Sand effect.
* background: turbulence( wheat 400x400, .35:.2 4 sharpen, normal, saturate .4 );;
* background: turbulence( wheat 400x400, .35:.2 4 sharpen, normal, saturate .4 );
*/

CssCrush_Plugin::register( 'noise', array(
Expand Down

0 comments on commit 608f580

Please sign in to comment.