1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ILess |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace ILess\Node; |
11
|
|
|
|
12
|
|
|
use ILess\Context; |
13
|
|
|
use ILess\DebugInfo; |
14
|
|
|
use ILess\FileInfo; |
15
|
|
|
use ILess\Node; |
16
|
|
|
use ILess\Output\OutputInterface; |
17
|
|
|
use ILess\Visitor\VisitorInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Directive. |
21
|
|
|
*/ |
22
|
|
|
class DirectiveNode extends Node implements |
23
|
|
|
MarkableAsReferencedInterface, ReferencedInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* The type. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $type = 'Directive'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The directive name. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
public $name; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Array of rules. |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
public $rules = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Current index. |
48
|
|
|
* |
49
|
|
|
* @var int |
50
|
|
|
*/ |
51
|
|
|
public $index = 0; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Array of variables. |
55
|
|
|
* |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
protected $variables; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Is referenced? |
62
|
|
|
* |
63
|
|
|
* @var bool |
64
|
|
|
*/ |
65
|
|
|
public $isReferenced = false; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var bool |
69
|
|
|
*/ |
70
|
|
|
public $isRooted = false; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var array |
74
|
|
|
*/ |
75
|
|
|
public $allExtends = []; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Constructor. |
79
|
|
|
* |
80
|
|
|
* @param string $name The name |
81
|
|
|
* @param array|string $value The value |
82
|
|
|
* @param RulesetNode|null|array $rules |
83
|
|
|
* @param int $index The index |
84
|
|
|
* @param FileInfo $currentFileInfo Current file info |
85
|
|
|
* @param DebugInfo $debugInfo The debug information |
86
|
|
|
* @param bool $isReferenced Is referenced? |
87
|
|
|
* @param bool $isRooted Is rooted? |
88
|
|
|
*/ |
89
|
|
|
public function __construct( |
90
|
|
|
$name, |
91
|
|
|
$value, |
92
|
|
|
$rules = null, |
93
|
|
|
$index = 0, |
94
|
|
|
FileInfo $currentFileInfo = null, |
95
|
|
|
DebugInfo $debugInfo = null, |
96
|
|
|
$isReferenced = false, |
97
|
|
|
$isRooted = false |
98
|
|
|
) { |
99
|
|
|
$this->name = $name; |
100
|
|
|
|
101
|
|
|
parent::__construct($value); |
102
|
|
|
|
103
|
|
|
if (null !== $rules) { |
104
|
|
|
if (is_array($rules)) { |
105
|
|
|
$this->rules = $rules; |
106
|
|
|
} else { |
107
|
|
|
$this->rules = [$rules]; |
108
|
|
|
$selectors = new SelectorNode([], [], null, $index, $currentFileInfo); |
109
|
|
|
$this->rules[0]->selectors = $selectors->createEmptySelectors(); |
110
|
|
|
} |
111
|
|
|
for ($i = 0; $i < count($this->rules); ++$i) { |
|
|
|
|
112
|
|
|
$this->rules[$i]->allowImports = true; |
113
|
|
|
} |
114
|
|
|
} else { |
115
|
|
|
// null |
116
|
|
|
$this->rules = $rules; |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$this->index = $index; |
120
|
|
|
$this->currentFileInfo = $currentFileInfo; |
121
|
|
|
$this->debugInfo = $debugInfo; |
122
|
|
|
$this->isReferenced = $isReferenced; |
123
|
|
|
$this->isRooted = $isRooted; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
|
|
public function accept(VisitorInterface $visitor) |
130
|
|
|
{ |
131
|
|
|
if ($this->rules) { |
132
|
|
|
$this->rules = $visitor->visitArray($this->rules); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if ($this->value) { |
136
|
|
|
$this->value = $visitor->visit($this->value); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* {@inheritdoc} |
142
|
|
|
*/ |
143
|
|
|
public function generateCSS(Context $context, OutputInterface $output) |
144
|
|
|
{ |
145
|
|
|
$value = $this->value; |
146
|
|
|
$rules = $this->rules; |
147
|
|
|
|
148
|
|
|
$output->add($this->name, $this->currentFileInfo, $this->index); |
149
|
|
|
|
150
|
|
|
if ($value) { |
151
|
|
|
$output->add(' '); |
152
|
|
|
$value->generateCSS($context, $output); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if (is_array($rules)) { |
156
|
|
|
$this->outputRuleset($context, $output, $rules); |
157
|
|
|
} else { |
158
|
|
|
$output->add(';'); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Compiles the node. |
164
|
|
|
* |
165
|
|
|
* @param Context $context The context |
166
|
|
|
* @param array|null $arguments Array of arguments |
167
|
|
|
* @param bool|null $important Important flag |
168
|
|
|
* |
169
|
|
|
* @return DirectiveNode |
170
|
|
|
*/ |
171
|
|
|
public function compile(Context $context, $arguments = null, $important = null) |
172
|
|
|
{ |
173
|
|
|
$rules = $this->rules; |
174
|
|
|
$value = $this->value; |
175
|
|
|
|
176
|
|
|
// media stored inside other directive should not bubble over it |
177
|
|
|
// backup media bubbling information |
178
|
|
|
$mediaPathBackup = $context->mediaPath; |
179
|
|
|
$mediaBlocksBackup = $context->mediaBlocks; |
180
|
|
|
|
181
|
|
|
$context->mediaPath = []; |
182
|
|
|
$context->mediaBlocks = []; |
183
|
|
|
|
184
|
|
|
if ($value) { |
185
|
|
|
$value = $value->compile($context); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
if ($rules) { |
189
|
|
|
$rules = [$rules[0]->compile($context)]; |
190
|
|
|
$rules[0]->root = true; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$context->mediaPath = $mediaPathBackup; |
194
|
|
|
$context->mediaBlocks = $mediaBlocksBackup; |
195
|
|
|
|
196
|
|
|
return new self($this->name, $value, $rules, $this->index, $this->currentFileInfo, |
197
|
|
|
$this->debugInfo, $this->isReferenced, $this->isRooted); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Returns the variable. |
202
|
|
|
* |
203
|
|
|
* @param string $name |
204
|
|
|
* |
205
|
|
|
* @return RuleNode|null |
206
|
|
|
*/ |
207
|
|
|
public function variable($name) |
208
|
|
|
{ |
209
|
|
|
if ($this->rules) { |
210
|
|
|
// assuming that there is only one rule at this point - that is how parser constructs the rule |
211
|
|
|
return $this->rules[0]->variable($name); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Finds a selector. |
217
|
|
|
* |
218
|
|
|
* @param Node $selector |
219
|
|
|
* @param Context $context |
220
|
|
|
* @param null $filter |
221
|
|
|
* |
222
|
|
|
* @return mixed |
223
|
|
|
*/ |
224
|
|
|
public function find(Node $selector, Context $context, $filter = null) |
225
|
|
|
{ |
226
|
|
|
if ($this->rules) { |
227
|
|
|
// assuming that there is only one rule at this point - that is how parser constructs the rule |
228
|
|
|
return $this->rules[0]->find($selector, $context, $this, $filter); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Returns the rulesets. |
234
|
|
|
*/ |
235
|
|
|
public function rulesets() |
236
|
|
|
{ |
237
|
|
|
if ($this->rules) { |
238
|
|
|
// assuming that there is only one rule at this point - that is how parser constructs the rule |
239
|
|
|
return $this->rules[0]->rulesets(); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Mark the directive as referenced. |
245
|
|
|
*/ |
246
|
|
|
public function markReferenced() |
247
|
|
|
{ |
248
|
|
|
$this->isReferenced = true; |
249
|
|
|
if ($this->rules) { |
250
|
|
|
for ($i = 0; $i < count($this->rules); ++$i) { |
|
|
|
|
251
|
|
|
if ($this->rules[$i] instanceof MarkableAsReferencedInterface) { |
252
|
|
|
$this->rules[$i]->markReferenced(); |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Is the directive charset directive? |
260
|
|
|
* |
261
|
|
|
* @return bool |
262
|
|
|
*/ |
263
|
|
|
public function isCharset() |
264
|
|
|
{ |
265
|
|
|
return $this->name === '@charset'; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @return bool |
270
|
|
|
*/ |
271
|
|
|
public function isRulesetLike() |
272
|
|
|
{ |
273
|
|
|
return is_array($this->rules) && !$this->isCharset(); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @return bool |
278
|
|
|
*/ |
279
|
|
|
public function getIsReferenced() |
280
|
|
|
{ |
281
|
|
|
return !$this->currentFileInfo || !$this->currentFileInfo->reference || $this->isReferenced; |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: