From 2c3bc6ea95f37055a2c14dfa9f0b0fc9cf7e3290 Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Tue, 12 Jul 2022 13:57:06 +0200 Subject: [PATCH 1/2] Use `Call::fromCall()` in `Visitor::visitCall()` irrespective of calling function name --- library/Icinga/Less/Visitor.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/library/Icinga/Less/Visitor.php b/library/Icinga/Less/Visitor.php index 2b574febf0..2a0853b246 100644 --- a/library/Icinga/Less/Visitor.php +++ b/library/Icinga/Less/Visitor.php @@ -65,11 +65,9 @@ class Visitor extends Less_VisitorReplacing public function visitCall($c) { - if ($c->name !== 'var') { - // We need to use our own tree call class , so that we can precompile the arguments before making - // the actual LESS function calls. Otherwise, it will produce lots of invalid argument exceptions! - $c = Call::fromCall($c); - } + // We need to use our own tree call class , so that we can precompile the arguments before making + // the actual LESS function calls. Otherwise, it will produce lots of invalid argument exceptions! + $c = Call::fromCall($c); return $c; } From 67116541460b25565e07243602c2e4bc3b837c59 Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Tue, 12 Jul 2022 14:45:01 +0200 Subject: [PATCH 2/2] Avoid rendering duplicated CSS definitions in `var()` function calls In `Call::compile()` in case we have CSS var() function calls and the keyword value of the function call prefixed by `--` and color property name prefixed by `@` are same, then avoid generation of duplicate CSS definitions. --- library/Icinga/Less/Call.php | 31 ++++++++-- .../library/Icinga/Util/LessParserTest.php | 60 +++++++++++++++++++ 2 files changed, 86 insertions(+), 5 deletions(-) diff --git a/library/Icinga/Less/Call.php b/library/Icinga/Less/Call.php index 0a78cb541f..67fa73e00c 100644 --- a/library/Icinga/Less/Call.php +++ b/library/Icinga/Less/Call.php @@ -6,6 +6,7 @@ use Less_Tree_Call; use Less_Tree_Color; +use Less_Tree_Keyword; use Less_Tree_Value; use Less_Tree_Variable; @@ -23,12 +24,17 @@ public function compile($env = null) return parent::compile($env); } + $keyWord = null; foreach ($this->args as $arg) { if (! is_array($arg->value)) { continue; } $name = null; + if ($arg->value[0] instanceof Less_Tree_Keyword) { + $keyWord = $arg->value[0]->value; + } + if ($arg->value[0] instanceof Less_Tree_Variable) { // This is the case when defining a variable with a callable LESS rules such as fade, fadeout.. // Example: `@foo: #fff; @foo-bar: fade(@foo, 10);` @@ -55,13 +61,28 @@ public function compile($env = null) $vr->compile($env); } - // Get the uppermost variable of the variable references - while (! $vr instanceof ColorProp) { - $vr = $vr->getRef(); + if ($this->name === 'var') { + // If calling var() CSS function, get the next variable in variable references + // if keyword value and color property name are same + if (substr($keyWord ?? '', 2) === $vr->getName()) { + // Get the uppermost variable of the variable references in case the keyword + // value and color property name are same. + $vr = $vr->getRef(); + } + } else { + // If not calling var() CSS function get the uppermost variable of the + // variable references + while (! $vr instanceof ColorProp) { + $vr = $vr->getRef(); + } } } elseif ($vr instanceof Less_Tree_Color) { - $vr = ColorProp::fromColor($vr); - $vr->setName($name); + // Only if keyword value and color property name are not same then set variable to + // ColorProp::fromColor($vr) + if (substr($keyWord ?? '', 2) !== substr($name, 1)) { + $vr = ColorProp::fromColor($vr); + $vr->setName($name); + } } $arg->value[0] = $vr; diff --git a/test/php/library/Icinga/Util/LessParserTest.php b/test/php/library/Icinga/Util/LessParserTest.php index 7f36c52cb1..f089c5cb9f 100644 --- a/test/php/library/Icinga/Util/LessParserTest.php +++ b/test/php/library/Icinga/Util/LessParserTest.php @@ -90,6 +90,66 @@ public function testNestedVariables() ); } + public function testNestedCssDefinitions() + { + $this->assertEquals( + <<compileLess(<<assertEquals( + <<compileLess(<<assertEquals(