]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - vendor/wikimedia/css-sanitizer/src/Grammar/TokenMatcher.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / wikimedia / css-sanitizer / src / Grammar / TokenMatcher.php
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/2016/CR-css-values-3-20160929/#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         protected function generateMatches( ComponentValueList $values, $start, array $options ) {
34                 $cv = isset( $values[$start] ) ? $values[$start] : null;
35                 if ( $cv instanceof Token && $cv->type() === $this->type &&
36                         ( !$this->callback || call_user_func( $this->callback, $cv ) )
37                 ) {
38                         yield $this->makeMatch( $values, $start, $this->next( $values, $start, $options ) );
39                 }
40         }
41 }