| 1441 |
ariadna |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Sabberworm\CSS\CSSList;
|
|
|
4 |
|
|
|
5 |
use Sabberworm\CSS\OutputFormat;
|
|
|
6 |
use Sabberworm\CSS\Parsing\ParserState;
|
|
|
7 |
use Sabberworm\CSS\Parsing\SourceException;
|
|
|
8 |
use Sabberworm\CSS\Property\Selector;
|
|
|
9 |
use Sabberworm\CSS\RuleSet\DeclarationBlock;
|
|
|
10 |
use Sabberworm\CSS\RuleSet\RuleSet;
|
|
|
11 |
use Sabberworm\CSS\Value\Value;
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* This class represents the root of a parsed CSS file. It contains all top-level CSS contents: mostly declaration
|
|
|
15 |
* blocks, but also any at-rules encountered (`Import` and `Charset`).
|
|
|
16 |
*/
|
|
|
17 |
class Document extends CSSBlockList
|
|
|
18 |
{
|
|
|
19 |
/**
|
|
|
20 |
* @param int $iLineNo
|
|
|
21 |
*/
|
|
|
22 |
public function __construct($iLineNo = 0)
|
|
|
23 |
{
|
|
|
24 |
parent::__construct($iLineNo);
|
|
|
25 |
}
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* @return Document
|
|
|
29 |
*
|
|
|
30 |
* @throws SourceException
|
|
|
31 |
*/
|
|
|
32 |
public static function parse(ParserState $oParserState)
|
|
|
33 |
{
|
|
|
34 |
$oDocument = new Document($oParserState->currentLine());
|
|
|
35 |
CSSList::parseList($oParserState, $oDocument);
|
|
|
36 |
return $oDocument;
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Gets all `DeclarationBlock` objects recursively, no matter how deeply nested the selectors are.
|
|
|
41 |
* Aliased as `getAllSelectors()`.
|
|
|
42 |
*
|
|
|
43 |
* @return array<int, DeclarationBlock>
|
|
|
44 |
*/
|
|
|
45 |
public function getAllDeclarationBlocks()
|
|
|
46 |
{
|
|
|
47 |
/** @var array<int, DeclarationBlock> $aResult */
|
|
|
48 |
$aResult = [];
|
|
|
49 |
$this->allDeclarationBlocks($aResult);
|
|
|
50 |
return $aResult;
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Gets all `DeclarationBlock` objects recursively.
|
|
|
55 |
*
|
|
|
56 |
* @return array<int, DeclarationBlock>
|
|
|
57 |
*
|
|
|
58 |
* @deprecated will be removed in version 9.0; use `getAllDeclarationBlocks()` instead
|
|
|
59 |
*/
|
|
|
60 |
public function getAllSelectors()
|
|
|
61 |
{
|
|
|
62 |
return $this->getAllDeclarationBlocks();
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Returns all `RuleSet` objects recursively found in the tree, no matter how deeply nested the rule sets are.
|
|
|
67 |
*
|
|
|
68 |
* @return array<int, RuleSet>
|
|
|
69 |
*/
|
|
|
70 |
public function getAllRuleSets()
|
|
|
71 |
{
|
|
|
72 |
/** @var array<int, RuleSet> $aResult */
|
|
|
73 |
$aResult = [];
|
|
|
74 |
$this->allRuleSets($aResult);
|
|
|
75 |
return $aResult;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Returns all `Value` objects found recursively in `Rule`s in the tree.
|
|
|
80 |
*
|
|
|
81 |
* @param CSSList|RuleSet|string $mElement
|
|
|
82 |
* the `CSSList` or `RuleSet` to start the search from (defaults to the whole document).
|
|
|
83 |
* If a string is given, it is used as rule name filter.
|
|
|
84 |
* @param bool $bSearchInFunctionArguments whether to also return Value objects used as Function arguments.
|
|
|
85 |
*
|
|
|
86 |
* @return array<int, Value>
|
|
|
87 |
*
|
|
|
88 |
* @see RuleSet->getRules()
|
|
|
89 |
*/
|
|
|
90 |
public function getAllValues($mElement = null, $bSearchInFunctionArguments = false)
|
|
|
91 |
{
|
|
|
92 |
$sSearchString = null;
|
|
|
93 |
if ($mElement === null) {
|
|
|
94 |
$mElement = $this;
|
|
|
95 |
} elseif (is_string($mElement)) {
|
|
|
96 |
$sSearchString = $mElement;
|
|
|
97 |
$mElement = $this;
|
|
|
98 |
}
|
|
|
99 |
/** @var array<int, Value> $aResult */
|
|
|
100 |
$aResult = [];
|
|
|
101 |
$this->allValues($mElement, $aResult, $sSearchString, $bSearchInFunctionArguments);
|
|
|
102 |
return $aResult;
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
/**
|
|
|
106 |
* Returns all `Selector` objects with the requested specificity found recursively in the tree.
|
|
|
107 |
*
|
|
|
108 |
* Note that this does not yield the full `DeclarationBlock` that the selector belongs to
|
|
|
109 |
* (and, currently, there is no way to get to that).
|
|
|
110 |
*
|
|
|
111 |
* @param string|null $sSpecificitySearch
|
|
|
112 |
* An optional filter by specificity.
|
|
|
113 |
* May contain a comparison operator and a number or just a number (defaults to "==").
|
|
|
114 |
*
|
|
|
115 |
* @return array<int, Selector>
|
|
|
116 |
* @example `getSelectorsBySpecificity('>= 100')`
|
|
|
117 |
*
|
|
|
118 |
*/
|
|
|
119 |
public function getSelectorsBySpecificity($sSpecificitySearch = null)
|
|
|
120 |
{
|
|
|
121 |
/** @var array<int, Selector> $aResult */
|
|
|
122 |
$aResult = [];
|
|
|
123 |
$this->allSelectors($aResult, $sSpecificitySearch);
|
|
|
124 |
return $aResult;
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* Expands all shorthand properties to their long value.
|
|
|
129 |
*
|
|
|
130 |
* @return void
|
|
|
131 |
*
|
|
|
132 |
* @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511
|
|
|
133 |
*/
|
|
|
134 |
public function expandShorthands()
|
|
|
135 |
{
|
|
|
136 |
foreach ($this->getAllDeclarationBlocks() as $oDeclaration) {
|
|
|
137 |
$oDeclaration->expandShorthands();
|
|
|
138 |
}
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
/**
|
|
|
142 |
* Create shorthands properties whenever possible.
|
|
|
143 |
*
|
|
|
144 |
* @return void
|
|
|
145 |
*
|
|
|
146 |
* @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511
|
|
|
147 |
*/
|
|
|
148 |
public function createShorthands()
|
|
|
149 |
{
|
|
|
150 |
foreach ($this->getAllDeclarationBlocks() as $oDeclaration) {
|
|
|
151 |
$oDeclaration->createShorthands();
|
|
|
152 |
}
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
/**
|
|
|
156 |
* Overrides `render()` to make format argument optional.
|
|
|
157 |
*
|
|
|
158 |
* @param OutputFormat|null $oOutputFormat
|
|
|
159 |
*
|
|
|
160 |
* @return string
|
|
|
161 |
*/
|
|
|
162 |
public function render($oOutputFormat = null)
|
|
|
163 |
{
|
|
|
164 |
if ($oOutputFormat === null) {
|
|
|
165 |
$oOutputFormat = new OutputFormat();
|
|
|
166 |
}
|
|
|
167 |
return $oOutputFormat->comments($this) . $this->renderListContents($oOutputFormat);
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
/**
|
|
|
171 |
* @return bool
|
|
|
172 |
*/
|
|
|
173 |
public function isRootList()
|
|
|
174 |
{
|
|
|
175 |
return true;
|
|
|
176 |
}
|
|
|
177 |
}
|