| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Sabberworm\CSS\Property\Selector; |
| 6 |
|
| 7 |
use Sabberworm\CSS\Comment\Comment; |
| 8 |
use Sabberworm\CSS\OutputFormat; |
| 9 |
use Sabberworm\CSS\Parsing\ParserState; |
| 10 |
use Sabberworm\CSS\Parsing\UnexpectedTokenException; |
| 11 |
use Sabberworm\CSS\ShortClassNameProvider; |
| 12 |
|
| 13 |
use function Safe\preg_match; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class representing a CSS compound selector. |
| 17 |
* Selectors have to be split at combinators (space, `>`, `+`, `~`) before being passed to this class. |
| 18 |
*/ |
| 19 |
class CompoundSelector implements Component |
| 20 |
{ |
| 21 |
use ShortClassNameProvider; |
| 22 |
|
| 23 |
private const PARSER_STOP_CHARACTERS = [ |
| 24 |
'{', |
| 25 |
'}', |
| 26 |
'\'', |
| 27 |
'"', |
| 28 |
'(', |
| 29 |
')', |
| 30 |
'[', |
| 31 |
']', |
| 32 |
',', |
| 33 |
' ', |
| 34 |
"\t", |
| 35 |
"\n", |
| 36 |
"\r", |
| 37 |
'>', |
| 38 |
'+', |
| 39 |
'~', |
| 40 |
ParserState::EOF, |
| 41 |
'', // `ParserState::peek()` returns empty string rather than `ParserState::EOF` when end of string is reached |
| 42 |
]; |
| 43 |
|
| 44 |
private const SELECTOR_VALIDATION_RX = '/ |
| 45 |
^ |
| 46 |
# not starting with whitespace |
| 47 |
(?!\\s) |
| 48 |
(?: |
| 49 |
(?: |
| 50 |
# any sequence of valid unescaped characters, except quotes |
| 51 |
[a-zA-Z0-9\\x{00A0}-\\x{FFFF}_^$|*=~\\[\\]()\\-\\s\\.:#+>,]++ |
| 52 |
| |
| 53 |
# one or more escaped characters |
| 54 |
(?:\\\\.)++ |
| 55 |
| |
| 56 |
# quoted text, like in `[id="example"]` |
| 57 |
(?: |
| 58 |
# opening quote |
| 59 |
([\'"]) |
| 60 |
(?: |
| 61 |
# sequence of characters except closing quote or backslash |
| 62 |
(?:(?!\\g{-1}|\\\\).)++ |
| 63 |
| |
| 64 |
# one or more escaped characters |
| 65 |
(?:\\\\.)++ |
| 66 |
)*+ # zero or more times |
| 67 |
# closing quote or end (unmatched quote is currently allowed) |
| 68 |
(?:\\g{-1}|$) |
| 69 |
) |
| 70 |
)++ # one or more times |
| 71 |
| |
| 72 |
# keyframe animation progress percentage (e.g. 50%) |
| 73 |
(?:\\d++%) |
| 74 |
) |
| 75 |
# not ending with whitespace |
| 76 |
(?<!\\s) |
| 77 |
$ |
| 78 |
/ux'; |
| 79 |
|
| 80 |
/** |
| 81 |
* @var non-empty-string |
| 82 |
*/ |
| 83 |
private $value; |
| 84 |
|
| 85 |
/** |
| 86 |
* @param non-empty-string $value |
| 87 |
*/ |
| 88 |
public function __construct(string $value) |
| 89 |
{ |
| 90 |
$this->setValue($value); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* @param list<Comment> $comments |
| 95 |
* |
| 96 |
* @throws UnexpectedTokenException |
| 97 |
* |
| 98 |
* @internal |
| 99 |
*/ |
| 100 |
public static function parse(ParserState $parserState, array &$comments = []): self |
| 101 |
{ |
| 102 |
$selectorParts = []; |
| 103 |
$stringWrapperCharacter = null; |
| 104 |
$functionNestingLevel = 0; |
| 105 |
$isWithinAttribute = false; |
| 106 |
|
| 107 |
while (true) { |
| 108 |
$selectorParts[] = $parserState->consumeUntil(self::PARSER_STOP_CHARACTERS, false, false, $comments); |
| 109 |
$nextCharacter = $parserState->peek(); |
| 110 |
switch ($nextCharacter) { |
| 111 |
case '': |
| 112 |
// EOF |
| 113 |
break 2; |
| 114 |
case '\'': |
| 115 |
// The fallthrough is intentional. |
| 116 |
case '"': |
| 117 |
$lastPart = \end($selectorParts); |
| 118 |
$backslashCount = \strspn(\strrev($lastPart), '\\'); |
| 119 |
$quoteIsEscaped = ($backslashCount % 2 === 1); |
| 120 |
if (!$quoteIsEscaped) { |
| 121 |
if (!\is_string($stringWrapperCharacter)) { |
| 122 |
$stringWrapperCharacter = $nextCharacter; |
| 123 |
} elseif ($stringWrapperCharacter === $nextCharacter) { |
| 124 |
$stringWrapperCharacter = null; |
| 125 |
} |
| 126 |
} |
| 127 |
break; |
| 128 |
case '(': |
| 129 |
if (!\is_string($stringWrapperCharacter)) { |
| 130 |
++$functionNestingLevel; |
| 131 |
} |
| 132 |
break; |
| 133 |
case ')': |
| 134 |
if (!\is_string($stringWrapperCharacter)) { |
| 135 |
if ($functionNestingLevel <= 0) { |
| 136 |
throw new UnexpectedTokenException( |
| 137 |
'anything but', |
| 138 |
')', |
| 139 |
'literal', |
| 140 |
$parserState->currentLine() |
| 141 |
); |
| 142 |
} |
| 143 |
--$functionNestingLevel; |
| 144 |
} |
| 145 |
break; |
| 146 |
case '[': |
| 147 |
if (!\is_string($stringWrapperCharacter)) { |
| 148 |
if ($isWithinAttribute) { |
| 149 |
throw new UnexpectedTokenException( |
| 150 |
'anything but', |
| 151 |
'[', |
| 152 |
'literal', |
| 153 |
$parserState->currentLine() |
| 154 |
); |
| 155 |
} |
| 156 |
$isWithinAttribute = true; |
| 157 |
} |
| 158 |
break; |
| 159 |
case ']': |
| 160 |
if (!\is_string($stringWrapperCharacter)) { |
| 161 |
if (!$isWithinAttribute) { |
| 162 |
throw new UnexpectedTokenException( |
| 163 |
'anything but', |
| 164 |
']', |
| 165 |
'literal', |
| 166 |
$parserState->currentLine() |
| 167 |
); |
| 168 |
} |
| 169 |
$isWithinAttribute = false; |
| 170 |
} |
| 171 |
break; |
| 172 |
case '{': |
| 173 |
// The fallthrough is intentional. |
| 174 |
case '}': |
| 175 |
if (!\is_string($stringWrapperCharacter)) { |
| 176 |
break 2; |
| 177 |
} |
| 178 |
break; |
| 179 |
case ',': |
| 180 |
// The fallthrough is intentional. |
| 181 |
case ' ': |
| 182 |
// The fallthrough is intentional. |
| 183 |
case "\t": |
| 184 |
// The fallthrough is intentional. |
| 185 |
case "\n": |
| 186 |
// The fallthrough is intentional. |
| 187 |
case "\r": |
| 188 |
// The fallthrough is intentional. |
| 189 |
case '>': |
| 190 |
// The fallthrough is intentional. |
| 191 |
case '+': |
| 192 |
// The fallthrough is intentional. |
| 193 |
case '~': |
| 194 |
if (!\is_string($stringWrapperCharacter) && $functionNestingLevel === 0 && !$isWithinAttribute) { |
| 195 |
break 2; |
| 196 |
} |
| 197 |
break; |
| 198 |
} |
| 199 |
$selectorParts[] = $parserState->consume(1); |
| 200 |
} |
| 201 |
|
| 202 |
if ($functionNestingLevel !== 0) { |
| 203 |
throw new UnexpectedTokenException(')', $nextCharacter, 'literal', $parserState->currentLine()); |
| 204 |
} |
| 205 |
if (\is_string($stringWrapperCharacter)) { |
| 206 |
throw new UnexpectedTokenException( |
| 207 |
$stringWrapperCharacter, |
| 208 |
$nextCharacter, |
| 209 |
'literal', |
| 210 |
$parserState->currentLine() |
| 211 |
); |
| 212 |
} |
| 213 |
|
| 214 |
$value = \implode('', $selectorParts); |
| 215 |
if ($value === '') { |
| 216 |
throw new UnexpectedTokenException('selector', $nextCharacter, 'literal', $parserState->currentLine()); |
| 217 |
} |
| 218 |
if (!self::isValid($value)) { |
| 219 |
throw new UnexpectedTokenException( |
| 220 |
'Selector component is not valid:', |
| 221 |
'`' . $value . '`', |
| 222 |
'custom', |
| 223 |
$parserState->currentLine() |
| 224 |
); |
| 225 |
} |
| 226 |
|
| 227 |
return new self($value); |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* @return non-empty-string |
| 232 |
*/ |
| 233 |
public function getValue(): string |
| 234 |
{ |
| 235 |
return $this->value; |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* @param non-empty-string $value |
| 240 |
* |
| 241 |
* @throws \UnexpectedValueException if `$value` contains invalid characters or has surrounding whitespce |
| 242 |
*/ |
| 243 |
public function setValue(string $value): void |
| 244 |
{ |
| 245 |
if (!self::isValid($value)) { |
| 246 |
throw new \UnexpectedValueException('`' . $value . '` is not a valid compound selector.'); |
| 247 |
} |
| 248 |
|
| 249 |
$this->value = $value; |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* @return int<0, max> |
| 254 |
*/ |
| 255 |
public function getSpecificity(): int |
| 256 |
{ |
| 257 |
return SpecificityCalculator::calculate($this->value); |
| 258 |
} |
| 259 |
|
| 260 |
public function render(OutputFormat $outputFormat): string |
| 261 |
{ |
| 262 |
return $this->getValue(); |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* @return array<string, bool|int|float|string|array<mixed>|null> |
| 267 |
* |
| 268 |
* @internal |
| 269 |
*/ |
| 270 |
public function getArrayRepresentation(): array |
| 271 |
{ |
| 272 |
return [ |
| 273 |
'class' => $this->getShortClassName(), |
| 274 |
'value' => $this->value, |
| 275 |
]; |
| 276 |
} |
| 277 |
|
| 278 |
private static function isValid(string $value): bool |
| 279 |
{ |
| 280 |
$numberOfMatches = preg_match(self::SELECTOR_VALIDATION_RX, $value); |
| 281 |
|
| 282 |
return $numberOfMatches === 1; |
| 283 |
} |
| 284 |
} |