-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathRule.php
More file actions
274 lines (245 loc) · 6.68 KB
/
Copy pathRule.php
File metadata and controls
274 lines (245 loc) · 6.68 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
declare(strict_types=1);
namespace Sabberworm\CSS\Rule;
use Sabberworm\CSS\Comment\Comment;
use Sabberworm\CSS\Comment\Commentable;
use Sabberworm\CSS\OutputFormat;
use Sabberworm\CSS\Parsing\ParserState;
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
use Sabberworm\CSS\Renderable;
use Sabberworm\CSS\Value\RuleValueList;
use Sabberworm\CSS\Value\Value;
/**
* `Rule`s just have a string key (the rule) and a 'Value'.
*
* In CSS, `Rule`s are expressed as follows: “key: value[0][0] value[0][1], value[1][0] value[1][1];”
*/
class Rule implements Renderable, Commentable
{
/**
* @var string
*/
private $rule;
/**
* @var RuleValueList|string|null
*/
private $value;
/**
* @var bool
*/
private $isImportant = false;
/**
* @var int<0, max> $lineNumber
*/
protected $lineNumber;
/**
* @var int
*
* @internal since 8.8.0
*/
protected $columnNumber;
/**
* @var array<array-key, Comment>
*
* @internal since 8.8.0
*/
protected $comments = [];
/**
* @param string $rule
* @param int<0, max> $lineNumber
* @param int $columnNumber
*/
public function __construct($rule, int $lineNumber = 0, $columnNumber = 0)
{
$this->rule = $rule;
$this->lineNumber = $lineNumber;
$this->columnNumber = $columnNumber;
}
/**
* @param list<Comment> $commentsBeforeRule
*
* @throws UnexpectedEOFException
* @throws UnexpectedTokenException
*
* @internal since V8.8.0
*/
public static function parse(ParserState $parserState, array $commentsBeforeRule = []): Rule
{
$comments = \array_merge($commentsBeforeRule, $parserState->consumeWhiteSpace());
$rule = new Rule(
$parserState->parseIdentifier(!$parserState->comes('--')),
$parserState->currentLine(),
$parserState->currentColumn()
);
$rule->setComments($comments);
$rule->addComments($parserState->consumeWhiteSpace());
$parserState->consume(':');
$value = Value::parseValue($parserState, self::listDelimiterForRule($rule->getRule()));
$rule->setValue($value);
$parserState->consumeWhiteSpace();
if ($parserState->comes('!')) {
$parserState->consume('!');
$parserState->consumeWhiteSpace();
$parserState->consume('important');
$rule->setIsImportant(true);
}
$parserState->consumeWhiteSpace();
while ($parserState->comes(';')) {
$parserState->consume(';');
}
return $rule;
}
/**
* Returns a list of delimiters (or separators).
* The first item is the innermost separator (or, put another way, the highest-precedence operator).
* The sequence continues to the outermost separator (or lowest-precedence operator).
*
* @param string $rule
*
* @return list<non-empty-string>
*/
private static function listDelimiterForRule($rule): array
{
if (\preg_match('/^font($|-)/', $rule)) {
return [',', '/', ' '];
}
switch ($rule) {
case 'src':
return [' ', ','];
default:
return [',', ' ', '/'];
}
}
/**
* @return int<0, max>
*/
public function getLineNo(): int
{
return $this->lineNumber;
}
/**
* @return int
*/
public function getColNo()
{
return $this->columnNumber;
}
/**
* @param int<0, max> $lineNumber
* @param int $columnNumber
*/
public function setPosition(int $lineNumber, $columnNumber): void
{
$this->columnNumber = $columnNumber;
$this->lineNumber = $lineNumber;
}
/**
* @param string $rule
*/
public function setRule($rule): void
{
$this->rule = $rule;
}
/**
* @return string
*/
public function getRule()
{
return $this->rule;
}
/**
* @return RuleValueList|string|null
*/
public function getValue()
{
return $this->value;
}
/**
* @param RuleValueList|string|null $value
*/
public function setValue($value): void
{
$this->value = $value;
}
/**
* Adds a value to the existing value. Value will be appended if a `RuleValueList` exists of the given type.
* Otherwise, the existing value will be wrapped by one.
*
* @param RuleValueList|array<int, RuleValueList> $value
* @param string $type
*/
public function addValue($value, $type = ' '): void
{
if (!\is_array($value)) {
$value = [$value];
}
if (!($this->value instanceof RuleValueList) || $this->value->getListSeparator() !== $type) {
$currentValue = $this->value;
$this->value = new RuleValueList($type, $this->lineNumber);
if ($currentValue) {
$this->value->addListComponent($currentValue);
}
}
foreach ($value as $valueItem) {
$this->value->addListComponent($valueItem);
}
}
/**
* @param bool $isImportant
*/
public function setIsImportant($isImportant): void
{
$this->isImportant = $isImportant;
}
/**
* @return bool
*/
public function getIsImportant()
{
return $this->isImportant;
}
/**
* @deprecated in V8.8.0, will be removed in V9.0.0. Use `render` instead.
*/
public function __toString(): string
{
return $this->render(new OutputFormat());
}
public function render(OutputFormat $outputFormat): string
{
$formatter = $outputFormat->getFormatter();
$result = "{$formatter->comments($this)}{$this->rule}:{$formatter->spaceAfterRuleName()}";
if ($this->value instanceof Value) { // Can also be a ValueList
$result .= $this->value->render($outputFormat);
} else {
$result .= $this->value;
}
if ($this->isImportant) {
$result .= ' !important';
}
$result .= ';';
return $result;
}
/**
* @param array<array-key, Comment> $comments
*/
public function addComments(array $comments): void
{
$this->comments = \array_merge($this->comments, $comments);
}
/**
* @return array<array-key, Comment>
*/
public function getComments(): array
{
return $this->comments;
}
/**
* @param array<array-key, Comment> $comments
*/
public function setComments(array $comments): void
{
$this->comments = $comments;
}
}