Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   1  <?php
   2  
   3  namespace Sabberworm\CSS\CSSList;
   4  
   5  use Sabberworm\CSS\RuleSet\DeclarationBlock;
   6  use Sabberworm\CSS\RuleSet\RuleSet;
   7  use Sabberworm\CSS\Property\Selector;
   8  use Sabberworm\CSS\Rule\Rule;
   9  use Sabberworm\CSS\Value\ValueList;
  10  use Sabberworm\CSS\Value\CSSFunction;
  11  
  12  /**
  13   * A CSSBlockList is a CSSList whose DeclarationBlocks are guaranteed to contain valid declaration blocks or at-rules.
  14   * Most CSSLists conform to this category but some at-rules (such as @keyframes) do not.
  15   */
  16  abstract class CSSBlockList extends CSSList {
  17  	public function __construct($iLineNo = 0) {
  18  	 	 parent::__construct($iLineNo);
  19  	 }
  20  
  21  	protected function allDeclarationBlocks(&$aResult) {
  22  	 	 foreach ($this->aContents as $mContent) {
  23  	 	 	 if ($mContent instanceof DeclarationBlock) {
  24  	 	 	 	 $aResult[] = $mContent;
  25  	 	 	 } else if ($mContent instanceof CSSBlockList) {
  26  	 	 	 	 $mContent->allDeclarationBlocks($aResult);
  27  	 	 	 }
  28  	 	 }
  29  	 }
  30  
  31  	protected function allRuleSets(&$aResult) {
  32  	 	 foreach ($this->aContents as $mContent) {
  33  	 	 	 if ($mContent instanceof RuleSet) {
  34  	 	 	 	 $aResult[] = $mContent;
  35  	 	 	 } else if ($mContent instanceof CSSBlockList) {
  36  	 	 	 	 $mContent->allRuleSets($aResult);
  37  	 	 	 }
  38  	 	 }
  39  	 }
  40  
  41  	protected function allValues($oElement, &$aResult, $sSearchString = null, $bSearchInFunctionArguments = false) {
  42  	 	 if ($oElement instanceof CSSBlockList) {
  43  	 	 	 foreach ($oElement->getContents() as $oContent) {
  44  	 	 	 	 $this->allValues($oContent, $aResult, $sSearchString, $bSearchInFunctionArguments);
  45  	 	 	 }
  46  	 	 } else if ($oElement instanceof RuleSet) {
  47  	 	 	 foreach ($oElement->getRules($sSearchString) as $oRule) {
  48  	 	 	 	 $this->allValues($oRule, $aResult, $sSearchString, $bSearchInFunctionArguments);
  49  	 	 	 }
  50  	 	 } else if ($oElement instanceof Rule) {
  51  	 	 	 $this->allValues($oElement->getValue(), $aResult, $sSearchString, $bSearchInFunctionArguments);
  52  	 	 } else if ($oElement instanceof ValueList) {
  53  	 	 	 if ($bSearchInFunctionArguments || !($oElement instanceof CSSFunction)) {
  54  	 	 	 	 foreach ($oElement->getListComponents() as $mComponent) {
  55  	 	 	 	 	 $this->allValues($mComponent, $aResult, $sSearchString, $bSearchInFunctionArguments);
  56  	 	 	 	 }
  57  	 	 	 }
  58  	 	 } else {
  59  	 	 	 //Non-List Value or CSSString (CSS identifier)
  60  	 	 	 $aResult[] = $oElement;
  61  	 	 }
  62  	 }
  63  
  64  	protected function allSelectors(&$aResult, $sSpecificitySearch = null) {
  65  	 	 $aDeclarationBlocks = array();
  66  	 	 $this->allDeclarationBlocks($aDeclarationBlocks);
  67  	 	 foreach ($aDeclarationBlocks as $oBlock) {
  68  	 	 	 foreach ($oBlock->getSelectors() as $oSelector) {
  69  	 	 	 	 if ($sSpecificitySearch === null) {
  70  	 	 	 	 	 $aResult[] = $oSelector;
  71  	 	 	 	 } else {
  72  	 	 	 	 	 $sComparator = '===';
  73  	 	 	 	 	 $aSpecificitySearch = explode(' ', $sSpecificitySearch);
  74  	 	 	 	 	 $iTargetSpecificity = $aSpecificitySearch[0];
  75  	 	 	 	 	 if(count($aSpecificitySearch) > 1) {
  76  	 	 	 	 	 	 $sComparator = $aSpecificitySearch[0];
  77  	 	 	 	 	 	 $iTargetSpecificity = $aSpecificitySearch[1];
  78  	 	 	 	 	 }
  79  	 	 	 	 	 $iTargetSpecificity = (int)$iTargetSpecificity;
  80  	 	 	 	 	 $iSelectorSpecificity = $oSelector->getSpecificity();
  81  	 	 	 	 	 $bMatches = false;
  82  	 	 	 	 	 switch($sComparator) {
  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  	 	 	 	 	 	 case '>':
  93  	 	 	 	 	 	 	 $bMatches = $iSelectorSpecificity > $iTargetSpecificity;
  94  	 	 	 	 	 	 break;
  95  	 	 	 	 	 	 default:
  96  	 	 	 	 	 	 	 $bMatches = $iSelectorSpecificity === $iTargetSpecificity;
  97  	 	 	 	 	 	 break;
  98  	 	 	 	 	 }
  99  	 	 	 	 	 if ($bMatches) {
 100  	 	 	 	 	 	 $aResult[] = $oSelector;
 101  	 	 	 	 	 }
 102  	 	 	 	 }
 103  	 	 	 }
 104  	 	 }
 105  	 }
 106  
 107  }