| 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 |
|
| 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 |
* @param int $iLineNo |
| 20 |
*/ |
| 21 |
public function __construct($iLineNo = 0) |
| 22 |
{ |
| 23 |
parent::__construct($iLineNo); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* @return Document |
| 28 |
* |
| 29 |
* @throws SourceException |
| 30 |
* |
| 31 |
* @internal since V8.8.0 |
| 32 |
*/ |
| 33 |
public static function parse(ParserState $oParserState) |
| 34 |
{ |
| 35 |
$oDocument = new Document($oParserState->currentLine()); |
| 36 |
CSSList::parseList($oParserState, $oDocument); |
| 37 |
return $oDocument; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Gets all `DeclarationBlock` objects recursively, no matter how deeply nested the selectors are. |
| 42 |
* Aliased as `getAllSelectors()`. |
| 43 |
* |
| 44 |
* @return array<int, DeclarationBlock> |
| 45 |
*/ |
| 46 |
public function getAllDeclarationBlocks() |
| 47 |
{ |
| 48 |
/** @var array<int, DeclarationBlock> $aResult */ |
| 49 |
$aResult = []; |
| 50 |
$this->allDeclarationBlocks($aResult); |
| 51 |
return $aResult; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Gets all `DeclarationBlock` objects recursively. |
| 56 |
* |
| 57 |
* @return array<int, DeclarationBlock> |
| 58 |
* |
| 59 |
* @deprecated will be removed in version 9.0; use `getAllDeclarationBlocks()` instead |
| 60 |
*/ |
| 61 |
public function getAllSelectors() |
| 62 |
{ |
| 63 |
return $this->getAllDeclarationBlocks(); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Returns all `RuleSet` objects recursively found in the tree, no matter how deeply nested the rule sets are. |
| 68 |
* |
| 69 |
* @return array<int, RuleSet> |
| 70 |
*/ |
| 71 |
public function getAllRuleSets() |
| 72 |
{ |
| 73 |
/** @var array<int, RuleSet> $aResult */ |
| 74 |
$aResult = []; |
| 75 |
$this->allRuleSets($aResult); |
| 76 |
return $aResult; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Returns all `Selector` objects with the requested specificity found recursively in the tree. |
| 81 |
* |
| 82 |
* Note that this does not yield the full `DeclarationBlock` that the selector belongs to |
| 83 |
* (and, currently, there is no way to get to that). |
| 84 |
* |
| 85 |
* @param string|null $sSpecificitySearch |
| 86 |
* An optional filter by specificity. |
| 87 |
* May contain a comparison operator and a number or just a number (defaults to "=="). |
| 88 |
* |
| 89 |
* @return array<int, Selector> |
| 90 |
* @example `getSelectorsBySpecificity('>= 100')` |
| 91 |
* |
| 92 |
*/ |
| 93 |
public function getSelectorsBySpecificity($sSpecificitySearch = null) |
| 94 |
{ |
| 95 |
/** @var array<int, Selector> $aResult */ |
| 96 |
$aResult = []; |
| 97 |
$this->allSelectors($aResult, $sSpecificitySearch); |
| 98 |
return $aResult; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Expands all shorthand properties to their long value. |
| 103 |
* |
| 104 |
* @return void |
| 105 |
* |
| 106 |
* @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511 |
| 107 |
*/ |
| 108 |
public function expandShorthands() |
| 109 |
{ |
| 110 |
foreach ($this->getAllDeclarationBlocks() as $oDeclaration) { |
| 111 |
$oDeclaration->expandShorthands(); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Create shorthands properties whenever possible. |
| 117 |
* |
| 118 |
* @return void |
| 119 |
* |
| 120 |
* @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511 |
| 121 |
*/ |
| 122 |
public function createShorthands() |
| 123 |
{ |
| 124 |
foreach ($this->getAllDeclarationBlocks() as $oDeclaration) { |
| 125 |
$oDeclaration->createShorthands(); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Overrides `render()` to make format argument optional. |
| 131 |
* |
| 132 |
* @param OutputFormat|null $oOutputFormat |
| 133 |
* |
| 134 |
* @return string |
| 135 |
*/ |
| 136 |
public function render($oOutputFormat = null) |
| 137 |
{ |
| 138 |
if ($oOutputFormat === null) { |
| 139 |
$oOutputFormat = new OutputFormat(); |
| 140 |
} |
| 141 |
return $oOutputFormat->comments($this) . $this->renderListContents($oOutputFormat); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* @return bool |
| 146 |
*/ |
| 147 |
public function isRootList() |
| 148 |
{ |
| 149 |
return true; |
| 150 |
} |
| 151 |
} |
| 152 |
|