Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 74
0.00% covered (danger)
0.00%
0 / 17
CRAP
0.00% covered (danger)
0.00%
0 / 1
TraditionalMode
0.00% covered (danger)
0.00%
0 / 74
0.00% covered (danger)
0.00%
0 / 17
650
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
 appendAttr
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 ul
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 perRow
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 setAdditionalOptions
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 caption
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 dimensions
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 scaleMedia
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 thumbWidth
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 thumbHeight
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 thumbStyle
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 boxWidth
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 boxStyle
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 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 line
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
2
 render
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 getModuleStyles
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\DOM\Document;
7use Wikimedia\Parsoid\DOM\DocumentFragment;
8use Wikimedia\Parsoid\DOM\Element;
9use Wikimedia\Parsoid\Ext\DOMDataUtils;
10use Wikimedia\Parsoid\Ext\DOMUtils;
11use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI;
12use Wikimedia\Parsoid\Ext\PHPUtils;
13
14class TraditionalMode extends Mode {
15    /**
16     * Create a TraditionalMode singleton.
17     * @param ?string $mode Only used by subclasses.
18     */
19    protected function __construct( ?string $mode = null ) {
20        parent::__construct( $mode ?? 'traditional' );
21        $this->scale = 1;
22        $this->padding = PHPUtils::arrayToObject( [ 'thumb' => 30, 'box' => 5, 'border' => 8 ] );
23    }
24
25    /** @var float */
26    protected $scale;
27    /** @var \stdClass */
28    protected $padding;
29
30    /**
31     * @param Element $ul
32     * @param string $k
33     * @param string $v
34     */
35    private function appendAttr( Element $ul, string $k, string $v ) {
36        $val = $ul->hasAttribute( $k ) ? $ul->getAttribute( $k ) : '';
37        if ( strlen( $val ) > 0 ) {
38            $val .= ' ';
39        }
40        $ul->setAttribute( $k, $val . $v );
41    }
42
43    /**
44     * @param Opts $opts
45     * @param DocumentFragment $domFragment
46     * @return Element
47     */
48    private function ul(
49        Opts $opts, DocumentFragment $domFragment
50    ): Element {
51        $ul = $domFragment->ownerDocument->createElement( 'ul' );
52        $cl = 'gallery mw-gallery-' . $this->mode;
53        $ul->setAttribute( 'class', $cl );
54        foreach ( $opts->attrs as $k => $v ) {
55            $this->appendAttr( $ul, $k, $v );
56        }
57        $domFragment->appendChild( $ul );
58        $this->perRow( $opts, $ul );
59        $this->setAdditionalOptions( $opts, $ul );
60        return $ul;
61    }
62
63    /**
64     * @param Opts $opts
65     * @param Element $ul
66     */
67    protected function perRow( Opts $opts, Element $ul ): void {
68        if ( $opts->imagesPerRow > 0 ) {
69            $padding = $this->padding;
70            $total = $opts->imageWidth + $padding->thumb + $padding->box + $padding->border;
71            $total *= $opts->imagesPerRow;
72            $this->appendAttr( $ul, 'style', 'max-width: ' . $total . 'px;' );
73        }
74    }
75
76    /**
77     * @param Opts $opts
78     * @param Element $ul
79     */
80    protected function setAdditionalOptions( Opts $opts, Element $ul ): void {
81    }
82
83    /**
84     * @param Opts $opts
85     * @param Element $ul
86     * @param DocumentFragment $caption
87     */
88    private function caption(
89        Opts $opts, Element $ul, DocumentFragment $caption
90    ) {
91        $doc = $ul->ownerDocument;
92        $li = $doc->createElement( 'li' );
93        $li->setAttribute( 'class', 'gallerycaption' );
94        DOMUtils::migrateChildren( $caption, $li );
95        $ul->appendChild( $doc->createTextNode( "\n" ) );
96        $ul->appendChild( $li );
97    }
98
99    /** @inheritDoc */
100    public function dimensions( Opts $opts ): string {
101        return "{$opts->imageWidth}x{$opts->imageHeight}px";
102    }
103
104    /**
105     * @param Opts $opts
106     * @param Element $wrapper
107     * @return int|float
108     */
109    protected function scaleMedia( Opts $opts, Element $wrapper ) {
110        return $opts->imageWidth;
111    }
112
113    /**
114     * @param float|int $width
115     * @return float|int
116     */
117    protected function thumbWidth( $width ) {
118        return $width + $this->padding->thumb;
119    }
120
121    /**
122     * @param float|int $height
123     * @return float|int
124     */
125    protected function thumbHeight( $height ) {
126        return $height + $this->padding->thumb;
127    }
128
129    /**
130     * @param float|int $width
131     * @param float|int $height
132     * @return string
133     */
134    protected function thumbStyle( $width, $height ): string {
135        $style = [ 'width: ' . $this->thumbWidth( $width ) . 'px;' ];
136        if ( $this->mode === 'traditional' ) {
137            $style[] = 'height: ' . $this->thumbHeight( $height ) . 'px;';
138        }
139        return implode( ' ', $style );
140    }
141
142    /**
143     * @param float|int $width
144     * @return float|int
145     */
146    protected function boxWidth( $width ) {
147        return $this->thumbWidth( $width ) + $this->padding->box;
148    }
149
150    /**
151     * @param float|int $width
152     * @param float|int $height
153     * @return string
154     */
155    protected function boxStyle( $width, $height ): string {
156        return 'width: ' . $this->boxWidth( $width ) . 'px;';
157    }
158
159    /**
160     * @param Document $doc
161     * @param Element $box
162     * @param ?Element $gallerytext
163     * @param float $width
164     */
165    protected function galleryText(
166        Document $doc, Element $box, ?Element $gallerytext,
167        float $width
168    ): void {
169        $div = $doc->createElement( 'div' );
170        $div->setAttribute( 'class', 'gallerytext' );
171        if ( $gallerytext ) {
172            ParsoidExtensionAPI::migrateChildrenAndTransferWrapperDataAttribs(
173                $gallerytext, $div
174            );
175        }
176        $box->appendChild( $div );
177    }
178
179    /**
180     * @param Opts $opts
181     * @param Element $ul
182     * @param ParsedLine $o
183     */
184    private function line( Opts $opts, Element $ul, ParsedLine $o ): void {
185        $doc = $ul->ownerDocument;
186
187        $width = $this->scaleMedia( $opts, $o->thumb );
188        $height = $opts->imageHeight;
189
190        $box = $doc->createElement( 'li' );
191        $box->setAttribute( 'class', 'gallerybox' );
192        $box->setAttribute( 'style', $this->boxStyle( $width, $height ) );
193        DOMDataUtils::getDataParsoid( $box )->dsr = $o->dsr;
194
195        $thumb = $doc->createElement( 'div' );
196        $thumb->setAttribute( 'class', 'thumb' );
197        $thumb->setAttribute( 'style', $this->thumbStyle( $width, $height ) );
198
199        $wrapper = $doc->createElement( 'span' );
200        $wrapper->setAttribute( 'typeof', $o->rdfaType );
201        ParsoidExtensionAPI::migrateChildrenAndTransferWrapperDataAttribs(
202            $o->thumb, $wrapper
203        );
204        $thumb->appendChild( $wrapper );
205
206        $box->appendChild( $thumb );
207        $this->galleryText( $doc, $box, $o->gallerytext, $width );
208        $ul->appendChild( $doc->createTextNode( "\n" ) );
209        $ul->appendChild( $box );
210    }
211
212    /** @inheritDoc */
213    public function render(
214        ParsoidExtensionAPI $extApi, Opts $opts, ?DocumentFragment $caption,
215        array $lines
216    ): DocumentFragment {
217        $domFragment = $extApi->htmlToDom( '' );
218        $ul = $this->ul( $opts, $domFragment );
219        if ( $caption ) {
220            $this->caption( $opts, $ul, $caption );
221        }
222        foreach ( $lines as $l ) {
223            $this->line( $opts, $ul, $l );
224        }
225        $ul->appendChild( $domFragment->ownerDocument->createTextNode( "\n" ) );
226        return $domFragment;
227    }
228
229    /**
230     * @return array
231     */
232    public function getModuleStyles(): array {
233        return [ 'mediawiki.page.gallery.styles' ];
234    }
235
236}