ResourceLoaderImage module definitions can define a defaultColor
authorjdlrobson <jdlrobson@gmail.com>
Mon, 25 Jun 2018 22:55:19 +0000 (15:55 -0700)
committerJdlrobson <jrobson@wikimedia.org>
Wed, 27 Jun 2018 20:36:47 +0000 (20:36 +0000)
Bug: T197909
Change-Id: I0745e112d11026ed59d8daca990b313305cd094a

docs/extension.schema.v1.json
docs/extension.schema.v2.json
includes/resourceloader/ResourceLoaderImage.php
includes/resourceloader/ResourceLoaderImageModule.php

index bcfd2aa..e7e0975 100644 (file)
                                                                "class": {
                                                                        "enum": ["ResourceLoaderImageModule"]
                                                                },
+                                                               "defaultColor": {
+                                                                       "type": "string"
+                                                               },
                                                                "data": {
                                                                        "type": "string"
                                                                },
index 31edbd0..24bfb63 100644 (file)
                                                                "class": {
                                                                        "enum": ["ResourceLoaderImageModule"]
                                                                },
+                                                               "defaultColor": {
+                                                                       "type": "string"
+                                                               },
                                                                "data": {
                                                                        "type": "string"
                                                                },
index d38a175..0adbd0c 100644 (file)
@@ -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' ) {
index 26d5e98..c779240 100644 (file)
@@ -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;
                        }