X-Git-Url: https://scripts.mit.edu/gitweb/autoinstallsdev/mediawiki.git/blobdiff_plain/19e297c21b10b1b8a3acad5e73fc71dcb35db44a..6932310fd58ebef145fa01eb76edf7150284d8ea:/vendor/wikimedia/css-sanitizer/src/Grammar/FunctionMatcher.php diff --git a/vendor/wikimedia/css-sanitizer/src/Grammar/FunctionMatcher.php b/vendor/wikimedia/css-sanitizer/src/Grammar/FunctionMatcher.php new file mode 100644 index 00000000..7269ce33 --- /dev/null +++ b/vendor/wikimedia/css-sanitizer/src/Grammar/FunctionMatcher.php @@ -0,0 +1,68 @@ + ')'`. + */ +class FunctionMatcher extends Matcher { + /** @var callable|null Function name */ + protected $nameCheck; + + /** @var Matcher */ + protected $matcher; + + /** + * @param string|callable|null $name Function name, case-insensitive, or a + * function to check the name. + * @param Matcher $matcher Matcher for the contents of the function + */ + public function __construct( $name, Matcher $matcher ) { + if ( is_string( $name ) ) { + $this->nameCheck = function ( $s ) use ( $name ) { + return !strcasecmp( $s, $name ); + }; + } elseif ( is_callable( $name ) || $name === null ) { + $this->nameCheck = $name; + } else { + throw new \InvalidArgumentException( '$name must be a string, callable, or null' ); + } + $this->matcher = $matcher; + } + + protected function generateMatches( ComponentValueList $values, $start, array $options ) { + $cv = isset( $values[$start] ) ? $values[$start] : null; + if ( $cv instanceof CSSFunction && + ( !$this->nameCheck || call_user_func( $this->nameCheck, $cv->getName() ) ) + ) { + // To successfully match, our sub-Matcher needs to match the whole + // content of the function. + $l = $cv->getValue()->count(); + $s = $this->next( $cv->getValue(), -1, $options ); + foreach ( $this->matcher->generateMatches( $cv->getValue(), $s, $options ) as $match ) { + if ( $match->getNext() === $l ) { + // Matched the whole content of the function, so yield the + // token after the function. + yield $this->makeMatch( $values, $start, $this->next( $values, $start, $options ), $match ); + return; + } + } + } + } +}