aboutsummaryrefslogtreecommitdiff
path: root/mod/less/vendors/lessphp/tests/sort.php
blob: 38662cb248d5cc862c93da4da6215af134551022 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
error_reporting(E_ALL);

require realpath(dirname(__FILE__)).'/../lessc.inc.php';

// sorts the selectors in stylesheet in order to normalize it for comparison

$exe = array_shift($argv); // remove filename

if (!$fname = array_shift($argv)) {
	$fname = "php://stdin";
}

// also sorts the tags in the block
function sort_key($block) {
	if (!isset($block->sort_key)) {
		sort($block->tags, SORT_STRING);
		$block->sort_key = implode(",", $block->tags);
	}

	return $block->sort_key;
}

class sort_css extends lessc {
	function __construct() {
		parent::__construct();
	}

	// normalize numbers
	function compileValue($value) {
		$ignore = array('list', 'keyword', 'string', 'color', 'function');
		if ($value[0] == 'number' || !in_array($value[0], $ignore)) {
			$value[1] = $value[1] + 0; // convert to either double or int
		}

		return parent::compileValue($value);
	}

	function parse_and_sort($str) {
		$root = $this->parseTree($str);

		$less = $this;
		usort($root->props, function($a, $b) use ($less) {

			$sort = strcmp(sort_key($a[1]), sort_key($b[1]));
			if ($sort == 0)
				return strcmp($less->compileBlock($a[1]), $less->compileBlock($b[1]));
			return $sort;
		});

		return $this->compileBlock($root);
	}
}

$sorter = new sort_css;
echo $sorter->parse_and_sort(file_get_contents($fname));