From f4329ddbc8611706e2247ea334dbff4396ba651e Mon Sep 17 00:00:00 2001 From: WMDE-Fisch Date: Wed, 15 Mar 2017 20:09:17 +0100 Subject: [PATCH] resourceloader: Use SVG url when ResourceLoaderImageModule can't embed When the SVG is too big to be embeded it is now included via URL. Previously it would produce an empty/broken 'url()' value. Bug: T160532 Change-Id: I158781f9430cfa35737397ac7537a471634c4480 --- .../ResourceLoaderImageModule.php | 34 +++++++---- .../ResourceLoaderImageModuleTest.php | 56 +++++++++++++++++++ 2 files changed, 80 insertions(+), 10 deletions(-) diff --git a/includes/resourceloader/ResourceLoaderImageModule.php b/includes/resourceloader/ResourceLoaderImageModule.php index ff1b7b1a88..1a3463f31a 100644 --- a/includes/resourceloader/ResourceLoaderImageModule.php +++ b/includes/resourceloader/ResourceLoaderImageModule.php @@ -315,11 +315,7 @@ class ResourceLoaderImageModule extends ResourceLoaderModule { $selectors = $this->getSelectors(); foreach ( $this->getImages( $context ) as $name => $image ) { - $declarations = $this->getCssDeclarations( - $image->getDataUri( $context, null, 'original' ), - $image->getUrl( $context, $script, null, 'rasterized' ) - ); - $declarations = implode( "\n\t", $declarations ); + $declarations = $this->getStyleDeclarations( $context, $image, $script ); $selector = strtr( $selectors['selectorWithoutVariant'], [ @@ -331,11 +327,7 @@ class ResourceLoaderImageModule extends ResourceLoaderModule { $rules[] = "$selector {\n\t$declarations\n}"; foreach ( $image->getVariants() as $variant ) { - $declarations = $this->getCssDeclarations( - $image->getDataUri( $context, $variant, 'original' ), - $image->getUrl( $context, $script, $variant, 'rasterized' ) - ); - $declarations = implode( "\n\t", $declarations ); + $declarations = $this->getStyleDeclarations( $context, $image, $script, $variant ); $selector = strtr( $selectors['selectorWithVariant'], [ @@ -352,6 +344,28 @@ class ResourceLoaderImageModule extends ResourceLoaderModule { return [ 'all' => $style ]; } + /** + * @param ResourceLoaderContext $context + * @param ResourceLoaderImage $image Image to get the style for + * @param string $script URL to load.php + * @param string|null $variant Variant to get the style for + * @return string + */ + private function getStyleDeclarations( + ResourceLoaderContext $context, + ResourceLoaderImage $image, + $script, + $variant = null + ) { + $imageDataUri = $image->getDataUri( $context, $variant, 'original' ); + $primaryUrl = $imageDataUri ?: $image->getUrl( $context, $script, $variant, 'original' ); + $declarations = $this->getCssDeclarations( + $primaryUrl, + $image->getUrl( $context, $script, $variant, 'rasterized' ) + ); + return implode( "\n\t", $declarations ); + } + /** * SVG support using a transparent gradient to guarantee cross-browser * compatibility (browsers able to understand gradient syntax support also SVG). diff --git a/tests/phpunit/includes/resourceloader/ResourceLoaderImageModuleTest.php b/tests/phpunit/includes/resourceloader/ResourceLoaderImageModuleTest.php index aeb82d14ff..a1acea6079 100644 --- a/tests/phpunit/includes/resourceloader/ResourceLoaderImageModuleTest.php +++ b/tests/phpunit/includes/resourceloader/ResourceLoaderImageModuleTest.php @@ -197,6 +197,62 @@ class ResourceLoaderImageModuleTest extends ResourceLoaderTestCase { ] ) ); $this->assertInstanceOf( ResourceLoaderImage::class, $context->getImageObj() ); } + + public static function providerGetStyleDeclarations() { + return [ + [ + false, +<<getResourceLoaderContext(); + $image = $this->getImageMock( $context, $dataUriReturnValue ); + + $styles = $module->getStyleDeclarations( + $context, + $image, + 'load.php' + ); + + $this->assertEquals( $expected, $styles ); + } + + private function getImageMock( ResourceLoaderContext $context, $dataUriReturnValue ) { + $image = $this->getMockBuilder( 'ResourceLoaderImage' ) + ->disableOriginalConstructor() + ->getMock(); + $image->method( 'getDataUri' ) + ->will( $this->returnValue( $dataUriReturnValue ) ); + $image->expects( $this->any() ) + ->method( 'getUrl' ) + ->will( $this->returnValueMap( [ + [ $context, 'load.php', null, 'original', 'original.svg' ], + [ $context, 'load.php', null, 'rasterized', 'rasterized.png' ], + ] ) ); + + return $image; + } } class ResourceLoaderImageModuleTestable extends ResourceLoaderImageModule { -- 2.20.1