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
CSSList.php
302 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Sabberworm\CSS\CSSList; |
| 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\SourceException; |
| 9 | use MailPoetVendor\Sabberworm\CSS\Parsing\UnexpectedEOFException; |
| 10 | use MailPoetVendor\Sabberworm\CSS\Parsing\UnexpectedTokenException; |
| 11 | use MailPoetVendor\Sabberworm\CSS\Property\AtRule; |
| 12 | use MailPoetVendor\Sabberworm\CSS\Property\Charset; |
| 13 | use MailPoetVendor\Sabberworm\CSS\Property\CSSNamespace; |
| 14 | use MailPoetVendor\Sabberworm\CSS\Property\Import; |
| 15 | use MailPoetVendor\Sabberworm\CSS\Property\Selector; |
| 16 | use MailPoetVendor\Sabberworm\CSS\Renderable; |
| 17 | use MailPoetVendor\Sabberworm\CSS\RuleSet\AtRuleSet; |
| 18 | use MailPoetVendor\Sabberworm\CSS\RuleSet\DeclarationBlock; |
| 19 | use MailPoetVendor\Sabberworm\CSS\RuleSet\RuleSet; |
| 20 | use MailPoetVendor\Sabberworm\CSS\Settings; |
| 21 | use MailPoetVendor\Sabberworm\CSS\Value\CSSString; |
| 22 | use MailPoetVendor\Sabberworm\CSS\Value\URL; |
| 23 | use MailPoetVendor\Sabberworm\CSS\Value\Value; |
| 24 | abstract class CSSList implements Renderable, Commentable |
| 25 | { |
| 26 | protected $aComments; |
| 27 | protected $aContents; |
| 28 | protected $iLineNo; |
| 29 | public function __construct($iLineNo = 0) |
| 30 | { |
| 31 | $this->aComments = []; |
| 32 | $this->aContents = []; |
| 33 | $this->iLineNo = $iLineNo; |
| 34 | } |
| 35 | public static function parseList(ParserState $oParserState, CSSList $oList) |
| 36 | { |
| 37 | $bIsRoot = $oList instanceof Document; |
| 38 | if (\is_string($oParserState)) { |
| 39 | $oParserState = new ParserState($oParserState, Settings::create()); |
| 40 | } |
| 41 | $bLenientParsing = $oParserState->getSettings()->bLenientParsing; |
| 42 | $aComments = []; |
| 43 | while (!$oParserState->isEnd()) { |
| 44 | $aComments = \array_merge($aComments, $oParserState->consumeWhiteSpace()); |
| 45 | $oListItem = null; |
| 46 | if ($bLenientParsing) { |
| 47 | try { |
| 48 | $oListItem = self::parseListItem($oParserState, $oList); |
| 49 | } catch (UnexpectedTokenException $e) { |
| 50 | $oListItem = \false; |
| 51 | } |
| 52 | } else { |
| 53 | $oListItem = self::parseListItem($oParserState, $oList); |
| 54 | } |
| 55 | if ($oListItem === null) { |
| 56 | // List parsing finished |
| 57 | return; |
| 58 | } |
| 59 | if ($oListItem) { |
| 60 | $oListItem->addComments($aComments); |
| 61 | $oList->append($oListItem); |
| 62 | } |
| 63 | $aComments = $oParserState->consumeWhiteSpace(); |
| 64 | } |
| 65 | $oList->addComments($aComments); |
| 66 | if (!$bIsRoot && !$bLenientParsing) { |
| 67 | throw new SourceException("Unexpected end of document", $oParserState->currentLine()); |
| 68 | } |
| 69 | } |
| 70 | private static function parseListItem(ParserState $oParserState, CSSList $oList) |
| 71 | { |
| 72 | $bIsRoot = $oList instanceof Document; |
| 73 | if ($oParserState->comes('@')) { |
| 74 | $oAtRule = self::parseAtRule($oParserState); |
| 75 | if ($oAtRule instanceof Charset) { |
| 76 | if (!$bIsRoot) { |
| 77 | throw new UnexpectedTokenException('@charset may only occur in root document', '', 'custom', $oParserState->currentLine()); |
| 78 | } |
| 79 | if (\count($oList->getContents()) > 0) { |
| 80 | throw new UnexpectedTokenException('@charset must be the first parseable token in a document', '', 'custom', $oParserState->currentLine()); |
| 81 | } |
| 82 | $oParserState->setCharset($oAtRule->getCharset()); |
| 83 | } |
| 84 | return $oAtRule; |
| 85 | } elseif ($oParserState->comes('}')) { |
| 86 | if ($bIsRoot) { |
| 87 | if ($oParserState->getSettings()->bLenientParsing) { |
| 88 | return DeclarationBlock::parse($oParserState); |
| 89 | } else { |
| 90 | throw new SourceException("Unopened {", $oParserState->currentLine()); |
| 91 | } |
| 92 | } else { |
| 93 | // End of list |
| 94 | return null; |
| 95 | } |
| 96 | } else { |
| 97 | return DeclarationBlock::parse($oParserState, $oList); |
| 98 | } |
| 99 | } |
| 100 | private static function parseAtRule(ParserState $oParserState) |
| 101 | { |
| 102 | $oParserState->consume('@'); |
| 103 | $sIdentifier = $oParserState->parseIdentifier(); |
| 104 | $iIdentifierLineNum = $oParserState->currentLine(); |
| 105 | $oParserState->consumeWhiteSpace(); |
| 106 | if ($sIdentifier === 'import') { |
| 107 | $oLocation = URL::parse($oParserState); |
| 108 | $oParserState->consumeWhiteSpace(); |
| 109 | $sMediaQuery = null; |
| 110 | if (!$oParserState->comes(';')) { |
| 111 | $sMediaQuery = \trim($oParserState->consumeUntil([';', ParserState::EOF])); |
| 112 | } |
| 113 | $oParserState->consumeUntil([';', ParserState::EOF], \true, \true); |
| 114 | return new Import($oLocation, $sMediaQuery ?: null, $iIdentifierLineNum); |
| 115 | } elseif ($sIdentifier === 'charset') { |
| 116 | $oCharsetString = CSSString::parse($oParserState); |
| 117 | $oParserState->consumeWhiteSpace(); |
| 118 | $oParserState->consumeUntil([';', ParserState::EOF], \true, \true); |
| 119 | return new Charset($oCharsetString, $iIdentifierLineNum); |
| 120 | } elseif (self::identifierIs($sIdentifier, 'keyframes')) { |
| 121 | $oResult = new KeyFrame($iIdentifierLineNum); |
| 122 | $oResult->setVendorKeyFrame($sIdentifier); |
| 123 | $oResult->setAnimationName(\trim($oParserState->consumeUntil('{', \false, \true))); |
| 124 | CSSList::parseList($oParserState, $oResult); |
| 125 | if ($oParserState->comes('}')) { |
| 126 | $oParserState->consume('}'); |
| 127 | } |
| 128 | return $oResult; |
| 129 | } elseif ($sIdentifier === 'namespace') { |
| 130 | $sPrefix = null; |
| 131 | $mUrl = Value::parsePrimitiveValue($oParserState); |
| 132 | if (!$oParserState->comes(';')) { |
| 133 | $sPrefix = $mUrl; |
| 134 | $mUrl = Value::parsePrimitiveValue($oParserState); |
| 135 | } |
| 136 | $oParserState->consumeUntil([';', ParserState::EOF], \true, \true); |
| 137 | if ($sPrefix !== null && !\is_string($sPrefix)) { |
| 138 | throw new UnexpectedTokenException('Wrong namespace prefix', $sPrefix, 'custom', $iIdentifierLineNum); |
| 139 | } |
| 140 | if (!($mUrl instanceof CSSString || $mUrl instanceof URL)) { |
| 141 | throw new UnexpectedTokenException('Wrong namespace url of invalid type', $mUrl, 'custom', $iIdentifierLineNum); |
| 142 | } |
| 143 | return new CSSNamespace($mUrl, $sPrefix, $iIdentifierLineNum); |
| 144 | } else { |
| 145 | // Unknown other at rule (font-face or such) |
| 146 | $sArgs = \trim($oParserState->consumeUntil('{', \false, \true)); |
| 147 | if (\substr_count($sArgs, "(") != \substr_count($sArgs, ")")) { |
| 148 | if ($oParserState->getSettings()->bLenientParsing) { |
| 149 | return null; |
| 150 | } else { |
| 151 | throw new SourceException("Unmatched brace count in media query", $oParserState->currentLine()); |
| 152 | } |
| 153 | } |
| 154 | $bUseRuleSet = \true; |
| 155 | foreach (\explode('/', AtRule::BLOCK_RULES) as $sBlockRuleName) { |
| 156 | if (self::identifierIs($sIdentifier, $sBlockRuleName)) { |
| 157 | $bUseRuleSet = \false; |
| 158 | break; |
| 159 | } |
| 160 | } |
| 161 | if ($bUseRuleSet) { |
| 162 | $oAtRule = new AtRuleSet($sIdentifier, $sArgs, $iIdentifierLineNum); |
| 163 | RuleSet::parseRuleSet($oParserState, $oAtRule); |
| 164 | } else { |
| 165 | $oAtRule = new AtRuleBlockList($sIdentifier, $sArgs, $iIdentifierLineNum); |
| 166 | CSSList::parseList($oParserState, $oAtRule); |
| 167 | if ($oParserState->comes('}')) { |
| 168 | $oParserState->consume('}'); |
| 169 | } |
| 170 | } |
| 171 | return $oAtRule; |
| 172 | } |
| 173 | } |
| 174 | private static function identifierIs($sIdentifier, $sMatch) |
| 175 | { |
| 176 | return \strcasecmp($sIdentifier, $sMatch) === 0 ?: \preg_match("/^(-\\w+-)?{$sMatch}\$/i", $sIdentifier) === 1; |
| 177 | } |
| 178 | public function getLineNo() |
| 179 | { |
| 180 | return $this->iLineNo; |
| 181 | } |
| 182 | public function prepend($oItem) |
| 183 | { |
| 184 | \array_unshift($this->aContents, $oItem); |
| 185 | } |
| 186 | public function append($oItem) |
| 187 | { |
| 188 | $this->aContents[] = $oItem; |
| 189 | } |
| 190 | public function splice($iOffset, $iLength = null, $mReplacement = null) |
| 191 | { |
| 192 | \array_splice($this->aContents, $iOffset, $iLength, $mReplacement); |
| 193 | } |
| 194 | public function remove($oItemToRemove) |
| 195 | { |
| 196 | $iKey = \array_search($oItemToRemove, $this->aContents, \true); |
| 197 | if ($iKey !== \false) { |
| 198 | unset($this->aContents[$iKey]); |
| 199 | return \true; |
| 200 | } |
| 201 | return \false; |
| 202 | } |
| 203 | public function replace($oOldItem, $mNewItem) |
| 204 | { |
| 205 | $iKey = \array_search($oOldItem, $this->aContents, \true); |
| 206 | if ($iKey !== \false) { |
| 207 | if (\is_array($mNewItem)) { |
| 208 | \array_splice($this->aContents, $iKey, 1, $mNewItem); |
| 209 | } else { |
| 210 | \array_splice($this->aContents, $iKey, 1, [$mNewItem]); |
| 211 | } |
| 212 | return \true; |
| 213 | } |
| 214 | return \false; |
| 215 | } |
| 216 | public function setContents(array $aContents) |
| 217 | { |
| 218 | $this->aContents = []; |
| 219 | foreach ($aContents as $content) { |
| 220 | $this->append($content); |
| 221 | } |
| 222 | } |
| 223 | public function removeDeclarationBlockBySelector($mSelector, $bRemoveAll = \false) |
| 224 | { |
| 225 | if ($mSelector instanceof DeclarationBlock) { |
| 226 | $mSelector = $mSelector->getSelectors(); |
| 227 | } |
| 228 | if (!\is_array($mSelector)) { |
| 229 | $mSelector = \explode(',', $mSelector); |
| 230 | } |
| 231 | foreach ($mSelector as $iKey => &$mSel) { |
| 232 | if (!$mSel instanceof Selector) { |
| 233 | if (!Selector::isValid($mSel)) { |
| 234 | throw new UnexpectedTokenException("Selector did not match '" . Selector::SELECTOR_VALIDATION_RX . "'.", $mSel, "custom"); |
| 235 | } |
| 236 | $mSel = new Selector($mSel); |
| 237 | } |
| 238 | } |
| 239 | foreach ($this->aContents as $iKey => $mItem) { |
| 240 | if (!$mItem instanceof DeclarationBlock) { |
| 241 | continue; |
| 242 | } |
| 243 | if ($mItem->getSelectors() == $mSelector) { |
| 244 | unset($this->aContents[$iKey]); |
| 245 | if (!$bRemoveAll) { |
| 246 | return; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | public function __toString() |
| 252 | { |
| 253 | return $this->render(new OutputFormat()); |
| 254 | } |
| 255 | protected function renderListContents(OutputFormat $oOutputFormat) |
| 256 | { |
| 257 | $sResult = ''; |
| 258 | $bIsFirst = \true; |
| 259 | $oNextLevel = $oOutputFormat; |
| 260 | if (!$this->isRootList()) { |
| 261 | $oNextLevel = $oOutputFormat->nextLevel(); |
| 262 | } |
| 263 | foreach ($this->aContents as $oContent) { |
| 264 | $sRendered = $oOutputFormat->safely(function () use($oNextLevel, $oContent) { |
| 265 | return $oContent->render($oNextLevel); |
| 266 | }); |
| 267 | if ($sRendered === null) { |
| 268 | continue; |
| 269 | } |
| 270 | if ($bIsFirst) { |
| 271 | $bIsFirst = \false; |
| 272 | $sResult .= $oNextLevel->spaceBeforeBlocks(); |
| 273 | } else { |
| 274 | $sResult .= $oNextLevel->spaceBetweenBlocks(); |
| 275 | } |
| 276 | $sResult .= $sRendered; |
| 277 | } |
| 278 | if (!$bIsFirst) { |
| 279 | // Had some output |
| 280 | $sResult .= $oOutputFormat->spaceAfterBlocks(); |
| 281 | } |
| 282 | return $sResult; |
| 283 | } |
| 284 | public abstract function isRootList(); |
| 285 | public function getContents() |
| 286 | { |
| 287 | return $this->aContents; |
| 288 | } |
| 289 | public function addComments(array $aComments) |
| 290 | { |
| 291 | $this->aComments = \array_merge($this->aComments, $aComments); |
| 292 | } |
| 293 | public function getComments() |
| 294 | { |
| 295 | return $this->aComments; |
| 296 | } |
| 297 | public function setComments(array $aComments) |
| 298 | { |
| 299 | $this->aComments = $aComments; |
| 300 | } |
| 301 | } |
| 302 |