From 6333fa6191ca5a2f5823ad4185a6e699e8674cfc Mon Sep 17 00:00:00 2001 From: Ori Livneh Date: Fri, 3 Apr 2015 16:17:13 -0700 Subject: [PATCH] Html::srcSet: allow density to be specified either with or without trailing 'x' $wgLogoHD is meant to contain high-density alternatives for $wgLogo, but its keys include the trailing 'x' (e.g., '1.5x'), making it unusable with Html::srcSet(). Fix that by normalizing all density values to have a single trailing 'x'. Change-Id: I62cc3a9e4aeff3a7cb102de2965b8b40fd106c37 --- includes/Html.php | 26 ++++++++++++++++++++------ tests/phpunit/includes/HtmlTest.php | 24 ++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/includes/Html.php b/includes/Html.php index effc488091..d312e0a6e9 100644 --- a/includes/Html.php +++ b/includes/Html.php @@ -1025,9 +1025,24 @@ class Html { } /** - * Generate a srcset attribute value from an array mapping pixel densities - * to URLs. Note that srcset supports width and height values as well, which - * are not used here. + * Generate a srcset attribute value. + * + * Generates a srcset attribute value from an array mapping pixel densities + * to URLs. A trailing 'x' in pixel density values is optional. + * + * @note srcset width and height values are not supported. + * + * @see http://www.whatwg.org/html/embedded-content-1.html#attr-img-srcset + * + * @par Example: + * @code + * Html::srcSet( array( + * '1x' => 'standard.jpeg', + * '1.5x' => 'large.jpeg', + * '3x' => 'extra-large.jpeg', + * ) ); + * // gives 'standard.jpeg 1x, large.jpeg 1.5x, extra-large.jpeg 2x' + * @endcode * * @param string[] $urls * @return string @@ -1035,9 +1050,8 @@ class Html { static function srcSet( $urls ) { $candidates = array(); foreach ( $urls as $density => $url ) { - // Image candidate syntax per current whatwg live spec, 2012-09-23: - // http://www.whatwg.org/html/embedded-content-1.html#attr-img-srcset - $candidates[] = "{$url} {$density}x"; + // Cast density to float to strip 'x'. + $candidates[] = $url . ' ' . (float)$density . 'x'; } return implode( ", ", $candidates ); } diff --git a/tests/phpunit/includes/HtmlTest.php b/tests/phpunit/includes/HtmlTest.php index 0b58536619..c5797c4f60 100644 --- a/tests/phpunit/includes/HtmlTest.php +++ b/tests/phpunit/includes/HtmlTest.php @@ -764,6 +764,30 @@ class HtmlTest extends MediaWikiTestCase { 'Label wrapper' ); } + + public static function provideSrcSetImages() { + return array( + array( array(), '', 'when there are no images, return empty string' ), + array( + array( '1x' => '1x.png', '1.5x' => '1_5x.png', '2x' => '2x.png' ), + '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x', + 'pixel depth keys may include a trailing "x"' + ), + array( + array( '1' => '1x.png', '1.5' => '1_5x.png', '2' => '2x.png' ), + '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x', + 'pixel depth keys may omit a trailing "x"' + ), + ); + } + + /** + * @dataProvider provideSrcSetImages + * @covers Html::srcSet + */ + public function testSrcSet( $images, $expected, $message ) { + $this->assertEquals( Html::srcSet( $images ), $expected, $message ); + } } class HtmlTestValue { -- 2.20.1