Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
94.44% |
17 / 18 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
AddWrapperDivClass | |
94.44% |
17 / 18 |
|
75.00% |
3 / 4 |
7.01 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
shouldRun | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
transformText | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
2 | |||
getLanguageWithFallbackGuess | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 |
1 | <?php |
2 | |
3 | namespace MediaWiki\OutputTransform\Stages; |
4 | |
5 | use Language; |
6 | use MediaWiki\Html\Html; |
7 | use MediaWiki\Languages\LanguageFactory; |
8 | use MediaWiki\OutputTransform\ContentTextTransformStage; |
9 | use MediaWiki\Parser\ParserOutput; |
10 | use ParserOptions; |
11 | |
12 | /** |
13 | * Wrap the output in a div with the provided class name |
14 | * @internal |
15 | */ |
16 | class AddWrapperDivClass extends ContentTextTransformStage { |
17 | |
18 | private LanguageFactory $langFactory; |
19 | private Language $contentLang; |
20 | |
21 | public function __construct( LanguageFactory $langFactory, Language $contentLang ) { |
22 | $this->langFactory = $langFactory; |
23 | $this->contentLang = $contentLang; |
24 | } |
25 | |
26 | public function shouldRun( ParserOutput $po, ?ParserOptions $popts, array $options = [] ): bool { |
27 | return ( $options['wrapperDivClass'] ?? '' ) !== '' && !( $options['unwrap'] ?? false ); |
28 | } |
29 | |
30 | protected function transformText( string $text, ParserOutput $po, ?ParserOptions $popts, array &$options ): string { |
31 | $wrapperDivClass = $options['wrapperDivClass']; |
32 | $pageLang = $this->getLanguageWithFallbackGuess( $po ); |
33 | $parsoidVersion = $po->getExtensionData( 'core:parsoid-version' ); |
34 | $extraAttrs = $parsoidVersion === null ? [] : [ |
35 | 'data-mw-parsoid-version' => $parsoidVersion, |
36 | ]; |
37 | return Html::rawElement( 'div', [ |
38 | 'class' => 'mw-content-' . $pageLang->getDir() . ' ' . $wrapperDivClass, |
39 | 'lang' => $pageLang->toBcp47Code(), |
40 | 'dir' => $pageLang->getDir(), |
41 | ] + $extraAttrs, $text ); |
42 | } |
43 | |
44 | private function getLanguageWithFallbackGuess( ParserOutput $po ): Language { |
45 | $pageLang = $po->getLanguage(); |
46 | if ( $pageLang ) { |
47 | return $this->langFactory->getLanguage( $pageLang ); |
48 | } |
49 | return $this->contentLang; |
50 | } |
51 | } |