Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 39 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
IncludeOnly | |
0.00% |
0 / 39 |
|
0.00% |
0 / 6 |
306 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
type | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
name | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
toEnd | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
ackEnd | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
transformation | |
0.00% |
0 / 34 |
|
0.00% |
0 / 1 |
156 |
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\Tokens\SelfclosingTagTk; |
8 | use Wikimedia\Parsoid\Tokens\SourceRange; |
9 | use Wikimedia\Parsoid\Utils\PHPUtils; |
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 | * @param TokenTransformManager $manager |
19 | * @param array $options options |
20 | */ |
21 | public function __construct( TokenTransformManager $manager, array $options ) { |
22 | parent::__construct( $manager, $options ); |
23 | } |
24 | |
25 | /** |
26 | * @return string |
27 | */ |
28 | protected function type(): string { |
29 | return 'tag'; |
30 | } |
31 | |
32 | /** |
33 | * @return string |
34 | */ |
35 | protected function name(): string { |
36 | return 'includeonly'; |
37 | } |
38 | |
39 | /** |
40 | * @return bool |
41 | */ |
42 | protected function toEnd(): bool { |
43 | return true; // Match the end-of-input if </noinclude> is missing. |
44 | } |
45 | |
46 | /** |
47 | * @return bool |
48 | */ |
49 | protected function ackEnd(): bool { |
50 | return false; |
51 | } |
52 | |
53 | /** |
54 | * @param array $collection |
55 | * @return TokenHandlerResult |
56 | */ |
57 | protected function transformation( array $collection ): TokenHandlerResult { |
58 | $start = array_shift( $collection ); |
59 | |
60 | // Handle self-closing tag case specially! |
61 | if ( $start instanceof SelfclosingTagTk ) { |
62 | $tsr = $start->dataParsoid->tsr ?? new SourceRange( null, null ); |
63 | $token = TokenCollector::buildMetaToken( |
64 | $this->manager, |
65 | 'mw:Includes/IncludeOnly', |
66 | false, |
67 | $tsr, |
68 | null |
69 | ); |
70 | if ( $start->dataParsoid->src ) { |
71 | $datamw = PHPUtils::jsonEncode( [ 'src' => $start->dataParsoid->src ] ); |
72 | $token->addAttribute( 'data-mw', $datamw ); |
73 | } |
74 | return ( $this->options['isInclude'] ) ? |
75 | new TokenHandlerResult( [] ) : new TokenHandlerResult( [ $token ] ); |
76 | } |
77 | |
78 | $tokens = []; |
79 | $end = array_pop( $collection ); |
80 | $eof = $end instanceof EOFTk; |
81 | |
82 | if ( $this->options['isInclude'] ) { |
83 | // Just pass through the full collection including delimiters |
84 | $tokens = $collection; |
85 | } elseif ( !$this->options['inTemplate'] ) { |
86 | // Content is stripped |
87 | // Add meta tags for open and close for roundtripping. |
88 | // |
89 | // We can make do entirely with a single meta-tag since |
90 | // there is no real content. However, we add a dummy end meta-tag |
91 | // so that all <*include*> meta tags show up in open/close pairs |
92 | // and can be handled similarly by downstream handlers. |
93 | $name = 'mw:Includes/IncludeOnly'; |
94 | $tokens[] = TokenCollector::buildStrippedMetaToken( $this->manager, $name, |
95 | $start, $eof ? null : $end ); |
96 | |
97 | if ( $start->dataParsoid->src ) { |
98 | $dataMw = PHPUtils::jsonEncode( [ 'src' => $start->dataParsoid->src ] ); |
99 | $tokens[0]->addAttribute( 'data-mw', $dataMw ); |
100 | } |
101 | |
102 | if ( $end && !$eof ) { |
103 | // This token is just a placeholder for RT purposes. Since the |
104 | // stripped token (above) got the entire tsr value, we are artificially |
105 | // setting the tsr on this node to zero-width to ensure that |
106 | // DSR computation comes out correct. |
107 | $endPos = isset( $end->dataParsoid->tsr ) ? $end->dataParsoid->tsr->end : null; |
108 | $tokens[] = TokenCollector::buildMetaToken( $this->manager, $name, |
109 | true, new SourceRange( $endPos, $endPos ), '' ); |
110 | } |
111 | } |
112 | |
113 | // Preserve EOF |
114 | if ( $eof ) { |
115 | $tokens[] = $end; |
116 | } |
117 | |
118 | return new TokenHandlerResult( $tokens ); |
119 | } |
120 | } |