Support mixins

This commit is contained in:
Eric Lippmann 2021-12-02 15:37:43 +01:00
parent d82fc24766
commit 853fa93ea5
2 changed files with 51 additions and 1 deletions

View File

@ -38,6 +38,11 @@ class ColorPropOrVariable extends Less_Tree
$v = $this->getVariable();
$compiled = $v->compile($env);
if ($compiled instanceof ColorProp) {
// We may already have a ColorProp, which is the case with mixin calls.
return $compiled;
}
if ($compiled instanceof Less_Tree_Color) {
return ColorProp::fromColor($compiled)
->setIndex($v->index)

View File

@ -21,6 +21,16 @@ class Visitor extends Less_VisitorReplacing
*/
protected $callingVar = false;
/**
* Whether defining a mixin
*
* If that's the case, don't try to replace compiled Less colors with CSS var() function calls.
* This is handled explicitly.
*
* @var bool|string
*/
protected $definingMixin = false;
/**
* Whether defining a variable
*
@ -59,6 +69,41 @@ class Visitor extends Less_VisitorReplacing
return $rs;
}
public function visitMixinCall($c)
{
foreach ($c->arguments as $a) {
$a['value'] = $this->visitObj($a['value']);
}
return $c;
}
public function visitMixinDefinition($m)
{
if ($this->definingMixin !== false) {
throw new LogicException('Already defining mixin');
}
foreach ($m->params as $p) {
if (! isset($p['value'])) {
continue;
}
$p['value'] = $this->visitObj($p['value']);
}
$this->definingMixin = spl_object_hash($m);
return $m;
}
public function visitMixinDefinitionOut($m)
{
if ($this->definingMixin !== false && $this->definingMixin === spl_object_hash($m)) {
$this->definingMixin = false;
}
}
public function visitRule($r)
{
if ($r->name[0] === '@' && $r->variable) {
@ -81,7 +126,7 @@ class Visitor extends Less_VisitorReplacing
public function visitVariable($v)
{
if ($this->callingVar !== false || $this->definingVar !== false) {
if ($this->callingVar !== false || $this->definingMixin !== false || $this->definingVar !== false) {
return $v;
}