]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blobdiff - vendor/wikimedia/remex-html/RemexHtml/Tokenizer/TokenGenerator.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / wikimedia / remex-html / RemexHtml / Tokenizer / TokenGenerator.php
diff --git a/vendor/wikimedia/remex-html/RemexHtml/Tokenizer/TokenGenerator.php b/vendor/wikimedia/remex-html/RemexHtml/Tokenizer/TokenGenerator.php
new file mode 100644 (file)
index 0000000..e9bf973
--- /dev/null
@@ -0,0 +1,45 @@
+<?php
+
+namespace RemexHtml\Tokenizer;
+
+/**
+ * This class provides a convenient iterative view of the token stream,
+ * implemented as a Generator. It is intended to be used as follows:
+ *
+ *   foreach ( TokenGenerator::generate( $html, [] ) as $token ) {
+ *       ...
+ *   }
+ *
+ * Performance is slightly slower than a plain TokenHandler, probably due to
+ * the need to convert event parameters to associative arrays.
+ */
+class TokenGenerator {
+       protected $handler;
+       protected $tokenizer;
+
+       protected function __construct( $text, $options ) {
+               $this->handler = new TokenGeneratorHandler( $this );
+               $this->tokenizer = new Tokenizer( $this->handler, $text, $options );
+       }
+
+       /**
+        * Get a Generator which iterates over all tokens in the supplied HTML
+        *
+        * @param string $text The HTML
+        * @param array $options The Tokenizer options, see Tokenizer::__construct()
+        * @return Generator
+        */
+       public static function generate( $text, $options ) {
+               $tg = new self( $text, $options );
+               $tg->tokenizer->beginStepping();
+               while ( $tg->tokenizer->step() ) {
+                       foreach ( $tg->handler->tokens as $token ) {
+                               yield $token;
+                       }
+                       $tg->handler->tokens = [];
+               }
+               foreach ( $tg->handler->tokens as $token ) {
+                       yield $token;
+               }
+       }
+}