Rule.php
224 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Sabberworm\CSS\Rule; |
| 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\Value\RuleValueList; |
| 12 | use MailPoetVendor\Sabberworm\CSS\Value\Value; |
| 13 | class Rule implements Renderable, Commentable |
| 14 | { |
| 15 | private $sRule; |
| 16 | private $mValue; |
| 17 | private $bIsImportant; |
| 18 | private $aIeHack; |
| 19 | protected $iLineNo; |
| 20 | protected $iColNo; |
| 21 | protected $aComments; |
| 22 | public function __construct($sRule, $iLineNo = 0, $iColNo = 0) |
| 23 | { |
| 24 | $this->sRule = $sRule; |
| 25 | $this->mValue = null; |
| 26 | $this->bIsImportant = \false; |
| 27 | $this->aIeHack = []; |
| 28 | $this->iLineNo = $iLineNo; |
| 29 | $this->iColNo = $iColNo; |
| 30 | $this->aComments = []; |
| 31 | } |
| 32 | public static function parse(ParserState $oParserState) |
| 33 | { |
| 34 | $aComments = $oParserState->consumeWhiteSpace(); |
| 35 | $oRule = new Rule($oParserState->parseIdentifier(!$oParserState->comes("--")), $oParserState->currentLine(), $oParserState->currentColumn()); |
| 36 | $oRule->setComments($aComments); |
| 37 | $oRule->addComments($oParserState->consumeWhiteSpace()); |
| 38 | $oParserState->consume(':'); |
| 39 | $oValue = Value::parseValue($oParserState, self::listDelimiterForRule($oRule->getRule())); |
| 40 | $oRule->setValue($oValue); |
| 41 | if ($oParserState->getSettings()->bLenientParsing) { |
| 42 | while ($oParserState->comes('\\')) { |
| 43 | $oParserState->consume('\\'); |
| 44 | $oRule->addIeHack($oParserState->consume()); |
| 45 | $oParserState->consumeWhiteSpace(); |
| 46 | } |
| 47 | } |
| 48 | $oParserState->consumeWhiteSpace(); |
| 49 | if ($oParserState->comes('!')) { |
| 50 | $oParserState->consume('!'); |
| 51 | $oParserState->consumeWhiteSpace(); |
| 52 | $oParserState->consume('important'); |
| 53 | $oRule->setIsImportant(\true); |
| 54 | } |
| 55 | $oParserState->consumeWhiteSpace(); |
| 56 | while ($oParserState->comes(';')) { |
| 57 | $oParserState->consume(';'); |
| 58 | } |
| 59 | $oParserState->consumeWhiteSpace(); |
| 60 | return $oRule; |
| 61 | } |
| 62 | private static function listDelimiterForRule($sRule) |
| 63 | { |
| 64 | if (\preg_match('/^font($|-)/', $sRule)) { |
| 65 | return [',', '/', ' ']; |
| 66 | } |
| 67 | return [',', ' ', '/']; |
| 68 | } |
| 69 | public function getLineNo() |
| 70 | { |
| 71 | return $this->iLineNo; |
| 72 | } |
| 73 | public function getColNo() |
| 74 | { |
| 75 | return $this->iColNo; |
| 76 | } |
| 77 | public function setPosition($iLine, $iColumn) |
| 78 | { |
| 79 | $this->iColNo = $iColumn; |
| 80 | $this->iLineNo = $iLine; |
| 81 | } |
| 82 | public function setRule($sRule) |
| 83 | { |
| 84 | $this->sRule = $sRule; |
| 85 | } |
| 86 | public function getRule() |
| 87 | { |
| 88 | return $this->sRule; |
| 89 | } |
| 90 | public function getValue() |
| 91 | { |
| 92 | return $this->mValue; |
| 93 | } |
| 94 | public function setValue($mValue) |
| 95 | { |
| 96 | $this->mValue = $mValue; |
| 97 | } |
| 98 | public function setValues(array $aSpaceSeparatedValues) |
| 99 | { |
| 100 | $oSpaceSeparatedList = null; |
| 101 | if (\count($aSpaceSeparatedValues) > 1) { |
| 102 | $oSpaceSeparatedList = new RuleValueList(' ', $this->iLineNo); |
| 103 | } |
| 104 | foreach ($aSpaceSeparatedValues as $aCommaSeparatedValues) { |
| 105 | $oCommaSeparatedList = null; |
| 106 | if (\count($aCommaSeparatedValues) > 1) { |
| 107 | $oCommaSeparatedList = new RuleValueList(',', $this->iLineNo); |
| 108 | } |
| 109 | foreach ($aCommaSeparatedValues as $mValue) { |
| 110 | if (!$oSpaceSeparatedList && !$oCommaSeparatedList) { |
| 111 | $this->mValue = $mValue; |
| 112 | return $mValue; |
| 113 | } |
| 114 | if ($oCommaSeparatedList) { |
| 115 | $oCommaSeparatedList->addListComponent($mValue); |
| 116 | } else { |
| 117 | $oSpaceSeparatedList->addListComponent($mValue); |
| 118 | } |
| 119 | } |
| 120 | if (!$oSpaceSeparatedList) { |
| 121 | $this->mValue = $oCommaSeparatedList; |
| 122 | return $oCommaSeparatedList; |
| 123 | } else { |
| 124 | $oSpaceSeparatedList->addListComponent($oCommaSeparatedList); |
| 125 | } |
| 126 | } |
| 127 | $this->mValue = $oSpaceSeparatedList; |
| 128 | return $oSpaceSeparatedList; |
| 129 | } |
| 130 | public function getValues() |
| 131 | { |
| 132 | if (!$this->mValue instanceof RuleValueList) { |
| 133 | return [[$this->mValue]]; |
| 134 | } |
| 135 | if ($this->mValue->getListSeparator() === ',') { |
| 136 | return [$this->mValue->getListComponents()]; |
| 137 | } |
| 138 | $aResult = []; |
| 139 | foreach ($this->mValue->getListComponents() as $mValue) { |
| 140 | if (!$mValue instanceof RuleValueList || $mValue->getListSeparator() !== ',') { |
| 141 | $aResult[] = [$mValue]; |
| 142 | continue; |
| 143 | } |
| 144 | if ($this->mValue->getListSeparator() === ' ' || \count($aResult) === 0) { |
| 145 | $aResult[] = []; |
| 146 | } |
| 147 | foreach ($mValue->getListComponents() as $mValue) { |
| 148 | $aResult[\count($aResult) - 1][] = $mValue; |
| 149 | } |
| 150 | } |
| 151 | return $aResult; |
| 152 | } |
| 153 | public function addValue($mValue, $sType = ' ') |
| 154 | { |
| 155 | if (!\is_array($mValue)) { |
| 156 | $mValue = [$mValue]; |
| 157 | } |
| 158 | if (!$this->mValue instanceof RuleValueList || $this->mValue->getListSeparator() !== $sType) { |
| 159 | $mCurrentValue = $this->mValue; |
| 160 | $this->mValue = new RuleValueList($sType, $this->iLineNo); |
| 161 | if ($mCurrentValue) { |
| 162 | $this->mValue->addListComponent($mCurrentValue); |
| 163 | } |
| 164 | } |
| 165 | foreach ($mValue as $mValueItem) { |
| 166 | $this->mValue->addListComponent($mValueItem); |
| 167 | } |
| 168 | } |
| 169 | public function addIeHack($iModifier) |
| 170 | { |
| 171 | $this->aIeHack[] = $iModifier; |
| 172 | } |
| 173 | public function setIeHack(array $aModifiers) |
| 174 | { |
| 175 | $this->aIeHack = $aModifiers; |
| 176 | } |
| 177 | public function getIeHack() |
| 178 | { |
| 179 | return $this->aIeHack; |
| 180 | } |
| 181 | public function setIsImportant($bIsImportant) |
| 182 | { |
| 183 | $this->bIsImportant = $bIsImportant; |
| 184 | } |
| 185 | public function getIsImportant() |
| 186 | { |
| 187 | return $this->bIsImportant; |
| 188 | } |
| 189 | public function __toString() |
| 190 | { |
| 191 | return $this->render(new OutputFormat()); |
| 192 | } |
| 193 | public function render(OutputFormat $oOutputFormat) |
| 194 | { |
| 195 | $sResult = "{$oOutputFormat->comments($this)}{$this->sRule}:{$oOutputFormat->spaceAfterRuleName()}"; |
| 196 | if ($this->mValue instanceof Value) { |
| 197 | // Can also be a ValueList |
| 198 | $sResult .= $this->mValue->render($oOutputFormat); |
| 199 | } else { |
| 200 | $sResult .= $this->mValue; |
| 201 | } |
| 202 | if (!empty($this->aIeHack)) { |
| 203 | $sResult .= ' \\' . \implode('\\', $this->aIeHack); |
| 204 | } |
| 205 | if ($this->bIsImportant) { |
| 206 | $sResult .= ' !important'; |
| 207 | } |
| 208 | $sResult .= ';'; |
| 209 | return $sResult; |
| 210 | } |
| 211 | public function addComments(array $aComments) |
| 212 | { |
| 213 | $this->aComments = \array_merge($this->aComments, $aComments); |
| 214 | } |
| 215 | public function getComments() |
| 216 | { |
| 217 | return $this->aComments; |
| 218 | } |
| 219 | public function setComments(array $aComments) |
| 220 | { |
| 221 | $this->aComments = $aComments; |
| 222 | } |
| 223 | } |
| 224 |