Debian Codegraph @

vendor/wikimedia/css-sanitizer/src/Grammar/AnythingMatcher.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 InvalidArgumentException;
10 use UnexpectedValueException;
11 use Wikimedia\CSS\Objects\ComponentValueList;
12 use Wikimedia\CSS\Objects\CSSFunction;
13 use Wikimedia\CSS\Objects\SimpleBlock;
14 use Wikimedia\CSS\Objects\Token;
15
16 /**
17 * Matcher that matches anything except bad strings, bad urls, and unmatched
18 * left-paren, left-brace, or left-bracket.
19 * @warning Be very careful using this!
20 * @see https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#any-value
21 */
22 class AnythingMatcher extends Matcher {
23
24 /** @var bool */
25 protected $toplevel;
26
27 /** @var string '', '*', or '+' */
28 protected $quantifier;
29
30 /** @var Matcher[] */
31 protected $matchers;
32
33 /**
34 * @param array $options
35 * - toplevel: (bool) If true, disallows some extra tokens (i.e. it's the
36 * draft's `<declaration-value>` instead of `<any-value>`)
37 * - quantifier: (string) Set to '*' or '+' to work like `<value>*` or
38 * `<value>+` but without backtracking. Note this will probably fail to
39 * match correctly if anything else is supposed to come after the
40 * AnythingMatcher, i.e. only use this where there's nothing else to the
41 * end of the input.
42 * @note To properly match the draft's `<declaration-value>` or
43 * `<any-value>`, specify '+' for the 'quantifier' option.
44 */
45 public function __construct( array $options = [] ) {
46 $this->toplevel = !empty( $options['toplevel'] );
47 $this->quantifier = $options['quantifier'] ?? '';
48 if ( !in_array( $this->quantifier, [ '', '+', '*' ], true ) ) {
49 throw new InvalidArgumentException( 'Invalid quantifier' );
50 }
51
52 $recurse = !$this->toplevel && $this->quantifier === '*'
53 ? $this : new static( [ 'quantifier' => '*' ] );
54 $this->matchers[Token::T_FUNCTION] = new FunctionMatcher( null, $recurse );
55 foreach ( [ Token::T_LEFT_PAREN, Token::T_LEFT_BRACE, Token::T_LEFT_BRACKET ] as $delim ) {
56 $this->matchers[$delim] = new BlockMatcher( $delim, $recurse );
57 }
58 }
59
60 /** @inheritDoc */
61 protected function generateMatches( ComponentValueList $values, $start, array $options ) {
62 $origStart = $start;
63 $lastMatch = $this->quantifier === '*' ? $this->makeMatch( $values, $start, $start ) : null;
64 do {
65 $newMatch = null;
66 // @phan-suppress-next-line PhanCoalescingNeverNullInLoop
67 $cv = $values[$start] ?? null;
68 if ( $cv instanceof Token ) {
69 switch ( $cv->type() ) {
70 case Token::T_BAD_STRING:
71 case Token::T_BAD_URL:
72 case Token::T_RIGHT_PAREN:
73 case Token::T_RIGHT_BRACE:
74 case Token::T_RIGHT_BRACKET:
75 case Token::T_EOF:
76 // Not allowed
77 break;
78
79 case Token::T_SEMICOLON:
80 if ( !$this->toplevel ) {
81 $newMatch = $this->makeMatch(
82 $values, $origStart, $this->next( $values, $start, $options ), $lastMatch
83 );
84 }
85 break;
86
87 case Token::T_DELIM:
88 if ( !$this->toplevel || $cv->value() !== '!' ) {
89 $newMatch = $this->makeMatch(
90 $values, $origStart, $this->next( $values, $start, $options ), $lastMatch
91 );
92 }
93 break;
94
95 case Token::T_WHITESPACE:
96 // If we encounter whitespace, assume it's significant.
97 $newMatch = $this->makeMatch(
98 $values, $origStart, $this->next( $values, $start, $options ),
99 new GrammarMatch( $values, $start, 1, 'significantWhitespace' ),
100 [ [ $lastMatch ] ]
101 );
102 break;
103
104 case Token::T_FUNCTION:
105 case Token::T_LEFT_PAREN:
106 case Token::T_LEFT_BRACE:
107 case Token::T_LEFT_BRACKET:
108 // Should never happen
109 // @codeCoverageIgnoreStart
110 throw new UnexpectedValueException( "How did a \"{$cv->type()}\" token get here?" );
111 // @codeCoverageIgnoreEnd
112
113 default:
114 $newMatch = $this->makeMatch(
115 $values, $origStart, $this->next( $values, $start, $options ), $lastMatch
116 );
117 break;
118 }
119 } elseif ( $cv instanceof CSSFunction || $cv instanceof SimpleBlock ) {
120 $tok = $cv instanceof SimpleBlock ? $cv->getStartTokenType() : Token::T_FUNCTION;
121 // We know there's only one way for the submatcher to match, so just grab the first one
122 $match = $this->matchers[$tok]
123 ->generateMatches( new ComponentValueList( [ $cv ] ), 0, $options )
124 ->current();
125 if ( $match ) {
126 $newMatch = $this->makeMatch(
127 $values, $origStart, $this->next( $values, $start, $options ), $match, [ [ $lastMatch ] ]
128 );
129 }
130 }
131 if ( $newMatch ) {
132 $lastMatch = $newMatch;
133 $start = $newMatch->getNext();
134 }
135 } while ( $this->quantifier !== '' && $newMatch );
136
137 if ( $lastMatch ) {
138 yield $lastMatch;
139 }
140 }
141 }