File indexing completed on 2024-12-22 05:36:22

0001 <?php
0002 error_reporting(E_ALL);
0003 
0004 require realpath(dirname(__FILE__)).'/../lessc.inc.php';
0005 
0006 // sorts the selectors in stylesheet in order to normalize it for comparison
0007 
0008 $exe = array_shift($argv); // remove filename
0009 
0010 if (!$fname = array_shift($argv)) {
0011   $fname = "php://stdin";
0012 }
0013 
0014 class lesscNormalized extends lessc {
0015   public $numberPrecision = 3;
0016 
0017   public function compileValue($value) {
0018     if ($value[0] == "raw_color") {
0019       $value = $this->coerceColor($value);
0020     }
0021 
0022     return parent::compileValue($value);
0023   }
0024 }
0025 
0026 class SortingFormatter extends lessc_formatter_lessjs {
0027   function sortKey($block) {
0028     if (!isset($block->sortKey)) {
0029       sort($block->selectors, SORT_STRING);
0030       $block->sortKey = implode(",", $block->selectors);
0031     }
0032 
0033     return $block->sortKey;
0034   }
0035 
0036   function sortBlock($block) {
0037     usort($block->children, function($a, $b) {
0038       $sort = strcmp($this->sortKey($a), $this->sortKey($b));
0039       if ($sort == 0) {
0040         // TODO
0041       }
0042       return $sort;
0043     });
0044 
0045   }
0046 
0047   function block($block) {
0048     $this->sortBlock($block);
0049     return parent::block($block);
0050   }
0051 
0052 }
0053 
0054 $less = new lesscNormalized();
0055 $less->setFormatter(new SortingFormatter);
0056 echo $less->parse(file_get_contents($fname));
0057