-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCssBlock.php
More file actions
206 lines (179 loc) · 4.1 KB
/
CssBlock.php
File metadata and controls
206 lines (179 loc) · 4.1 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
class CssBlock
{
/**
* @var int
*/
protected static $instanceCount = 0;
/**
* @var bool
*/
public $isRoot = false;
/**
* @var CssBlock[]
*/
public $blocks = [];
/**
* @var string
*/
public $prelude = '';
/**
* @var string[]
*/
public $rules = [];
/**
* @var string
*/
public $type;
/**
* @var string[]
*/
public $identifiers = [];
/**
* @var string[]
*/
public $classes = [];
/**
* @var int
*/
public $id;
/**
* CssBlock constructor.
*
* @param string $prelude
* @param bool $root
*/
function __construct($prelude, $root = false)
{
$prelude = preg_replace('/\s+/sm', ' ', $prelude);
$this->prelude = trim($prelude);
$this->isRoot = $root;
$this->type = $this->getType();
$this->identifiers = $this->identifiers();
$this->classes = $this->classes();
$this->id = self::$instanceCount++;
}
/**
* @return CssBlock
*/
static function root()
{
return new self('@media all', true);
}
/**
* @param string $prelude
*
* @return CssBlock
*/
function addBlock($prelude)
{
$block = new CssBlock($prelude);
$this->blocks[] = $block;
return $block;
}
/**
* @param string $rules
*/
function addRules($rules)
{
$split = explode(';', $rules);
$trimmed = array_map('trim', $split);
$this->rules = array_merge($this->rules, $trimmed);
}
/**
* @return bool
*/
function isAtRule()
{
return strlen($this->prelude) && $this->prelude[0] === '@';
}
/**
* @return bool
*/
function isQualifiedRule()
{
return ! $this->isAtRule();
}
/**
* @return bool|string
*/
function getAtRuleType()
{
if ( ! $this->isAtRule()) {
return false;
}
$parts = explode(' ', $this->prelude);
return substr($parts[0], 1);
}
/**
* @return Generator
*/
function walk()
{
foreach ($this->blocks as $block) {
yield from $block->walk();
}
yield $this;
}
/**
* @return string
*/
function getType()
{
return $this->isAtRule() ? 'at-rule' : 'qualified-rule';
}
/**
* return array
*/
function classes()
{
$regex = "/\.{$this->identRegex()}/";
$classes = [];
if (preg_match_all($regex, $this->prelude, $matches)) {
foreach ($matches[0] as $class) {
$classes[] = ltrim($class, '.');
}
}
return $classes;
}
/**
* return array
*/
function identifiers()
{
$regex = "/\#{$this->identRegex()}/";
$identifiers = [];
if (preg_match_all($regex, $this->prelude, $matches)) {
foreach ($matches[0] as $identifier) {
$identifiers[] = ltrim($identifier, '#');
}
}
return $identifiers;
}
/**
* @return string
*/
protected function identRegex()
{
static $ident = null;
if ($ident === null) {
$h = "[0-9a-f]";
$unicode = str_replace("{h}", $h, "\{h}{1,6}(?:\r\n|[ \t\r\n\f])?");
$escape = str_replace("{unicode}", $unicode, "(?:{unicode}|\[^\r\n\f0-9a-f])");
$nonascii = "[\240-\377]";
$nmchar = str_replace(array("{nonascii}", "{escape}"), array(
$nonascii,
$escape
), "(?:[_a-z0-9-]|{nonascii}|{escape})");
$nmstart = str_replace(array("{nonascii}", "{escape}"), array(
$nonascii,
$escape
), "(?:[_a-z]|{nonascii}|{escape})");
$ident = str_replace(array("{nmstart}", "{nmchar}"), array(
$nmstart,
$nmchar
), "-?{nmstart}{nmchar}*");
}
return $ident;
}
}