Debian Codegraph @

vendor/wikimedia/css-sanitizer/src/Grammar/UrlMatcher.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 Wikimedia\CSS\Objects\ComponentValueList;
11 use Wikimedia\CSS\Objects\CSSFunction;
12 use Wikimedia\CSS\Objects\Token;
13
14 /**
15 * Matcher that matches a CSSFunction for a URL function or a T_URL token
16 */
17 class UrlMatcher extends FunctionMatcher {
18 /** @var callable|null */
19 protected $urlCheck;
20
21 /**
22 * @param callable|null $urlCheck Function to check that the URL is really valid.
23 * Prototype is bool func( string $url, ComponentValue[] $modifiers )
24 * @param array $options Additional options:
25 * - modifierMatcher: (Matcher) Matcher for URL modifiers. The default is
26 * a NothingMatcher.
27 */
28 public function __construct( ?callable $urlCheck = null, array $options = [] ) {
29 if ( isset( $options['modifierMatcher'] ) ) {
30 $modifierMatcher = $options['modifierMatcher'];
31 if ( !$modifierMatcher instanceof Matcher ) {
32 throw new InvalidArgumentException( 'modifierMatcher must be a Matcher' );
33 }
34 } else {
35 $modifierMatcher = new NothingMatcher;
36 }
37
38 $funcContents = new Juxtaposition( [
39 TokenMatcher::create( Token::T_STRING )->capture( 'url' ),
40 Quantifier::star( $modifierMatcher->capture( 'modifier' ) ),
41 ] );
42
43 $this->urlCheck = $urlCheck;
44 parent::__construct(
45 static function ( $name ) {
46 return in_array( strtolower( $name ), [ 'url', 'src' ], true );
47 },
48 $funcContents
49 );
50 }
51
52 /**
53 * Return a Matcher for any grammatically-correct modifier
54 * @return Matcher
55 */
56 public static function anyModifierMatcher() {
57 return Alternative::create( [
58 new TokenMatcher( Token::T_IDENT ),
59 new FunctionMatcher( null, new AnythingMatcher( [ 'quantifier' => '*' ] ) ),
60 ] );
61 }
62
63 /** @inheritDoc */
64 protected function generateMatches( ComponentValueList $values, $start, array $options ) {
65 // First, is it a URL token?
66 // @phan-suppress-next-line PhanCoalescingNeverNull
67 $cv = $values[$start] ?? null;
68 if ( $cv instanceof Token && $cv->type() === Token::T_URL ) {
69 $url = $cv->value();
70 if ( !$this->urlCheck || ( $this->urlCheck )( $url, [] ) ) {
71 $match = new GrammarMatch( $values, $start, 1, 'url' );
72 yield $this->makeMatch( $values, $start, $this->next( $values, $start, $options ), $match );
73 }
74 return;
75 }
76
77 // Nope. Try it as a FunctionMatcher and extract the URL and modifiers
78 // for each match.
79 foreach ( parent::generateMatches( $values, $start, $options ) as $match ) {
80 $url = null;
81 $modifiers = [];
82 foreach ( $match->getCapturedMatches() as $submatch ) {
83 $cvs = $submatch->getValues();
84 if ( $cvs[0] instanceof Token && $submatch->getName() === 'url' ) {
85 $url = $cvs[0]->value();
86 } elseif ( $submatch->getName() === 'modifier' ) {
87 if ( $cvs[0] instanceof CSSFunction ) {
88 $modifiers[] = $cvs[0];
89 } elseif ( $cvs[0] instanceof Token && $cvs[0]->type() === Token::T_IDENT ) {
90 $modifiers[] = $cvs[0];
91 }
92 }
93 }
94 if ( $url && ( !$this->urlCheck || ( $this->urlCheck )( $url, $modifiers ) ) ) {
95 yield $match;
96 }
97 }
98 }
99 }