/**
* Set how wide each image will be, in pixels.
*
- * @param int $num Integer > 0; invalid numbers will be ignored
+ * @param string $num Number. Unit other than 'px is invalid. Invalid numbers
+ * and those below 0 are ignored.
*/
public function setWidths( $num ) {
- if ( $num > 0 ) {
- $this->mWidths = (int)$num;
+ $parsed = Parser::parseWidthParam( $num, false );
+ if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
+ $this->mWidths = $parsed['width'];
}
}
/**
* Set how high each image will be, in pixels.
*
- * @param int $num Integer > 0; invalid numbers will be ignored
+ * @param string $num Number. Unit other than 'px is invalid. Invalid numbers
+ * and those below 0 are ignored.
*/
public function setHeights( $num ) {
- if ( $num > 0 ) {
- $this->mHeights = (int)$num;
+ $parsed = Parser::parseWidthParam( $num, false );
+ if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
+ $this->mHeights = $parsed['width'];
}
}
if ( $argA == 'nowiki' ) {
// {{filepath: | option [| size] }}
$isNowiki = true;
- $parsedWidthParam = $parser->parseWidthParam( $argB );
+ $parsedWidthParam = Parser::parseWidthParam( $argB );
} else {
// {{filepath: [| size [|option]] }}
- $parsedWidthParam = $parser->parseWidthParam( $argA );
+ $parsedWidthParam = Parser::parseWidthParam( $argA );
$isNowiki = ( $argB == 'nowiki' );
}
# Special case; width and height come in one variable together
if ( $type === 'handler' && $paramName === 'width' ) {
- $parsedWidthParam = $this->parseWidthParam( $value );
+ $parsedWidthParam = self::parseWidthParam( $value );
if ( isset( $parsedWidthParam['width'] ) ) {
$width = $parsedWidthParam['width'];
if ( $handler->validateParam( 'width', $width ) ) {
* Parsed a width param of imagelink like 300px or 200x300px
*
* @param string $value
+ * @param bool $parseHeight
*
* @return array
* @since 1.20
*/
- public function parseWidthParam( $value ) {
+ public static function parseWidthParam( $value, $parseHeight = true ) {
$parsedWidthParam = [];
if ( $value === '' ) {
return $parsedWidthParam;
$m = [];
# (T15500) In both cases (width/height and width only),
# permit trailing "px" for backward compatibility.
- if ( preg_match( '/^([0-9]*)x([0-9]*)\s*(?:px)?\s*$/', $value, $m ) ) {
+ if ( $parseHeight && preg_match( '/^([0-9]*)x([0-9]*)\s*(?:px)?\s*$/', $value, $m ) ) {
$width = intval( $m[1] );
$height = intval( $m[2] );
$parsedWidthParam['width'] = $width;
</ul>
!! end
+!! test
+Gallery (without px units)
+!! wikitext
+<gallery widths="70" heights="40">
+File:Foobar.jpg
+</gallery>
+!! html/php
+<ul class="gallery mw-gallery-traditional">
+ <li class="gallerybox" style="width: 105px"><div style="width: 105px">
+ <div class="thumb" style="width: 100px;"><div style="margin:31px auto;"><a href="/wiki/File:Foobar.jpg" class="image"><img alt="Foobar.jpg" src="http://example.com/images/thumb/3/3a/Foobar.jpg/70px-Foobar.jpg" width="70" height="8" srcset="http://example.com/images/thumb/3/3a/Foobar.jpg/105px-Foobar.jpg 1.5x, http://example.com/images/thumb/3/3a/Foobar.jpg/140px-Foobar.jpg 2x" /></a></div></div>
+ <div class="gallerytext">
+ </div>
+ </div></li>
+</ul>
+
+!! html/parsoid
+<ul class="gallery mw-gallery-traditional" typeof="mw:Extension/gallery" about="#mwt2" data-mw='{"name":"gallery","attrs":{"widths":"70","heights":"40"},"body":{"extsrc":"\nFile:Foobar.jpg\n"}}'>
+<li class="gallerybox" style="width: 105px;"><div class="thumb" style="width: 100px; height: 70px;"><figure-inline typeof="mw:Image"><a href="./File:Foobar.jpg"><img resource="./File:Foobar.jpg" src="//example.com/images/thumb/3/3a/Foobar.jpg/70px-Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="8" width="70"/></a></figure-inline></div><div class="gallerytext"></div></li>
+</ul>
+!! end
+
+!! test
+Gallery (with invalid units)
+!! wikitext
+<gallery widths="70em" heights="40em">
+File:Foobar.jpg
+</gallery>
+!! html/php
+<ul class="gallery mw-gallery-traditional">
+ <li class="gallerybox" style="width: 155px"><div style="width: 155px">
+ <div class="thumb" style="width: 150px;"><div style="margin:68px auto;"><a href="/wiki/File:Foobar.jpg" class="image"><img alt="Foobar.jpg" src="http://example.com/images/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg" width="120" height="14" srcset="http://example.com/images/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg 1.5x, http://example.com/images/thumb/3/3a/Foobar.jpg/240px-Foobar.jpg 2x" /></a></div></div>
+ <div class="gallerytext">
+ </div>
+ </div></li>
+</ul>
+
+!! html/parsoid
+<ul class="gallery mw-gallery-traditional" typeof="mw:Extension/gallery" about="#mwt2" data-mw='{"name":"gallery","attrs":{"widths":"70em","heights":"40em"},"body":{"extsrc":"\nFile:Foobar.jpg\n"}}'>
+<li class="gallerybox" style="width: 155px;"><div class="thumb" style="width: 150px; height: 150px;"><figure-inline typeof="mw:Image"><a href="./File:Foobar.jpg"><img resource="./File:Foobar.jpg" src="//example.com/images/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="14" width="120"/></a></figure-inline></div><div class="gallerytext"></div></li>
+</ul>
+!! end
+
!! test
Gallery with link that has fragment
!! options