-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathCombinator.php
More file actions
113 lines (98 loc) · 2.65 KB
/
Copy pathCombinator.php
File metadata and controls
113 lines (98 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
declare(strict_types=1);
namespace Sabberworm\CSS\Property\Selector;
use Sabberworm\CSS\Comment\Comment;
use Sabberworm\CSS\OutputFormat;
use Sabberworm\CSS\Parsing\ParserState;
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
use Sabberworm\CSS\ShortClassNameProvider;
/**
* Class representing a CSS selector combinator (space, `>`, `+`, or `~`).
*
* @phpstan-type ValidCombinatorValue ' '|'>'|'+'|'~'
*/
class Combinator implements Component
{
use ShortClassNameProvider;
/**
* @var ValidCombinatorValue
*/
private $value;
/**
* @param ValidCombinatorValue $value
*/
public function __construct(string $value)
{
$this->setValue($value);
}
/**
* @param list<Comment> $comments
*
* @throws UnexpectedTokenException
*
* @internal
*/
public static function parse(ParserState $parserState, array &$comments = []): self
{
$consumedWhitespace = $parserState->consumeWhiteSpace($comments);
$nextToken = $parserState->peek();
if (\in_array($nextToken, ['>', '+', '~'], true)) {
$value = $nextToken;
$parserState->consume(1);
$parserState->consumeWhiteSpace($comments);
} elseif ($consumedWhitespace !== '') {
$value = ' ';
} else {
throw new UnexpectedTokenException(
'combinator',
$nextToken,
'literal',
$parserState->currentLine()
);
}
return new self($value);
}
/**
* @return ValidCombinatorValue
*/
public function getValue(): string
{
return $this->value;
}
/**
* @param non-empty-string $value
*
* @throws \UnexpectedValueException if `$value` is not either space, '>', '+' or '~'
*/
public function setValue(string $value): void
{
if (!\in_array($value, [' ', '>', '+', '~'], true)) {
throw new \UnexpectedValueException('`' . $value . '` is not a valid selector combinator.');
}
$this->value = $value;
}
/**
* @return int<0, max>
*/
public function getSpecificity(): int
{
return 0;
}
public function render(OutputFormat $outputFormat): string
{
// TODO: allow optional spacing controlled via OutputFormat
return $this->getValue();
}
/**
* @return array<string, bool|int|float|string|array<mixed>|null>
*
* @internal
*/
public function getArrayRepresentation(): array
{
return [
'class' => $this->getShortClassName(),
'value' => $this->value,
];
}
}