]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blobdiff - vendor/wikimedia/css-sanitizer/src/Grammar/BlockMatcher.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / vendor / wikimedia / css-sanitizer / src / Grammar / BlockMatcher.php
diff --git a/vendor/wikimedia/css-sanitizer/src/Grammar/BlockMatcher.php b/vendor/wikimedia/css-sanitizer/src/Grammar/BlockMatcher.php
new file mode 100644 (file)
index 0000000..f217057
--- /dev/null
@@ -0,0 +1,62 @@
+<?php
+/**
+ * @file
+ * @license https://opensource.org/licenses/Apache-2.0 Apache-2.0
+ */
+
+namespace Wikimedia\CSS\Grammar;
+
+use Wikimedia\CSS\Objects\ComponentValueList;
+use Wikimedia\CSS\Objects\SimpleBlock;
+use Wikimedia\CSS\Objects\Token;
+
+/**
+ * Matcher that matches a SimpleBlock
+ *
+ * The grammar definitions in the standards seem to be written assuming they're
+ * being passed a sequence of Tokens only, even though they're defined over a
+ * sequence of ComponentValues (which includes SimpleBlocks and CSSFunctions)
+ * instead.
+ *
+ * Thus, to be safe you'll want to use this when a grammar specifies something
+ * like `'[' <stuff> ']'`.
+ */
+class BlockMatcher extends Matcher {
+       /** @var string One of the Token::T_* constants */
+       protected $blockType;
+
+       /** @var Matcher */
+       protected $matcher;
+
+       /**
+        * @param string $blockType One of the Token::T_* constants
+        * @param Matcher $matcher Matcher for the contents of the block
+        */
+       public function __construct( $blockType, Matcher $matcher ) {
+               if ( SimpleBlock::matchingDelimiter( $blockType ) === null ) {
+                       throw new \InvalidArgumentException(
+                               'A block is delimited by either {}, [], or ().'
+                       );
+               }
+               $this->blockType = $blockType;
+               $this->matcher = $matcher;
+       }
+
+       protected function generateMatches( ComponentValueList $values, $start, array $options ) {
+               $cv = isset( $values[$start] ) ? $values[$start] : null;
+               if ( $cv instanceof SimpleBlock && $cv->getStartTokenType() === $this->blockType ) {
+                       // To successfully match, our sub-Matcher needs to match the whole
+                       // content of the block.
+                       $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 block, so yield the
+                                       // token after the block.
+                                       yield $this->makeMatch( $values, $start, $this->next( $values, $start, $options ), $match );
+                                       return;
+                               }
+                       }
+               }
+       }
+}