AtRuleSet.php
2 years ago
DeclarationBlock.php
2 years ago
RuleSet.php
8 months ago
index.php
3 years ago
RuleSet.php
191 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Sabberworm\CSS\RuleSet; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Sabberworm\CSS\Comment\Comment; |
| 5 | use MailPoetVendor\Sabberworm\CSS\Comment\Commentable; |
| 6 | use MailPoetVendor\Sabberworm\CSS\OutputFormat; |
| 7 | use MailPoetVendor\Sabberworm\CSS\Parsing\ParserState; |
| 8 | use MailPoetVendor\Sabberworm\CSS\Parsing\UnexpectedEOFException; |
| 9 | use MailPoetVendor\Sabberworm\CSS\Parsing\UnexpectedTokenException; |
| 10 | use MailPoetVendor\Sabberworm\CSS\Renderable; |
| 11 | use MailPoetVendor\Sabberworm\CSS\Rule\Rule; |
| 12 | abstract class RuleSet implements Renderable, Commentable |
| 13 | { |
| 14 | private $aRules; |
| 15 | protected $iLineNo; |
| 16 | protected $aComments; |
| 17 | public function __construct($iLineNo = 0) |
| 18 | { |
| 19 | $this->aRules = []; |
| 20 | $this->iLineNo = $iLineNo; |
| 21 | $this->aComments = []; |
| 22 | } |
| 23 | public static function parseRuleSet(ParserState $oParserState, RuleSet $oRuleSet) |
| 24 | { |
| 25 | while ($oParserState->comes(';')) { |
| 26 | $oParserState->consume(';'); |
| 27 | } |
| 28 | while (!$oParserState->comes('}')) { |
| 29 | $oRule = null; |
| 30 | if ($oParserState->getSettings()->bLenientParsing) { |
| 31 | try { |
| 32 | $oRule = Rule::parse($oParserState); |
| 33 | } catch (UnexpectedTokenException $e) { |
| 34 | try { |
| 35 | $sConsume = $oParserState->consumeUntil(["\n", ";", '}'], \true); |
| 36 | // We need to “unfind” the matches to the end of the ruleSet as this will be matched later |
| 37 | if ($oParserState->streql(\substr($sConsume, -1), '}')) { |
| 38 | $oParserState->backtrack(1); |
| 39 | } else { |
| 40 | while ($oParserState->comes(';')) { |
| 41 | $oParserState->consume(';'); |
| 42 | } |
| 43 | } |
| 44 | } catch (UnexpectedTokenException $e) { |
| 45 | // We’ve reached the end of the document. Just close the RuleSet. |
| 46 | return; |
| 47 | } |
| 48 | } |
| 49 | } else { |
| 50 | $oRule = Rule::parse($oParserState); |
| 51 | } |
| 52 | if ($oRule) { |
| 53 | $oRuleSet->addRule($oRule); |
| 54 | } |
| 55 | } |
| 56 | $oParserState->consume('}'); |
| 57 | } |
| 58 | public function getLineNo() |
| 59 | { |
| 60 | return $this->iLineNo; |
| 61 | } |
| 62 | public function addRule(Rule $oRule,?Rule $oSibling = null) |
| 63 | { |
| 64 | $sRule = $oRule->getRule(); |
| 65 | if (!isset($this->aRules[$sRule])) { |
| 66 | $this->aRules[$sRule] = []; |
| 67 | } |
| 68 | $iPosition = \count($this->aRules[$sRule]); |
| 69 | if ($oSibling !== null) { |
| 70 | $iSiblingPos = \array_search($oSibling, $this->aRules[$sRule], \true); |
| 71 | if ($iSiblingPos !== \false) { |
| 72 | $iPosition = $iSiblingPos; |
| 73 | $oRule->setPosition($oSibling->getLineNo(), $oSibling->getColNo() - 1); |
| 74 | } |
| 75 | } |
| 76 | if ($oRule->getLineNo() === 0 && $oRule->getColNo() === 0) { |
| 77 | //this node is added manually, give it the next best line |
| 78 | $rules = $this->getRules(); |
| 79 | $pos = \count($rules); |
| 80 | if ($pos > 0) { |
| 81 | $last = $rules[$pos - 1]; |
| 82 | $oRule->setPosition($last->getLineNo() + 1, 0); |
| 83 | } |
| 84 | } |
| 85 | \array_splice($this->aRules[$sRule], $iPosition, 0, [$oRule]); |
| 86 | } |
| 87 | public function getRules($mRule = null) |
| 88 | { |
| 89 | if ($mRule instanceof Rule) { |
| 90 | $mRule = $mRule->getRule(); |
| 91 | } |
| 92 | $aResult = []; |
| 93 | foreach ($this->aRules as $sName => $aRules) { |
| 94 | // Either no search rule is given or the search rule matches the found rule exactly |
| 95 | // or the search rule ends in “-” and the found rule starts with the search rule. |
| 96 | if (!$mRule || $sName === $mRule || \strrpos($mRule, '-') === \strlen($mRule) - \strlen('-') && (\strpos($sName, $mRule) === 0 || $sName === \substr($mRule, 0, -1))) { |
| 97 | $aResult = \array_merge($aResult, $aRules); |
| 98 | } |
| 99 | } |
| 100 | \usort($aResult, function (Rule $first, Rule $second) { |
| 101 | if ($first->getLineNo() === $second->getLineNo()) { |
| 102 | return $first->getColNo() - $second->getColNo(); |
| 103 | } |
| 104 | return $first->getLineNo() - $second->getLineNo(); |
| 105 | }); |
| 106 | return $aResult; |
| 107 | } |
| 108 | public function setRules(array $aRules) |
| 109 | { |
| 110 | $this->aRules = []; |
| 111 | foreach ($aRules as $rule) { |
| 112 | $this->addRule($rule); |
| 113 | } |
| 114 | } |
| 115 | public function getRulesAssoc($mRule = null) |
| 116 | { |
| 117 | $aResult = []; |
| 118 | foreach ($this->getRules($mRule) as $oRule) { |
| 119 | $aResult[$oRule->getRule()] = $oRule; |
| 120 | } |
| 121 | return $aResult; |
| 122 | } |
| 123 | public function removeRule($mRule) |
| 124 | { |
| 125 | if ($mRule instanceof Rule) { |
| 126 | $sRule = $mRule->getRule(); |
| 127 | if (!isset($this->aRules[$sRule])) { |
| 128 | return; |
| 129 | } |
| 130 | foreach ($this->aRules[$sRule] as $iKey => $oRule) { |
| 131 | if ($oRule === $mRule) { |
| 132 | unset($this->aRules[$sRule][$iKey]); |
| 133 | } |
| 134 | } |
| 135 | } else { |
| 136 | foreach ($this->aRules as $sName => $aRules) { |
| 137 | // Either no search rule is given or the search rule matches the found rule exactly |
| 138 | // or the search rule ends in “-” and the found rule starts with the search rule or equals it |
| 139 | // (without the trailing dash). |
| 140 | if (!$mRule || $sName === $mRule || \strrpos($mRule, '-') === \strlen($mRule) - \strlen('-') && (\strpos($sName, $mRule) === 0 || $sName === \substr($mRule, 0, -1))) { |
| 141 | unset($this->aRules[$sName]); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | public function __toString() |
| 147 | { |
| 148 | return $this->render(new OutputFormat()); |
| 149 | } |
| 150 | protected function renderRules(OutputFormat $oOutputFormat) |
| 151 | { |
| 152 | $sResult = ''; |
| 153 | $bIsFirst = \true; |
| 154 | $oNextLevel = $oOutputFormat->nextLevel(); |
| 155 | foreach ($this->aRules as $aRules) { |
| 156 | foreach ($aRules as $oRule) { |
| 157 | $sRendered = $oNextLevel->safely(function () use($oRule, $oNextLevel) { |
| 158 | return $oRule->render($oNextLevel); |
| 159 | }); |
| 160 | if ($sRendered === null) { |
| 161 | continue; |
| 162 | } |
| 163 | if ($bIsFirst) { |
| 164 | $bIsFirst = \false; |
| 165 | $sResult .= $oNextLevel->spaceBeforeRules(); |
| 166 | } else { |
| 167 | $sResult .= $oNextLevel->spaceBetweenRules(); |
| 168 | } |
| 169 | $sResult .= $sRendered; |
| 170 | } |
| 171 | } |
| 172 | if (!$bIsFirst) { |
| 173 | // Had some output |
| 174 | $sResult .= $oOutputFormat->spaceAfterRules(); |
| 175 | } |
| 176 | return $oOutputFormat->removeLastSemicolon($sResult); |
| 177 | } |
| 178 | public function addComments(array $aComments) |
| 179 | { |
| 180 | $this->aComments = \array_merge($this->aComments, $aComments); |
| 181 | } |
| 182 | public function getComments() |
| 183 | { |
| 184 | return $this->aComments; |
| 185 | } |
| 186 | public function setComments(array $aComments) |
| 187 | { |
| 188 | $this->aComments = $aComments; |
| 189 | } |
| 190 | } |
| 191 |