Update lessphp to b7cd5c79e8
authorOri Livneh <ori@wikimedia.org>
Thu, 6 Mar 2014 04:53:03 +0000 (20:53 -0800)
committerKrinkle <krinklemail@gmail.com>
Fri, 7 Mar 2014 00:27:37 +0000 (00:27 +0000)
Source:
* https://github.com/leafo/lessphp/blob/b7cd5c79e8/lessc.inc.php
* https://github.com/leafo/lessphp/compare/261f1bd2...b7cd5c79e8

Updates:
 * 1d62556008: CS fixes
 * d3fd5cec9e: use the built-in php magic database instead of the system one
 * 34fdf60f32: data-uri: add tests and use lessc::findImport to locate the file
 * 72b25725d9: implement data-uri([mimetype,] url) from less.js 1.4.0
 * 5cf7eb272e: Update lessc.inc.php
 * 7e64e41a59: Fix color contrast and threshold calculation
 * 69adc23a92: Use property_exist to avoid "Undefined property: stdClass::$parent"

Change-Id: Ie626b4f1117b44669bd2c6c43a0186d95b36435a

includes/libs/lessc.inc.php

index 3dce961..57d45ed 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 
 /**
- * lessphp v0.4.0@261f1bd28f
+ * lessphp v0.4.0@b7cd5c79e8
  * http://leafo.net/lessphp
  *
  * LESS CSS compiler, adapted from http://lesscss.org
@@ -1011,6 +1011,39 @@ class lessc {
                return $this->lib_rgbahex($color);
        }
 
+       /**
+        * Given an url, decide whether to output a regular link or the base64-encoded contents of the file
+        *
+        * @param  array  $value either an argument list (two strings) or a single string
+        * @return string        formatted url(), either as a link or base64-encoded
+        */
+       protected function lib_data_uri($value) {
+               $mime = ($value[0] === 'list') ? $value[2][0][2] : null;
+               $url = ($value[0] === 'list') ? $value[2][1][2][0] : $value[2][0];
+
+               $fullpath = $this->findImport($url);
+
+               if($fullpath && ($fsize = filesize($fullpath)) !== false) {
+                       // IE8 can't handle data uris larger than 32KB
+                       if($fsize/1024 < 32) {
+                               if(is_null($mime)) {
+                                       if(class_exists('finfo')) { // php 5.3+
+                                               $finfo = new finfo(FILEINFO_MIME);
+                                               $mime = explode('; ', $finfo->file($fullpath));
+                                               $mime = $mime[0];
+                                       } elseif(function_exists('mime_content_type')) { // PHP 5.2
+                                               $mime = mime_content_type($fullpath);
+                                       }
+                               }
+
+                               if(!is_null($mime)) // fallback if the mime type is still unknown
+                                       $url = sprintf('data:%s;base64,%s', $mime, base64_encode(file_get_contents($fullpath)));
+                       }
+               }
+
+               return 'url("'.$url.'")';
+       }
+
        // utility func to unquote a string
        protected function lib_e($arg) {
                switch ($arg[0]) {
@@ -1234,24 +1267,44 @@ class lessc {
        }
 
        protected function lib_contrast($args) {
-               if ($args[0] != 'list' || count($args[2]) < 3) {
-                       return array(array('color', 0, 0, 0), 0);
-               }
+           $darkColor  = array('color', 0, 0, 0);
+           $lightColor = array('color', 255, 255, 255);
+           $threshold  = 0.43;
 
-               list($inputColor, $darkColor, $lightColor) = $args[2];
+           if ( $args[0] == 'list' ) {
+               $inputColor = ( isset($args[2][0]) ) ? $this->assertColor($args[2][0])  : $lightColor;
+               $darkColor  = ( isset($args[2][1]) ) ? $this->assertColor($args[2][1])  : $darkColor;
+               $lightColor = ( isset($args[2][2]) ) ? $this->assertColor($args[2][2])  : $lightColor;
+               $threshold  = ( isset($args[2][3]) ) ? $this->assertNumber($args[2][3]) : $threshold;
+           }
+           else {
+               $inputColor  = $this->assertColor($args);
+           }
 
-               $inputColor = $this->assertColor($inputColor);
-               $darkColor = $this->assertColor($darkColor);
-               $lightColor = $this->assertColor($lightColor);
-               $hsl = $this->toHSL($inputColor);
+           $inputColor = $this->coerceColor($inputColor);
+           $darkColor  = $this->coerceColor($darkColor);
+           $lightColor = $this->coerceColor($lightColor);
 
-               if ($hsl[3] > 50) {
-                       return $darkColor;
-               }
+           //Figure out which is actually light and dark!
+           if ( $this->lib_luma($darkColor) > $this->lib_luma($lightColor) ) {
+               $t  = $lightColor;
+               $lightColor = $darkColor;
+               $darkColor  = $t;
+           }
 
-               return $lightColor;
+           $inputColor_alpha = $this->lib_alpha($inputColor);
+           if ( ( $this->lib_luma($inputColor) * $inputColor_alpha) < $threshold) {
+               return $lightColor;
+           }
+           return $darkColor;
        }
 
+       protected function lib_luma($color) {
+           $color = $this->coerceColor($color);
+           return (0.2126 * $color[0] / 255) + (0.7152 * $color[1] / 255) + (0.0722 * $color[2] / 255);
+       }
+
+
        public function assertColor($value, $error = "expected color value") {
                $color = $this->coerceColor($value);
                if (is_null($color)) $this->throwError($error);
@@ -1475,8 +1528,9 @@ class lessc {
 
                        list(, $name, $args) = $value;
                        if ($name == "%") $name = "_sprintf";
+
                        $f = isset($this->libFunctions[$name]) ?
-                               $this->libFunctions[$name] : array($this, 'lib_'.$name);
+                               $this->libFunctions[$name] : array($this, 'lib_'.str_replace('-', '_', $name));
 
                        if (is_callable($f)) {
                                if ($args[0] == 'list')
@@ -2338,7 +2392,7 @@ class lessc_parser {
                        $this->throwError();
 
                // TODO report where the block was opened
-               if (!is_null($this->env->parent))
+               if ( !property_exists($this->env, 'parent') || !is_null($this->env->parent) )
                        throw new exception('parse error: unclosed block');
 
                return $this->env;