Ori.livneh has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/84726


Change subject: Make LESS embed() actually embed images
......................................................................

Make LESS embed() actually embed images

I intend to abandon this patch and rebase it onto Id052a04dd once it's ready.
Looking to get a bit of feedback first without generating too much noise on the
original change.

Change-Id: I9bddc0667a75500a1a6d9f085aae0acf5b8c9a0d
---
M includes/DefaultSettings.php
A tests/phpunit/data/less/question.png
M tests/phpunit/data/less/styles.less
3 files changed, 19 insertions(+), 11 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/26/84726/1

diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php
index 4b4f4b2..ae1fae3 100644
--- a/includes/DefaultSettings.php
+++ b/includes/DefaultSettings.php
@@ -3306,11 +3306,7 @@
  */
 $wgResourceLoaderLESSFunctions = array(
        /**
-        * Mark an image URI as embeddable. Expands a URL argument to a CSS
-        * URL value with a a CSSMin @embed annotation prepended. This
-        * exploits the fact that the return value of LESS functions is not
-        * subject to further transformations by the LESS compiler to ensure
-        * @embed comments are not munged or reordered.
+        * Convert an image URI to a base64-encoded data URI.
         *
         * @par Example:
         * @code
@@ -3318,12 +3314,24 @@
         *       background-image: embed('../images/button-bg.png');
         *   }
         * @endcode
-        *
-        * @todo: 'embed' should invoke CSSMin directly to generate data
-        * URIs rather than generate comment directives.
         */
-       'embed' => function( $arg ) {
-               return '/* @embed */ url(' . $arg[2][0] . ')';
+       'embed' => function( $frame, $less ) {
+               // The last import path is always the path of the current file.
+               $base = $less->importDir[ count( $less->importDir ) - 1 ];
+
+               $url = $frame[2][0];
+               $pre = isset( $frame[2][1] ) ? ' ' . $frame[2][1] : '';
+               $post = isset( $frame[2][2] ) ? $frame[2][2] . ' ' : '';
+               $file = $base . $url;
+
+               if ( file_exists( $file ) ) {
+                       $type = CSSMin::getMimeType( $file );
+                       if ( $type && filesize( $file ) < 
CSSMin::EMBED_SIZE_LIMIT ) {
+                               $data = base64_encode( file_get_contents( $file 
) );
+                               return 
"{$pre}url(data:{$type};base64,{$data}){$post};{$pre}url({$url}){$post}!ie;";
+                       }
+               }
+               return "{$pre}url({$url}){$post};";
        },
 );
 
diff --git a/tests/phpunit/data/less/question.png 
b/tests/phpunit/data/less/question.png
new file mode 100644
index 0000000..590f0cb
--- /dev/null
+++ b/tests/phpunit/data/less/question.png
Binary files differ
diff --git a/tests/phpunit/data/less/styles.less 
b/tests/phpunit/data/less/styles.less
index e8a481a..ee01eac 100644
--- a/tests/phpunit/data/less/styles.less
+++ b/tests/phpunit/data/less/styles.less
@@ -3,5 +3,5 @@
 /* @noflip */
 .unit-tests {
        color: @unitTestColor;
-       background-image: embed('background.png');
+       background-image: embed('question.png');
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/84726
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I9bddc0667a75500a1a6d9f085aae0acf5b8c9a0d
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Ori.livneh <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to