| 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 |
* The root `CSSList` of a parsed file. Contains all top-level CSS contents, mostly declaration blocks, |
| 15 |
* but also any at-rules encountered. |
| 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. |
| 41 |
* |
| 42 |
* @return array<int, DeclarationBlock> |
| 43 |
*/ |
| 44 |
public function getAllDeclarationBlocks() |
| 45 |
{ |
| 46 |
/** @var array<int, DeclarationBlock> $aResult */ |
| 47 |
$aResult = []; |
| 48 |
$this->allDeclarationBlocks($aResult); |
| 49 |
return $aResult; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Gets all `DeclarationBlock` objects recursively. |
| 54 |
* |
| 55 |
* @return array<int, DeclarationBlock> |
| 56 |
* |
| 57 |
* @deprecated will be removed in version 9.0; use `getAllDeclarationBlocks()` instead |
| 58 |
*/ |
| 59 |
public function getAllSelectors() |
| 60 |
{ |
| 61 |
return $this->getAllDeclarationBlocks(); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Returns all `RuleSet` objects found recursively in the tree. |
| 66 |
* |
| 67 |
* @return array<int, RuleSet> |
| 68 |
*/ |
| 69 |
public function getAllRuleSets() |
| 70 |
{ |
| 71 |
/** @var array<int, RuleSet> $aResult */ |
| 72 |
$aResult = []; |
| 73 |
$this->allRuleSets($aResult); |
| 74 |
return $aResult; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Returns all `Value` objects found recursively in the tree. |
| 79 |
* |
| 80 |
* @param CSSList|RuleSet|string $mElement |
| 81 |
* the `CSSList` or `RuleSet` to start the search from (defaults to the whole document). |
| 82 |
* If a string is given, it is used as rule name filter. |
| 83 |
* @param bool $bSearchInFunctionArguments whether to also return Value objects used as Function arguments. |
| 84 |
* |
| 85 |
* @return array<int, Value> |
| 86 |
* |
| 87 |
* @see RuleSet->getRules() |
| 88 |
*/ |
| 89 |
public function getAllValues($mElement = null, $bSearchInFunctionArguments = false) |
| 90 |
{ |
| 91 |
$sSearchString = null; |
| 92 |
if ($mElement === null) { |
| 93 |
$mElement = $this; |
| 94 |
} elseif (is_string($mElement)) { |
| 95 |
$sSearchString = $mElement; |
| 96 |
$mElement = $this; |
| 97 |
} |
| 98 |
/** @var array<int, Value> $aResult */ |
| 99 |
$aResult = []; |
| 100 |
$this->allValues($mElement, $aResult, $sSearchString, $bSearchInFunctionArguments); |
| 101 |
return $aResult; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Returns all `Selector` objects found recursively in the tree. |
| 106 |
* |
| 107 |
* Note that this does not yield the full `DeclarationBlock` that the selector belongs to |
| 108 |
* (and, currently, there is no way to get to that). |
| 109 |
* |
| 110 |
* @param string|null $sSpecificitySearch |
| 111 |
* An optional filter by specificity. |
| 112 |
* May contain a comparison operator and a number or just a number (defaults to "=="). |
| 113 |
* |
| 114 |
* @return array<int, Selector> |
| 115 |
* @example `getSelectorsBySpecificity('>= 100')` |
| 116 |
* |
| 117 |
*/ |
| 118 |
public function getSelectorsBySpecificity($sSpecificitySearch = null) |
| 119 |
{ |
| 120 |
/** @var array<int, Selector> $aResult */ |
| 121 |
$aResult = []; |
| 122 |
$this->allSelectors($aResult, $sSpecificitySearch); |
| 123 |
return $aResult; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Expands all shorthand properties to their long value. |
| 128 |
* |
| 129 |
* @return void |
| 130 |
*/ |
| 131 |
public function expandShorthands() |
| 132 |
{ |
| 133 |
foreach ($this->getAllDeclarationBlocks() as $oDeclaration) { |
| 134 |
$oDeclaration->expandShorthands(); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Create shorthands properties whenever possible. |
| 140 |
* |
| 141 |
* @return void |
| 142 |
*/ |
| 143 |
public function createShorthands() |
| 144 |
{ |
| 145 |
foreach ($this->getAllDeclarationBlocks() as $oDeclaration) { |
| 146 |
$oDeclaration->createShorthands(); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Overrides `render()` to make format argument optional. |
| 152 |
* |
| 153 |
* @param OutputFormat|null $oOutputFormat |
| 154 |
* |
| 155 |
* @return string |
| 156 |
*/ |
| 157 |
public function render(OutputFormat $oOutputFormat = null) |
| 158 |
{ |
| 159 |
if ($oOutputFormat === null) { |
| 160 |
$oOutputFormat = new OutputFormat(); |
| 161 |
} |
| 162 |
return parent::render($oOutputFormat); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* @return bool |
| 167 |
*/ |
| 168 |
public function isRootList() |
| 169 |
{ |
| 170 |
return true; |
| 171 |
} |
| 172 |
} |
| 173 |
|