Debian Codegraph @

vendor/wikimedia/css-sanitizer/src/Grammar/FunctionMatcher.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 Closure;
10 use InvalidArgumentException;
11 use Wikimedia\CSS\Objects\ComponentValueList;
12 use Wikimedia\CSS\Objects\CSSFunction;
13
14 /**
15 * Matcher that matches a CSSFunction
16 *
17 * The grammar definitions in the standards seem to be written assuming they're
18 * being passed a sequence of Tokens only, even though they're defined over a
19 * sequence of ComponentValues (which includes SimpleBlocks and CSSFunctions)
20 * instead.
21 *
22 * Thus, to be safe you'll want to use this when a grammar specifies something
23 * like `FUNCTION <stuff> ')'`.
24 */
25 class FunctionMatcher extends Matcher {
26 /** @var callable|null Function name */
27 protected $nameCheck;
28
29 /** @var Matcher */
30 protected $matcher;
31
32 /**
33 * @param string|Closure|null $name Function name, case-insensitive, or a
34 * function to check the name.
35 * @param Matcher $matcher Matcher for the contents of the function
36 */
37 public function __construct( $name, Matcher $matcher ) {
38 if ( is_string( $name ) ) {
39 $this->nameCheck = static function ( $s ) use ( $name ) {
40 return !strcasecmp( $s, $name );
41 };
42 } elseif ( is_callable( $name ) || $name === null ) {
43 $this->nameCheck = $name;
44 } else {
45 throw new InvalidArgumentException( '$name must be a string, callable, or null' );
46 }
47 $this->matcher = $matcher;
48 }
49
50 /** @inheritDoc */
51 protected function generateMatches( ComponentValueList $values, $start, array $options ) {
52 // @phan-suppress-next-line PhanCoalescingNeverNull
53 $cv = $values[$start] ?? null;
54 if ( $cv instanceof CSSFunction &&
55 ( !$this->nameCheck || ( $this->nameCheck )( $cv->getName() ) )
56 ) {
57 // To successfully match, our sub-Matcher needs to match the whole
58 // content of the function.
59 $l = $cv->getValue()->count();
60 $s = $this->next( $cv->getValue(), -1, $options );
61 foreach ( $this->matcher->generateMatches( $cv->getValue(), $s, $options ) as $match ) {
62 if ( $match->getNext() === $l ) {
63 // Matched the whole content of the function, so yield the
64 // token after the function.
65 yield $this->makeMatch( $values, $start, $this->next( $values, $start, $options ), $match );
66 return;
67 }
68 }
69 }
70 }
71 }