Skip to content

Commit

Permalink
Merge "Support expand operator in Mixin Call"
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Apr 24, 2024
2 parents 7d05070 + d85ae42 commit 394ef4c
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 20 deletions.
27 changes: 16 additions & 11 deletions lib/Less/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1521,6 +1521,7 @@ private function parseMixinArgs( $isCall ) {
$expressionContainsNamed = null;
$name = null;
$returner = [ 'args' => [], 'variadic' => false ];
$expand = false;

$this->save();

Expand Down Expand Up @@ -1591,17 +1592,21 @@ private function parseMixinArgs( $isCall ) {
}

$nameLoop = ( $name = $val->name );
} elseif ( !$isCall && $this->matchStr( '...' ) ) {
$returner['variadic'] = true;
if ( $this->matchChar( ";" ) && !$isSemiColonSeperated ) {
$isSemiColonSeperated = true;
}
if ( $isSemiColonSeperated ) {
$argsSemiColon[] = [ 'name' => $arg->name, 'variadic' => true ];
} elseif ( $this->matchStr( '...' ) ) {
if ( !$isCall ) {
$returner['variadic'] = true;
if ( $this->matchChar( ";" ) && !$isSemiColonSeperated ) {
$isSemiColonSeperated = true;
}
if ( $isSemiColonSeperated ) {
$argsSemiColon[] = [ 'name' => $arg->name, 'variadic' => true ];
} else {
$argsComma[] = [ 'name' => $arg->name, 'variadic' => true ];
}
break;
} else {
$argsComma[] = [ 'name' => $arg->name, 'variadic' => true ];
$expand = true;
}
break;
} elseif ( !$isCall ) {
$name = $nameLoop = $val->name;
$value = null;
Expand All @@ -1612,7 +1617,7 @@ private function parseMixinArgs( $isCall ) {
$expressions[] = $value;
}

$argsComma[] = [ 'name' => $nameLoop, 'value' => $value ];
$argsComma[] = [ 'name' => $nameLoop, 'value' => $value, 'expand' => $expand ];

if ( $this->matchChar( ',' ) ) {
continue;
Expand All @@ -1629,7 +1634,7 @@ private function parseMixinArgs( $isCall ) {
if ( count( $expressions ) > 1 ) {
$value = new Less_Tree_Value( $expressions );
}
$argsSemiColon[] = [ 'name' => $name, 'value' => $value ];
$argsSemiColon[] = [ 'name' => $name, 'value' => $value, 'expand' => $expand ];

$name = null;
$expressions = [];
Expand Down
9 changes: 8 additions & 1 deletion lib/Less/Tree/Mixin/Call.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ public function compile( $env ) {

$args = [];
foreach ( $this->arguments as $a ) {
$args[] = [ 'name' => $a['name'], 'value' => $a['value']->compile( $env ) ];
$argValue = $a['value']->compile( $env );
if ( ( $a['expand'] ) && is_array( $argValue->value ) ) {
foreach ( $argValue->value as $value ) {
$args[] = [ 'name' => null, 'value' => $value ];
}
} else {
$args[] = [ 'name' => $a['name'], 'value' => $argValue ];
}
}

$defNone = 0;
Expand Down
25 changes: 19 additions & 6 deletions lib/Less/Tree/Mixin/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Less_Tree_Mixin_Definition extends Less_Tree_Ruleset {
public $frames = [];
public $condition;
public $variadic;
public $optionalParameters = [];

public function __construct( $name, $params, $rules, $condition, $variadic = false, $frames = [] ) {
$this->name = $name;
Expand All @@ -28,6 +29,8 @@ public function __construct( $name, $params, $rules, $condition, $variadic = fal
foreach ( $params as $p ) {
if ( !isset( $p['name'] ) || ( $p['name'] && !isset( $p['value'] ) ) ) {
$this->required++;
} else {
$this->optionalParameters[ (string)$p['name'] ] = true;
}
}
}
Expand Down Expand Up @@ -206,23 +209,33 @@ public function makeImportant() {
return new self( $this->name, $this->params, $important_rules, $this->condition, $this->variadic, $this->frames );
}

/**
* @param array[] $args
* @param Less_Environment|null $env
* @see less-2.5.3.js#Definition.prototype.matchArgs
*/
public function matchArgs( $args, $env = null ) {
$argsLength = count( $args );

$allArgsCnt = count( $args );
$requiredArgsCnt = 0;
foreach ( $args as $arg ) {
if ( !array_key_exists( $arg['name'], $this->optionalParameters ) ) {
$requiredArgsCnt++;
}
}
if ( !$this->variadic ) {
if ( $argsLength < $this->required ) {
if ( $requiredArgsCnt < $this->required ) {
return false;
}
if ( $argsLength > count( $this->params ) ) {
if ( $allArgsCnt > count( $this->params ) ) {
return false;
}
} else {
if ( $argsLength < ( $this->required - 1 ) ) {
if ( $requiredArgsCnt < ( $this->required - 1 ) ) {
return false;
}
}

$len = min( $argsLength, $this->arity );
$len = min( $requiredArgsCnt, $this->arity );

for ( $i = 0; $i < $len; $i++ ) {
if ( !isset( $this->params[$i]['name'] ) && !isset( $this->params[$i]['variadic'] ) ) {
Expand Down
163 changes: 163 additions & 0 deletions test/Fixtures/lessjs-2.5.3/override/mixins-args.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
#hidden {
color: transparent;
}
#hidden1 {
color: transparent;
}
.two-args {
color: blue;
width: 10px;
height: 99%;
border: 2px dotted black;
}
.one-arg {
width: 15px;
height: 49%;
}
.no-parens {
width: 5px;
height: 49%;
}
.no-args {
width: 5px;
height: 49%;
}
.var-args {
width: 45;
height: 17%;
}
.multi-mix {
width: 10px;
height: 29%;
margin: 4;
padding: 5;
}
body {
padding: 30px;
color: #f00;
}
.scope-mix {
width: 8;
}
.content {
width: 600px;
}
.content .column {
margin: 600px;
}
#same-var-name {
radius: 5px;
}
#var-inside {
width: 10px;
}
.arguments {
border: 1px solid black;
width: 1px;
}
.arguments2 {
border: 0px;
width: 0px;
}
.arguments3 {
border: 0px;
width: 0px;
}
.arguments4 {
border: 0 1 2 3 4;
rest: 1 2 3 4;
width: 0;
}
.edge-case {
border: "{";
width: "{";
}
.slash-vs-math {
border-radius: 0.4px;
border-radius: 0.5px;
border-radius: 6px;
}
.comma-vs-semi-colon {
one: a;
two: b, c;
one: d, e;
two: f;
one: g;
one: h;
one: i;
one: j;
one: k;
two: l;
one: m, n;
one: o, p;
two: q;
one: r, s;
two: t;
}
#named-conflict {
four: a, 11, 12, 13;
four: a, 21, 22, 23;
}
.test-mixin-default-arg {
defaults: 1px 1px 1px;
defaults: 2px 2px 2px;
}
.selector {
margin: 2, 2, 2, 2;
}
.selector2 {
margin: 2, 2, 2, 2;
}
.selector3 {
margin: 4;
}
mixins-args-expand-op-1 {
m3: 1, 2, 3;
}
mixins-args-expand-op-2 {
m3: 4, 5, 6;
}
mixins-args-expand-op-3a {
m3: a, b, c;
}
mixins-args-expand-op-3b {
m4: 0, a, b, c;
}
mixins-args-expand-op-3c {
m4: a, b, c, 4;
}
mixins-args-expand-op-4a {
m3: a, b, c, d;
}
mixins-args-expand-op-4b {
m4: 0, a, b, c, d;
}
mixins-args-expand-op-4c {
m4: a, b, c, d, 4;
}
mixins-args-expand-op-5a {
m3: 1, 2, 3;
}
mixins-args-expand-op-5b {
m4: 0, 1, 2, 3;
}
mixins-args-expand-op-5c {
m4: 1, 2, 3, 4;
}
mixins-args-expand-op-6 {
m4: 0, 1, 2, 3;
}
mixins-args-expand-op-7 {
m4: 0, 1, 2, 3;
}
mixins-args-expand-op-8 {
m4: 1, 1.5, 2, 3;
}
mixins-args-expand-op-9 {
aa: 4 5 6 1 2 3 and again 4 5 6;
a4: and;
a8: 5;
}
#test-mixin-matching-when-default-2645 {
height: 20px;
}
2 changes: 0 additions & 2 deletions test/phpunit/FixturesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ class phpunit_FixturesTest extends phpunit_bootstrap {
// Temporary disabled
'css' => true, // T352911 & T352866
'import-reference' => true, // T352862
'mixin-args' => true, // T352897
'mixins-args' => true, // T352897
'mixins-guards' => true, // T352867
'urls' => true, // T353147
'variables' => true, // T352830, T352866
Expand Down

0 comments on commit 394ef4c

Please sign in to comment.