Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 40 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| OnlyInclude | |
0.00% |
0 / 40 |
|
0.00% |
0 / 5 |
272 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| resetState | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| onCompoundTk | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| onAny | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| onAnyInclude | |
0.00% |
0 / 31 |
|
0.00% |
0 / 1 |
90 | |||
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Wikimedia\Parsoid\Wt2Html\TT; |
| 5 | |
| 6 | use Wikimedia\Parsoid\NodeData\DataParsoid; |
| 7 | use Wikimedia\Parsoid\Tokens\CompoundTk; |
| 8 | use Wikimedia\Parsoid\Tokens\EndTagTk; |
| 9 | use Wikimedia\Parsoid\Tokens\EOFTk; |
| 10 | use Wikimedia\Parsoid\Tokens\KV; |
| 11 | use Wikimedia\Parsoid\Tokens\SelfclosingTagTk; |
| 12 | use Wikimedia\Parsoid\Tokens\Token; |
| 13 | use Wikimedia\Parsoid\Tokens\XMLTagTk; |
| 14 | use Wikimedia\Parsoid\Wt2Html\TokenHandlerPipeline; |
| 15 | |
| 16 | /** |
| 17 | * OnlyInclude needs to buffer tokens in case an onlyinclude block is encountered later. |
| 18 | */ |
| 19 | class OnlyInclude extends UniversalTokenHandler { |
| 20 | private array $accum = []; |
| 21 | private bool $inOnlyInclude = false; |
| 22 | private bool $foundOnlyInclude = false; |
| 23 | private bool $enabled = true; |
| 24 | |
| 25 | public function __construct( TokenHandlerPipeline $manager, array $options ) { |
| 26 | parent::__construct( $manager, $options ); |
| 27 | $this->resetState( $options ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Resets any internal state for this token handler. |
| 32 | * |
| 33 | * @param array $options |
| 34 | */ |
| 35 | public function resetState( array $options ): void { |
| 36 | parent::resetState( $options ); |
| 37 | $this->inOnlyInclude = false; |
| 38 | $this->foundOnlyInclude = false; |
| 39 | $this->enabled = $this->options['inTemplate'] && |
| 40 | $this->env->nativeTemplateExpansionEnabled(); |
| 41 | } |
| 42 | |
| 43 | /** @inheritDoc */ |
| 44 | public function onCompoundTk( CompoundTk $ctk, TokenHandler $tokensHandler ): ?array { |
| 45 | return $this->enabled ? $this->onAnyInclude( $ctk ) : null; |
| 46 | } |
| 47 | |
| 48 | /** @inheritDoc */ |
| 49 | public function onAny( $token ): ?array { |
| 50 | return $this->enabled ? $this->onAnyInclude( $token ) : null; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @param Token|string $token |
| 55 | * @return ?array<string|Token> |
| 56 | */ |
| 57 | private function onAnyInclude( $token ): ?array { |
| 58 | if ( $token instanceof EOFTk ) { |
| 59 | $this->inOnlyInclude = false; |
| 60 | if ( count( $this->accum ) && !$this->foundOnlyInclude ) { |
| 61 | $res = $this->accum; |
| 62 | $res[] = $token; |
| 63 | $this->accum = []; |
| 64 | return $res; |
| 65 | } else { |
| 66 | $this->foundOnlyInclude = false; |
| 67 | $this->accum = []; |
| 68 | return null; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | if ( $token instanceof XMLTagTk && $token->getName() === 'onlyinclude' ) { |
| 73 | // FIXME: This doesn't seem to consider self-closing tags |
| 74 | // or close really |
| 75 | if ( !$this->inOnlyInclude ) { |
| 76 | $this->foundOnlyInclude = true; |
| 77 | $this->inOnlyInclude = true; |
| 78 | } else { |
| 79 | $this->inOnlyInclude = false; |
| 80 | } |
| 81 | |
| 82 | $tagName = 'mw:Includes/OnlyInclude'; |
| 83 | if ( $token instanceof EndTagTk ) { |
| 84 | $tagName .= '/End'; |
| 85 | } |
| 86 | |
| 87 | $dp = new DataParsoid; |
| 88 | $dp->tsr = $token->dataParsoid->tsr; |
| 89 | $dp->src = $dp->tsr->substr( |
| 90 | $this->manager->getFrame()->getSource() |
| 91 | ); |
| 92 | |
| 93 | // FIXME: Just drop these, we're inTemplate |
| 94 | $meta = new SelfclosingTagTk( |
| 95 | 'meta', [ new KV( 'typeof', $tagName ) ], $dp |
| 96 | ); |
| 97 | |
| 98 | return [ $meta ]; |
| 99 | } else { |
| 100 | if ( $this->inOnlyInclude ) { |
| 101 | return null; |
| 102 | } else { |
| 103 | $this->accum[] = $token; |
| 104 | return []; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |