Debian Codegraph @

vendor/wikimedia/css-sanitizer/src/Grammar/Juxtaposition.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 Wikimedia\CSS\Objects\ComponentValueList;
11 use Wikimedia\CSS\Objects\Token;
12 use Wikimedia\CSS\Util;
13
14 /**
15 * Matcher that groups other matchers (juxtaposition)
16 * @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#component-combinators
17 * @see https://www.w3.org/TR/2024/WD-css-values-4-20240312/#comb-comma
18 */
19 class Juxtaposition extends Matcher {
20 /** @var Matcher[] */
21 protected $matchers;
22
23 /** @var bool Whether non-empty matches are comma-separated */
24 protected $commas;
25
26 /**
27 * @param Matcher[] $matchers
28 * @param bool $commas Whether matches are comma-separated
29 */
30 public function __construct( array $matchers, $commas = false ) {
31 Util::assertAllInstanceOf( $matchers, Matcher::class, '$matchers' );
32 $this->matchers = $matchers;
33 $this->commas = (bool)$commas;
34 }
35
36 /** @inheritDoc */
37 protected function generateMatches( ComponentValueList $values, $start, array $options ) {
38 $used = [];
39
40 // Match each of our matchers in turn, pushing each one onto a stack as
41 // we process it and popping a match once it's exhausted.
42 $stack = [
43 [
44 new GrammarMatch( $values, $start, 0 ),
45 $start,
46 $this->matchers[0]->generateMatches( $values, $start, $options ),
47 false
48 ]
49 ];
50 do {
51 /** @var $lastEnd int */
52 /** @var $iter Iterator<GrammarMatch> */
53 /** @var $needEmpty bool */
54 [ , $lastEnd, $iter, $needEmpty ] = $stack[count( $stack ) - 1];
55
56 // If the top of the stack has no more matches, pop it and loop.
57 if ( !$iter->valid() ) {
58 array_pop( $stack );
59 continue;
60 }
61
62 // Find the next match for the current top of the stack.
63 $match = $iter->current();
64 $iter->next();
65
66 // In some cases, we can only match if the rest of the pattern
67 // is empty. If we're in that situation, ignore all non-empty
68 // matches.
69 if ( $needEmpty && $match->getLength() !== 0 ) {
70 continue;
71 }
72
73 $thisEnd = $nextFrom = $match->getNext();
74
75 // Dealing with commas is a bit tricky. There are three cases:
76 // 1. If the current match is empty, don't look for a following
77 // comma now and reset $thisEnd to $lastEnd.
78 // 2. If there is a comma following, update $nextFrom to be after
79 // the comma.
80 // 3. If there's no comma following, every subsequent Matcher must
81 // be empty in order for the group as a whole to match, so set
82 // the flag.
83 // Unlike '#', this doesn't specify skipping whitespace around the
84 // commas if the production isn't already skipping whitespace.
85 if ( $this->commas ) {
86 if ( $match->getLength() === 0 ) {
87 $thisEnd = $lastEnd;
88 // @phan-suppress-next-line PhanRedundantConditionInLoop https://github.com/phan/phan/issues/5441
89 } elseif ( isset( $values[$nextFrom] ) && $values[$nextFrom] instanceof Token &&
90 // @phan-suppress-next-line PhanUndeclaredMethod False positive
91 $values[$nextFrom]->type() === Token::T_COMMA
92 ) {
93 $nextFrom = $this->next( $values, $nextFrom, $options );
94 } else {
95 $needEmpty = true;
96 }
97 }
98
99 // If we ran out of Matchers, yield the final position. Otherwise,
100 // push the next matcher onto the stack.
101 if ( count( $stack ) >= count( $this->matchers ) ) {
102 $newMatch = $this->makeMatch( $values, $start, $thisEnd, $match, $stack );
103 $mid = $newMatch->getUniqueID();
104 if ( !isset( $used[$mid] ) ) {
105 $used[$mid] = 1;
106 yield $newMatch;
107 }
108 } else {
109 $stack[] = [
110 $match,
111 $thisEnd,
112 $this->matchers[count( $stack )]->generateMatches( $values, $nextFrom, $options ),
113 $needEmpty
114 ];
115 }
116 } while ( $stack );
117 }
118 }