Handler
2 years ago
Shortcut
2 years ago
Tokenizer
2 years ago
Parser.php
2 years ago
ParserInterface.php
2 years ago
Reader.php
2 years ago
Token.php
2 years ago
TokenStream.php
2 years ago
index.php
2 years ago
Parser.php
245 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Component\CssSelector\Parser; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Symfony\Component\CssSelector\Exception\SyntaxErrorException; |
| 5 | use MailPoetVendor\Symfony\Component\CssSelector\Node; |
| 6 | use MailPoetVendor\Symfony\Component\CssSelector\Parser\Tokenizer\Tokenizer; |
| 7 | class Parser implements ParserInterface |
| 8 | { |
| 9 | private $tokenizer; |
| 10 | public function __construct(?Tokenizer $tokenizer = null) |
| 11 | { |
| 12 | $this->tokenizer = $tokenizer ?? new Tokenizer(); |
| 13 | } |
| 14 | public function parse(string $source) : array |
| 15 | { |
| 16 | $reader = new Reader($source); |
| 17 | $stream = $this->tokenizer->tokenize($reader); |
| 18 | return $this->parseSelectorList($stream); |
| 19 | } |
| 20 | public static function parseSeries(array $tokens) : array |
| 21 | { |
| 22 | foreach ($tokens as $token) { |
| 23 | if ($token->isString()) { |
| 24 | throw SyntaxErrorException::stringAsFunctionArgument(); |
| 25 | } |
| 26 | } |
| 27 | $joined = \trim(\implode('', \array_map(function (Token $token) { |
| 28 | return $token->getValue(); |
| 29 | }, $tokens))); |
| 30 | $int = function ($string) { |
| 31 | if (!\is_numeric($string)) { |
| 32 | throw SyntaxErrorException::stringAsFunctionArgument(); |
| 33 | } |
| 34 | return (int) $string; |
| 35 | }; |
| 36 | switch (\true) { |
| 37 | case 'odd' === $joined: |
| 38 | return [2, 1]; |
| 39 | case 'even' === $joined: |
| 40 | return [2, 0]; |
| 41 | case 'n' === $joined: |
| 42 | return [1, 0]; |
| 43 | case !\str_contains($joined, 'n'): |
| 44 | return [0, $int($joined)]; |
| 45 | } |
| 46 | $split = \explode('n', $joined); |
| 47 | $first = $split[0] ?? null; |
| 48 | return [$first ? '-' === $first || '+' === $first ? $int($first . '1') : $int($first) : 1, isset($split[1]) && $split[1] ? $int($split[1]) : 0]; |
| 49 | } |
| 50 | private function parseSelectorList(TokenStream $stream) : array |
| 51 | { |
| 52 | $stream->skipWhitespace(); |
| 53 | $selectors = []; |
| 54 | while (\true) { |
| 55 | $selectors[] = $this->parserSelectorNode($stream); |
| 56 | if ($stream->getPeek()->isDelimiter([','])) { |
| 57 | $stream->getNext(); |
| 58 | $stream->skipWhitespace(); |
| 59 | } else { |
| 60 | break; |
| 61 | } |
| 62 | } |
| 63 | return $selectors; |
| 64 | } |
| 65 | private function parserSelectorNode(TokenStream $stream) : Node\SelectorNode |
| 66 | { |
| 67 | [$result, $pseudoElement] = $this->parseSimpleSelector($stream); |
| 68 | while (\true) { |
| 69 | $stream->skipWhitespace(); |
| 70 | $peek = $stream->getPeek(); |
| 71 | if ($peek->isFileEnd() || $peek->isDelimiter([','])) { |
| 72 | break; |
| 73 | } |
| 74 | if (null !== $pseudoElement) { |
| 75 | throw SyntaxErrorException::pseudoElementFound($pseudoElement, 'not at the end of a selector'); |
| 76 | } |
| 77 | if ($peek->isDelimiter(['+', '>', '~'])) { |
| 78 | $combinator = $stream->getNext()->getValue(); |
| 79 | $stream->skipWhitespace(); |
| 80 | } else { |
| 81 | $combinator = ' '; |
| 82 | } |
| 83 | [$nextSelector, $pseudoElement] = $this->parseSimpleSelector($stream); |
| 84 | $result = new Node\CombinedSelectorNode($result, $combinator, $nextSelector); |
| 85 | } |
| 86 | return new Node\SelectorNode($result, $pseudoElement); |
| 87 | } |
| 88 | private function parseSimpleSelector(TokenStream $stream, bool $insideNegation = \false) : array |
| 89 | { |
| 90 | $stream->skipWhitespace(); |
| 91 | $selectorStart = \count($stream->getUsed()); |
| 92 | $result = $this->parseElementNode($stream); |
| 93 | $pseudoElement = null; |
| 94 | while (\true) { |
| 95 | $peek = $stream->getPeek(); |
| 96 | if ($peek->isWhitespace() || $peek->isFileEnd() || $peek->isDelimiter([',', '+', '>', '~']) || $insideNegation && $peek->isDelimiter([')'])) { |
| 97 | break; |
| 98 | } |
| 99 | if (null !== $pseudoElement) { |
| 100 | throw SyntaxErrorException::pseudoElementFound($pseudoElement, 'not at the end of a selector'); |
| 101 | } |
| 102 | if ($peek->isHash()) { |
| 103 | $result = new Node\HashNode($result, $stream->getNext()->getValue()); |
| 104 | } elseif ($peek->isDelimiter(['.'])) { |
| 105 | $stream->getNext(); |
| 106 | $result = new Node\ClassNode($result, $stream->getNextIdentifier()); |
| 107 | } elseif ($peek->isDelimiter(['['])) { |
| 108 | $stream->getNext(); |
| 109 | $result = $this->parseAttributeNode($result, $stream); |
| 110 | } elseif ($peek->isDelimiter([':'])) { |
| 111 | $stream->getNext(); |
| 112 | if ($stream->getPeek()->isDelimiter([':'])) { |
| 113 | $stream->getNext(); |
| 114 | $pseudoElement = $stream->getNextIdentifier(); |
| 115 | continue; |
| 116 | } |
| 117 | $identifier = $stream->getNextIdentifier(); |
| 118 | if (\in_array(\strtolower($identifier), ['first-line', 'first-letter', 'before', 'after'])) { |
| 119 | // Special case: CSS 2.1 pseudo-elements can have a single ':'. |
| 120 | // Any new pseudo-element must have two. |
| 121 | $pseudoElement = $identifier; |
| 122 | continue; |
| 123 | } |
| 124 | if (!$stream->getPeek()->isDelimiter(['('])) { |
| 125 | $result = new Node\PseudoNode($result, $identifier); |
| 126 | continue; |
| 127 | } |
| 128 | $stream->getNext(); |
| 129 | $stream->skipWhitespace(); |
| 130 | if ('not' === \strtolower($identifier)) { |
| 131 | if ($insideNegation) { |
| 132 | throw SyntaxErrorException::nestedNot(); |
| 133 | } |
| 134 | [$argument, $argumentPseudoElement] = $this->parseSimpleSelector($stream, \true); |
| 135 | $next = $stream->getNext(); |
| 136 | if (null !== $argumentPseudoElement) { |
| 137 | throw SyntaxErrorException::pseudoElementFound($argumentPseudoElement, 'inside ::not()'); |
| 138 | } |
| 139 | if (!$next->isDelimiter([')'])) { |
| 140 | throw SyntaxErrorException::unexpectedToken('")"', $next); |
| 141 | } |
| 142 | $result = new Node\NegationNode($result, $argument); |
| 143 | } else { |
| 144 | $arguments = []; |
| 145 | $next = null; |
| 146 | while (\true) { |
| 147 | $stream->skipWhitespace(); |
| 148 | $next = $stream->getNext(); |
| 149 | if ($next->isIdentifier() || $next->isString() || $next->isNumber() || $next->isDelimiter(['+', '-'])) { |
| 150 | $arguments[] = $next; |
| 151 | } elseif ($next->isDelimiter([')'])) { |
| 152 | break; |
| 153 | } else { |
| 154 | throw SyntaxErrorException::unexpectedToken('an argument', $next); |
| 155 | } |
| 156 | } |
| 157 | if (empty($arguments)) { |
| 158 | throw SyntaxErrorException::unexpectedToken('at least one argument', $next); |
| 159 | } |
| 160 | $result = new Node\FunctionNode($result, $identifier, $arguments); |
| 161 | } |
| 162 | } else { |
| 163 | throw SyntaxErrorException::unexpectedToken('selector', $peek); |
| 164 | } |
| 165 | } |
| 166 | if (\count($stream->getUsed()) === $selectorStart) { |
| 167 | throw SyntaxErrorException::unexpectedToken('selector', $stream->getPeek()); |
| 168 | } |
| 169 | return [$result, $pseudoElement]; |
| 170 | } |
| 171 | private function parseElementNode(TokenStream $stream) : Node\ElementNode |
| 172 | { |
| 173 | $peek = $stream->getPeek(); |
| 174 | if ($peek->isIdentifier() || $peek->isDelimiter(['*'])) { |
| 175 | if ($peek->isIdentifier()) { |
| 176 | $namespace = $stream->getNext()->getValue(); |
| 177 | } else { |
| 178 | $stream->getNext(); |
| 179 | $namespace = null; |
| 180 | } |
| 181 | if ($stream->getPeek()->isDelimiter(['|'])) { |
| 182 | $stream->getNext(); |
| 183 | $element = $stream->getNextIdentifierOrStar(); |
| 184 | } else { |
| 185 | $element = $namespace; |
| 186 | $namespace = null; |
| 187 | } |
| 188 | } else { |
| 189 | $element = $namespace = null; |
| 190 | } |
| 191 | return new Node\ElementNode($namespace, $element); |
| 192 | } |
| 193 | private function parseAttributeNode(Node\NodeInterface $selector, TokenStream $stream) : Node\AttributeNode |
| 194 | { |
| 195 | $stream->skipWhitespace(); |
| 196 | $attribute = $stream->getNextIdentifierOrStar(); |
| 197 | if (null === $attribute && !$stream->getPeek()->isDelimiter(['|'])) { |
| 198 | throw SyntaxErrorException::unexpectedToken('"|"', $stream->getPeek()); |
| 199 | } |
| 200 | if ($stream->getPeek()->isDelimiter(['|'])) { |
| 201 | $stream->getNext(); |
| 202 | if ($stream->getPeek()->isDelimiter(['='])) { |
| 203 | $namespace = null; |
| 204 | $stream->getNext(); |
| 205 | $operator = '|='; |
| 206 | } else { |
| 207 | $namespace = $attribute; |
| 208 | $attribute = $stream->getNextIdentifier(); |
| 209 | $operator = null; |
| 210 | } |
| 211 | } else { |
| 212 | $namespace = $operator = null; |
| 213 | } |
| 214 | if (null === $operator) { |
| 215 | $stream->skipWhitespace(); |
| 216 | $next = $stream->getNext(); |
| 217 | if ($next->isDelimiter([']'])) { |
| 218 | return new Node\AttributeNode($selector, $namespace, $attribute, 'exists', null); |
| 219 | } elseif ($next->isDelimiter(['='])) { |
| 220 | $operator = '='; |
| 221 | } elseif ($next->isDelimiter(['^', '$', '*', '~', '|', '!']) && $stream->getPeek()->isDelimiter(['='])) { |
| 222 | $operator = $next->getValue() . '='; |
| 223 | $stream->getNext(); |
| 224 | } else { |
| 225 | throw SyntaxErrorException::unexpectedToken('operator', $next); |
| 226 | } |
| 227 | } |
| 228 | $stream->skipWhitespace(); |
| 229 | $value = $stream->getNext(); |
| 230 | if ($value->isNumber()) { |
| 231 | // if the value is a number, it's casted into a string |
| 232 | $value = new Token(Token::TYPE_STRING, (string) $value->getValue(), $value->getPosition()); |
| 233 | } |
| 234 | if (!($value->isIdentifier() || $value->isString())) { |
| 235 | throw SyntaxErrorException::unexpectedToken('string or identifier', $value); |
| 236 | } |
| 237 | $stream->skipWhitespace(); |
| 238 | $next = $stream->getNext(); |
| 239 | if (!$next->isDelimiter([']'])) { |
| 240 | throw SyntaxErrorException::unexpectedToken('"]"', $next); |
| 241 | } |
| 242 | return new Node\AttributeNode($selector, $namespace, $attribute, $operator, $value->getValue()); |
| 243 | } |
| 244 | } |
| 245 |