From b59f26879e6b9f78c641c6c8da3467fbce92d096 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Wed, 23 Jun 2021 10:59:47 +0200 Subject: [PATCH] LessParser: Add custom function `extract-variable-default` --- library/Icinga/Util/LessParser.php | 57 +++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/library/Icinga/Util/LessParser.php b/library/Icinga/Util/LessParser.php index e3439a782..0e5cccaca 100644 --- a/library/Icinga/Util/LessParser.php +++ b/library/Icinga/Util/LessParser.php @@ -3,11 +3,66 @@ namespace Icinga\Util; +use Less_Tree_Anonymous; +use Less_Tree_Expression; +use Less_Tree_Quoted; +use Less_Tree_Value; use lessc; require_once 'lessphp/lessc.inc.php'; class LessParser extends lessc { - // Overwrite methods here + public function __construct() + { + $this->registerFunction('extract-variable-default', [$this, 'extractVariableDefault']); + } + + /** + * Extract default from given variable call + * + * How to use: + * + * color: extract-variable-default(@mixin-parameter); + * color: @mixin-parameter; + * + * border: extract-variable-default(1px solid @mixin-parameter); + * border: 1px solid @mixin-parameter; + * + * background: drop-shadow(5px 0 3px extract-variable-default(@mixin-parameter, true)); + * background: drop-shadow(5px 0 3px @mixin-parameter); + * + * @param mixed $value + * @param bool $valAsDefault + * + * @return mixed + */ + public function extractVariableDefault($value, $valAsDefault = false) + { + $defaultValue = 'inherit'; + if ($value instanceof Less_Tree_Quoted) { + $stripped = preg_replace( + '~var\s*\(\s*[-\w]+\s*,\s*([^)]+)\)~', + '$1', + $value->value, + -1, + $replacements + ); + if ($replacements > 0) { + $defaultValue = $stripped; + } + } elseif ($value instanceof Less_Tree_Expression) { + foreach ($value->value as $i => $item) { + $value->value[$i] = $this->extractVariableDefault($item, true); + } + + return $value; + } + + if ($valAsDefault && $defaultValue === 'inherit') { + return $value; + } + + return new Less_Tree_Value([new Less_Tree_Anonymous($defaultValue)]); + } }