Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
IncludeOnly
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 6
306
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 type
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 name
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 toEnd
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 ackEnd
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 transformation
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
156
1<?php
2declare( strict_types = 1 );
3
4namespace Wikimedia\Parsoid\Wt2Html\TT;
5
6use Wikimedia\Parsoid\Tokens\EOFTk;
7use Wikimedia\Parsoid\Tokens\SelfclosingTagTk;
8use Wikimedia\Parsoid\Tokens\SourceRange;
9use Wikimedia\Parsoid\Utils\PHPUtils;
10use Wikimedia\Parsoid\Wt2Html\TokenTransformManager;
11
12/**
13 * Simple noinclude / onlyinclude implementation.
14 * Strips all tokens in noinclude sections.
15 */
16class 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                $datamw = PHPUtils::jsonEncode( [ 'src' => $start->dataParsoid->src ] );
53                $token->addAttribute( 'data-mw', $datamw );
54            }
55            return ( $this->options['isInclude'] ) ?
56                new TokenHandlerResult( [] ) : new TokenHandlerResult( [ $token ] );
57        }
58
59        $tokens = [];
60        $end = array_pop( $collection );
61        $eof = $end instanceof EOFTk;
62
63        if ( $this->options['isInclude'] ) {
64            // Just pass through the full collection including delimiters
65            $tokens = $collection;
66        } elseif ( !$this->options['inTemplate'] ) {
67            // Content is stripped
68            // Add meta tags for open and close for roundtripping.
69            //
70            // We can make do entirely with a single meta-tag since
71            // there is no real content.  However, we add a dummy end meta-tag
72            // so that all <*include*> meta tags show up in open/close pairs
73            // and can be handled similarly by downstream handlers.
74            $name = 'mw:Includes/IncludeOnly';
75            $tokens[] = TokenCollector::buildStrippedMetaToken( $this->manager, $name,
76                $start, $eof ? null : $end );
77
78            if ( $start->dataParsoid->src ) {
79                $dataMw = PHPUtils::jsonEncode( [ 'src' => $start->dataParsoid->src ] );
80                $tokens[0]->addAttribute( 'data-mw', $dataMw );
81            }
82
83            if ( $end && !$eof ) {
84                // This token is just a placeholder for RT purposes. Since the
85                // stripped token (above) got the entire tsr value, we are artificially
86                // setting the tsr on this node to zero-width to ensure that
87                // DSR computation comes out correct.
88                $endPos = isset( $end->dataParsoid->tsr ) ? $end->dataParsoid->tsr->end : null;
89                $tokens[] = TokenCollector::buildMetaToken( $this->manager, $name,
90                    true, new SourceRange( $endPos, $endPos ), '' );
91            }
92        }
93
94        // Preserve EOF
95        if ( $eof ) {
96            $tokens[] = $end;
97        }
98
99        return new TokenHandlerResult( $tokens );
100    }
101}