forked from mlocati/postcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoot.php
More file actions
119 lines (108 loc) · 3.03 KB
/
Root.php
File metadata and controls
119 lines (108 loc) · 3.03 KB
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
<?php
namespace PostCSS;
/**
* Represents a CSS file and contains all its parsed nodes.
*
* @link https://github.com/postcss/postcss/blob/master/lib/root.es6
*
* @example
* $root = \PostCSS\Parser::parse('a{color:black} b{z-index:2}');
* $root->type //=> 'root'
* count($root->nodes) //=> 2
*/
class Root extends Container
{
/**
* @param array $defaults
*/
public function __construct(array $defaults = [])
{
parent::__construct($defaults);
$this->type = 'root';
if ($this->nodes === null) {
$this->nodes = [];
}
}
/**
* {@inheritdoc}
*
* @see Container::removeChild()
*/
public function removeChild($child)
{
$child = $this->index($child);
if ($child === 0 && count($this->nodes) > 1) {
$before = $this->nodes[$child]->raws->before;
if ($before !== null) {
$this->nodes[1]->raws->before = $before;
} else {
unset($this->nodes[1]->raws->before);
}
}
return parent::removeChild($child);
}
/**
* {@inheritdoc}
*
* @see Container::normalize()
*/
public function normalize($child, $sample = null, $type = null)
{
$nodes = parent::normalize($child);
if ($sample) {
if ($type === 'prepend') {
if (count($this->nodes) > 1) {
$before = $this->nodes[1]->raws->before;
if ($before !== null) {
$sample->raws->before = $before;
} else {
unset($sample->raws->before);
}
} else {
unset($sample->raws->before);
}
} elseif ($this->first !== $sample) {
foreach ($nodes as $node) {
$before = $sample->raws->before;
if ($before !== null) {
$node->raws->before = $before;
} else {
unset($sample->raws->before);
}
}
}
}
return $nodes;
}
/**
* Returns a Result instance representing the root's CSS.
*
* @param array $opts Options with only `to` and `map` keys. Same values as LazyResult::__constructor
*
* @return Result Result with current root's CSS
*/
public function toResult(array $opts = [])
{
$lazy = new LazyResult(new Processor(), $this, $opts);
return $lazy->stringify();
}
/**
* @deprecated Use Root->removeChild
*/
public function remove()
{
$args = func_get_args();
if (count($args) !== 1) {
throw new \Exception(__METHOD__.' expects 1 parameter ($child)');
}
$child = $args[0];
$this->removeChild($child);
}
/**
* @deprecated Use Root->source->input->map
*/
public function prevMap()
{
return $this->source['input']->map;
}
}