Debian Codegraph @

vendor/wikimedia/css-sanitizer/src/Grammar/Quantifier.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 Iterator;
10 use UnexpectedValueException;
11 use Wikimedia\CSS\Objects\ComponentValueList;
12 use Wikimedia\CSS\Objects\Token;
13
14 /**
15 * Matcher that matches a sub-Matcher a certain number of times
16 * ("?", "*", "+", "#", "{A,B}" multipliers)
17 * @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#component-multipliers
18 */
19 class Quantifier extends Matcher {
20 /** @var Matcher */
21 protected $matcher;
22
23 /** @var int */
24 protected $min;
25
26 /** @var int */
27 protected $max;
28
29 /** @var bool Whether matches are comma-separated */
30 protected $commas;
31
32 /**
33 * @param Matcher $matcher
34 * @param int|float $min Minimum number of matches
35 * @param int|float $max Maximum number of matches
36 * @param bool $commas Whether matches are comma-separated
37 */
38 public function __construct( Matcher $matcher, $min, $max, $commas ) {
39 $this->matcher = $matcher;
40 $this->min = $min;
41 $this->max = $max;
42 $this->commas = (bool)$commas;
43 }
44
45 /**
46 * Implements "?": 0 or 1 matches
47 * @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#mult-opt
48 * @param Matcher $matcher
49 * @return static
50 */
51 public static function optional( Matcher $matcher ) {
52 return new static( $matcher, 0, 1, false );
53 }
54
55 /**
56 * Implements "*": 0 or more matches
57 * @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#mult-zero-plus
58 * @param Matcher $matcher
59 * @return static
60 */
61 public static function star( Matcher $matcher ) {
62 return new static( $matcher, 0, INF, false );
63 }
64
65 /**
66 * Implements "+": 1 or more matches
67 * @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#mult-one-plus
68 * @param Matcher $matcher
69 * @return static
70 */
71 public static function plus( Matcher $matcher ) {
72 return new static( $matcher, 1, INF, false );
73 }
74
75 /**
76 * Implements "{A,B}": Between A and B matches
77 * @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#mult-num-range
78 * @param Matcher $matcher
79 * @param int|float $min Minimum number of matches
80 * @param int|float $max Maximum number of matches
81 * @return static
82 */
83 public static function count( Matcher $matcher, $min, $max ) {
84 return new static( $matcher, $min, $max, false );
85 }
86
87 /**
88 * Implements "#" and "#{A,B}": Between A and B matches, comma-separated
89 * @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#mult-comma
90 * @param Matcher $matcher
91 * @param int|float $min Minimum number of matches
92 * @param int|float $max Maximum number of matches
93 * @return static
94 */
95 public static function hash( Matcher $matcher, $min = 1, $max = INF ) {
96 return new static( $matcher, $min, $max, true );
97 }
98
99 /** @inheritDoc */
100 protected function generateMatches( ComponentValueList $values, $start, array $options ) {
101 $used = [];
102
103 // Maintain a stack of matches for backtracking purposes.
104 $stack = [
105 [
106 new GrammarMatch( $values, $start, 0 ),
107 $this->matcher->generateMatches( $values, $start, $options )
108 ]
109 ];
110 do {
111 /** @var $lastMatch GrammarMatch */
112 /** @var $iter Iterator<GrammarMatch> */
113 [ $lastMatch, $iter ] = $stack[count( $stack ) - 1];
114
115 // If the top of the stack has no more matches, pop it, maybe
116 // yield the last matched position, and loop.
117 if ( !$iter->valid() ) {
118 array_pop( $stack );
119 $ct = count( $stack );
120 $pos = $lastMatch->getNext();
121 if ( $ct >= $this->min && $ct <= $this->max ) {
122 $newMatch = $this->makeMatch( $values, $start, $pos, $lastMatch, $stack );
123 $mid = $newMatch->getUniqueID();
124 if ( !isset( $used[$mid] ) ) {
125 $used[$mid] = 1;
126 yield $newMatch;
127 }
128 }
129 continue;
130 }
131
132 // Find the next match for the current top of the stack.
133 $match = $iter->current();
134 $iter->next();
135
136 // Quantifiers don't work well when the quantified thing can be empty.
137 if ( $match->getLength() === 0 ) {
138 throw new UnexpectedValueException( 'Empty match in quantifier!' );
139 }
140
141 $nextFrom = $match->getNext();
142
143 // There can only be more matches after this one if we haven't
144 // reached our maximum yet.
145 $canBeMore = count( $stack ) < $this->max;
146
147 // Commas are slightly tricky:
148 // 1. If there is a following comma, start the next Matcher after it.
149 // 2. If not, there can't be any more Matchers following.
150 // And in either case optional whitespace is always allowed.
151 if ( $this->commas ) {
152 $n = $nextFrom;
153 // https://github.com/phan/phan/issues/5441
154 // @phan-suppress-next-line PhanRedundantConditionInLoop
155 if ( isset( $values[$n] ) && $values[$n] instanceof Token &&
156 // @phan-suppress-next-line PhanUndeclaredMethod False positive
157 $values[$n]->type() === Token::T_WHITESPACE
158 ) {
159 $n = $this->next( $values, $n, [ 'skip-whitespace' => true ] + $options );
160 }
161 // @phan-suppress-next-line PhanRedundantConditionInLoop
162 if ( isset( $values[$n] ) && $values[$n] instanceof Token &&
163 // @phan-suppress-next-line PhanUndeclaredMethod False positive
164 $values[$n]->type() === Token::T_COMMA
165 ) {
166 $nextFrom = $this->next( $values, $n, [ 'skip-whitespace' => true ] + $options );
167 } else {
168 $canBeMore = false;
169 }
170 }
171
172 // If there can be more matches, push another one onto the stack
173 // and try it. Otherwise, yield and continue with the current match.
174 if ( $canBeMore ) {
175 $stack[] = [ $match, $this->matcher->generateMatches( $values, $nextFrom, $options ) ];
176 } else {
177 $ct = count( $stack );
178 $pos = $match->getNext();
179 if ( $ct >= $this->min && $ct <= $this->max ) {
180 $newMatch = $this->makeMatch( $values, $start, $pos, $match, $stack );
181 $mid = $newMatch->getUniqueID();
182 if ( !isset( $used[$mid] ) ) {
183 $used[$mid] = 1;
184 yield $newMatch;
185 }
186 }
187 }
188 } while ( $stack );
189 }
190 }