31
31
*
32
32
* It can also contain `Import` and `Charset` objects stemming from at-rules.
33
33
*/
34
- abstract class CSSList implements Renderable, Commentable
34
+ abstract class CSSList implements Commentable, CSSListItem, Renderable
35
35
{
36
36
/**
37
37
* @var list<Comment>
@@ -41,7 +41,7 @@ abstract class CSSList implements Renderable, Commentable
41
41
protected $ comments = [];
42
42
43
43
/**
44
- * @var array<int<0, max>, RuleSet|CSSList|Import|Charset >
44
+ * @var array<int<0, max>, CSSListItem >
45
45
*
46
46
* @internal since 8.8.0
47
47
*/
@@ -105,7 +105,10 @@ public static function parseList(ParserState $parserState, CSSList $list): void
105
105
}
106
106
107
107
/**
108
- * @return AtRuleBlockList|KeyFrame|Charset|CSSNamespace|Import|AtRuleSet|DeclarationBlock|false|null
108
+ * @return CSSListItem|false|null
109
+ * If `null` is returned, it means the end of the list has been reached.
110
+ * If `false` is returned, it means an invalid item has been encountered,
111
+ * but parsing of the next item should proceed.
109
112
*
110
113
* @throws SourceException
111
114
* @throws UnexpectedEOFException
@@ -139,7 +142,7 @@ private static function parseListItem(ParserState $parserState, CSSList $list)
139
142
} elseif ($ parserState ->comes ('} ' )) {
140
143
if ($ isRoot ) {
141
144
if ($ parserState ->getSettings ()->usesLenientParsing ()) {
142
- return DeclarationBlock::parse ($ parserState );
145
+ return DeclarationBlock::parse ($ parserState ) ?? false ;
143
146
} else {
144
147
throw new SourceException ('Unopened { ' , $ parserState ->currentLine ());
145
148
}
@@ -148,18 +151,16 @@ private static function parseListItem(ParserState $parserState, CSSList $list)
148
151
return null ;
149
152
}
150
153
} else {
151
- return DeclarationBlock::parse ($ parserState , $ list );
154
+ return DeclarationBlock::parse ($ parserState , $ list ) ?? false ;
152
155
}
153
156
}
154
157
155
158
/**
156
- * @return AtRuleBlockList|KeyFrame|Charset|CSSNamespace|Import|AtRuleSet|null
157
- *
158
159
* @throws SourceException
159
160
* @throws UnexpectedTokenException
160
161
* @throws UnexpectedEOFException
161
162
*/
162
- private static function parseAtRule (ParserState $ parserState )
163
+ private static function parseAtRule (ParserState $ parserState ): ? CSSListItem
163
164
{
164
165
$ parserState ->consume ('@ ' );
165
166
$ identifier = $ parserState ->parseIdentifier ();
@@ -262,28 +263,24 @@ public function getLineNo(): int
262
263
263
264
/**
264
265
* Prepends an item to the list of contents.
265
- *
266
- * @param RuleSet|CSSList|Import|Charset $item
267
266
*/
268
- public function prepend ($ item ): void
267
+ public function prepend (CSSListItem $ item ): void
269
268
{
270
269
\array_unshift ($ this ->contents , $ item );
271
270
}
272
271
273
272
/**
274
273
* Appends an item to the list of contents.
275
- *
276
- * @param RuleSet|CSSList|Import|Charset $item
277
274
*/
278
- public function append ($ item ): void
275
+ public function append (CSSListItem $ item ): void
279
276
{
280
277
$ this ->contents [] = $ item ;
281
278
}
282
279
283
280
/**
284
281
* Splices the list of contents.
285
282
*
286
- * @param array<int, RuleSet|CSSList|Import|Charset > $replacement
283
+ * @param array<int, CSSListItem > $replacement
287
284
*/
288
285
public function splice (int $ offset , ?int $ length = null , ?array $ replacement = null ): void
289
286
{
@@ -293,11 +290,8 @@ public function splice(int $offset, ?int $length = null, ?array $replacement = n
293
290
/**
294
291
* Inserts an item in the CSS list before its sibling. If the desired sibling cannot be found,
295
292
* the item is appended at the end.
296
- *
297
- * @param RuleSet|CSSList|Import|Charset $item
298
- * @param RuleSet|CSSList|Import|Charset $sibling
299
293
*/
300
- public function insertBefore ($ item , $ sibling ): void
294
+ public function insertBefore (CSSListItem $ item , CSSListItem $ sibling ): void
301
295
{
302
296
if (\in_array ($ sibling , $ this ->contents , true )) {
303
297
$ this ->replace ($ sibling , [$ item , $ sibling ]);
@@ -309,13 +303,13 @@ public function insertBefore($item, $sibling): void
309
303
/**
310
304
* Removes an item from the CSS list.
311
305
*
312
- * @param RuleSet|Import|Charset|CSSList $itemToRemove
306
+ * @param CSSListItem $itemToRemove
313
307
* May be a `RuleSet` (most likely a `DeclarationBlock`), an `Import`,
314
308
* a `Charset` or another `CSSList` (most likely a `MediaQuery`)
315
309
*
316
310
* @return bool whether the item was removed
317
311
*/
318
- public function remove ($ itemToRemove ): bool
312
+ public function remove (CSSListItem $ itemToRemove ): bool
319
313
{
320
314
$ key = \array_search ($ itemToRemove , $ this ->contents , true );
321
315
if ($ key !== false ) {
@@ -329,12 +323,12 @@ public function remove($itemToRemove): bool
329
323
/**
330
324
* Replaces an item from the CSS list.
331
325
*
332
- * @param RuleSet|Import|Charset|CSSList $oldItem
326
+ * @param CSSListItem $oldItem
333
327
* May be a `RuleSet` (most likely a `DeclarationBlock`), an `Import`, a `Charset`
334
328
* or another `CSSList` (most likely a `MediaQuery`)
335
- * @param RuleSet|Import|Charset|CSSList| array<RuleSet|Import|Charset|CSSList > $newItem
329
+ * @param CSSListItem| array<CSSListItem > $newItem
336
330
*/
337
- public function replace ($ oldItem , $ newItem ): bool
331
+ public function replace (CSSListItem $ oldItem , $ newItem ): bool
338
332
{
339
333
$ key = \array_search ($ oldItem , $ this ->contents , true );
340
334
if ($ key !== false ) {
@@ -350,7 +344,7 @@ public function replace($oldItem, $newItem): bool
350
344
}
351
345
352
346
/**
353
- * @param array<int, RuleSet|Import|Charset|CSSList > $contents
347
+ * @param array<int, CSSListItem > $contents
354
348
*/
355
349
public function setContents (array $ contents ): void
356
350
{
@@ -441,7 +435,7 @@ abstract public function isRootList(): bool;
441
435
/**
442
436
* Returns the stored items.
443
437
*
444
- * @return array<int<0, max>, RuleSet|Import|Charset|CSSList >
438
+ * @return array<int<0, max>, CSSListItem >
445
439
*/
446
440
public function getContents (): array
447
441
{
0 commit comments