Debian Codegraph @

vendor/wikimedia/parsoid/src/Wt2Html/TT/IncludeOnly.php

raw
1 <?php
2 declare( strict_types = 1 );
3
4 namespace Wikimedia\Parsoid\Wt2Html\TT;
5
6 use Wikimedia\Parsoid\NodeData\DataMw;
7 use Wikimedia\Parsoid\Tokens\EOFTk;
8 use Wikimedia\Parsoid\Tokens\SelfclosingTagTk;
9 use Wikimedia\Parsoid\Tokens\SourceRange;
10 use Wikimedia\Parsoid\Wt2Html\TokenTransformManager;
11
12 /**
13 * Simple noinclude / onlyinclude implementation.
14 * Strips all tokens in noinclude sections.
15 */
16 class IncludeOnly extends TokenCollector {
17
18 public function __construct( TokenTransformManager $manager, array $options ) {
19 parent::__construct( $manager, $options );
20 }
21
22 protected function type(): string {
23 return 'tag';
24 }
25
26 protected function name(): string {
27 return 'includeonly';
28 }
29
30 protected function toEnd(): bool {
31 return true; // Match the end-of-input if </noinclude> is missing.
32 }
33
34 protected function ackEnd(): bool {
35 return false;
36 }
37
38 protected function transformation( array $collection ): TokenHandlerResult {
39 $start = array_shift( $collection );
40
41 // Handle self-closing tag case specially!
42 if ( $start instanceof SelfclosingTagTk ) {
43 $tsr = $start->dataParsoid->tsr ?? new SourceRange( null, null );
44 $token = TokenCollector::buildMetaToken(
45 $this->manager,
46 'mw:Includes/IncludeOnly',
47 false,
48 $tsr,
49 null
50 );
51 if ( $start->dataParsoid->src ) {
52 $token->dataMw = new DataMw( [ 'src' => $start->dataParsoid->src ] );
53 }
54 return ( $this->options['isInclude'] ) ?
55 new TokenHandlerResult( [] ) : new TokenHandlerResult( [ $token ] );
56 }
57
58 $tokens = [];
59 $end = array_pop( $collection );
60 $eof = $end instanceof EOFTk;
61
62 if ( $this->options['isInclude'] ) {
63 // Just pass through the full collection including delimiters
64 $tokens = $collection;
65 } elseif ( !$this->options['inTemplate'] ) {
66 // Content is stripped
67 // Add meta tags for open and close for roundtripping.
68 //
69 // We can make do entirely with a single meta-tag since
70 // there is no real content. However, we add a dummy end meta-tag
71 // so that all <*include*> meta tags show up in open/close pairs
72 // and can be handled similarly by downstream handlers.
73 $name = 'mw:Includes/IncludeOnly';
74 $tokens[] = TokenCollector::buildStrippedMetaToken( $this->manager, $name,
75 $start, $eof ? null : $end );
76
77 if ( $start->dataParsoid->src ) {
78 $tokens[0]->dataMw = new DataMw( [ 'src' => $start->dataParsoid->src ] );
79 }
80
81 if ( $end && !$eof ) {
82 // This token is just a placeholder for RT purposes. Since the
83 // stripped token (above) got the entire tsr value, we are artificially
84 // setting the tsr on this node to zero-width to ensure that
85 // DSR computation comes out correct.
86 $endPos = isset( $end->dataParsoid->tsr ) ? $end->dataParsoid->tsr->end : null;
87 $tokens[] = TokenCollector::buildMetaToken( $this->manager, $name,
88 true, new SourceRange( $endPos, $endPos ), '' );
89 }
90 }
91
92 // Preserve EOF
93 if ( $eof ) {
94 $tokens[] = $end;
95 }
96
97 return new TokenHandlerResult( $tokens );
98 }
99 }