aboutsummaryrefslogtreecommitdiff
path: root/vendors/lessphp/tests/sort.php
diff options
context:
space:
mode:
authorSilvio Rhatto <rhatto@riseup.net>2014-03-15 14:42:42 -0300
committerSilvio Rhatto <rhatto@riseup.net>2014-03-15 14:42:42 -0300
commit551497ef94f6239b109316d8fab32f0909b13f73 (patch)
tree236615e9881988ce05f40583ff891bf0fcef2c14 /vendors/lessphp/tests/sort.php
downloadelgg-551497ef94f6239b109316d8fab32f0909b13f73.tar.gz
elgg-551497ef94f6239b109316d8fab32f0909b13f73.tar.bz2
Squashed 'mod/less/' content from commit 380edad
git-subtree-dir: mod/less git-subtree-split: 380edadb3a5e524a8e6ef6df063b678f2f00516f
Diffstat (limited to 'vendors/lessphp/tests/sort.php')
-rw-r--r--vendors/lessphp/tests/sort.php57
1 files changed, 57 insertions, 0 deletions
diff --git a/vendors/lessphp/tests/sort.php b/vendors/lessphp/tests/sort.php
new file mode 100644
index 000000000..38662cb24
--- /dev/null
+++ b/vendors/lessphp/tests/sort.php
@@ -0,0 +1,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));
+