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 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
PackedMode
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 8
210
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 perRow
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 dimensions
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 scaleMedia
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
20
 thumbWidth
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 useTraditionalGalleryText
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 galleryText
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
12
 getModules
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2declare( strict_types = 1 );
3
4namespace Wikimedia\Parsoid\Ext\Gallery;
5
6use Wikimedia\Parsoid\Core\DOMCompat;
7use Wikimedia\Parsoid\DOM\Document;
8use Wikimedia\Parsoid\DOM\Element;
9use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI;
10use Wikimedia\Parsoid\Utils\DOMUtils;
11
12class PackedMode extends TraditionalMode {
13    /**
14     * Create a PackedMode singleton.
15     * @param ?string $mode Only used by subclasses.
16     */
17    protected function __construct( ?string $mode = null ) {
18        parent::__construct( $mode ?? 'packed' );
19        $this->scale = 1.5;
20        $this->padding = (object)[ 'thumb' => 0, 'box' => 2, 'border' => 8 ];
21    }
22
23    /** @inheritDoc */
24    protected function perRow( Opts $opts, Element $ul ): void {
25        /* do nothing */
26    }
27
28    /** @inheritDoc */
29    public function dimensions( Opts $opts ): string {
30        $height = floor( $opts->imageHeight * $this->scale );
31        // The legacy parser does this so that the width is not the contraining factor
32        $width = floor( ( $opts->imageHeight * 10 + 100 ) * $this->scale );
33        return "{$width}x{$height}px";
34    }
35
36    /** @inheritDoc */
37    public function scaleMedia( Opts $opts, Element $wrapper ) {
38        $elt = $wrapper->firstChild->firstChild;
39        '@phan-var Element $elt'; // @var Element $elt
40        $isAudio = ( DOMUtils::nodeName( $elt ) === 'audio' );
41        $width = DOMCompat::getAttribute( $elt, 'width' );
42        // In the ->dimensions method, we set a large width but the legacy
43        // parser doesn't do that for audio files.  Since we don't know
44        // beforehand that the media is an audio file, undo that here.
45        // Audio has no dimensions and should get the default width
46        if ( !is_numeric( $width ) || $isAudio ) {
47            $width = $opts->imageWidth;
48        } else {
49            $width = intval( $width, 10 );
50            $width /= $this->scale;
51        }
52        $elt->setAttribute( 'width', strval( ceil( $width ) ) );
53        if ( $isAudio ) {
54            // Update the inline style that AddMediaInfo adds as well
55            $elt->setAttribute( 'style', "width: {$width}px;" );
56        }
57        $elt->setAttribute( 'height', "$opts->imageHeight" );
58        return $width;
59    }
60
61    /** @inheritDoc */
62    protected function thumbWidth( $width ) {
63        // The legacy parser requires at least 60px wide,
64        // so that the caption is wide enough to work
65        if ( $width < 60 ) {
66            $width = 60;
67        }
68        return $width + $this->padding->thumb;
69    }
70
71    protected function useTraditionalGalleryText(): bool {
72        return true;
73    }
74
75    /** @inheritDoc */
76    protected function galleryText(
77        Document $doc, Element $box, ?Element $gallerytext, float $width
78    ): void {
79        if ( $this->useTraditionalGalleryText() ) {
80            parent::galleryText( $doc, $box, $gallerytext, $width );
81            return;
82        }
83        if ( !$gallerytext ) {
84            return;
85        }
86        $div = $doc->createElement( 'div' );
87        $div->setAttribute( 'class', 'gallerytext' );
88        ParsoidExtensionAPI::migrateChildrenAndTransferWrapperDataAttribs(
89            $gallerytext, $div
90        );
91        $wrapper = $doc->createElement( 'div' );
92        $wrapper->setAttribute( 'class', 'gallerytextwrapper' );
93        $wrapper->setAttribute( 'style', 'width: ' . ceil( $width - 20 ) . 'px;' );
94        $wrapper->appendChild( $div );
95        $box->appendChild( $wrapper );
96    }
97
98    /**
99     * @return list{'mediawiki.page.gallery'}
100     */
101    public function getModules(): array {
102        return [ 'mediawiki.page.gallery' ];
103    }
104}