Skip to content

Commit

Permalink
Implemented less/less.js#2646
Browse files Browse the repository at this point in the history
  • Loading branch information
mishal committed Sep 18, 2015
1 parent a0fdcd9 commit aa8c805
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
26 changes: 21 additions & 5 deletions lib/ILess/Node/MixinDefinitionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ class MixinDefinitionNode extends RulesetNode
*/
public $compileFirst = true;

/**
* @var array
*/
protected $optionalParameters = [];

/**
* Constructor
*
Expand All @@ -118,7 +123,8 @@ public function __construct(
$condition = null,
$variadic = false,
$frames = []
) {
)
{
$this->name = $name;
$this->selectors = [new SelectorNode([new ElementNode(null, $name)])];

Expand All @@ -133,6 +139,8 @@ public function __construct(
foreach ($params as $p) {
if (!isset($p['name']) || ($p['name'] && !isset($p['value']))) {
$this->required++;
} else {
$this->optionalParameters[] = $p['name'];
}
}
}
Expand Down Expand Up @@ -219,7 +227,8 @@ public function compileParams(
Context $mixinEnv,
$arguments = [],
array &$compiledArguments = []
) {
)
{
$frame = new RulesetNode([], []);
$params = $this->params;
$argsCount = 0;
Expand Down Expand Up @@ -350,20 +359,27 @@ public function matchArgs(array $args, Context $context)
{
$argsLength = count($args);

$requiredArgsCount = 0;
foreach ($args as $arg) {
if (!isset($arg['name']) || !in_array($arg['name'], $this->optionalParameters)) {
$requiredArgsCount++;
}
}

if (!$this->variadic) {
if ($argsLength < $this->required) {
if ($requiredArgsCount < $this->required) {
return false;
}
if ($argsLength > count($this->params)) {
return false;
}
} else {
if ($argsLength < ($this->required - 1)) {
if ($requiredArgsCount < ($this->required - 1)) {
return false;
}
}

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

for ($i = 0; $i < $len; $i++) {
if (!isset($this->params[$i]['name']) && !isset($this->params[$i]['variadic'])) {
Expand Down
3 changes: 3 additions & 0 deletions tests/ILess/Test/Parser/_fixtures/less.js/css/mixins-args.css
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,6 @@ mixins-args-expand-op-9 {
a4: and;
a8: 5;
}
#test-mixin-matching-when-default-2645 {
height: 20px;
}
13 changes: 13 additions & 0 deletions tests/ILess/Test/Parser/_fixtures/less.js/less/mixins-args.less
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,16 @@ mixins-args-expand-op- {
a8: extract(@a, 8);
}
}
#test-mixin-matching-when-default-2645 {
.mixin(@height) {
height: @height;
}

.mixin(@width, @height: 10px) {
width: @width;

.mixin(@height: @height);
}

.mixin(@height: 20px);
}

0 comments on commit aa8c805

Please sign in to comment.