| 1 |
<?php |
| 2 |
declare( strict_types = 1 ); |
| 3 |
|
| 4 |
namespace Wikimedia\Parsoid\Wt2Html\TT; |
| 5 |
|
| 6 |
use Wikimedia\Parsoid\Tokens\EOFTk; |
| 7 |
use Wikimedia\Parsoid\Utils\PHPUtils; |
| 8 |
use Wikimedia\Parsoid\Utils\TokenUtils; |
| 9 |
use Wikimedia\Parsoid\Wt2Html\TokenTransformManager; |
| 10 |
|
| 11 |
/** |
| 12 |
* Simple noinclude implementation. |
| 13 |
* Strips all tokens in noinclude sections. |
| 14 |
*/ |
| 15 |
class NoInclude extends TokenCollector { |
| 16 |
/** |
| 17 |
* @param TokenTransformManager $manager manager environment |
| 18 |
* @param array $options options |
| 19 |
*/ |
| 20 |
public function __construct( TokenTransformManager $manager, array $options ) { |
| 21 |
parent::__construct( $manager, $options ); |
| 22 |
} |
| 23 |
|
| 24 |
protected function type(): string { |
| 25 |
return 'tag'; |
| 26 |
} |
| 27 |
|
| 28 |
protected function name(): string { |
| 29 |
return 'noinclude'; |
| 30 |
} |
| 31 |
|
| 32 |
protected function toEnd(): bool { |
| 33 |
return true; // Match the end-of-input if </noinclude> is missing. |
| 34 |
} |
| 35 |
|
| 36 |
protected function ackEnd(): bool { |
| 37 |
return true; |
| 38 |
} |
| 39 |
|
| 40 |
protected function transformation( array $collection ): TokenHandlerResult { |
| 41 |
$start = array_shift( $collection ); |
| 42 |
$sc = TokenUtils::getTokenType( $start ); |
| 43 |
|
| 44 |
// A stray end tag. |
| 45 |
if ( $sc === 'EndTagTk' ) { |
| 46 |
$meta = TokenCollector::buildMetaToken( $this->manager, 'mw:Includes/NoInclude', |
| 47 |
true, ( $start->dataParsoid->tsr ?? null ), null ); |
| 48 |
return new TokenHandlerResult( [ $meta ] ); |
| 49 |
} |
| 50 |
|
| 51 |
// Handle self-closing tag case specially! |
| 52 |
if ( $sc === 'SelfclosingTagTk' ) { |
| 53 |
return ( $this->options['isInclude'] ) ? |
| 54 |
new TokenHandlerResult( [] ) : |
| 55 |
new TokenHandlerResult( [ |
| 56 |
TokenCollector::buildMetaToken( |
| 57 |
$this->manager, |
| 58 |
'mw:Includes/NoInclude', |
| 59 |
false, |
| 60 |
( $start->dataParsoid->tsr ?? null ), |
| 61 |
null ) |
| 62 |
] ); |
| 63 |
} |
| 64 |
|
| 65 |
$tokens = []; |
| 66 |
$end = array_pop( $collection ); |
| 67 |
$eof = $end instanceof EOFTk; |
| 68 |
|
| 69 |
if ( empty( $this->options['isInclude'] ) ) { |
| 70 |
// Content is preserved |
| 71 |
// Add meta tags for open and close |
| 72 |
$startTSR = $start->dataParsoid->tsr ?? null; |
| 73 |
$endTSR = $end->dataParsoid->tsr ?? null; |
| 74 |
$tokens[] = TokenCollector::buildMetaToken( $this->manager, 'mw:Includes/NoInclude', |
| 75 |
false, $startTSR, null ); |
| 76 |
|
| 77 |
PHPUtils::pushArray( $tokens, $collection ); |
| 78 |
if ( $end && !$eof ) { |
| 79 |
$tokens[] = TokenCollector::buildMetaToken( $this->manager, 'mw:Includes/NoInclude', |
| 80 |
true, $endTSR, null ); |
| 81 |
} |
| 82 |
} elseif ( !$this->options['inTemplate'] ) { |
| 83 |
// Content is stripped |
| 84 |
$tokens[] = TokenCollector::buildStrippedMetaToken( $this->manager, |
| 85 |
'mw:Includes/NoInclude', $start, $eof ? null : $end ); |
| 86 |
} |
| 87 |
|
| 88 |
// Preserve EOF |
| 89 |
if ( $eof ) { |
| 90 |
$tokens[] = $end; |
| 91 |
} |
| 92 |
|
| 93 |
return new TokenHandlerResult( $tokens ); |
| 94 |
} |
| 95 |
} |