| 1 |
<?php |
| 2 |
|
| 3 |
namespace Sabberworm\CSS\CSSList; |
| 4 |
|
| 5 |
use Sabberworm\CSS\CSSElement; |
| 6 |
use Sabberworm\CSS\Property\Selector; |
| 7 |
use Sabberworm\CSS\Rule\Rule; |
| 8 |
use Sabberworm\CSS\RuleSet\DeclarationBlock; |
| 9 |
use Sabberworm\CSS\RuleSet\RuleSet; |
| 10 |
use Sabberworm\CSS\Value\CSSFunction; |
| 11 |
use Sabberworm\CSS\Value\Value; |
| 12 |
use Sabberworm\CSS\Value\ValueList; |
| 13 |
|
| 14 |
/** |
| 15 |
* A `CSSBlockList` is a `CSSList` whose `DeclarationBlock`s are guaranteed to contain valid declaration blocks or |
| 16 |
* at-rules. |
| 17 |
* |
| 18 |
* Most `CSSList`s conform to this category but some at-rules (such as `@keyframes`) do not. |
| 19 |
*/ |
| 20 |
abstract class CSSBlockList extends CSSList |
| 21 |
{ |
| 22 |
/** |
| 23 |
* @param int $iLineNo |
| 24 |
*/ |
| 25 |
public function __construct($iLineNo = 0) |
| 26 |
{ |
| 27 |
parent::__construct($iLineNo); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* @param array<int, DeclarationBlock> $aResult |
| 32 |
* |
| 33 |
* @return void |
| 34 |
*/ |
| 35 |
protected function allDeclarationBlocks(array &$aResult) |
| 36 |
{ |
| 37 |
foreach ($this->aContents as $mContent) { |
| 38 |
if ($mContent instanceof DeclarationBlock) { |
| 39 |
$aResult[] = $mContent; |
| 40 |
} elseif ($mContent instanceof CSSBlockList) { |
| 41 |
$mContent->allDeclarationBlocks($aResult); |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* @param array<int, RuleSet> $aResult |
| 48 |
* |
| 49 |
* @return void |
| 50 |
*/ |
| 51 |
protected function allRuleSets(array &$aResult) |
| 52 |
{ |
| 53 |
foreach ($this->aContents as $mContent) { |
| 54 |
if ($mContent instanceof RuleSet) { |
| 55 |
$aResult[] = $mContent; |
| 56 |
} elseif ($mContent instanceof CSSBlockList) { |
| 57 |
$mContent->allRuleSets($aResult); |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Returns all `Value` objects found recursively in `Rule`s in the tree. |
| 64 |
* |
| 65 |
* @param CSSElement|string|null $element |
| 66 |
* This is the `CSSList` or `RuleSet` to start the search from (defaults to the whole document). |
| 67 |
* If a string is given, it is used as a rule name filter. |
| 68 |
* Passing a string for this parameter is deprecated in version 8.9.0, and will not work from v9.0; |
| 69 |
* use the following parameter to pass a rule name filter instead. |
| 70 |
* @param string|bool|null $ruleSearchPatternOrSearchInFunctionArguments |
| 71 |
* This allows filtering rules by property name |
| 72 |
* (e.g. if "color" is passed, only `Value`s from `color` properties will be returned, |
| 73 |
* or if "font-" is provided, `Value`s from all font rules, like `font-size`, and including `font` itself, |
| 74 |
* will be returned). |
| 75 |
* If a Boolean is provided, it is treated as the `$searchInFunctionArguments` argument. |
| 76 |
* Passing a Boolean for this parameter is deprecated in version 8.9.0, and will not work from v9.0; |
| 77 |
* use the `$searchInFunctionArguments` parameter instead. |
| 78 |
* @param bool $searchInFunctionArguments whether to also return Value objects used as Function arguments. |
| 79 |
* |
| 80 |
* @return array<int, Value> |
| 81 |
* |
| 82 |
* @see RuleSet->getRules() |
| 83 |
*/ |
| 84 |
public function getAllValues( |
| 85 |
$element = null, |
| 86 |
$ruleSearchPatternOrSearchInFunctionArguments = null, |
| 87 |
$searchInFunctionArguments = false |
| 88 |
) { |
| 89 |
if (\is_bool($ruleSearchPatternOrSearchInFunctionArguments)) { |
| 90 |
$searchInFunctionArguments = $ruleSearchPatternOrSearchInFunctionArguments; |
| 91 |
$searchString = null; |
| 92 |
} else { |
| 93 |
$searchString = $ruleSearchPatternOrSearchInFunctionArguments; |
| 94 |
} |
| 95 |
|
| 96 |
if ($element === null) { |
| 97 |
$element = $this; |
| 98 |
} elseif (\is_string($element)) { |
| 99 |
$searchString = $element; |
| 100 |
$element = $this; |
| 101 |
} |
| 102 |
|
| 103 |
$result = []; |
| 104 |
$this->allValues($element, $result, $searchString, $searchInFunctionArguments); |
| 105 |
return $result; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* @param CSSElement|string $oElement |
| 110 |
* @param array<int, Value> $aResult |
| 111 |
* @param string|null $sSearchString |
| 112 |
* @param bool $bSearchInFunctionArguments |
| 113 |
* |
| 114 |
* @return void |
| 115 |
*/ |
| 116 |
protected function allValues($oElement, array &$aResult, $sSearchString = null, $bSearchInFunctionArguments = false) |
| 117 |
{ |
| 118 |
if ($oElement instanceof CSSBlockList) { |
| 119 |
foreach ($oElement->getContents() as $oContent) { |
| 120 |
$this->allValues($oContent, $aResult, $sSearchString, $bSearchInFunctionArguments); |
| 121 |
} |
| 122 |
} elseif ($oElement instanceof RuleSet) { |
| 123 |
foreach ($oElement->getRules($sSearchString) as $oRule) { |
| 124 |
$this->allValues($oRule, $aResult, $sSearchString, $bSearchInFunctionArguments); |
| 125 |
} |
| 126 |
} elseif ($oElement instanceof Rule) { |
| 127 |
$this->allValues($oElement->getValue(), $aResult, $sSearchString, $bSearchInFunctionArguments); |
| 128 |
} elseif ($oElement instanceof ValueList) { |
| 129 |
if ($bSearchInFunctionArguments || !($oElement instanceof CSSFunction)) { |
| 130 |
foreach ($oElement->getListComponents() as $mComponent) { |
| 131 |
$this->allValues($mComponent, $aResult, $sSearchString, $bSearchInFunctionArguments); |
| 132 |
} |
| 133 |
} |
| 134 |
} else { |
| 135 |
// Non-List `Value` or `CSSString` (CSS identifier) |
| 136 |
$aResult[] = $oElement; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* @param array<int, Selector> $aResult |
| 142 |
* @param string|null $sSpecificitySearch |
| 143 |
* |
| 144 |
* @return void |
| 145 |
*/ |
| 146 |
protected function allSelectors(array &$aResult, $sSpecificitySearch = null) |
| 147 |
{ |
| 148 |
/** @var array<int, DeclarationBlock> $aDeclarationBlocks */ |
| 149 |
$aDeclarationBlocks = []; |
| 150 |
$this->allDeclarationBlocks($aDeclarationBlocks); |
| 151 |
foreach ($aDeclarationBlocks as $oBlock) { |
| 152 |
foreach ($oBlock->getSelectors() as $oSelector) { |
| 153 |
if ($sSpecificitySearch === null) { |
| 154 |
$aResult[] = $oSelector; |
| 155 |
} else { |
| 156 |
$sComparator = '==='; |
| 157 |
$aSpecificitySearch = explode(' ', $sSpecificitySearch); |
| 158 |
$iTargetSpecificity = $aSpecificitySearch[0]; |
| 159 |
if (count($aSpecificitySearch) > 1) { |
| 160 |
$sComparator = $aSpecificitySearch[0]; |
| 161 |
$iTargetSpecificity = $aSpecificitySearch[1]; |
| 162 |
} |
| 163 |
$iTargetSpecificity = (int)$iTargetSpecificity; |
| 164 |
$iSelectorSpecificity = $oSelector->getSpecificity(); |
| 165 |
$bMatches = false; |
| 166 |
switch ($sComparator) { |
| 167 |
case '<=': |
| 168 |
$bMatches = $iSelectorSpecificity <= $iTargetSpecificity; |
| 169 |
break; |
| 170 |
case '<': |
| 171 |
$bMatches = $iSelectorSpecificity < $iTargetSpecificity; |
| 172 |
break; |
| 173 |
case '>=': |
| 174 |
$bMatches = $iSelectorSpecificity >= $iTargetSpecificity; |
| 175 |
break; |
| 176 |
case '>': |
| 177 |
$bMatches = $iSelectorSpecificity > $iTargetSpecificity; |
| 178 |
break; |
| 179 |
default: |
| 180 |
$bMatches = $iSelectorSpecificity === $iTargetSpecificity; |
| 181 |
break; |
| 182 |
} |
| 183 |
if ($bMatches) { |
| 184 |
$aResult[] = $oSelector; |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|