Debian Codegraph @

vendor/sabberworm/php-css-parser/src/CSSList/Document.php

raw
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Sabberworm\CSS\CSSList;
6
7 use Sabberworm\CSS\OutputFormat;
8 use Sabberworm\CSS\Parsing\ParserState;
9 use Sabberworm\CSS\Parsing\SourceException;
10 use Sabberworm\CSS\Property\Selector;
11
12 /**
13 * This class represents the root of a parsed CSS file. It contains all top-level CSS contents: mostly declaration
14 * blocks, but also any at-rules encountered (`Import` and `Charset`).
15 */
16 class Document extends CSSBlockList
17 {
18 /**
19 * @throws SourceException
20 *
21 * @internal since V8.8.0
22 */
23 public static function parse(ParserState $parserState): Document
24 {
25 $document = new Document($parserState->currentLine());
26 CSSList::parseList($parserState, $document);
27
28 return $document;
29 }
30
31 /**
32 * Returns all `Selector` objects with the requested specificity found recursively in the tree.
33 *
34 * Note that this does not yield the full `DeclarationBlock` that the selector belongs to
35 * (and, currently, there is no way to get to that).
36 *
37 * @param string|null $specificitySearch
38 * An optional filter by specificity.
39 * May contain a comparison operator and a number or just a number (defaults to "==").
40 *
41 * @return list<Selector>
42 *
43 * @example `getSelectorsBySpecificity('>= 100')`
44 */
45 public function getSelectorsBySpecificity(?string $specificitySearch = null): array
46 {
47 return $this->getAllSelectors($specificitySearch);
48 }
49
50 /**
51 * Overrides `render()` to make format argument optional.
52 */
53 public function render(?OutputFormat $outputFormat = null): string
54 {
55 if ($outputFormat === null) {
56 $outputFormat = new OutputFormat();
57 }
58 return $outputFormat->getFormatter()->comments($this) . $this->renderListContents($outputFormat);
59 }
60
61 public function isRootList(): bool
62 {
63 return true;
64 }
65 }