From 8c32946ca1f54e98a138cc4b4bbce4ff69d10f3b Mon Sep 17 00:00:00 2001 From: Matthias Mullie Date: Mon, 18 Jul 2016 14:52:08 +0200 Subject: [PATCH] Exclude duplicate srcset urls Bug: T135550 Change-Id: I956dc155426739d60052a0dc77dafdf0414d5bd7 --- includes/Html.php | 16 ++++++++++++++-- includes/media/MediaTransformOutput.php | 6 ++++-- tests/parser/parserTests.txt | 14 ++++++++++++++ tests/phpunit/includes/HtmlTest.php | 10 ++++++++++ 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/includes/Html.php b/includes/Html.php index e5128d166c..a5567fc043 100644 --- a/includes/Html.php +++ b/includes/Html.php @@ -1020,9 +1020,21 @@ class Html { static function srcSet( array $urls ) { $candidates = []; foreach ( $urls as $density => $url ) { - // Cast density to float to strip 'x'. - $candidates[] = $url . ' ' . (float)$density . 'x'; + // Cast density to float to strip 'x', then back to string to serve + // as array index. + $density = (string)(float)$density; + $candidates[$density] = $url; } + + // Remove duplicates that are the same as a smaller value + ksort( $candidates, SORT_NUMERIC ); + $candidates = array_unique( $candidates ); + + // Append density info to the url + foreach ( $candidates as $density => $url ) { + $candidates[$density] = $url . ' ' . $density . 'x'; + } + return implode( ", ", $candidates ); } } diff --git a/includes/media/MediaTransformOutput.php b/includes/media/MediaTransformOutput.php index 9176b54666..b3a555aa5c 100644 --- a/includes/media/MediaTransformOutput.php +++ b/includes/media/MediaTransformOutput.php @@ -421,8 +421,10 @@ class ThumbnailImage extends MediaTransformOutput { } // Additional densities for responsive images, if specified. - if ( !empty( $this->responsiveUrls ) ) { - $attribs['srcset'] = Html::srcSet( $this->responsiveUrls ); + // If any of these urls is the same as src url, it'll be excluded. + $responsiveUrls = array_diff( $this->responsiveUrls, [ $this->url ] ); + if ( !empty( $responsiveUrls ) ) { + $attribs['srcset'] = Html::srcSet( $responsiveUrls ); } Hooks::run( 'ThumbnailBeforeProduceHTML', [ $this, &$attribs, &$linkAttribs ] ); diff --git a/tests/parser/parserTests.txt b/tests/parser/parserTests.txt index dd506074bc..4c853b11b4 100644 --- a/tests/parser/parserTests.txt +++ b/tests/parser/parserTests.txt @@ -27080,3 +27080,17 @@ Empty LI (T49673)
  • b
  • !! end + +!! test +Thumbnail output +!! wikitext +[[File:Thumb.png|thumb]] +!! html/php+tidy +
    +
    Thumb.png +
    +
    +
    +
    +
    +!! end diff --git a/tests/phpunit/includes/HtmlTest.php b/tests/phpunit/includes/HtmlTest.php index 47217939bf..e44db83b44 100644 --- a/tests/phpunit/includes/HtmlTest.php +++ b/tests/phpunit/includes/HtmlTest.php @@ -738,6 +738,16 @@ class HtmlTest extends MediaWikiTestCase { '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x', 'pixel depth keys may omit a trailing "x"' ], + [ + [ '1' => 'small.png', '1.5' => 'large.png', '2' => 'large.png' ], + 'small.png 1x, large.png 1.5x', + 'omit larger duplicates' + ], + [ + [ '1' => 'small.png', '2' => 'large.png', '1.5' => 'large.png' ], + 'small.png 1x, large.png 1.5x', + 'omit larger duplicates in irregular order' + ], ]; } -- 2.20.1