From 8faba225fefd729836161701be4dbafec3f43a22 Mon Sep 17 00:00:00 2001 From: jdlrobson Date: Mon, 25 Jun 2018 15:55:19 -0700 Subject: [PATCH] ResourceLoaderImage module definitions can define a defaultColor Bug: T197909 Change-Id: I0745e112d11026ed59d8daca990b313305cd094a --- docs/extension.schema.v1.json | 3 +++ docs/extension.schema.v2.json | 3 +++ includes/resourceloader/ResourceLoaderImage.php | 11 +++++++++-- includes/resourceloader/ResourceLoaderImageModule.php | 5 ++++- 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/docs/extension.schema.v1.json b/docs/extension.schema.v1.json index bcfd2aa3d5..e7e09750e4 100644 --- a/docs/extension.schema.v1.json +++ b/docs/extension.schema.v1.json @@ -285,6 +285,9 @@ "class": { "enum": ["ResourceLoaderImageModule"] }, + "defaultColor": { + "type": "string" + }, "data": { "type": "string" }, diff --git a/docs/extension.schema.v2.json b/docs/extension.schema.v2.json index 31edbd09dd..24bfb63f82 100644 --- a/docs/extension.schema.v2.json +++ b/docs/extension.schema.v2.json @@ -288,6 +288,9 @@ "class": { "enum": ["ResourceLoaderImageModule"] }, + "defaultColor": { + "type": "string" + }, "data": { "type": "string" }, diff --git a/includes/resourceloader/ResourceLoaderImage.php b/includes/resourceloader/ResourceLoaderImage.php index d38a175008..0adbd0cb38 100644 --- a/includes/resourceloader/ResourceLoaderImage.php +++ b/includes/resourceloader/ResourceLoaderImage.php @@ -44,14 +44,18 @@ class ResourceLoaderImage { * @param string|array $descriptor Path to image file, or array structure containing paths * @param string $basePath Directory to which paths in descriptor refer * @param array $variants + * @param string|null $defaultColor of the base variant * @throws InvalidArgumentException */ - public function __construct( $name, $module, $descriptor, $basePath, $variants ) { + public function __construct( $name, $module, $descriptor, $basePath, $variants, + $defaultColor = null + ) { $this->name = $name; $this->module = $module; $this->descriptor = $descriptor; $this->basePath = $basePath; $this->variants = $variants; + $this->defaultColor = $defaultColor; // Expand shorthands: // [ "en,de,fr" => "foo.svg" ] @@ -250,7 +254,10 @@ class ResourceLoaderImage { if ( $variant && isset( $this->variants[$variant] ) ) { $data = $this->variantize( $this->variants[$variant], $context ); } else { - $data = file_get_contents( $path ); + $defaultColor = $this->defaultColor; + $data = $defaultColor ? + $this->variantize( [ 'color' => $defaultColor ], $context ) : + file_get_contents( $path ); } if ( $format === 'rasterized' ) { diff --git a/includes/resourceloader/ResourceLoaderImageModule.php b/includes/resourceloader/ResourceLoaderImageModule.php index 26d5e98212..c7792400c4 100644 --- a/includes/resourceloader/ResourceLoaderImageModule.php +++ b/includes/resourceloader/ResourceLoaderImageModule.php @@ -39,6 +39,7 @@ class ResourceLoaderImageModule extends ResourceLoaderModule { protected $origin = self::ORIGIN_CORE_SITEWIDE; protected $images = []; + protected $defaultColor = null; protected $variants = []; protected $prefix = null; protected $selectorWithoutVariant = '.{prefix}-{name}'; @@ -182,6 +183,7 @@ class ResourceLoaderImageModule extends ResourceLoaderModule { $this->{$member} = $option; break; + case 'defaultColor': case 'prefix': case 'selectorWithoutVariant': case 'selectorWithVariant': @@ -264,7 +266,8 @@ class ResourceLoaderImageModule extends ResourceLoaderModule { $this->getName(), $fileDescriptor, $this->localBasePath, - $variantConfig + $variantConfig, + $this->defaultColor ); $this->imageObjects[$skin][$image->getName()] = $image; } -- 2.20.1