]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - vendor/oyejorge/less.php/lib/Less/Tree/Mixin/Definition.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / oyejorge / less.php / lib / Less / Tree / Mixin / Definition.php
1 <?php
2
3 class Less_Tree_Mixin_Definition extends Less_Tree_Ruleset{
4         public $name;
5         public $selectors;
6         public $params;
7         public $arity           = 0;
8         public $rules;
9         public $lookups         = array();
10         public $required        = 0;
11         public $frames          = array();
12         public $condition;
13         public $variadic;
14         public $type            = 'MixinDefinition';
15
16
17         // less.js : /lib/less/tree/mixin.js : tree.mixin.Definition
18         public function __construct($name, $params, $rules, $condition, $variadic = false, $frames = array() ){
19                 $this->name = $name;
20                 $this->selectors = array(new Less_Tree_Selector(array( new Less_Tree_Element(null, $name))));
21
22                 $this->params = $params;
23                 $this->condition = $condition;
24                 $this->variadic = $variadic;
25                 $this->rules = $rules;
26
27                 if( $params ){
28                         $this->arity = count($params);
29                         foreach( $params as $p ){
30                                 if (! isset($p['name']) || ($p['name'] && !isset($p['value']))) {
31                                         $this->required++;
32                                 }
33                         }
34                 }
35
36                 $this->frames = $frames;
37                 $this->SetRulesetIndex();
38         }
39
40
41
42         //function accept( $visitor ){
43         //      $this->params = $visitor->visit($this->params);
44         //      $this->rules = $visitor->visit($this->rules);
45         //      $this->condition = $visitor->visit($this->condition);
46         //}
47
48
49         public function toCSS(){
50                 return '';
51         }
52
53         // less.js : /lib/less/tree/mixin.js : tree.mixin.Definition.evalParams
54         public function compileParams($env, $mixinFrames, $args = array() , &$evaldArguments = array() ){
55                 $frame = new Less_Tree_Ruleset(null, array());
56                 $params = $this->params;
57                 $mixinEnv = null;
58                 $argsLength = 0;
59
60                 if( $args ){
61                         $argsLength = count($args);
62                         for($i = 0; $i < $argsLength; $i++ ){
63                                 $arg = $args[$i];
64
65                                 if( $arg && $arg['name'] ){
66                                         $isNamedFound = false;
67
68                                         foreach($params as $j => $param){
69                                                 if( !isset($evaldArguments[$j]) && $arg['name'] === $params[$j]['name']) {
70                                                         $evaldArguments[$j] = $arg['value']->compile($env);
71                                                         array_unshift($frame->rules, new Less_Tree_Rule( $arg['name'], $arg['value']->compile($env) ) );
72                                                         $isNamedFound = true;
73                                                         break;
74                                                 }
75                                         }
76                                         if ($isNamedFound) {
77                                                 array_splice($args, $i, 1);
78                                                 $i--;
79                                                 $argsLength--;
80                                                 continue;
81                                         } else {
82                                                 throw new Less_Exception_Compiler("Named argument for " . $this->name .' '.$args[$i]['name'] . ' not found');
83                                         }
84                                 }
85                         }
86                 }
87
88                 $argIndex = 0;
89                 foreach($params as $i => $param){
90
91                         if ( isset($evaldArguments[$i]) ){ continue; }
92
93                         $arg = null;
94                         if( isset($args[$argIndex]) ){
95                                 $arg = $args[$argIndex];
96                         }
97
98                         if (isset($param['name']) && $param['name']) {
99
100                                 if( isset($param['variadic']) ){
101                                         $varargs = array();
102                                         for ($j = $argIndex; $j < $argsLength; $j++) {
103                                                 $varargs[] = $args[$j]['value']->compile($env);
104                                         }
105                                         $expression = new Less_Tree_Expression($varargs);
106                                         array_unshift($frame->rules, new Less_Tree_Rule($param['name'], $expression->compile($env)));
107                                 }else{
108                                         $val = ($arg && $arg['value']) ? $arg['value'] : false;
109
110                                         if ($val) {
111                                                 $val = $val->compile($env);
112                                         } else if ( isset($param['value']) ) {
113
114                                                 if( !$mixinEnv ){
115                                                         $mixinEnv = new Less_Environment();
116                                                         $mixinEnv->frames = array_merge( array($frame), $mixinFrames);
117                                                 }
118
119                                                 $val = $param['value']->compile($mixinEnv);
120                                                 $frame->resetCache();
121                                         } else {
122                                                 throw new Less_Exception_Compiler("Wrong number of arguments for " . $this->name . " (" . $argsLength . ' for ' . $this->arity . ")");
123                                         }
124
125                                         array_unshift($frame->rules, new Less_Tree_Rule($param['name'], $val));
126                                         $evaldArguments[$i] = $val;
127                                 }
128                         }
129
130                         if ( isset($param['variadic']) && $args) {
131                                 for ($j = $argIndex; $j < $argsLength; $j++) {
132                                         $evaldArguments[$j] = $args[$j]['value']->compile($env);
133                                 }
134                         }
135                         $argIndex++;
136                 }
137
138                 ksort($evaldArguments);
139                 $evaldArguments = array_values($evaldArguments);
140
141                 return $frame;
142         }
143
144         public function compile($env) {
145                 if( $this->frames ){
146                         return new Less_Tree_Mixin_Definition($this->name, $this->params, $this->rules, $this->condition, $this->variadic, $this->frames );
147                 }
148                 return new Less_Tree_Mixin_Definition($this->name, $this->params, $this->rules, $this->condition, $this->variadic, $env->frames );
149         }
150
151         public function evalCall($env, $args = NULL, $important = NULL) {
152
153                 Less_Environment::$mixin_stack++;
154
155                 $_arguments = array();
156
157                 if( $this->frames ){
158                         $mixinFrames = array_merge($this->frames, $env->frames);
159                 }else{
160                         $mixinFrames = $env->frames;
161                 }
162
163                 $frame = $this->compileParams($env, $mixinFrames, $args, $_arguments);
164
165                 $ex = new Less_Tree_Expression($_arguments);
166                 array_unshift($frame->rules, new Less_Tree_Rule('@arguments', $ex->compile($env)));
167
168
169                 $ruleset = new Less_Tree_Ruleset(null, $this->rules);
170                 $ruleset->originalRuleset = $this->ruleset_id;
171
172
173                 $ruleSetEnv = new Less_Environment();
174                 $ruleSetEnv->frames = array_merge( array($this, $frame), $mixinFrames );
175                 $ruleset = $ruleset->compile( $ruleSetEnv );
176
177                 if( $important ){
178                         $ruleset = $ruleset->makeImportant();
179                 }
180
181                 Less_Environment::$mixin_stack--;
182
183                 return $ruleset;
184         }
185
186
187         public function matchCondition($args, $env) {
188
189                 if( !$this->condition ){
190                         return true;
191                 }
192
193                 // set array to prevent error on array_merge
194                 if(!is_array($this->frames)) {
195              $this->frames = array();
196         }
197
198                 $frame = $this->compileParams($env, array_merge($this->frames,$env->frames), $args );
199
200                 $compile_env = new Less_Environment();
201                 $compile_env->frames = array_merge(
202                                 array($frame)           // the parameter variables
203                                 , $this->frames         // the parent namespace/mixin frames
204                                 , $env->frames          // the current environment frames
205                         );
206
207                 $compile_env->functions = $env->functions;
208
209                 return (bool)$this->condition->compile($compile_env);
210         }
211
212         public function matchArgs($args, $env = NULL){
213                 $argsLength = count($args);
214
215                 if( !$this->variadic ){
216                         if( $argsLength < $this->required ){
217                                 return false;
218                         }
219                         if( $argsLength > count($this->params) ){
220                                 return false;
221                         }
222                 }else{
223                         if( $argsLength < ($this->required - 1)){
224                                 return false;
225                         }
226                 }
227
228                 $len = min($argsLength, $this->arity);
229
230                 for( $i = 0; $i < $len; $i++ ){
231                         if( !isset($this->params[$i]['name']) && !isset($this->params[$i]['variadic']) ){
232                                 if( $args[$i]['value']->compile($env)->toCSS() != $this->params[$i]['value']->compile($env)->toCSS() ){
233                                         return false;
234                                 }
235                         }
236                 }
237
238                 return true;
239         }
240
241 }