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\Node; |
14
|
|
|
use ILess\Visitor\VisitorInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Extend. |
18
|
|
|
*/ |
19
|
|
|
class ExtendNode extends Node |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Node type. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $type = 'Extend'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The selector. |
30
|
|
|
* |
31
|
|
|
* @var SelectorNode |
32
|
|
|
*/ |
33
|
|
|
public $selector; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The option (all). |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
public $option; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Current index. |
44
|
|
|
* |
45
|
|
|
* @var int |
46
|
|
|
*/ |
47
|
|
|
public $index = 0; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Allow before flag. |
51
|
|
|
* |
52
|
|
|
* @var bool |
53
|
|
|
*/ |
54
|
|
|
public $allowBefore = false; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Allow after flag. |
58
|
|
|
* |
59
|
|
|
* @var bool |
60
|
|
|
*/ |
61
|
|
|
public $allowAfter = false; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Array of self selectors. |
65
|
|
|
* |
66
|
|
|
* @var array |
67
|
|
|
*/ |
68
|
|
|
public $selfSelectors = []; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* First extend on this path flag. |
72
|
|
|
* |
73
|
|
|
* @var bool |
74
|
|
|
* |
75
|
|
|
* @see Visitor_ExtendFinder::visitRuleset |
76
|
|
|
*/ |
77
|
|
|
public $firstExtendOnThisSelectorPath = false; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* The ruleset. |
81
|
|
|
* |
82
|
|
|
* @var RulesetNode |
83
|
|
|
*/ |
84
|
|
|
public $ruleset; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @var int |
88
|
|
|
*/ |
89
|
|
|
public static $nextId = 0; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @var int |
93
|
|
|
*/ |
94
|
|
|
public $objectId = 0; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @var bool |
98
|
|
|
*/ |
99
|
|
|
public $hasFoundMatches = false; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Constructor. |
103
|
|
|
* |
104
|
|
|
* @param SelectorNode $selector The selector |
105
|
|
|
* @param string $option The option |
106
|
|
|
* @param int $index The index |
107
|
|
|
*/ |
108
|
|
|
public function __construct(SelectorNode $selector, $option, $index = 0) |
109
|
|
|
{ |
110
|
|
|
$this->selector = $selector; |
111
|
|
|
$this->option = $option; |
112
|
|
|
$this->index = $index; |
113
|
|
|
$this->objectId = self::$nextId++; |
114
|
|
|
$this->parentIds = [$this->objectId]; |
115
|
|
|
|
116
|
|
|
switch ($option) { |
117
|
|
|
case 'all': |
118
|
|
|
$this->allowBefore = true; |
119
|
|
|
$this->allowAfter = true; |
120
|
|
|
break; |
121
|
|
|
default: |
122
|
|
|
$this->allowBefore = false; |
123
|
|
|
$this->allowAfter = false; |
124
|
|
|
break; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @var array |
130
|
|
|
*/ |
131
|
|
|
public $parentIds = []; |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
|
|
public function accept(VisitorInterface $visitor) |
137
|
|
|
{ |
138
|
|
|
$this->selector = $visitor->visit($this->selector); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Compiles the node. |
143
|
|
|
* |
144
|
|
|
* @param Context $context The context |
145
|
|
|
* @param array|null $arguments Array of arguments |
146
|
|
|
* @param bool|null $important Important flag |
147
|
|
|
* |
148
|
|
|
* @return ExtendNode |
149
|
|
|
*/ |
150
|
|
|
public function compile(Context $context, $arguments = null, $important = null) |
151
|
|
|
{ |
152
|
|
|
return new self($this->selector->compile($context), $this->option, $this->index); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Finds selectors. |
157
|
|
|
* |
158
|
|
|
* @param array $selectors Array of ILess\ILess\Node\SelectorNode instances |
159
|
|
|
*/ |
160
|
|
|
public function findSelfSelectors($selectors) |
161
|
|
|
{ |
162
|
|
|
$selfElements = []; |
163
|
|
|
for ($i = 0; $i < count($selectors); ++$i) { |
|
|
|
|
164
|
|
|
$selectorElements = $selectors[$i]->elements; |
165
|
|
|
if ($i > 0 && count($selectorElements) && $selectorElements[0]->combinator->value === '') { |
166
|
|
|
$selectorElements[0]->combinator->value = ' '; |
167
|
|
|
} |
168
|
|
|
$selfElements = array_merge($selfElements, $selectors[$i]->elements); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$this->selfSelectors = [ |
172
|
|
|
(object) ['elements' => (array) $selfElements], |
173
|
|
|
]; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
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: