Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
17 / 17 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| FunctionMatcher | |
100.00% |
17 / 17 |
|
100.00% |
2 / 2 |
10 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 | |||
| generateMatches | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
6 | |||
| 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 | $cv = $values[$start] ?? null; |
| 53 | if ( $cv instanceof CSSFunction && |
| 54 | ( !$this->nameCheck || ( $this->nameCheck )( $cv->getName() ) ) |
| 55 | ) { |
| 56 | // To successfully match, our sub-Matcher needs to match the whole |
| 57 | // content of the function. |
| 58 | $l = $cv->getValue()->count(); |
| 59 | $s = $this->next( $cv->getValue(), -1, $options ); |
| 60 | foreach ( $this->matcher->generateMatches( $cv->getValue(), $s, $options ) as $match ) { |
| 61 | if ( $match->getNext() === $l ) { |
| 62 | // Matched the whole content of the function, so yield the |
| 63 | // token after the function. |
| 64 | yield $this->makeMatch( $values, $start, $this->next( $values, $start, $options ), $match ); |
| 65 | return; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | } |