| 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\DOMUtils; |
| 9 |
use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI; |
| 10 |
use Wikimedia\Parsoid\Utils\DOMCompat; |
| 11 |
|
| 12 |
class 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 |
DOMUtils::assertElt( $elt ); |
| 40 |
$width = DOMCompat::getAttribute( $elt, 'width' ); |
| 41 |
if ( !is_numeric( $width ) ) { |
| 42 |
$width = $opts->imageWidth; |
| 43 |
} else { |
| 44 |
$width = intval( $width, 10 ); |
| 45 |
$width /= $this->scale; |
| 46 |
} |
| 47 |
$elt->setAttribute( 'width', strval( ceil( $width ) ) ); |
| 48 |
$elt->setAttribute( 'height', "$opts->imageHeight" ); |
| 49 |
return $width; |
| 50 |
} |
| 51 |
|
| 52 |
protected function useTraditionalGalleryText(): bool { |
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
/** @inheritDoc */ |
| 57 |
protected function galleryText( |
| 58 |
Document $doc, Element $box, ?Element $gallerytext, float $width |
| 59 |
): void { |
| 60 |
if ( $this->useTraditionalGalleryText() ) { |
| 61 |
parent::galleryText( $doc, $box, $gallerytext, $width ); |
| 62 |
return; |
| 63 |
} |
| 64 |
if ( !$gallerytext ) { |
| 65 |
return; |
| 66 |
} |
| 67 |
$div = $doc->createElement( 'div' ); |
| 68 |
$div->setAttribute( 'class', 'gallerytext' ); |
| 69 |
ParsoidExtensionAPI::migrateChildrenAndTransferWrapperDataAttribs( |
| 70 |
$gallerytext, $div |
| 71 |
); |
| 72 |
$wrapper = $doc->createElement( 'div' ); |
| 73 |
$wrapper->setAttribute( 'class', 'gallerytextwrapper' ); |
| 74 |
$wrapper->setAttribute( 'style', 'width: ' . ceil( $width - 20 ) . 'px;' ); |
| 75 |
$wrapper->appendChild( $div ); |
| 76 |
$box->appendChild( $wrapper ); |
| 77 |
} |
| 78 |
|
| 79 |
public function getModules(): array { |
| 80 |
return [ 'mediawiki.page.gallery' ]; |
| 81 |
} |
| 82 |
} |