Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 33 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
PackedMode | |
0.00% |
0 / 33 |
|
0.00% |
0 / 7 |
110 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
perRow | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
dimensions | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
scaleMedia | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
useTraditionalGalleryText | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
galleryText | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
getModules | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace Wikimedia\Parsoid\Ext\Gallery; |
5 | |
6 | use Wikimedia\Parsoid\DOM\Document; |
7 | use Wikimedia\Parsoid\DOM\Element; |
8 | use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI; |
9 | use Wikimedia\Parsoid\Utils\DOMCompat; |
10 | |
11 | class PackedMode extends TraditionalMode { |
12 | /** |
13 | * Create a PackedMode singleton. |
14 | * @param ?string $mode Only used by subclasses. |
15 | */ |
16 | protected function __construct( ?string $mode = null ) { |
17 | parent::__construct( $mode ?? 'packed' ); |
18 | $this->scale = 1.5; |
19 | $this->padding = (object)[ 'thumb' => 0, 'box' => 2, 'border' => 8 ]; |
20 | } |
21 | |
22 | /** @inheritDoc */ |
23 | protected function perRow( Opts $opts, Element $ul ): void { |
24 | /* do nothing */ |
25 | } |
26 | |
27 | /** @inheritDoc */ |
28 | public function dimensions( Opts $opts ): string { |
29 | $height = floor( $opts->imageHeight * $this->scale ); |
30 | // The legacy parser does this so that the width is not the contraining factor |
31 | $width = floor( ( $opts->imageHeight * 10 + 100 ) * $this->scale ); |
32 | return "{$width}x{$height}px"; |
33 | } |
34 | |
35 | /** @inheritDoc */ |
36 | public function scaleMedia( Opts $opts, Element $wrapper ) { |
37 | $elt = $wrapper->firstChild->firstChild; |
38 | '@phan-var Element $elt'; // @var Element $elt |
39 | $width = DOMCompat::getAttribute( $elt, 'width' ); |
40 | if ( !is_numeric( $width ) ) { |
41 | $width = $opts->imageWidth; |
42 | } else { |
43 | $width = intval( $width, 10 ); |
44 | $width /= $this->scale; |
45 | } |
46 | $elt->setAttribute( 'width', strval( ceil( $width ) ) ); |
47 | $elt->setAttribute( 'height', "$opts->imageHeight" ); |
48 | return $width; |
49 | } |
50 | |
51 | protected function useTraditionalGalleryText(): bool { |
52 | return true; |
53 | } |
54 | |
55 | /** @inheritDoc */ |
56 | protected function galleryText( |
57 | Document $doc, Element $box, ?Element $gallerytext, float $width |
58 | ): void { |
59 | if ( $this->useTraditionalGalleryText() ) { |
60 | parent::galleryText( $doc, $box, $gallerytext, $width ); |
61 | return; |
62 | } |
63 | if ( !$gallerytext ) { |
64 | return; |
65 | } |
66 | $div = $doc->createElement( 'div' ); |
67 | $div->setAttribute( 'class', 'gallerytext' ); |
68 | ParsoidExtensionAPI::migrateChildrenAndTransferWrapperDataAttribs( |
69 | $gallerytext, $div |
70 | ); |
71 | $wrapper = $doc->createElement( 'div' ); |
72 | $wrapper->setAttribute( 'class', 'gallerytextwrapper' ); |
73 | $wrapper->setAttribute( 'style', 'width: ' . ceil( $width - 20 ) . 'px;' ); |
74 | $wrapper->appendChild( $div ); |
75 | $box->appendChild( $wrapper ); |
76 | } |
77 | |
78 | /** |
79 | * @return list{'mediawiki.page.gallery'} |
80 | */ |
81 | public function getModules(): array { |
82 | return [ 'mediawiki.page.gallery' ]; |
83 | } |
84 | } |