forked from scssphp/scssphp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModifiableCssNode.php
170 lines (142 loc) · 4.14 KB
/
ModifiableCssNode.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/**
* SCSSPHP
*
* @copyright 2012-2020 Leaf Corcoran
*
* @license http://opensource.org/licenses/MIT MIT
*
* @link http://scssphp.github.io/scssphp
*/
namespace ScssPhp\ScssPhp\Ast\Css;
use ScssPhp\ScssPhp\Serializer\Serializer;
use ScssPhp\ScssPhp\Visitor\ModifiableCssVisitor;
/**
* A modifiable version of {@see CssNode}.
*
* Almost all CSS nodes are the modifiable classes under the covers. However,
* modification should only be done within the evaluation step, so the
* unmodifiable types are used elsewhere to enforce that constraint.
*
* @internal
*/
abstract class ModifiableCssNode implements CssNode
{
/**
* @var ModifiableCssParentNode|null
*/
private $parent;
/**
* The index of `$this` in parent's children.
*
* This makes {@see remove} more efficient.
*
* @var int|null
*/
private $indexInParent;
/**
* @var bool
*/
private $groupEnd = false;
public function getParent(): ?ModifiableCssParentNode
{
return $this->parent;
}
protected function setParent(ModifiableCssParentNode $parent, int $indexInParent): void
{
$this->parent = $parent;
$this->indexInParent = $indexInParent;
}
public function isGroupEnd(): bool
{
return $this->groupEnd;
}
public function setGroupEnd(bool $groupEnd): void
{
$this->groupEnd = $groupEnd;
}
/**
* Whether this node has a visible sibling after it.
*/
public function hasFollowingSibling(): bool
{
$parent = $this->parent;
if ($parent === null) {
return false;
}
assert($this->indexInParent !== null);
$siblings = $parent->getChildren();
for ($i = $this->indexInParent + 1; $i < \count($siblings); $i++) {
$sibling = $siblings[$i];
if (!$this->isInvisible($sibling)) {
return true;
}
}
return false;
}
/**
* Returns whether $node is invisible for the purposes of
* {@see hasFollowingSibling}.
*
* This can return a false negative for a comment node in compressed mode,
* since the AST doesn't know the output style, but that's an extremely
* narrow edge case so we don't worry about it.
*/
private function isInvisible(CssNode $node): bool
{
if ($node instanceof CssParentNode) {
// An unknown at-rule is never invisible. Because we don't know the
// semantics of unknown rules, we can't guarantee that (for example)
// `@foo {}` isn't meaningful.
if ($node instanceof CssAtRule) {
return false;
}
if ($node instanceof CssStyleRule && $node->getSelector()->getValue()->isInvisible()) {
return true;
}
foreach ($node->getChildren() as $child) {
if (!$this->isInvisible($child)) {
return false;
}
}
return true;
}
return false;
}
/**
* Calls the appropriate visit method on $visitor.
*
* @template T
*
* @param ModifiableCssVisitor<T> $visitor
*
* @return T
*/
abstract public function accept($visitor);
/**
* Removes $this from {@see parent}'s child list.
*
* @throws \LogicException if {@see parent} is `null`.
*/
public function remove(): void
{
$parent = $this->parent;
if ($parent === null) {
throw new \LogicException("Can't remove a node without a parent.");
}
assert($this->indexInParent !== null);
$parent->removeChildAt($this->indexInParent);
$children = $parent->getChildren();
for ($i = $this->indexInParent; $i < \count($children); $i++) {
$child = $children[$i];
assert($child->indexInParent !== null);
$child->indexInParent = $child->indexInParent - 1;
}
$this->parent = null;
$this->indexInParent = null;
}
public function __toString(): string
{
return Serializer::serialize($this, true)->getCss();
}
}