AtRuleBlockList.php
2 years ago
CSSBlockList.php
4 years ago
CSSList.php
2 years ago
Document.php
8 months ago
KeyFrame.php
2 years ago
index.php
3 years ago
CSSBlockList.php
104 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Sabberworm\CSS\CSSList; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Sabberworm\CSS\Property\Selector; |
| 5 | use MailPoetVendor\Sabberworm\CSS\Rule\Rule; |
| 6 | use MailPoetVendor\Sabberworm\CSS\RuleSet\DeclarationBlock; |
| 7 | use MailPoetVendor\Sabberworm\CSS\RuleSet\RuleSet; |
| 8 | use MailPoetVendor\Sabberworm\CSS\Value\CSSFunction; |
| 9 | use MailPoetVendor\Sabberworm\CSS\Value\Value; |
| 10 | use MailPoetVendor\Sabberworm\CSS\Value\ValueList; |
| 11 | abstract class CSSBlockList extends CSSList |
| 12 | { |
| 13 | public function __construct($iLineNo = 0) |
| 14 | { |
| 15 | parent::__construct($iLineNo); |
| 16 | } |
| 17 | protected function allDeclarationBlocks(array &$aResult) |
| 18 | { |
| 19 | foreach ($this->aContents as $mContent) { |
| 20 | if ($mContent instanceof DeclarationBlock) { |
| 21 | $aResult[] = $mContent; |
| 22 | } elseif ($mContent instanceof CSSBlockList) { |
| 23 | $mContent->allDeclarationBlocks($aResult); |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | protected function allRuleSets(array &$aResult) |
| 28 | { |
| 29 | foreach ($this->aContents as $mContent) { |
| 30 | if ($mContent instanceof RuleSet) { |
| 31 | $aResult[] = $mContent; |
| 32 | } elseif ($mContent instanceof CSSBlockList) { |
| 33 | $mContent->allRuleSets($aResult); |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | protected function allValues($oElement, array &$aResult, $sSearchString = null, $bSearchInFunctionArguments = \false) |
| 38 | { |
| 39 | if ($oElement instanceof CSSBlockList) { |
| 40 | foreach ($oElement->getContents() as $oContent) { |
| 41 | $this->allValues($oContent, $aResult, $sSearchString, $bSearchInFunctionArguments); |
| 42 | } |
| 43 | } elseif ($oElement instanceof RuleSet) { |
| 44 | foreach ($oElement->getRules($sSearchString) as $oRule) { |
| 45 | $this->allValues($oRule, $aResult, $sSearchString, $bSearchInFunctionArguments); |
| 46 | } |
| 47 | } elseif ($oElement instanceof Rule) { |
| 48 | $this->allValues($oElement->getValue(), $aResult, $sSearchString, $bSearchInFunctionArguments); |
| 49 | } elseif ($oElement instanceof ValueList) { |
| 50 | if ($bSearchInFunctionArguments || !$oElement instanceof CSSFunction) { |
| 51 | foreach ($oElement->getListComponents() as $mComponent) { |
| 52 | $this->allValues($mComponent, $aResult, $sSearchString, $bSearchInFunctionArguments); |
| 53 | } |
| 54 | } |
| 55 | } else { |
| 56 | // Non-List `Value` or `CSSString` (CSS identifier) |
| 57 | $aResult[] = $oElement; |
| 58 | } |
| 59 | } |
| 60 | protected function allSelectors(array &$aResult, $sSpecificitySearch = null) |
| 61 | { |
| 62 | $aDeclarationBlocks = []; |
| 63 | $this->allDeclarationBlocks($aDeclarationBlocks); |
| 64 | foreach ($aDeclarationBlocks as $oBlock) { |
| 65 | foreach ($oBlock->getSelectors() as $oSelector) { |
| 66 | if ($sSpecificitySearch === null) { |
| 67 | $aResult[] = $oSelector; |
| 68 | } else { |
| 69 | $sComparator = '==='; |
| 70 | $aSpecificitySearch = \explode(' ', $sSpecificitySearch); |
| 71 | $iTargetSpecificity = $aSpecificitySearch[0]; |
| 72 | if (\count($aSpecificitySearch) > 1) { |
| 73 | $sComparator = $aSpecificitySearch[0]; |
| 74 | $iTargetSpecificity = $aSpecificitySearch[1]; |
| 75 | } |
| 76 | $iTargetSpecificity = (int) $iTargetSpecificity; |
| 77 | $iSelectorSpecificity = $oSelector->getSpecificity(); |
| 78 | $bMatches = \false; |
| 79 | switch ($sComparator) { |
| 80 | case '<=': |
| 81 | $bMatches = $iSelectorSpecificity <= $iTargetSpecificity; |
| 82 | break; |
| 83 | case '<': |
| 84 | $bMatches = $iSelectorSpecificity < $iTargetSpecificity; |
| 85 | break; |
| 86 | case '>=': |
| 87 | $bMatches = $iSelectorSpecificity >= $iTargetSpecificity; |
| 88 | break; |
| 89 | case '>': |
| 90 | $bMatches = $iSelectorSpecificity > $iTargetSpecificity; |
| 91 | break; |
| 92 | default: |
| 93 | $bMatches = $iSelectorSpecificity === $iTargetSpecificity; |
| 94 | break; |
| 95 | } |
| 96 | if ($bMatches) { |
| 97 | $aResult[] = $oSelector; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 |