Gallery: Use Parser::parseWidthParam() for gallery dimensions
authorPrateek Saxena <prtksxna@gmail.com>
Sun, 19 Jun 2016 06:41:43 +0000 (12:11 +0530)
committerArlo Breault <abreault@wikimedia.org>
Thu, 25 Jan 2018 22:35:40 +0000 (17:35 -0500)
Used by the `setWidths` and `setHeights` methods to make sure we are
using correct values.

Makes `parseWidthParam` static to be used in the gallery class.

Bug: T129372
Change-Id: I38b9ef0ea26e3748ad5d5458fadd2545f677ef93

includes/gallery/ImageGalleryBase.php
includes/parser/CoreParserFunctions.php
includes/parser/Parser.php
tests/parser/parserTests.txt

index 80fd22e..700c8ee 100644 (file)
@@ -207,22 +207,26 @@ abstract class ImageGalleryBase extends ContextSource {
        /**
         * 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'];
                }
        }
 
index ad56afc..c6a10ae 100644 (file)
@@ -1005,10 +1005,10 @@ class CoreParserFunctions {
                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' );
                }
 
index dcd16eb..831c1ff 100644 (file)
@@ -5210,7 +5210,7 @@ class Parser {
 
                                # 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 ) ) {
@@ -6053,11 +6053,12 @@ class Parser {
         * 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;
@@ -6065,7 +6066,7 @@ class Parser {
                $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;
index aa03c0e..e6fa203 100644 (file)
@@ -20701,6 +20701,48 @@ image:foobar.jpg|Blabla|alt=This is a foo-bar.|blabla.
 </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