Skip to content

Commit

Permalink
Fix comment nodes in function & add optional relative amounts for col…
Browse files Browse the repository at this point in the history
…or functions

A lot of changes from upstream Less.js 2.x were ported in this patch,
but let's go over them;

Firstly, comments nodes are now allowed in function arguments, see
less/less.js@7d86a5e

We added support for isruleset, see
less/less.js@297ac17

Support optional relative amounts for color functions, see
less/less.js@622a521

Less_Tree_Quoted escape now defaults to true, see
less/less.js@ccd49bb

And a minor fix for replace function and _percent, see
less/less.js@13ef5b7

Bug: T354895
Change-Id: I2fcb32e5eb67bbcbdcc76e96a6b8b1c6fec50c7e
  • Loading branch information
anny21 committed Jan 19, 2024
1 parent 2083ffd commit 29cf2b9
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 41 deletions.
95 changes: 70 additions & 25 deletions lib/Less/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public function hue( $color = null ) {
}

$c = $color->toHSL();
return new Less_Tree_Dimension( Less_Parser::round( $c['h'] ) );
return new Less_Tree_Dimension( $c['h'] );
}

public function saturation( $color = null ) {
Expand All @@ -179,7 +179,7 @@ public function saturation( $color = null ) {
}

$c = $color->toHSL();
return new Less_Tree_Dimension( Less_Parser::round( $c['s'] * 100 ), '%' );
return new Less_Tree_Dimension( $c['s'] * 100, '%' );
}

public function lightness( $color = null ) {
Expand All @@ -188,7 +188,7 @@ public function lightness( $color = null ) {
}

$c = $color->toHSL();
return new Less_Tree_Dimension( Less_Parser::round( $c['l'] * 100 ), '%' );
return new Less_Tree_Dimension( $c['l'] * 100, '%' );
}

public function hsvhue( $color = null ) {
Expand All @@ -197,7 +197,7 @@ public function hsvhue( $color = null ) {
}

$hsv = $color->toHSV();
return new Less_Tree_Dimension( Less_Parser::round( $hsv['h'] ) );
return new Less_Tree_Dimension( $hsv['h'] );
}

public function hsvsaturation( $color = null ) {
Expand All @@ -206,7 +206,7 @@ public function hsvsaturation( $color = null ) {
}

$hsv = $color->toHSV();
return new Less_Tree_Dimension( Less_Parser::round( $hsv['s'] * 100 ), '%' );
return new Less_Tree_Dimension( $hsv['s'] * 100, '%' );
}

public function hsvvalue( $color = null ) {
Expand All @@ -215,7 +215,7 @@ public function hsvvalue( $color = null ) {
}

$hsv = $color->toHSV();
return new Less_Tree_Dimension( Less_Parser::round( $hsv['v'] * 100 ), '%' );
return new Less_Tree_Dimension( $hsv['v'] * 100, '%' );
}

public function red( $color = null ) {
Expand Down Expand Up @@ -256,7 +256,7 @@ public function luma( $color = null ) {
throw new Less_Exception_Compiler( 'The first argument to luma must be a color' . ( $color instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) );
}

return new Less_Tree_Dimension( Less_Parser::round( $color->luma() * $color->alpha * 100 ), '%' );
return new Less_Tree_Dimension( $color->luma() * $color->alpha * 100, '%' );
}

public function luminance( $color = null ) {
Expand All @@ -269,10 +269,10 @@ public function luminance( $color = null ) {
+ ( 0.7152 * $color->rgb[1] / 255 )
+ ( 0.0722 * $color->rgb[2] / 255 );

return new Less_Tree_Dimension( Less_Parser::round( $luminance * $color->alpha * 100 ), '%' );
return new Less_Tree_Dimension( $luminance * $color->alpha * 100, '%' );
}

public function saturate( $color = null, $amount = null ) {
public function saturate( $color = null, $amount = null, $method = null ) {
// filter: saturate(3.2);
// should be kept as is, so check for color
if ( $color instanceof Less_Tree_Dimension ) {
Expand All @@ -288,8 +288,11 @@ public function saturate( $color = null, $amount = null ) {

$hsl = $color->toHSL();

$hsl['s'] += $amount->value / 100;
$hsl['s'] = self::clamp( $hsl['s'] );
if ( isset( $method ) && $method->value === "relative" ) {
$hsl['s'] += $hsl['s'] * $amount->value / 100;
} else {
$hsl['s'] += $amount->value / 100;
} $hsl['s'] = self::clamp( $hsl['s'] );

return $this->hsla( $hsl['h'], $hsl['s'], $hsl['l'], $hsl['a'] );
}
Expand All @@ -298,7 +301,7 @@ public function saturate( $color = null, $amount = null ) {
* @param Less_Tree_Color|null $color
* @param Less_Tree_Dimension|null $amount
*/
public function desaturate( $color = null, $amount = null ) {
public function desaturate( $color = null, $amount = null, $method = null ) {
if ( !$color instanceof Less_Tree_Color ) {
throw new Less_Exception_Compiler( 'The first argument to desaturate must be a color' . ( $color instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) );
}
Expand All @@ -307,13 +310,19 @@ public function desaturate( $color = null, $amount = null ) {
}

$hsl = $color->toHSL();
$hsl['s'] -= $amount->value / 100;

if ( isset( $method ) && $method->value === "relative" ) {
$hsl['s'] -= $hsl['s'] * $amount->value / 100;
} else {
$hsl['s'] -= $amount->value / 100;
}

$hsl['s'] = self::clamp( $hsl['s'] );

return $this->hsla( $hsl['h'], $hsl['s'], $hsl['l'], $hsl['a'] );
}

public function lighten( $color = null, $amount = null ) {
public function lighten( $color = null, $amount = null, $method = null ) {
if ( !$color instanceof Less_Tree_Color ) {
throw new Less_Exception_Compiler( 'The first argument to lighten must be a color' . ( $color instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) );
}
Expand All @@ -323,13 +332,18 @@ public function lighten( $color = null, $amount = null ) {

$hsl = $color->toHSL();

$hsl['l'] += $amount->value / 100;
if ( isset( $method ) && $method->value === "relative" ) {
$hsl['l'] += $hsl['l'] * $amount->value / 100;
} else {
$hsl['l'] += $amount->value / 100;
}

$hsl['l'] = self::clamp( $hsl['l'] );

return $this->hsla( $hsl['h'], $hsl['s'], $hsl['l'], $hsl['a'] );
}

public function darken( $color = null, $amount = null ) {
public function darken( $color = null, $amount = null, $method = null ) {
if ( !$color instanceof Less_Tree_Color ) {
throw new Less_Exception_Compiler( 'The first argument to darken must be a color' . ( $color instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) );
}
Expand All @@ -338,13 +352,17 @@ public function darken( $color = null, $amount = null ) {
}

$hsl = $color->toHSL();
$hsl['l'] -= $amount->value / 100;
if ( isset( $method ) && $method->value === "relative" ) {
$hsl['l'] -= $hsl['l'] * $amount->value / 100;
} else {
$hsl['l'] -= $amount->value / 100;
}
$hsl['l'] = self::clamp( $hsl['l'] );

return $this->hsla( $hsl['h'], $hsl['s'], $hsl['l'], $hsl['a'] );
}

public function fadein( $color = null, $amount = null ) {
public function fadein( $color = null, $amount = null, $method = null ) {
if ( !$color instanceof Less_Tree_Color ) {
throw new Less_Exception_Compiler( 'The first argument to fadein must be a color' . ( $color instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) );
}
Expand All @@ -353,12 +371,18 @@ public function fadein( $color = null, $amount = null ) {
}

$hsl = $color->toHSL();
$hsl['a'] += $amount->value / 100;

if ( isset( $method ) && $method->value === "relative" ) {
$hsl['a'] += $hsl['a'] * $amount->value / 100;
} else {
$hsl['a'] += $amount->value / 100;
}

$hsl['a'] = self::clamp( $hsl['a'] );
return $this->hsla( $hsl['h'], $hsl['s'], $hsl['l'], $hsl['a'] );
}

public function fadeout( $color = null, $amount = null ) {
public function fadeout( $color = null, $amount = null, $method = null ) {
if ( !$color instanceof Less_Tree_Color ) {
throw new Less_Exception_Compiler( 'The first argument to fadeout must be a color' . ( $color instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) );
}
Expand All @@ -367,7 +391,13 @@ public function fadeout( $color = null, $amount = null ) {
}

$hsl = $color->toHSL();
$hsl['a'] -= $amount->value / 100;

if ( isset( $method ) && $method->value === "relative" ) {
$hsl['a'] -= $hsl['a'] * $amount->value / 100;
} else {
$hsl['a'] -= $amount->value / 100;
}

$hsl['a'] = self::clamp( $hsl['a'] );
return $this->hsla( $hsl['h'], $hsl['s'], $hsl['l'], $hsl['a'] );
}
Expand Down Expand Up @@ -514,10 +544,16 @@ public function replace( $string, $pattern, $replacement, $flags = null ) {
if ( $flags && $flags->value ) {
$expr .= self::replace_flags( $flags->value );
}
$replacement = ( $replacement instanceof Less_Tree_Quoted ) ?
$replacement->value : $replacement->toCSS();

$result = preg_replace( $expr, $replacement->value, $result );
$result = preg_replace( $expr, $replacement, $result, 1 );

if ( property_exists( $string, 'quote' ) ) {
if ( $flags && $flags->value && preg_match( '/g/', $flags->value ) ) {
$result = preg_replace( $expr, $replacement, $result );
}

if ( $string instanceof Less_Tree_Quoted ) {
return new Less_Tree_Quoted( $string->quote, $result, $string->escaped );
}
return new Less_Tree_Quoted( '', $result );
Expand All @@ -533,14 +569,19 @@ public function _percent( $string, ...$args ) {
foreach ( $args as $arg ) {
if ( preg_match( '/%[sda]/i', $result, $token ) ) {
$token = $token[0];
$value = stristr( $token, 's' ) ? $arg->value : $arg->toCSS();
$value = ( ( $arg instanceof Less_Tree_Quoted ) &&
stristr( $token, 's' ) ? $arg->value : $arg->toCSS() );

$value = preg_match( '/[A-Z]$/', $token ) ? urlencode( $value ) : $value;
$result = preg_replace( '/%[sda]/i', $value, $result, 1 );
}
}
$result = str_replace( '%%', '%', $result );

return new Less_Tree_Quoted( $string->quote, $result, $string->escaped );
if ( $string instanceof Less_Tree_Quoted ) {
return new Less_Tree_Quoted( $string->quote, $result, $string->escaped );
}
return new Less_Tree_Quoted( '', $result );
}

public function unit( $val, $unit = null ) {
Expand Down Expand Up @@ -781,6 +822,10 @@ public function color( $c ) {
throw new Less_Exception_Compiler( "argument must be a color keyword or 3/6 digit hex e.g. #FFF" );
}

public function isruleset( $n ) {
return new Less_Tree_Keyword( $n instanceof Less_Tree_DetachedRuleset ? 'true' : 'false' );
}

public function iscolor( $n ) {
return new Less_Tree_Keyword( $n instanceof Less_Tree_Color ? 'true' : 'false' );
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Less/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1241,7 +1241,7 @@ private function parseEntitiesDimension() {
return;
}

$value = $this->MatchReg( '/\\G([+-]?\d*\.?\d+)(%|[a-z]+)?/' );
$value = $this->MatchReg( '/\\G([+-]?\d*\.?\d+)(%|[a-z]+)?/i' );
if ( $value ) {
if ( isset( $value[2] ) ) {
return new Less_Tree_Dimension( $value[1], $value[2] );
Expand Down
11 changes: 11 additions & 0 deletions lib/Less/Tree/Call.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ public function compile( $env = null ) {
$args[] = $a->compile( $env );
}

foreach ( $args as $key => $arg ) {
if ( $arg instanceof Less_Tree_Expression ) {
$arg->throwAwayComments();

if ( count( $arg->value ) === 1 ) {
$subNode = $arg->value[0];
array_splice( $args, $key, 1, [ $subNode ] );
}
}
}

Less_Environment::$mathOn = $currentMathContext;

$nameLC = strtolower( $this->name );
Expand Down
2 changes: 1 addition & 1 deletion lib/Less/Tree/Quoted.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Less_Tree_Quoted extends Less_Tree implements Less_Tree_HasValueProperty {
/**
* @param string $str
*/
public function __construct( $str, $content = '', $escaped = false, $index = false, $currentFileInfo = null ) {
public function __construct( $str, $content = '', $escaped = true, $index = false, $currentFileInfo = null ) {
$this->escaped = $escaped;
$this->value = $content;
if ( $str ) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Less/Tree/Unit.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function compare( $other ) {
}

public function is( $unitString ) {
return $this->toString() === $unitString;
return strtoupper( $this->toString() ) === strtoupper( $unitString );
}

public function isLength() {
Expand Down
10 changes: 5 additions & 5 deletions test/Fixtures/bug-reports/css/210.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ body .color-channels {
--saturation: 50%;
--lightness: 40%;
--hsvhue: 210;
--hsvsaturation: 67%;
--hsvsaturation: 66.66666667%;
--hsvvalue: 60%;
--red: 51;
--green: 102;
--blue: 153;
--alpha: 0.9;
--luma: 11%;
--luminance: 33%;
--luma: 11.25581169%;
--luminance: 33.4728%;
}
body .color-definitions .rgb {
color: #336699;
Expand All @@ -29,10 +29,10 @@ body .color-definitions .hsla {
color: rgba(51, 102, 153, 0.9);
}
body .color-definitions .hsv {
color: #326699;
color: #336699;
}
body .color-definitions .hsva {
color: rgba(50, 102, 153, 0.9);
color: rgba(51, 102, 153, 0.9);
}
body .css-filters .saturate {
color: saturate(200%);
Expand Down
8 changes: 4 additions & 4 deletions test/Fixtures/lessjs-2.5.3/expected/functions.css
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
luma-green: 71.52%;
luma-blue: 7.22%;
luma-yellow: 92.78%;
luma-cyan: 78.74%;
luma-cyan: 78.73999999999999%;
luma-differs-from-luminance: 23.89833349%;
luminance-white: 100%;
luminance-black: 0%;
Expand Down Expand Up @@ -96,12 +96,12 @@
pi: 3.14159265;
mod: 2m;
abs: 4%;
tan: 0.90040404;
sin: 0.17364818;
tan: 0.90040415;
sin: 0.17364819;
cos: 0.84385396;
atan: 0.1rad;
atan: 34deg;
atan: 45deg;
atan: 44.9999964deg;
pow: 64px;
pow: 64;
pow: 27;
Expand Down
Loading

0 comments on commit 29cf2b9

Please sign in to comment.