From 60a64e89125b1bda123cb03095a46575665b1291 Mon Sep 17 00:00:00 2001 From: Prateek Saxena Date: Sun, 19 Jun 2016 12:11:43 +0530 Subject: [PATCH] Gallery: Use Parser::parseWidthParam() for gallery dimensions 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 | 16 ++++++---- includes/parser/CoreParserFunctions.php | 4 +-- includes/parser/Parser.php | 7 +++-- tests/parser/parserTests.txt | 42 +++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 11 deletions(-) diff --git a/includes/gallery/ImageGalleryBase.php b/includes/gallery/ImageGalleryBase.php index 80fd22ebaa..700c8ee50f 100644 --- a/includes/gallery/ImageGalleryBase.php +++ b/includes/gallery/ImageGalleryBase.php @@ -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']; } } diff --git a/includes/parser/CoreParserFunctions.php b/includes/parser/CoreParserFunctions.php index ad56afc413..c6a10aefbc 100644 --- a/includes/parser/CoreParserFunctions.php +++ b/includes/parser/CoreParserFunctions.php @@ -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' ); } diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php index dcd16eb4fa..831c1fff46 100644 --- a/includes/parser/Parser.php +++ b/includes/parser/Parser.php @@ -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; diff --git a/tests/parser/parserTests.txt b/tests/parser/parserTests.txt index aa03c0ed9d..e6fa203419 100644 --- a/tests/parser/parserTests.txt +++ b/tests/parser/parserTests.txt @@ -20701,6 +20701,48 @@ image:foobar.jpg|Blabla|alt=This is a foo-bar.|blabla. !! end +!! test +Gallery (without px units) +!! wikitext + +File:Foobar.jpg + +!! html/php + + +!! html/parsoid + +!! end + +!! test +Gallery (with invalid units) +!! wikitext + +File:Foobar.jpg + +!! html/php + + +!! html/parsoid + +!! end + !! test Gallery with link that has fragment !! options -- 2.20.1