]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - vendor/wikimedia/css-sanitizer/src/Grammar/KeywordMatcher.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / wikimedia / css-sanitizer / src / Grammar / KeywordMatcher.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 one of a set of keywords, case-insensitively
14  *
15  * This is intended for matching specific <ident-token>s, but will work for
16  * other types (case-insensitively) too. For delimiter (or case-sensitive)
17  * matching, use DelimMatcher.
18  *
19  * @see https://www.w3.org/TR/2016/CR-css-values-3-20160929/#component-types
20  */
21 class KeywordMatcher extends Matcher {
22         /** @var string One of the Token::T_* constants */
23         protected $type;
24
25         /** @var array Associative array with keys being the values to match */
26         protected $values;
27
28         /**
29          * @param string|string[] $values Token values to match
30          * @param array $options Options
31          *  - type: (string) Token type to match. Default is Token::T_IDENT.
32          */
33         public function __construct( $values, array $options = [] ) {
34                 $options += [
35                         'type' => Token::T_IDENT,
36                 ];
37
38                 $this->values = array_flip( array_map( 'strtolower', (array)$values ) );
39                 $this->type = $options['type'];
40         }
41
42         protected function generateMatches( ComponentValueList $values, $start, array $options ) {
43                 $cv = isset( $values[$start] ) ? $values[$start] : null;
44                 if ( $cv instanceof Token && $cv->type() === $this->type &&
45                         isset( $this->values[strtolower( $cv->value() )] )
46                 ) {
47                         yield $this->makeMatch( $values, $start, $this->next( $values, $start, $options ) );
48                 }
49         }
50 }