Debian Codegraph @

vendor/sabberworm/php-css-parser/src/Value/Value.php

raw
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Sabberworm\CSS\Value;
6
7 use Sabberworm\CSS\CSSElement;
8 use Sabberworm\CSS\Parsing\ParserState;
9 use Sabberworm\CSS\Parsing\SourceException;
10 use Sabberworm\CSS\Parsing\UnexpectedEOFException;
11 use Sabberworm\CSS\Parsing\UnexpectedTokenException;
12 use Sabberworm\CSS\Position\Position;
13 use Sabberworm\CSS\Position\Positionable;
14 use Sabberworm\CSS\ShortClassNameProvider;
15
16 use function Safe\preg_match;
17
18 /**
19 * Abstract base class for specific classes of CSS values: `Size`, `Color`, `CSSString` and `URL`, and another
20 * abstract subclass `ValueList`.
21 */
22 abstract class Value implements CSSElement, Positionable
23 {
24 use Position;
25 use ShortClassNameProvider;
26
27 /**
28 * @param int<1, max>|null $lineNumber
29 */
30 public function __construct(?int $lineNumber = null)
31 {
32 $this->setPosition($lineNumber);
33 }
34
35 /**
36 * @param array<non-empty-string> $listDelimiters
37 *
38 * @return Value|string
39 *
40 * @throws UnexpectedTokenException
41 * @throws UnexpectedEOFException
42 *
43 * @internal since V8.8.0
44 */
45 public static function parseValue(ParserState $parserState, array $listDelimiters = [])
46 {
47 /** @var list<Value|string> $stack */
48 $stack = [];
49 $parserState->consumeWhiteSpace();
50 //Build a list of delimiters and parsed values
51 while (
52 !($parserState->comes('}') || $parserState->comes(';') || $parserState->comes('!')
53 || $parserState->comes(')')
54 || $parserState->isEnd())
55 ) {
56 if (\count($stack) > 0) {
57 $foundDelimiter = false;
58 foreach ($listDelimiters as $delimiter) {
59 if ($parserState->comes($delimiter)) {
60 \array_push($stack, $parserState->consume($delimiter));
61 $parserState->consumeWhiteSpace();
62 $foundDelimiter = true;
63 break;
64 }
65 }
66 if (!$foundDelimiter) {
67 //Whitespace was the list delimiter
68 \array_push($stack, ' ');
69 }
70 }
71 \array_push($stack, self::parsePrimitiveValue($parserState));
72 $parserState->consumeWhiteSpace();
73 }
74 // Convert the list to list objects
75 foreach ($listDelimiters as $delimiter) {
76 $stackSize = \count($stack);
77 if ($stackSize === 1) {
78 return $stack[0];
79 }
80 $newStack = [];
81 for ($offset = 0; $offset < $stackSize; ++$offset) {
82 if ($offset === ($stackSize - 1) || $delimiter !== $stack[$offset + 1]) {
83 $newStack[] = $stack[$offset];
84 continue;
85 }
86 $length = 2; //Number of elements to be joined
87 for ($i = $offset + 3; $i < $stackSize; $i += 2, ++$length) {
88 if ($delimiter !== $stack[$i]) {
89 break;
90 }
91 }
92 $list = new RuleValueList($delimiter, $parserState->currentLine());
93 for ($i = $offset; $i - $offset < $length * 2; $i += 2) {
94 $list->addListComponent($stack[$i]);
95 }
96 $newStack[] = $list;
97 $offset += $length * 2 - 2;
98 }
99 $stack = $newStack;
100 }
101 if (!isset($stack[0])) {
102 throw new UnexpectedTokenException(
103 " {$parserState->peek()} ",
104 $parserState->peek(1, -1) . $parserState->peek(2),
105 'literal',
106 $parserState->currentLine()
107 );
108 }
109 return $stack[0];
110 }
111
112 /**
113 * @return CSSFunction|string
114 *
115 * @throws UnexpectedEOFException
116 * @throws UnexpectedTokenException
117 *
118 * @internal since V8.8.0
119 */
120 public static function parseIdentifierOrFunction(ParserState $parserState, bool $ignoreCase = false)
121 {
122 $anchor = $parserState->anchor();
123 $result = $parserState->parseIdentifier($ignoreCase);
124
125 if ($parserState->comes('(')) {
126 $anchor->backtrack();
127 if ($parserState->streql('url', $result)) {
128 $result = URL::parse($parserState);
129 } elseif ($parserState->streql('calc', $result)) {
130 $result = CalcFunction::parse($parserState);
131 } else {
132 $result = CSSFunction::parse($parserState, $ignoreCase);
133 }
134 }
135
136 return $result;
137 }
138
139 /**
140 * @return CSSFunction|CSSString|LineName|Size|URL|string
141 *
142 * @throws UnexpectedEOFException
143 * @throws UnexpectedTokenException
144 * @throws SourceException
145 *
146 * @internal since V8.8.0
147 */
148 public static function parsePrimitiveValue(ParserState $parserState)
149 {
150 $value = null;
151 $parserState->consumeWhiteSpace();
152 if (
153 \is_numeric($parserState->peek())
154 || ($parserState->comes('-.')
155 && \is_numeric($parserState->peek(1, 2)))
156 || (($parserState->comes('-') || $parserState->comes('.')) && \is_numeric($parserState->peek(1, 1)))
157 ) {
158 $value = Size::parse($parserState);
159 } elseif ($parserState->comes('#') || $parserState->comes('rgb', true) || $parserState->comes('hsl', true)) {
160 $value = Color::parse($parserState);
161 } elseif ($parserState->comes("'") || $parserState->comes('"')) {
162 $value = CSSString::parse($parserState);
163 } elseif ($parserState->comes('progid:') && $parserState->getSettings()->usesLenientParsing()) {
164 $value = self::parseMicrosoftFilter($parserState);
165 } elseif ($parserState->comes('[')) {
166 $value = LineName::parse($parserState);
167 } elseif ($parserState->comes('U+')) {
168 $value = self::parseUnicodeRangeValue($parserState);
169 } else {
170 $nextCharacter = $parserState->peek(1);
171 try {
172 $value = self::parseIdentifierOrFunction($parserState);
173 } catch (UnexpectedTokenException $e) {
174 if (\in_array($nextCharacter, ['+', '-', '*', '/'], true)) {
175 $value = $parserState->consume(1);
176 } else {
177 throw $e;
178 }
179 }
180 }
181 $parserState->consumeWhiteSpace();
182
183 return $value;
184 }
185
186 /**
187 * @return array<string, bool|int|float|string|array<mixed>|null>
188 *
189 * @internal
190 */
191 public function getArrayRepresentation(): array
192 {
193 return [
194 'class' => $this->getShortClassName(),
195 ];
196 }
197
198 /**
199 * @throws UnexpectedEOFException
200 * @throws UnexpectedTokenException
201 */
202 private static function parseMicrosoftFilter(ParserState $parserState): CSSFunction
203 {
204 $function = $parserState->consumeUntil('(', false, true);
205 $arguments = Value::parseValue($parserState, [',', '=']);
206 return new CSSFunction($function, $arguments, ',', $parserState->currentLine());
207 }
208
209 /**
210 * @throws UnexpectedEOFException
211 * @throws UnexpectedTokenException
212 */
213 private static function parseUnicodeRangeValue(ParserState $parserState): string
214 {
215 $codepointMaxLength = 6; // Code points outside BMP can use up to six digits
216 $range = '';
217 $parserState->consume('U+');
218 do {
219 if ($parserState->comes('-')) {
220 $codepointMaxLength = 13; // Max length is 2 six-digit code points + the dash(-) between them
221 }
222 $range .= $parserState->consume(1);
223 } while (
224 (\strlen($range) < $codepointMaxLength) && (preg_match('/[A-Fa-f0-9\\?-]/', $parserState->peek()) === 1)
225 );
226
227 return "U+{$range}";
228 }
229 }