2 lines
15 KiB
PHP
2 lines
15 KiB
PHP
|
<?php
|
||
|
namespace Sabberworm\CSS\RuleSet; use Sabberworm\CSS\Property\Selector; use Sabberworm\CSS\Rule\Rule; use Sabberworm\CSS\Value\RuleValueList; use Sabberworm\CSS\Value\Value; use Sabberworm\CSS\Value\Size; use Sabberworm\CSS\Value\Color; use Sabberworm\CSS\Value\URL; use Sabberworm\CSS\Parsing\OutputException; class DeclarationBlock extends RuleSet { private $aSelectors; public function __construct($iLineNo = 0) { parent::__construct($iLineNo); $this->aSelectors = array(); } public function setSelectors($mSelector) { if (is_array($mSelector)) { $this->aSelectors = $mSelector; } else { $this->aSelectors = explode(',', $mSelector); } foreach ($this->aSelectors as $iKey => $mSelector) { if (!($mSelector instanceof Selector)) { $this->aSelectors[$iKey] = new Selector($mSelector); } } } public function removeSelector($mSelector) { if($mSelector instanceof Selector) { $mSelector = $mSelector->getSelector(); } foreach($this->aSelectors as $iKey => $oSelector) { if($oSelector->getSelector() === $mSelector) { unset($this->aSelectors[$iKey]); return true; } } return false; } public function getSelector() { return $this->getSelectors(); } public function setSelector($mSelector) { $this->setSelectors($mSelector); } public function getSelectors() { return $this->aSelectors; } public function expandShorthands() { $this->expandBorderShorthand(); $this->expandDimensionsShorthand(); $this->expandFontShorthand(); $this->expandBackgroundShorthand(); $this->expandListStyleShorthand(); } public function createShorthands() { $this->createBackgroundShorthand(); $this->createDimensionsShorthand(); $this->createBorderShorthand(); $this->createFontShorthand(); $this->createListStyleShorthand(); } public function expandBorderShorthand() { $aBorderRules = array( 'border', 'border-left', 'border-right', 'border-top', 'border-bottom' ); $aBorderSizes = array( 'thin', 'medium', 'thick' ); $aRules = $this->getRulesAssoc(); foreach ($aBorderRules as $sBorderRule) { if (!isset($aRules[$sBorderRule])) continue; $oRule = $aRules[$sBorderRule]; $mRuleValue = $oRule->getValue(); $aValues = array(); if (!$mRuleValue instanceof RuleValueList) { $aValues[] = $mRuleValue; } else { $aValues = $mRuleValue->getListComponents(); } foreach ($aValues as $mValue) { if ($mValue instanceof Value) { $mNewValue = clone $mValue; } else { $mNewValue = $mValue; } if ($mValue instanceof Size) { $sNewRuleName = $sBorderRule . "-width"; } else if ($mValue instanceof Color) { $sNewRuleName = $sBorderRule . "-color"; } else { if (in_array($mValue, $aBorderSizes)) { $sNewRuleName = $sBorderRule . "-width"; } else { $sNewRuleName = $sBorderRule . "-style"; } } $oNewRule = new Rule($sNewRuleName, $this->iLineNo); $oNewRule->setIsImportant($oRule->getIsImportant()); $oNewRule->addValue(array($mNewValue)); $this->addRule($oNewRule); } $this->removeRule($sBorderRule); } } public function expandDimensionsShorthand() { $aExpansions = array( 'margin' => 'margin-%s', 'padding' => 'padding-%s', 'border-color' => 'border-%s-color', 'border-style' => 'border-%s-style', 'border-width' => 'border-%s-width' ); $aRules = $this->getRulesAssoc(); foreach ($aExpansions as $sProperty => $sExpanded) { if (!isset($aRules[$sProperty])) continue; $oRule = $aRules[$sProperty]; $mRuleValue = $oRule->getValue(); $aValues = array(); if (!$mRuleValue instanceof RuleValueList) { $aValues[] = $mRuleValue; } else { $aValues = $mRuleValue->getListComponents(); } $top = $right = $bottom = $left = null; switch (count($aValues)) { case 1: $top = $right = $bottom = $left = $aValues[0]; break; case 2: $top = $bottom = $aValues[0]; $left = $right = $aValues[1]; break; case 3: $top = $aValues[0]; $left = $right = $aValues[1]; $bottom = $aValues[2]; break; case 4: $top = $aValues[0]; $right = $aValues[1]; $bottom = $aValues[2]; $left = $aValues[3]; break; } foreach (array('top', 'right', 'bottom', 'left') as $sPosition) { $oNewRule = new Rule(sprintf($sExpanded, $sPosition), $this->iLineNo); $oNewRule->setIsImportant($oRule->getIsImportant()); $oNewRule->addValue(${$sPosition}); $this->addRule($oNewRule); } $this->remove
|