Debian Codegraph @

vendor/wikimedia/css-sanitizer/src/Grammar/TokenMatcher.php

raw
1 <?php
2 /**
3 * @file
4 * @license https://opensource.org/licenses/Apache-2.0 Apache-2.0
5 */
6
7 namespace Wikimedia\CSS\Grammar;
8
9 use Wikimedia\CSS\Objects\ComponentValueList;
10 use Wikimedia\CSS\Objects\Token;
11
12 /**
13 * Matcher that matches a token of a particular type
14 * @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#component-types
15 */
16 class TokenMatcher extends Matcher {
17 /** @var string One of the Token::T_* constants */
18 protected $type;
19
20 /** @var callable|null Something to call to further validate the token */
21 protected $callback = null;
22
23 /**
24 * @param string $type Token type to match
25 * @param callable|null $callback Something to call to further validate the token.
26 * bool callback( Token )
27 */
28 public function __construct( $type, ?callable $callback = null ) {
29 $this->type = $type;
30 $this->callback = $callback;
31 }
32
33 /** @inheritDoc */
34 protected function generateMatches( ComponentValueList $values, $start, array $options ) {
35 // @phan-suppress-next-line PhanCoalescingNeverNull
36 $cv = $values[$start] ?? null;
37 if ( $cv instanceof Token && $cv->type() === $this->type &&
38 ( !$this->callback || ( $this->callback )( $cv ) )
39 ) {
40 yield $this->makeMatch( $values, $start, $this->next( $values, $start, $options ) );
41 }
42 }
43 }