Debian Codegraph @

vendor/wikimedia/less.php/lib/Less/Tree/Mixin/Definition.php

raw
1 <?php
2 /**
3 * @private
4 */
5 class Less_Tree_Mixin_Definition extends Less_Tree_Ruleset {
6 /** @var string */
7 public $name;
8 /** @var Less_Tree_Selector[] */
9 public $selectors;
10 /** @var array[] */
11 public $params;
12 /** @var int */
13 public $arity = 0;
14 /** @var Less_Tree[] */
15 public $rules;
16 /** @var array[][] */
17 public $lookups = [];
18 /** @var int */
19 public $required = 0;
20 /** @var array */
21 public $frames = [];
22 /** @var Less_Tree_Condition|null */
23 public $condition;
24 /** @var bool */
25 public $variadic;
26 /** @var string[] */
27 public $optionalParameters = [];
28
29 public function __construct( $name, $params, $rules, $condition, $variadic = false, $frames = [] ) {
30 $this->name = $name;
31 $this->selectors = [ new Less_Tree_Selector( [ new Less_Tree_Element( null, $name ) ] ) ];
32
33 $this->params = $params;
34 $this->condition = $condition;
35 $this->variadic = $variadic;
36 $this->rules = $rules;
37
38 if ( $params ) {
39 $this->arity = count( $params );
40 foreach ( $params as $p ) {
41 // NOTE: Less.js 3.13.1 does a !p.name check in the second half that we omit, because it is impossible.
42 if ( !isset( $p['name'] ) || !isset( $p['value'] ) ) {
43 $this->required++;
44 } else {
45 $this->optionalParameters[] = $p['name'];
46 }
47 }
48 }
49
50 $this->frames = $frames;
51 $this->SetRulesetIndex();
52 }
53
54 /**
55 * @param Less_Environment $env
56 * @see less-2.5.3.js#Definition.prototype.evalParams
57 */
58 public function compileParams( $env, $mixinFrames, $args = [], &$evaldArguments = [] ) {
59 $frame = new Less_Tree_Ruleset( null, [] );
60 $params = $this->params;
61 $mixinEnv = null;
62 $argsLength = 0;
63
64 if ( $args ) {
65 $argsLength = count( $args );
66 for ( $i = 0; $i < $argsLength; $i++ ) {
67 $arg = $args[$i];
68
69 if ( $arg && $arg['name'] ) {
70 $isNamedFound = false;
71
72 foreach ( $params as $j => $param ) {
73 if ( !isset( $evaldArguments[$j] ) && $arg['name'] === $param['name'] ) {
74 $evaldArguments[$j] = $arg['value']->compile( $env );
75 array_unshift( $frame->rules, new Less_Tree_Declaration( $arg['name'], $arg['value']->compile( $env ) ) );
76 $isNamedFound = true;
77 break;
78 }
79 }
80 if ( $isNamedFound ) {
81 array_splice( $args, $i, 1 );
82 $i--;
83 $argsLength--;
84 } else {
85 throw new Less_Exception_Compiler( "Named argument for " . $this->name . ' ' . $args[$i]['name'] . ' not found' );
86 }
87 }
88 }
89 }
90
91 $argIndex = 0;
92 foreach ( $params as $i => $param ) {
93 if ( isset( $evaldArguments[$i] ) ) {
94 continue;
95 }
96
97 $arg = $args[$argIndex] ?? null;
98
99 $name = $param['name'] ?? null;
100 if ( $name ) {
101 if ( isset( $param['variadic'] ) ) {
102 $varargs = [];
103 for ( $j = $argIndex; $j < $argsLength; $j++ ) {
104 $varargs[] = $args[$j]['value']->compile( $env );
105 }
106 $expression = new Less_Tree_Expression( $varargs );
107 array_unshift( $frame->rules, new Less_Tree_Declaration( $name, $expression->compile( $env ) ) );
108 } else {
109 $val = ( $arg && $arg['value'] ) ? $arg['value'] : false;
110
111 if ( $val ) {
112 // This was a mixin call, pass in a detached ruleset of it's eval'd rules
113 if ( is_array( $val ) ) {
114 $val = new Less_Tree_DetachedRuleset( new Less_Tree_Ruleset( null, $val ) );
115 } else {
116 $val = $val->compile( $env );
117 }
118 } elseif ( isset( $param['value'] ) ) {
119
120 if ( !$mixinEnv ) {
121 $mixinEnv = $env->copyEvalEnv( array_merge( [ $frame ], $mixinFrames ) );
122 }
123
124 $val = $param['value']->compile( $mixinEnv );
125 $frame->resetCache();
126 } else {
127 throw new Less_Exception_Compiler( "Wrong number of arguments for " . $this->name . " (" . $argsLength . ' for ' . $this->arity . ")" );
128 }
129
130 array_unshift( $frame->rules, new Less_Tree_Declaration( $name, $val ) );
131 $evaldArguments[$i] = $val;
132 }
133 }
134
135 if ( isset( $param['variadic'] ) && $args ) {
136 for ( $j = $argIndex; $j < $argsLength; $j++ ) {
137 $evaldArguments[$j] = $args[$j]['value']->compile( $env );
138 }
139 }
140 $argIndex++;
141 }
142
143 ksort( $evaldArguments );
144 $evaldArguments = array_values( $evaldArguments );
145
146 return $frame;
147 }
148
149 public function compile( $env ) {
150 if ( $this->frames ) {
151 return new self( $this->name, $this->params, $this->rules, $this->condition, $this->variadic, $this->frames );
152 }
153 return new self( $this->name, $this->params, $this->rules, $this->condition, $this->variadic, $env->frames );
154 }
155
156 /**
157 * @param Less_Environment $env
158 * @param array|null $args
159 * @param bool|null $important
160 * @return Less_Tree_Ruleset
161 */
162 public function evalCall( $env, $args = null, $important = null ) {
163 Less_Environment::$mixin_stack++;
164
165 $_arguments = [];
166
167 if ( $this->frames ) {
168 $mixinFrames = array_merge( $this->frames, $env->frames );
169 } else {
170 $mixinFrames = $env->frames;
171 }
172
173 $frame = $this->compileParams( $env, $mixinFrames, $args, $_arguments );
174
175 $ex = new Less_Tree_Expression( $_arguments );
176 array_unshift( $frame->rules, new Less_Tree_Declaration( '@arguments', $ex->compile( $env ) ) );
177
178 $ruleset = new Less_Tree_Ruleset( null, $this->rules );
179 $ruleset->originalRuleset = $this->ruleset_id;
180
181 $ruleSetEnv = $env->copyEvalEnv( array_merge( [ $this, $frame ], $mixinFrames ) );
182 $ruleset = $ruleset->compile( $ruleSetEnv );
183
184 if ( $important ) {
185 $ruleset = $ruleset->makeImportant();
186 }
187
188 Less_Environment::$mixin_stack--;
189
190 return $ruleset;
191 }
192
193 /**
194 * @param array $args
195 * @param Less_Environment $env
196 * @return bool
197 */
198 public function matchCondition( $args, $env ) {
199 if ( !$this->condition ) {
200 return true;
201 }
202
203 // set array to prevent error on array_merge
204 if ( !is_array( $this->frames ) ) {
205 $this->frames = [];
206 }
207
208 $frame = $this->compileParams( $env, array_merge( $this->frames, $env->frames ), $args );
209
210 $compile_env = $env->copyEvalEnv(
211 array_merge(
212 [ $frame ], // the parameter variables
213 $this->frames, // the parent namespace/mixin frames
214 $env->frames // the current environment frames
215 )
216 );
217 $compile_env->functions = $env->functions;
218
219 return (bool)$this->condition->compile( $compile_env );
220 }
221
222 public function makeImportant() {
223 $important_rules = [];
224 foreach ( $this->rules as $rule ) {
225 if ( $rule instanceof Less_Tree_Declaration || $rule instanceof self || $rule instanceof Less_Tree_NameValue ) {
226 $important_rules[] = $rule->makeImportant();
227 } else {
228 $important_rules[] = $rule;
229 }
230 }
231 return new self( $this->name, $this->params, $important_rules, $this->condition, $this->variadic, $this->frames );
232 }
233
234 /**
235 * @param array[] $args
236 * @param Less_Environment|null $env
237 * @see less-2.5.3.js#Definition.prototype.matchArgs
238 */
239 public function matchArgs( $args, $env = null ) {
240 $allArgsCnt = count( $args );
241 $requiredArgsCnt = 0;
242 foreach ( $args as $arg ) {
243 // NOTE: A positional mixin arg will have a name of null in Less_Tree_Mixin_Call::compile,
244 // which is never in the optionalParameters array.
245 if ( !in_array( $arg['name'], $this->optionalParameters, true ) ) {
246 $requiredArgsCnt++;
247 }
248 }
249 if ( !$this->variadic ) {
250 if ( $requiredArgsCnt < $this->required ) {
251 return false;
252 }
253 if ( $allArgsCnt > count( $this->params ) ) {
254 return false;
255 }
256 } else {
257 if ( $requiredArgsCnt < ( $this->required - 1 ) ) {
258 return false;
259 }
260 }
261
262 $len = min( $requiredArgsCnt, $this->arity );
263
264 for ( $i = 0; $i < $len; $i++ ) {
265 if ( !isset( $this->params[$i]['name'] ) && !isset( $this->params[$i]['variadic'] ) ) {
266 if ( $args[$i]['value']->compile( $env )->toCSS() != $this->params[$i]['value']->compile( $env )->toCSS() ) {
267 return false;
268 }
269 }
270 }
271
272 return true;
273 }
274
275 }