MDL-65759 library: Update php-css-parser to 8.3.0
[moodle.git] / lib / php-css-parser / CSSList / Document.php
CommitLineData
fbe18cc0
FM
1<?php
2
3namespace Sabberworm\CSS\CSSList;
4
376eb156
MM
5use Sabberworm\CSS\Parsing\ParserState;
6
fbe18cc0
FM
7/**
8 * The root CSSList of a parsed file. Contains all top-level css contents, mostly declaration blocks, but also any @-rules encountered.
9 */
10class Document extends CSSBlockList {
11 /**
12 * Document constructor.
13 * @param int $iLineNo
14 */
15 public function __construct($iLineNo = 0) {
16 parent::__construct($iLineNo);
17 }
18
376eb156
MM
19 public static function parse(ParserState $oParserState) {
20 $oDocument = new Document($oParserState->currentLine());
21 CSSList::parseList($oParserState, $oDocument);
22 return $oDocument;
23 }
24
fbe18cc0
FM
25 /**
26 * Gets all DeclarationBlock objects recursively.
27 */
28 public function getAllDeclarationBlocks() {
29 $aResult = array();
30 $this->allDeclarationBlocks($aResult);
31 return $aResult;
32 }
33
34 /**
35 * @deprecated use getAllDeclarationBlocks()
36 */
37 public function getAllSelectors() {
38 return $this->getAllDeclarationBlocks();
39 }
40
41 /**
42 * Returns all RuleSet objects found recursively in the tree.
43 */
44 public function getAllRuleSets() {
45 $aResult = array();
46 $this->allRuleSets($aResult);
47 return $aResult;
48 }
49
50 /**
51 * Returns all Value objects found recursively in the tree.
52 * @param (object|string) $mElement the CSSList or RuleSet to start the search from (defaults to the whole document). If a string is given, it is used as rule name filter (@see{RuleSet->getRules()}).
53 * @param (bool) $bSearchInFunctionArguments whether to also return Value objects used as Function arguments.
54 */
55 public function getAllValues($mElement = null, $bSearchInFunctionArguments = false) {
56 $sSearchString = null;
57 if ($mElement === null) {
58 $mElement = $this;
59 } else if (is_string($mElement)) {
60 $sSearchString = $mElement;
61 $mElement = $this;
62 }
63 $aResult = array();
64 $this->allValues($mElement, $aResult, $sSearchString, $bSearchInFunctionArguments);
65 return $aResult;
66 }
67
68 /**
69 * Returns all Selector objects found recursively in the tree.
70 * Note that this does not yield the full DeclarationBlock that the selector belongs to (and, currently, there is no way to get to that).
71 * @param $sSpecificitySearch An optional filter by specificity. May contain a comparison operator and a number or just a number (defaults to "==").
72 * @example getSelectorsBySpecificity('>= 100')
73 */
74 public function getSelectorsBySpecificity($sSpecificitySearch = null) {
75 if (is_numeric($sSpecificitySearch) || is_numeric($sSpecificitySearch[0])) {
76 $sSpecificitySearch = "== $sSpecificitySearch";
77 }
78 $aResult = array();
79 $this->allSelectors($aResult, $sSpecificitySearch);
80 return $aResult;
81 }
82
83 /**
84 * Expands all shorthand properties to their long value
85 */
86 public function expandShorthands() {
87 foreach ($this->getAllDeclarationBlocks() as $oDeclaration) {
88 $oDeclaration->expandShorthands();
89 }
90 }
91
92 /**
93 * Create shorthands properties whenever possible
94 */
95 public function createShorthands() {
96 foreach ($this->getAllDeclarationBlocks() as $oDeclaration) {
97 $oDeclaration->createShorthands();
98 }
99 }
100
101 // Override render() to make format argument optional
102 public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat = null) {
103 if($oOutputFormat === null) {
104 $oOutputFormat = new \Sabberworm\CSS\OutputFormat();
105 }
106 return parent::render($oOutputFormat);
107 }
108
109 public function isRootList() {
110 return true;
111 }
112
113}