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\Exception\Exception; |
14
|
|
|
use ILess\FileInfo; |
15
|
|
|
use ILess\Output\OutputInterface; |
16
|
|
|
use ILess\Visitor\VisitorInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Media. |
20
|
|
|
*/ |
21
|
|
|
class MediaNode extends DirectiveNode |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Media type. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $type = 'Media'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Current index. |
32
|
|
|
* |
33
|
|
|
* @var int |
34
|
|
|
*/ |
35
|
|
|
public $index = 0; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Features. |
39
|
|
|
* |
40
|
|
|
* @var ValueNode |
41
|
|
|
*/ |
42
|
|
|
public $features; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Rules. |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
public $rules = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Referenced flag. |
53
|
|
|
* |
54
|
|
|
* @var bool |
55
|
|
|
*/ |
56
|
|
|
public $isReferenced = false; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var array |
60
|
|
|
*/ |
61
|
|
|
public $allExtends = []; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Constructor. |
65
|
|
|
* |
66
|
|
|
* @param array $value The array of values |
67
|
|
|
* @param array $features The array of features |
68
|
|
|
* @param int $index The index |
69
|
|
|
* @param FileInfo $currentFileInfo The current file info |
70
|
|
|
*/ |
71
|
|
|
public function __construct( |
72
|
|
|
array $value = [], |
73
|
|
|
array $features = [], |
74
|
|
|
$index = 0, |
75
|
|
|
FileInfo $currentFileInfo = null |
76
|
|
|
) { |
77
|
|
|
$this->index = $index; |
78
|
|
|
$this->currentFileInfo = $currentFileInfo; |
79
|
|
|
|
80
|
|
|
$selector = new SelectorNode([], [], null, $this->index, $this->currentFileInfo); |
81
|
|
|
$selectors = $selector->createEmptySelectors(); |
82
|
|
|
|
83
|
|
|
$this->features = new ValueNode($features); |
84
|
|
|
$this->rules = [new RulesetNode($selectors, $value)]; |
85
|
|
|
$this->rules[0]->allowImports = true; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
|
|
public function accept(VisitorInterface $visitor) |
92
|
|
|
{ |
93
|
|
|
if ($this->features) { |
94
|
|
|
$this->features = $visitor->visit($this->features); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->rules = $visitor->visitArray($this->rules); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
|
public function generateCSS(Context $context, OutputInterface $output) |
104
|
|
|
{ |
105
|
|
|
$output->add('@media ', $this->currentFileInfo, $this->index); |
106
|
|
|
$this->features->generateCSS($context, $output); |
107
|
|
|
$this->outputRuleset($context, $output, $this->rules); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Compiles the node. |
112
|
|
|
* |
113
|
|
|
* @param Context $context The context |
114
|
|
|
* @param array|null $arguments Array of arguments |
115
|
|
|
* @param bool|null $important Important flag |
116
|
|
|
* |
117
|
|
|
* @return RulesetNode |
118
|
|
|
*/ |
119
|
|
|
public function compile(Context $context, $arguments = null, $important = null) |
120
|
|
|
{ |
121
|
|
|
if (!$context->mediaBlocks) { |
122
|
|
|
$context->mediaBlocks = []; |
123
|
|
|
$context->mediaPath = []; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$media = new self([], [], $this->index, $this->currentFileInfo); |
127
|
|
|
|
128
|
|
|
if ($this->debugInfo) { |
129
|
|
|
$this->rules[0]->debugInfo = $this->debugInfo; |
130
|
|
|
$media->debugInfo = $this->debugInfo; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$strictMathBypass = false; |
134
|
|
|
if (!$context->strictMath) { |
135
|
|
|
$strictMathBypass = true; |
136
|
|
|
$context->strictMath = true; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
try { |
140
|
|
|
$media->features = $this->features->compile($context); |
141
|
|
|
} catch (Exception $e) { |
142
|
|
|
// empty on purpose |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if ($strictMathBypass) { |
146
|
|
|
$context->strictMath = false; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$context->mediaPath[] = $media; |
150
|
|
|
$context->mediaBlocks[] = $media; |
151
|
|
|
|
152
|
|
|
$this->rules[0]->functionRegistry = |
153
|
|
|
isset($context->frames[0]) && $context->frames[0]->functionRegistry ? |
154
|
|
|
$context->frames[0]->functionRegistry->inherit() : |
155
|
|
|
$context->getFunctionRegistry()->inherit(); |
156
|
|
|
|
157
|
|
|
array_unshift($context->frames, $this->rules[0]); |
158
|
|
|
$media->rules = [$this->rules[0]->compile($context)]; |
159
|
|
|
array_shift($context->frames); |
160
|
|
|
|
161
|
|
|
array_pop($context->mediaPath); |
162
|
|
|
|
163
|
|
|
return count($context->mediaPath) == 0 ? $media->compileTop($context) : $media->compileNested($context); |
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Compiles top media. |
168
|
|
|
* |
169
|
|
|
* @param Context $context |
170
|
|
|
* |
171
|
|
|
* @return RulesetNode |
172
|
|
|
*/ |
173
|
|
|
public function compileTop(Context $context) |
174
|
|
|
{ |
175
|
|
|
$result = $this; |
176
|
|
|
if (count($context->mediaBlocks) > 1) { |
177
|
|
|
$selector = new SelectorNode([], [], null, $this->index, $this->currentFileInfo); |
178
|
|
|
$selectors = $selector->createEmptySelectors(); |
179
|
|
|
|
180
|
|
|
$result = new RulesetNode($selectors, $context->mediaBlocks); |
181
|
|
|
$result->multiMedia = true; |
182
|
|
|
} |
183
|
|
|
$context->mediaBlocks = []; |
184
|
|
|
$context->mediaPath = []; |
185
|
|
|
|
186
|
|
|
return $result; |
|
|
|
|
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Compiles nested media. |
191
|
|
|
* |
192
|
|
|
* @param Context $context |
193
|
|
|
* |
194
|
|
|
* @return RulesetNode |
195
|
|
|
*/ |
196
|
|
|
public function compileNested(Context $context) |
197
|
|
|
{ |
198
|
|
|
$path = array_merge($context->mediaPath, [$this]); |
199
|
|
|
|
200
|
|
|
// Extract the media-query conditions separated with `,` (OR). |
201
|
|
|
foreach ($path as $key => $p) { |
202
|
|
|
$value = $p->features instanceof ValueNode ? $p->features->value : $p->features; |
203
|
|
|
$path[$key] = is_array($value) ? $value : [$value]; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
// Trace all permutations to generate the resulting media-query. |
207
|
|
|
// |
208
|
|
|
// (a, b and c) with nested (d, e) -> |
209
|
|
|
//a and d |
210
|
|
|
//a and e |
211
|
|
|
//b and c and d |
212
|
|
|
//b and c and e |
213
|
|
|
|
214
|
|
|
$permuted = $this->permute($path); |
215
|
|
|
$expressions = []; |
216
|
|
|
foreach ($permuted as $path) { |
217
|
|
|
for ($i = 0, $len = count($path); $i < $len; ++$i) { |
218
|
|
|
$path[$i] = $path[$i] instanceof GenerateCSSInterface ? $path[$i] : new AnonymousNode($path[$i]); |
219
|
|
|
} |
220
|
|
|
for ($i = count($path) - 1; $i > 0; --$i) { |
221
|
|
|
array_splice($path, $i, 0, [new AnonymousNode('and')]); |
222
|
|
|
} |
223
|
|
|
$expressions[] = new ExpressionNode($path); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$this->features = new ValueNode($expressions); |
227
|
|
|
|
228
|
|
|
// Fake a tree-node that doesn't output anything. |
229
|
|
|
return new RulesetNode([], []); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Creates permutations. |
234
|
|
|
* |
235
|
|
|
* @param array $array The array |
236
|
|
|
* |
237
|
|
|
* @return array |
238
|
|
|
*/ |
239
|
|
|
public function permute(array $array) |
240
|
|
|
{ |
241
|
|
|
if (!count($array)) { |
242
|
|
|
return []; |
243
|
|
|
} elseif (count($array) === 1) { |
244
|
|
|
return $array[0]; |
245
|
|
|
} else { |
246
|
|
|
$result = []; |
247
|
|
|
$rest = $this->permute(array_slice($array, 1)); |
248
|
|
|
foreach ($rest as $r) { |
249
|
|
|
foreach ($array[0] as $a) { |
250
|
|
|
$result[] = array_merge( |
251
|
|
|
is_array($a) ? $a : [$a], is_array($r) ? $r : [$r] |
252
|
|
|
); |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return $result; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Bubbles the selectors. |
262
|
|
|
* |
263
|
|
|
* @param array $selectors |
264
|
|
|
*/ |
265
|
|
|
public function bubbleSelectors(array $selectors) |
266
|
|
|
{ |
267
|
|
|
if (!$selectors) { |
268
|
|
|
return; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
$this->rules = [ |
272
|
|
|
new RulesetNode($selectors, [$this->rules[0]]), |
273
|
|
|
]; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @return bool |
278
|
|
|
*/ |
279
|
|
|
public function isRulesetLike() |
280
|
|
|
{ |
281
|
|
|
return true; |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..