5 * http://leafo.net/lessphp
7 * LESS CSS compiler, adapted from http://lesscss.org
9 * Copyright 2013, Leaf Corcoran <leafot@gmail.com>
10 * Copyright 2016, Marcus Schwarz <github@maswaba.de>
11 * Licensed under MIT or GPLv3, see LICENSE
16 * The LESS compiler and parser.
18 * Converting LESS to CSS is a three stage process. The incoming file is parsed
19 * by `lessc_parser` into a syntax tree, then it is compiled into another tree
20 * representing the CSS structure by `lessc`. The CSS tree is fed into a
21 * formatter, like `lessc_formatter` which then outputs CSS as a string.
23 * During the first compile, all values are *reduced*, which means that their
24 * types are brought to the lowest form before being dump as strings. This
25 * handles math equations, variable dereferences, and the like.
27 * The `parse` function of `lessc` is the entry point.
31 * The `lessc` class creates an instance of the parser, feeds it LESS code,
32 * then transforms the resulting tree to a CSS tree. This class also holds the
33 * evaluation context, such as all available mixins and variables at any given
36 * The `lessc_parser` class is only concerned with parsing its input.
38 * The `lessc_formatter` takes a CSS tree, and dumps it to a formatted string,
39 * handling things like indentation.
42 static public $VERSION = "v0.6.0";
44 static public $TRUE = array("keyword", "true");
45 static public $FALSE = array("keyword", "false");
47 protected $libFunctions = array();
48 protected $registeredVars = array();
49 protected $preserveComments = false;
51 public $vPrefix = '@'; // prefix of abstract properties
52 public $mPrefix = '$'; // prefix of abstract blocks
53 public $parentSelector = '&';
55 static public $lengths = array( "px", "m", "cm", "mm", "in", "pt", "pc" );
56 static public $times = array( "s", "ms" );
57 static public $angles = array( "rad", "deg", "grad", "turn" );
59 static public $lengths_to_base = array( 1, 3779.52755906, 37.79527559, 3.77952756, 96, 1.33333333, 16 );
60 public $importDisabled = false;
61 public $importDir = array();
63 protected $numberPrecision = null;
65 protected $allParsedFiles = array();
67 // set to the parser that generated the current line when compiling
68 // so we know how to create error messages
69 protected $sourceParser = null;
70 protected $sourceLoc = null;
72 static protected $nextImportId = 0; // uniquely identify imports
74 // attempts to find the path of an import url, returns null for css files
75 protected function findImport($url) {
76 foreach ((array)$this->importDir
as $dir) {
77 $full = $dir.(substr($dir, -1) != '/' ?
'/' : '').$url;
78 if ($this->fileExists($file = $full.'.less') ||
$this->fileExists($file = $full)) {
86 protected function fileExists($name) {
87 return is_file($name);
90 static public function compressList($items, $delim) {
91 if (!isset($items[1]) && isset($items[0])) return $items[0];
92 else return array('list', $delim, $items);
95 static public function preg_quote($what) {
96 return preg_quote($what, '/');
99 protected function tryImport($importPath, $parentBlock, $out) {
100 if ($importPath[0] == "function" && $importPath[1] == "url") {
101 $importPath = $this->flattenList($importPath[2]);
104 $str = $this->coerceString($importPath);
105 if ($str === null) return false;
107 $url = $this->compileValue($this->lib_e($str));
109 // don't import if it ends in css
110 if (substr_compare($url, '.css', -4, 4) === 0) return false;
112 $realPath = $this->findImport($url);
114 if ($realPath === null) return false;
116 if ($this->importDisabled
) {
117 return array(false, "/* import disabled */");
120 if (isset($this->allParsedFiles
[realpath($realPath)])) {
121 return array(false, null);
124 $this->addParsedFile($realPath);
125 $parser = $this->makeParser($realPath);
126 $root = $parser->parse(file_get_contents($realPath));
128 // set the parents of all the block props
129 foreach ($root->props
as $prop) {
130 if ($prop[0] == "block") {
131 $prop[1]->parent
= $parentBlock;
135 // copy mixins into scope, set their parents
136 // bring blocks from import into current block
137 // TODO: need to mark the source parser these came from this file
138 foreach ($root->children
as $childName => $child) {
139 if (isset($parentBlock->children
[$childName])) {
140 $parentBlock->children
[$childName] = array_merge(
141 $parentBlock->children
[$childName],
144 $parentBlock->children
[$childName] = $child;
148 $pi = pathinfo($realPath);
149 $dir = $pi["dirname"];
151 [$top, $bottom] = $this->sortProps($root->props
, true);
152 $this->compileImportedProps($top, $parentBlock, $out, $parser, $dir);
154 return array(true, $bottom, $parser, $dir);
157 protected function compileImportedProps($props, $block, $out, $sourceParser, $importDir) {
158 $oldSourceParser = $this->sourceParser
;
160 $oldImport = $this->importDir
;
162 // TODO: this is because the importDir api is stupid
163 $this->importDir
= (array)$this->importDir
;
164 array_unshift($this->importDir
, $importDir);
166 foreach ($props as $prop) {
167 $this->compileProp($prop, $block, $out);
170 $this->importDir
= $oldImport;
171 $this->sourceParser
= $oldSourceParser;
175 * Recursively compiles a block.
177 * A block is analogous to a CSS block in most cases. A single LESS document
178 * is encapsulated in a block when parsed, but it does not have parent tags
179 * so all of it's children appear on the root level when compiled.
181 * Blocks are made up of props and children.
183 * Props are property instructions, array tuples which describe an action
184 * to be taken, eg. write a property, set a variable, mixin a block.
186 * The children of a block are just all the blocks that are defined within.
187 * This is used to look up mixins when performing a mixin.
189 * Compiling the block involves pushing a fresh environment on the stack,
190 * and iterating through the props, compiling each one.
192 * See lessc::compileProp()
195 protected function compileBlock($block) {
196 switch ($block->type
) {
198 $this->compileRoot($block);
201 $this->compileCSSBlock($block);
204 $this->compileMedia($block);
207 $name = "@" . $block->name
;
208 if (!empty($block->value
)) {
209 $name .= " " . $this->compileValue($this->reduce($block->value
));
212 $this->compileNestedBlock($block, array($name));
215 $block->parser
->throwError("unknown block type: $block->type\n", $block->count
);
219 protected function compileCSSBlock($block) {
220 $env = $this->pushEnv();
222 $selectors = $this->compileSelectors($block->tags
);
223 $env->selectors
= $this->multiplySelectors($selectors);
224 $out = $this->makeOutputBlock(null, $env->selectors
);
226 $this->scope
->children
[] = $out;
227 $this->compileProps($block, $out);
229 $block->scope
= $env; // mixins carry scope with them!
233 protected function compileMedia($media) {
234 $env = $this->pushEnv($media);
235 $parentScope = $this->mediaParent($this->scope
);
237 $query = $this->compileMediaQuery($this->multiplyMedia($env));
239 $this->scope
= $this->makeOutputBlock($media->type
, array($query));
240 $parentScope->children
[] = $this->scope
;
242 $this->compileProps($media, $this->scope
);
244 if (count($this->scope
->lines
) > 0) {
245 $orphanSelelectors = $this->findClosestSelectors();
246 if (!is_null($orphanSelelectors)) {
247 $orphan = $this->makeOutputBlock(null, $orphanSelelectors);
248 $orphan->lines
= $this->scope
->lines
;
249 array_unshift($this->scope
->children
, $orphan);
250 $this->scope
->lines
= array();
254 $this->scope
= $this->scope
->parent
;
258 protected function mediaParent($scope) {
259 while (!empty($scope->parent
)) {
260 if (!empty($scope->type
) && $scope->type
!= "media") {
263 $scope = $scope->parent
;
269 protected function compileNestedBlock($block, $selectors) {
270 $this->pushEnv($block);
271 $this->scope
= $this->makeOutputBlock($block->type
, $selectors);
272 $this->scope
->parent
->children
[] = $this->scope
;
274 $this->compileProps($block, $this->scope
);
276 $this->scope
= $this->scope
->parent
;
280 protected function compileRoot($root) {
282 $this->scope
= $this->makeOutputBlock($root->type
);
283 $this->compileProps($root, $this->scope
);
287 protected function compileProps($block, $out) {
288 foreach ($this->sortProps($block->props
) as $prop) {
289 $this->compileProp($prop, $block, $out);
291 $out->lines
= $this->deduplicate($out->lines
);
295 * Deduplicate lines in a block. Comments are not deduplicated. If a
296 * duplicate rule is detected, the comments immediately preceding each
297 * occurence are consolidated.
299 protected function deduplicate($lines) {
303 foreach($lines as $line) {
304 if (strpos($line, '/*') === 0) {
308 if (!in_array($line, $unique)) {
311 array_splice($unique, array_search($line, $unique), 0, $comments);
314 return array_merge($unique, $comments);
317 protected function sortProps($props, $split = false) {
323 foreach ($props as $prop) {
330 if (isset($prop[1][0]) && $prop[1][0] == $this->vPrefix
) {
331 $vars = array_merge($vars, $stack);
333 $other = array_merge($other, $stack);
338 $id = self
::$nextImportId++
;
341 $imports = array_merge($imports, $stack);
342 $other[] = array("import_mixin", $id);
347 $other = array_merge($other, $stack);
352 $other = array_merge($other, $stack);
355 return array(array_merge($vars, $imports, $vars), $other);
357 return array_merge($vars, $imports, $vars, $other);
361 protected function compileMediaQuery($queries) {
362 $compiledQueries = array();
363 foreach ($queries as $query) {
365 foreach ($query as $q) {
368 $parts[] = implode(" ", array_slice($q, 1));
372 $parts[] = "($q[1]: " .
373 $this->compileValue($this->reduce($q[2])) . ")";
375 $parts[] = "($q[1])";
379 $parts[] = $this->compileValue($this->reduce($q));
384 if (count($parts) > 0) {
385 $compiledQueries[] = implode(" and ", $parts);
390 if (!empty($parts)) {
392 implode($this->formatter
->selectorSeparator
, $compiledQueries);
397 protected function multiplyMedia($env, $childQueries = null) {
399 !empty($env->block
->type
) && $env->block
->type
!= "media")
401 return $childQueries;
404 // plain old block, skip
405 if (empty($env->block
->type
)) {
406 return $this->multiplyMedia($env->parent
, $childQueries);
410 $queries = $env->block
->queries
;
411 if (is_null($childQueries)) {
414 foreach ($queries as $parent) {
415 foreach ($childQueries as $child) {
416 $out[] = array_merge($parent, $child);
421 return $this->multiplyMedia($env->parent
, $out);
424 protected function expandParentSelectors(&$tag, $replace) {
425 $parts = explode("$&$", $tag);
427 foreach ($parts as &$part) {
428 $part = str_replace($this->parentSelector
, $replace, $part, $c);
431 $tag = implode($this->parentSelector
, $parts);
435 protected function findClosestSelectors() {
438 while ($env !== null) {
439 if (isset($env->selectors
)) {
440 $selectors = $env->selectors
;
450 // multiply $selectors against the nearest selectors in env
451 protected function multiplySelectors($selectors) {
452 // find parent selectors
454 $parentSelectors = $this->findClosestSelectors();
455 if (is_null($parentSelectors)) {
456 // kill parent reference in top level selector
457 foreach ($selectors as &$s) {
458 $this->expandParentSelectors($s, "");
465 foreach ($parentSelectors as $parent) {
466 foreach ($selectors as $child) {
467 $count = $this->expandParentSelectors($child, $parent);
469 // don't prepend the parent tag if & was used
471 $out[] = trim($child);
473 $out[] = trim($parent . ' ' . $child);
481 // reduces selector expressions
482 protected function compileSelectors($selectors) {
485 foreach ($selectors as $s) {
488 $out[] = trim($this->compileValue($this->reduce($value)));
497 protected function eq($left, $right) {
498 return $left == $right;
501 protected function patternMatch($block, $orderedArgs, $keywordArgs) {
502 // match the guards if it has them
503 // any one of the groups must have all its guards pass for a match
504 if (!empty($block->guards
)) {
505 $groupPassed = false;
506 foreach ($block->guards
as $guardGroup) {
507 foreach ($guardGroup as $guard) {
509 $this->zipSetArgs($block->args
, $orderedArgs, $keywordArgs);
512 if ($guard[0] == "negate") {
517 $passed = $this->reduce($guard) == self
::$TRUE;
518 if ($negate) $passed = !$passed;
525 $groupPassed = false;
530 if ($groupPassed) break;
538 if (empty($block->args
)) {
539 return $block->isVararg ||
empty($orderedArgs) && empty($keywordArgs);
542 $remainingArgs = $block->args
;
544 $remainingArgs = array();
545 foreach ($block->args
as $arg) {
546 if ($arg[0] == "arg" && isset($keywordArgs[$arg[1]])) {
550 $remainingArgs[] = $arg;
555 // try to match by arity or by argument literal
556 foreach ($remainingArgs as $i => $arg) {
559 if (empty($orderedArgs[$i]) ||
!$this->eq($arg[1], $orderedArgs[$i])) {
564 // no arg and no default value
565 if (!isset($orderedArgs[$i]) && !isset($arg[2])) {
570 $i--; // rest can be empty
575 if ($block->isVararg
) {
576 return true; // not having enough is handled above
578 $numMatched = $i +
1;
579 // greater than because default values always match
580 return $numMatched >= count($orderedArgs);
584 protected function patternMatchAll($blocks, $orderedArgs, $keywordArgs, $skip=array()) {
586 foreach ($blocks as $block) {
587 // skip seen blocks that don't have arguments
588 if (isset($skip[$block->id
]) && !isset($block->args
)) {
592 if ($this->patternMatch($block, $orderedArgs, $keywordArgs)) {
600 // attempt to find blocks matched by path and args
601 protected function findBlocks($searchIn, $path, $orderedArgs, $keywordArgs, $seen=array()) {
602 if ($searchIn == null) return null;
603 if (isset($seen[$searchIn->id
])) return null;
604 $seen[$searchIn->id
] = true;
608 if (isset($searchIn->children
[$name])) {
609 $blocks = $searchIn->children
[$name];
610 if (count($path) == 1) {
611 $matches = $this->patternMatchAll($blocks, $orderedArgs, $keywordArgs, $seen);
612 if (!empty($matches)) {
613 // This will return all blocks that match in the closest
614 // scope that has any matching block, like lessjs
619 foreach ($blocks as $subBlock) {
620 $subMatches = $this->findBlocks($subBlock,
621 array_slice($path, 1), $orderedArgs, $keywordArgs, $seen);
623 if (!is_null($subMatches)) {
624 foreach ($subMatches as $sm) {
630 return count($matches) > 0 ?
$matches : null;
633 if ($searchIn->parent
=== $searchIn) return null;
634 return $this->findBlocks($searchIn->parent
, $path, $orderedArgs, $keywordArgs, $seen);
637 // sets all argument names in $args to either the default value
638 // or the one passed in through $values
639 protected function zipSetArgs($args, $orderedValues, $keywordValues) {
640 $assignedValues = array();
643 foreach ($args as $a) {
644 if ($a[0] == "arg") {
645 if (isset($keywordValues[$a[1]])) {
647 $value = $keywordValues[$a[1]];
648 } elseif (isset($orderedValues[$i])) {
650 $value = $orderedValues[$i];
652 } elseif (isset($a[2])) {
656 $this->throwError("Failed to assign arg " . $a[1]);
660 $value = $this->reduce($value);
661 $this->set($a[1], $value);
662 $assignedValues[] = $value;
671 if ($last !== false && $last[0] === "rest") {
672 $rest = array_slice($orderedValues, count($args) - 1);
673 $this->set($last[1], $this->reduce(array("list", " ", $rest)));
676 // wow is this the only true use of PHP's + operator for arrays?
677 $this->env
->arguments
= $assignedValues +
$orderedValues;
680 // compile a prop and update $lines or $blocks appropriately
681 protected function compileProp($prop, $block, $out) {
682 // set error position context
683 $this->sourceLoc
= isset($prop[-1]) ?
$prop[-1] : -1;
687 [, $name, $value] = $prop;
688 if ($name[0] == $this->vPrefix
) {
689 $this->set($name, $value);
691 $out->lines
[] = $this->formatter
->property($name,
692 $this->compileValue($this->reduce($value)));
697 $this->compileBlock($child);
701 [, $path, $args, $suffix] = $prop;
703 $orderedArgs = array();
704 $keywordArgs = array();
705 foreach ((array)$args as $arg) {
709 if (!isset($arg[2])) {
710 $orderedArgs[] = $this->reduce(array("variable", $arg[1]));
712 $keywordArgs[$arg[1]] = $this->reduce($arg[2]);
717 $orderedArgs[] = $this->reduce($arg[1]);
720 $this->throwError("Unknown arg type: " . $arg[0]);
724 $mixins = $this->findBlocks($block, $path, $orderedArgs, $keywordArgs);
726 if ($mixins === null) {
727 $block->parser
->throwError("{$prop[1][0]} is undefined", $block->count
);
730 if(strpos($prop[1][0], "$") === 0) {
731 //Use Ruleset Logic - Only last element
732 $mixins = array(array_pop($mixins));
735 foreach ($mixins as $mixin) {
736 if ($mixin === $block && !$orderedArgs) {
741 if (isset($mixin->parent
->scope
)) {
743 $mixinParentEnv = $this->pushEnv();
744 $mixinParentEnv->storeParent
= $mixin->parent
->scope
;
748 if (isset($mixin->args
)) {
751 $this->zipSetArgs($mixin->args
, $orderedArgs, $keywordArgs);
754 $oldParent = $mixin->parent
;
755 if ($mixin != $block) $mixin->parent
= $block;
757 foreach ($this->sortProps($mixin->props
) as $subProp) {
758 if ($suffix !== null &&
759 $subProp[0] == "assign" &&
760 is_string($subProp[1]) &&
761 $subProp[1][0] != $this->vPrefix
)
765 array($subProp[2], array('keyword', $suffix))
769 $this->compileProp($subProp, $mixin, $out);
772 $mixin->parent
= $oldParent;
774 if ($haveArgs) $this->popEnv();
775 if ($haveScope) $this->popEnv();
780 $out->lines
[] = $prop[1];
783 [, $name, $value] = $prop;
784 $out->lines
[] = "@$name " . $this->compileValue($this->reduce($value)).';';
787 $out->lines
[] = $prop[1];
790 [, $importPath, $importId] = $prop;
791 $importPath = $this->reduce($importPath);
793 if (!isset($this->env
->imports
)) {
794 $this->env
->imports
= array();
797 $result = $this->tryImport($importPath, $block, $out);
799 $this->env
->imports
[$importId] = $result === false ?
800 array(false, "@import " . $this->compileValue($importPath).";") :
805 [,$importId] = $prop;
806 $import = $this->env
->imports
[$importId];
807 if ($import[0] === false) {
808 if (isset($import[1])) {
809 $out->lines
[] = $import[1];
812 [, $bottom, $parser, $importDir] = $import;
813 $this->compileImportedProps($bottom, $block, $out, $parser, $importDir);
818 $block->parser
->throwError("unknown op: {$prop[0]}\n", $block->count
);
824 * Compiles a primitive value into a CSS property value.
826 * Values in lessphp are typed by being wrapped in arrays, their format is
829 * array(type, contents [, additional_contents]*)
831 * The input is expected to be reduced. This function will not work on
832 * things like expressions and variables.
834 public function compileValue($value) {
838 // [2] - array of values
839 return implode($value[1], array_map(array($this, 'compileValue'), $value[2]));
841 if (!empty($this->formatter
->compressColors
)) {
842 return $this->compileValue($this->coerceColor($value));
849 [, $num, $unit] = $value;
852 if ($this->numberPrecision
!== null) {
853 $num = round($num, $this->numberPrecision
);
857 // [1] - contents of string (includes quotes)
858 [, $delim, $content] = $value;
859 foreach ($content as &$part) {
860 if (is_array($part)) {
861 $part = $this->compileValue($part);
864 return $delim . implode($content) . $delim;
866 // [1] - red component (either number or a %)
867 // [2] - green component
868 // [3] - blue component
869 // [4] - optional alpha component
870 [, $r, $g, $b] = $value;
875 if (count($value) == 5 && $value[4] != 1) { // rgba
876 return 'rgba('.$r.','.$g.','.$b.','.$value[4].')';
879 $h = sprintf("#%02x%02x%02x", $r, $g, $b);
881 if (!empty($this->formatter
->compressColors
)) {
882 // Converting hex color to short notation (e.g. #003399 to #039)
883 if ($h[1] === $h[2] && $h[3] === $h[4] && $h[5] === $h[6]) {
884 $h = '#' . $h[1] . $h[3] . $h[5];
891 [, $name, $args] = $value;
892 return $name.'('.$this->compileValue($args).')';
893 default: // assumed to be unit
894 $this->throwError("unknown value type: $value[0]");
898 protected function lib_pow($args) {
899 [$base, $exp] = $this->assertArgs($args, 2, "pow");
900 return array( "number", pow($this->assertNumber($base), $this->assertNumber($exp)), $args[2][0][2] );
903 protected function lib_pi() {
907 protected function lib_mod($args) {
908 [$a, $b] = $this->assertArgs($args, 2, "mod");
909 return array( "number", $this->assertNumber($a) %
$this->assertNumber($b), $args[2][0][2] );
912 protected function lib_convert($args) {
913 [$value, $to] = $this->assertArgs($args, 2, "convert");
915 // If it's a keyword, grab the string version instead
916 if( is_array( $to ) && $to[0] == "keyword" )
919 return $this->convert( $value, $to );
922 protected function lib_abs($num) {
923 return array( "number", abs($this->assertNumber($num)), $num[2] );
926 protected function lib_min($args) {
927 $values = $this->assertMinArgs($args, 1, "min");
929 $first_format = $values[0][2];
932 $min_value = $values[0][1];
934 for( $a = 0; $a < sizeof( $values ); $a++
)
936 $converted = $this->convert( $values[$a], $first_format );
938 if( $converted[1] < $min_value )
941 $min_value = $values[$a][1];
945 return $values[ $min_index ];
948 protected function lib_max($args) {
949 $values = $this->assertMinArgs($args, 1, "max");
951 $first_format = $values[0][2];
954 $max_value = $values[0][1];
956 for( $a = 0; $a < sizeof( $values ); $a++
)
958 $converted = $this->convert( $values[$a], $first_format );
960 if( $converted[1] > $max_value )
963 $max_value = $values[$a][1];
967 return $values[ $max_index ];
970 protected function lib_tan($num) {
971 return tan($this->assertNumber($num));
974 protected function lib_sin($num) {
975 return sin($this->assertNumber($num));
978 protected function lib_cos($num) {
979 return cos($this->assertNumber($num));
982 protected function lib_atan($num) {
983 $num = atan($this->assertNumber($num));
984 return array("number", $num, "rad");
987 protected function lib_asin($num) {
988 $num = asin($this->assertNumber($num));
989 return array("number", $num, "rad");
992 protected function lib_acos($num) {
993 $num = acos($this->assertNumber($num));
994 return array("number", $num, "rad");
997 protected function lib_sqrt($num) {
998 return sqrt($this->assertNumber($num));
1001 protected function lib_extract($value) {
1002 [$list, $idx] = $this->assertArgs($value, 2, "extract");
1003 $idx = $this->assertNumber($idx);
1005 if ($list[0] == "list" && isset($list[2][$idx - 1])) {
1006 return $list[2][$idx - 1];
1010 protected function lib_isnumber($value) {
1011 return $this->toBool($value[0] == "number");
1014 protected function lib_isstring($value) {
1015 return $this->toBool($value[0] == "string");
1018 protected function lib_iscolor($value) {
1019 return $this->toBool($this->coerceColor($value));
1022 protected function lib_iskeyword($value) {
1023 return $this->toBool($value[0] == "keyword");
1026 protected function lib_ispixel($value) {
1027 return $this->toBool($value[0] == "number" && $value[2] == "px");
1030 protected function lib_ispercentage($value) {
1031 return $this->toBool($value[0] == "number" && $value[2] == "%");
1034 protected function lib_isem($value) {
1035 return $this->toBool($value[0] == "number" && $value[2] == "em");
1038 protected function lib_isrem($value) {
1039 return $this->toBool($value[0] == "number" && $value[2] == "rem");
1042 protected function lib_rgbahex($color) {
1043 $color = $this->coerceColor($color);
1044 if (is_null($color))
1045 $this->throwError("color expected for rgbahex");
1047 return sprintf("#%02x%02x%02x%02x",
1048 isset($color[4]) ?
$color[4]*255 : 255,
1049 $color[1],$color[2], $color[3]);
1052 protected function lib_argb($color){
1053 return $this->lib_rgbahex($color);
1057 * Given an url, decide whether to output a regular link or the base64-encoded contents of the file
1059 * @param array $value either an argument list (two strings) or a single string
1060 * @return string formatted url(), either as a link or base64-encoded
1062 protected function lib_data_uri($value) {
1063 $mime = ($value[0] === 'list') ?
$value[2][0][2] : null;
1064 $url = ($value[0] === 'list') ?
$value[2][1][2][0] : $value[2][0];
1066 $fullpath = $this->findImport($url);
1068 if($fullpath && ($fsize = filesize($fullpath)) !== false) {
1069 // IE8 can't handle data uris larger than 32KB
1070 if($fsize/1024 < 32) {
1071 if(is_null($mime)) {
1072 if(class_exists('finfo')) { // php 5.3+
1073 $finfo = new finfo(FILEINFO_MIME
);
1074 $mime = explode('; ', $finfo->file($fullpath));
1076 } elseif(function_exists('mime_content_type')) { // PHP 5.2
1077 $mime = mime_content_type($fullpath);
1081 if(!is_null($mime)) // fallback if the mime type is still unknown
1082 $url = sprintf('data:%s;base64,%s', $mime, base64_encode(file_get_contents($fullpath)));
1086 return 'url("'.$url.'")';
1089 // utility func to unquote a string
1090 protected function lib_e($arg) {
1094 if (isset($items[0])) {
1095 return $this->lib_e($items[0]);
1097 $this->throwError("unrecognised input");
1104 return array("keyword", $this->compileValue($arg));
1108 protected function lib__sprintf($args) {
1109 if ($args[0] != "list") return $args;
1111 $string = array_shift($values);
1112 $template = $this->compileValue($this->lib_e($string));
1115 if (preg_match_all('/%[dsa]/', $template, $m)) {
1116 foreach ($m[0] as $match) {
1117 $val = isset($values[$i]) ?
1118 $this->reduce($values[$i]) : array('keyword', '');
1120 // lessjs compat, renders fully expanded color, not raw color
1121 if ($color = $this->coerceColor($val)) {
1126 $rep = $this->compileValue($this->lib_e($val));
1127 $template = preg_replace('/'.self
::preg_quote($match).'/',
1128 $rep, $template, 1);
1132 $d = $string[0] == "string" ?
$string[1] : '"';
1133 return array("string", $d, array($template));
1136 protected function lib_floor($arg) {
1137 $value = $this->assertNumber($arg);
1138 return array("number", floor($value), $arg[2]);
1141 protected function lib_ceil($arg) {
1142 $value = $this->assertNumber($arg);
1143 return array("number", ceil($value), $arg[2]);
1146 protected function lib_round($arg) {
1147 if($arg[0] != "list") {
1148 $value = $this->assertNumber($arg);
1149 return array("number", round($value), $arg[2]);
1151 $value = $this->assertNumber($arg[2][0]);
1152 $precision = $this->assertNumber($arg[2][1]);
1153 return array("number", round($value, $precision), $arg[2][0][2]);
1157 protected function lib_unit($arg) {
1158 if ($arg[0] == "list") {
1159 [$number, $newUnit] = $arg[2];
1160 return array("number", $this->assertNumber($number),
1161 $this->compileValue($this->lib_e($newUnit)));
1163 return array("number", $this->assertNumber($arg), "");
1168 * Helper function to get arguments for color manipulation functions.
1169 * takes a list that contains a color like thing and a percentage
1171 public function colorArgs($args) {
1172 if ($args[0] != 'list' ||
count($args[2]) < 2) {
1173 return array(array('color', 0, 0, 0), 0);
1175 [$color, $delta] = $args[2];
1176 $color = $this->assertColor($color);
1177 $delta = floatval($delta[1]);
1179 return array($color, $delta);
1182 protected function lib_darken($args) {
1183 [$color, $delta] = $this->colorArgs($args);
1185 $hsl = $this->toHSL($color);
1186 $hsl[3] = $this->clamp($hsl[3] - $delta, 100);
1187 return $this->toRGB($hsl);
1190 protected function lib_lighten($args) {
1191 [$color, $delta] = $this->colorArgs($args);
1193 $hsl = $this->toHSL($color);
1194 $hsl[3] = $this->clamp($hsl[3] +
$delta, 100);
1195 return $this->toRGB($hsl);
1198 protected function lib_saturate($args) {
1199 [$color, $delta] = $this->colorArgs($args);
1201 $hsl = $this->toHSL($color);
1202 $hsl[2] = $this->clamp($hsl[2] +
$delta, 100);
1203 return $this->toRGB($hsl);
1206 protected function lib_desaturate($args) {
1207 [$color, $delta] = $this->colorArgs($args);
1209 $hsl = $this->toHSL($color);
1210 $hsl[2] = $this->clamp($hsl[2] - $delta, 100);
1211 return $this->toRGB($hsl);
1214 protected function lib_spin($args) {
1215 [$color, $delta] = $this->colorArgs($args);
1217 $hsl = $this->toHSL($color);
1219 $hsl[1] = $hsl[1] +
$delta %
360;
1220 if ($hsl[1] < 0) $hsl[1] +
= 360;
1222 return $this->toRGB($hsl);
1225 protected function lib_fadeout($args) {
1226 [$color, $delta] = $this->colorArgs($args);
1227 $color[4] = $this->clamp((isset($color[4]) ?
$color[4] : 1) - $delta/100);
1231 protected function lib_fadein($args) {
1232 [$color, $delta] = $this->colorArgs($args);
1233 $color[4] = $this->clamp((isset($color[4]) ?
$color[4] : 1) +
$delta/100);
1237 protected function lib_hue($color) {
1238 $hsl = $this->toHSL($this->assertColor($color));
1239 return round($hsl[1]);
1242 protected function lib_saturation($color) {
1243 $hsl = $this->toHSL($this->assertColor($color));
1244 return round($hsl[2]);
1247 protected function lib_lightness($color) {
1248 $hsl = $this->toHSL($this->assertColor($color));
1249 return round($hsl[3]);
1252 // get the alpha of a color
1253 // defaults to 1 for non-colors or colors without an alpha
1254 protected function lib_alpha($value) {
1255 if (!is_null($color = $this->coerceColor($value))) {
1256 return isset($color[4]) ?
$color[4] : 1;
1260 // set the alpha of the color
1261 protected function lib_fade($args) {
1262 [$color, $alpha] = $this->colorArgs($args);
1263 $color[4] = $this->clamp($alpha / 100.0);
1267 protected function lib_percentage($arg) {
1268 $num = $this->assertNumber($arg);
1269 return array("number", $num*100, "%");
1272 // mixes two colors by weight
1273 // mix(@color1, @color2, [@weight: 50%]);
1274 // http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html#mix-instance_method
1275 protected function lib_mix($args) {
1276 if ($args[0] != "list" ||
count($args[2]) < 2)
1277 $this->throwError("mix expects (color1, color2, weight)");
1279 [$first, $second] = $args[2];
1280 $first = $this->assertColor($first);
1281 $second = $this->assertColor($second);
1283 $first_a = $this->lib_alpha($first);
1284 $second_a = $this->lib_alpha($second);
1286 if (isset($args[2][2])) {
1287 $weight = $args[2][2][1] / 100.0;
1292 $w = $weight * 2 - 1;
1293 $a = $first_a - $second_a;
1295 $w1 = (($w * $a == -1 ?
$w : ($w +
$a)/(1 +
$w * $a)) +
1) / 2.0;
1298 $new = array('color',
1299 $w1 * $first[1] +
$w2 * $second[1],
1300 $w1 * $first[2] +
$w2 * $second[2],
1301 $w1 * $first[3] +
$w2 * $second[3],
1304 if ($first_a != 1.0 ||
$second_a != 1.0) {
1305 $new[] = $first_a * $weight +
$second_a * ($weight - 1);
1308 return $this->fixColor($new);
1311 protected function lib_contrast($args) {
1312 $darkColor = array('color', 0, 0, 0);
1313 $lightColor = array('color', 255, 255, 255);
1316 if ( $args[0] == 'list' ) {
1317 $inputColor = ( isset($args[2][0]) ) ?
$this->assertColor($args[2][0]) : $lightColor;
1318 $darkColor = ( isset($args[2][1]) ) ?
$this->assertColor($args[2][1]) : $darkColor;
1319 $lightColor = ( isset($args[2][2]) ) ?
$this->assertColor($args[2][2]) : $lightColor;
1320 if( isset($args[2][3]) ) {
1321 if( isset($args[2][3][2]) && $args[2][3][2] == '%' ) {
1322 $args[2][3][1] /= 100;
1323 unset($args[2][3][2]);
1325 $threshold = $this->assertNumber($args[2][3]);
1329 $inputColor = $this->assertColor($args);
1332 $inputColor = $this->coerceColor($inputColor);
1333 $darkColor = $this->coerceColor($darkColor);
1334 $lightColor = $this->coerceColor($lightColor);
1336 //Figure out which is actually light and dark!
1337 if ( $this->lib_luma($darkColor) > $this->lib_luma($lightColor) ) {
1339 $lightColor = $darkColor;
1343 $inputColor_alpha = $this->lib_alpha($inputColor);
1344 if ( ( $this->lib_luma($inputColor) * $inputColor_alpha) < $threshold) {
1350 protected function lib_luma($color) {
1351 $color = $this->coerceColor($color);
1352 return (0.2126 * $color[1] / 255) +
(0.7152 * $color[2] / 255) +
(0.0722 * $color[3] / 255);
1356 public function assertColor($value, $error = "expected color value") {
1357 $color = $this->coerceColor($value);
1358 if (is_null($color)) $this->throwError($error);
1362 public function assertNumber($value, $error = "expecting number") {
1363 if ($value[0] == "number") return $value[1];
1364 $this->throwError($error);
1367 public function assertArgs($value, $expectedArgs, $name="") {
1368 if ($expectedArgs == 1) {
1371 if ($value[0] !== "list" ||
$value[1] != ",") $this->throwError("expecting list");
1372 $values = $value[2];
1373 $numValues = count($values);
1374 if ($expectedArgs != $numValues) {
1376 $name = $name . ": ";
1379 $this->throwError("${name}expecting $expectedArgs arguments, got $numValues");
1386 public function assertMinArgs($value, $expectedMinArgs, $name="") {
1387 if ($value[0] !== "list" ||
$value[1] != ",") $this->throwError("expecting list");
1388 $values = $value[2];
1389 $numValues = count($values);
1390 if ($expectedMinArgs > $numValues) {
1392 $name = $name . ": ";
1395 $this->throwError("${name}expecting at least $expectedMinArgs arguments, got $numValues");
1401 protected function toHSL($color) {
1402 if ($color[0] == 'hsl') return $color;
1404 $r = $color[1] / 255;
1405 $g = $color[2] / 255;
1406 $b = $color[3] / 255;
1408 $min = min($r, $g, $b);
1409 $max = max($r, $g, $b);
1411 $L = ($min +
$max) / 2;
1416 $S = ($max - $min)/($max +
$min);
1418 $S = ($max - $min)/(2.0 - $max - $min);
1420 if ($r == $max) $H = ($g - $b)/($max - $min);
1421 elseif ($g == $max) $H = 2.0 +
($b - $r)/($max - $min);
1422 elseif ($b == $max) $H = 4.0 +
($r - $g)/($max - $min);
1427 ($H < 0 ?
$H +
6 : $H)*60,
1432 if (count($color) > 4) $out[] = $color[4]; // copy alpha
1436 protected function toRGB_helper($comp, $temp1, $temp2) {
1437 if ($comp < 0) $comp +
= 1.0;
1438 elseif ($comp > 1) $comp -= 1.0;
1440 if (6 * $comp < 1) return $temp1 +
($temp2 - $temp1) * 6 * $comp;
1441 if (2 * $comp < 1) return $temp2;
1442 if (3 * $comp < 2) return $temp1 +
($temp2 - $temp1)*((2/3) - $comp) * 6;
1448 * Converts a hsl array into a color value in rgb.
1449 * Expects H to be in range of 0 to 360, S and L in 0 to 100
1451 protected function toRGB($color) {
1452 if ($color[0] == 'color') return $color;
1454 $H = $color[1] / 360;
1455 $S = $color[2] / 100;
1456 $L = $color[3] / 100;
1465 $temp1 = 2.0 * $L - $temp2;
1467 $r = $this->toRGB_helper($H +
1/3, $temp1, $temp2);
1468 $g = $this->toRGB_helper($H, $temp1, $temp2);
1469 $b = $this->toRGB_helper($H - 1/3, $temp1, $temp2);
1472 // $out = array('color', round($r*255), round($g*255), round($b*255));
1473 $out = array('color', $r*255, $g*255, $b*255);
1474 if (count($color) > 4) $out[] = $color[4]; // copy alpha
1478 protected function clamp($v, $max = 1, $min = 0) {
1479 return min($max, max($min, $v));
1483 * Convert the rgb, rgba, hsl color literals of function type
1484 * as returned by the parser into values of color type.
1486 protected function funcToColor($func) {
1488 if ($func[2][0] != 'list') return false; // need a list of arguments
1489 $rawComponents = $func[2][2];
1491 if ($fname == 'hsl' ||
$fname == 'hsla') {
1492 $hsl = array('hsl');
1494 foreach ($rawComponents as $c) {
1495 $val = $this->reduce($c);
1496 $val = isset($val[1]) ?
floatval($val[1]) : 0;
1498 if ($i == 0) $clamp = 360;
1499 elseif ($i < 3) $clamp = 100;
1502 $hsl[] = $this->clamp($val, $clamp);
1506 while (count($hsl) < 4) $hsl[] = 0;
1507 return $this->toRGB($hsl);
1509 } elseif ($fname == 'rgb' ||
$fname == 'rgba') {
1510 $components = array();
1512 foreach ($rawComponents as $c) {
1513 $c = $this->reduce($c);
1515 if ($c[0] == "number" && $c[2] == "%") {
1516 $components[] = 255 * ($c[1] / 100);
1518 $components[] = floatval($c[1]);
1520 } elseif ($i == 4) {
1521 if ($c[0] == "number" && $c[2] == "%") {
1522 $components[] = 1.0 * ($c[1] / 100);
1524 $components[] = floatval($c[1]);
1530 while (count($components) < 3) $components[] = 0;
1531 array_unshift($components, 'color');
1532 return $this->fixColor($components);
1538 protected function reduce($value, $forExpression = false) {
1539 switch ($value[0]) {
1541 $reduced = $this->reduce($value[1]);
1542 $var = $this->compileValue($reduced);
1543 $res = $this->reduce(array("variable", $this->vPrefix
. $var));
1545 if ($res[0] == "raw_color") {
1546 $res = $this->coerceColor($res);
1549 if (empty($value[2])) $res = $this->lib_e($res);
1554 if (is_array($key)) {
1555 $key = $this->reduce($key);
1556 $key = $this->vPrefix
. $this->compileValue($this->lib_e($key));
1559 $seen =& $this->env
->seenNames
;
1561 if (!empty($seen[$key])) {
1562 $this->throwError("infinite loop detected: $key");
1566 $out = $this->reduce($this->get($key));
1567 $seen[$key] = false;
1570 foreach ($value[2] as &$item) {
1571 $item = $this->reduce($item, $forExpression);
1575 return $this->evaluate($value);
1577 foreach ($value[2] as &$part) {
1578 if (is_array($part)) {
1579 $strip = $part[0] == "variable";
1580 $part = $this->reduce($part);
1581 if ($strip) $part = $this->lib_e($part);
1587 return $this->lib_e($this->reduce($inner));
1589 $color = $this->funcToColor($value);
1590 if ($color) return $color;
1592 [, $name, $args] = $value;
1593 if ($name == "%") $name = "_sprintf";
1595 $f = isset($this->libFunctions
[$name]) ?
1596 $this->libFunctions
[$name] : array($this, 'lib_'.str_replace('-', '_', $name));
1598 if (is_callable($f)) {
1599 if ($args[0] == 'list')
1600 $args = self
::compressList($args[2], $args[1]);
1602 $ret = call_user_func($f, $this->reduce($args, true), $this);
1604 if (is_null($ret)) {
1605 return array("string", "", array(
1606 $name, "(", $args, ")"
1610 // convert to a typed value if the result is a php primitive
1611 if (is_numeric($ret)) $ret = array('number', $ret, "");
1612 elseif (!is_array($ret)) $ret = array('keyword', $ret);
1617 // plain function, reduce args
1618 $value[2] = $this->reduce($value[2]);
1621 [, $op, $exp] = $value;
1622 $exp = $this->reduce($exp);
1624 if ($exp[0] == "number") {
1633 return array("string", "", array($op, $exp));
1636 if ($forExpression) {
1637 switch ($value[0]) {
1639 if ($color = $this->coerceColor($value)) {
1644 return $this->coerceColor($value);
1652 // coerce a value for use in color operation
1653 protected function coerceColor($value) {
1655 case 'color': return $value;
1657 $c = array("color", 0, 0, 0);
1658 $colorStr = substr($value[1], 1);
1659 $num = hexdec($colorStr);
1660 $width = strlen($colorStr) == 3 ?
16 : 256;
1662 for ($i = 3; $i > 0; $i--) { // 3 2 1
1666 $c[$i] = $t * (256/$width) +
$t * floor(16/$width);
1672 if (isset(self
::$cssColors[$name])) {
1673 $rgba = explode(',', self
::$cssColors[$name]);
1676 return array('color', $rgba[0], $rgba[1], $rgba[2], $rgba[3]);
1678 return array('color', $rgba[0], $rgba[1], $rgba[2]);
1684 // make something string like into a string
1685 protected function coerceString($value) {
1686 switch ($value[0]) {
1690 return array("string", "", array($value[1]));
1695 // turn list of length 1 into value type
1696 protected function flattenList($value) {
1697 if ($value[0] == "list" && count($value[2]) == 1) {
1698 return $this->flattenList($value[2][0]);
1703 public function toBool($a) {
1704 if ($a) return self
::$TRUE;
1705 else return self
::$FALSE;
1708 // evaluate an expression
1709 protected function evaluate($exp) {
1710 [, $op, $left, $right, $whiteBefore, $whiteAfter] = $exp;
1712 $left = $this->reduce($left, true);
1713 $right = $this->reduce($right, true);
1715 if ($leftColor = $this->coerceColor($left)) {
1719 if ($rightColor = $this->coerceColor($right)) {
1720 $right = $rightColor;
1726 // operators that work on all types
1728 return $this->toBool($left == self
::$TRUE && $right == self
::$TRUE);
1732 return $this->toBool($this->eq($left, $right) );
1735 if ($op == "+" && !is_null($str = $this->stringConcatenate($left, $right))) {
1739 // type based operators
1740 $fname = "op_${ltype}_${rtype}";
1741 if (is_callable(array($this, $fname))) {
1742 $out = $this->$fname($op, $left, $right);
1743 if (!is_null($out)) return $out;
1746 // make the expression look it did before being parsed
1748 if ($whiteBefore) $paddedOp = " " . $paddedOp;
1749 if ($whiteAfter) $paddedOp .= " ";
1751 return array("string", "", array($left, $paddedOp, $right));
1754 protected function stringConcatenate($left, $right) {
1755 if ($strLeft = $this->coerceString($left)) {
1756 if ($right[0] == "string") {
1759 $strLeft[2][] = $right;
1763 if ($strRight = $this->coerceString($right)) {
1764 array_unshift($strRight[2], $left);
1769 protected function convert( $number, $to )
1771 $value = $this->assertNumber( $number );
1778 // check if the from value is a length
1779 if( ( $from_index = array_search( $from, self
::$lengths ) ) !== false ) {
1780 // make sure to value is too
1781 if( in_array( $to, self
::$lengths ) ) {
1782 // do the actual conversion
1783 $to_index = array_search( $to, self
::$lengths );
1784 $px = $value * self
::$lengths_to_base[ $from_index ];
1785 $result = $px * ( 1 / self
::$lengths_to_base[ $to_index ] );
1787 $result = round( $result, 8 );
1788 return array( "number", $result, $to );
1792 // do the same check for times
1793 if( in_array( $from, self
::$times ) ) {
1794 if( in_array( $to, self
::$times ) ) {
1795 // currently only ms and s are valid
1797 $result = $value * 1000;
1799 $result = $value / 1000;
1801 $result = round( $result, 8 );
1802 return array( "number", $result, $to );
1806 // lastly check for an angle
1807 if( in_array( $from, self
::$angles ) ) {
1808 // convert whatever angle it is into degrees
1809 if( $from == "rad" )
1810 $deg = rad2deg( $value );
1812 else if( $from == "turn" )
1813 $deg = $value * 360;
1815 else if( $from == "grad" )
1816 $deg = $value / (400 / 360);
1821 // Then convert it from degrees into desired unit
1826 $result = deg2rad( $deg );
1829 $result = $value / 360;
1832 $result = $value * (400 / 360);
1834 $result = round( $result, 8 );
1835 return array( "number", $result, $to );
1838 // we don't know how to convert these
1839 $this->throwError( "Cannot convert {$from} to {$to}" );
1842 // make sure a color's components don't go out of bounds
1843 protected function fixColor($c) {
1844 foreach (range(1, 3) as $i) {
1845 if ($c[$i] < 0) $c[$i] = 0;
1846 if ($c[$i] > 255) $c[$i] = 255;
1852 protected function op_number_color($op, $lft, $rgt) {
1853 if ($op == '+' ||
$op == '*') {
1854 return $this->op_color_number($op, $rgt, $lft);
1858 protected function op_color_number($op, $lft, $rgt) {
1859 if ($rgt[0] == '%') $rgt[1] /= 100;
1861 return $this->op_color_color($op, $lft,
1862 array_fill(1, count($lft) - 1, $rgt[1]));
1865 protected function op_color_color($op, $left, $right) {
1866 $out = array('color');
1867 $max = count($left) > count($right) ?
count($left) : count($right);
1868 foreach (range(1, $max - 1) as $i) {
1869 $lval = isset($left[$i]) ?
$left[$i] : 0;
1870 $rval = isset($right[$i]) ?
$right[$i] : 0;
1873 $out[] = $lval +
$rval;
1876 $out[] = $lval - $rval;
1879 $out[] = $lval * $rval;
1882 $out[] = $lval %
$rval;
1885 if ($rval == 0) $this->throwError("evaluate error: can't divide by zero");
1886 $out[] = $lval / $rval;
1889 $this->throwError('evaluate error: color op number failed on op '.$op);
1892 return $this->fixColor($out);
1895 function lib_red($color){
1896 $color = $this->coerceColor($color);
1897 if (is_null($color)) {
1898 $this->throwError('color expected for red()');
1904 function lib_green($color){
1905 $color = $this->coerceColor($color);
1906 if (is_null($color)) {
1907 $this->throwError('color expected for green()');
1913 function lib_blue($color){
1914 $color = $this->coerceColor($color);
1915 if (is_null($color)) {
1916 $this->throwError('color expected for blue()');
1923 // operator on two numbers
1924 protected function op_number_number($op, $left, $right) {
1925 $unit = empty($left[2]) ?
$right[2] : $left[2];
1930 $value = $left[1] +
$right[1];
1933 $value = $left[1] * $right[1];
1936 $value = $left[1] - $right[1];
1939 $value = $left[1] %
$right[1];
1942 if ($right[1] == 0) $this->throwError('parse error: divide by zero');
1943 $value = $left[1] / $right[1];
1946 return $this->toBool($left[1] < $right[1]);
1948 return $this->toBool($left[1] > $right[1]);
1950 return $this->toBool($left[1] >= $right[1]);
1952 return $this->toBool($left[1] <= $right[1]);
1954 $this->throwError('parse error: unknown number operator: '.$op);
1957 return array("number", $value, $unit);
1961 /* environment functions */
1963 protected function makeOutputBlock($type, $selectors = null) {
1965 $b->lines
= array();
1966 $b->children
= array();
1967 $b->selectors
= $selectors;
1969 $b->parent
= $this->scope
;
1973 // the state of execution
1974 protected function pushEnv($block = null) {
1976 $e->parent
= $this->env
;
1977 $e->store
= array();
1984 // pop something off the stack
1985 protected function popEnv() {
1987 $this->env
= $this->env
->parent
;
1991 // set something in the current env
1992 protected function set($name, $value) {
1993 $this->env
->store
[$name] = $value;
1997 // get the highest occurrence entry for a name
1998 protected function get($name) {
1999 $current = $this->env
;
2001 // track scope to evaluate
2002 $scope_secondary = array();
2004 $isArguments = $name == $this->vPrefix
. 'arguments';
2006 if ($isArguments && isset($current->arguments
)) {
2007 return array('list', ' ', $current->arguments
);
2010 if (isset($current->store
[$name]))
2011 return $current->store
[$name];
2012 // has secondary scope?
2013 if (isset($current->storeParent
))
2014 $scope_secondary[] = $current->storeParent
;
2016 if (isset($current->parent
))
2017 $current = $current->parent
;
2022 while (count($scope_secondary)) {
2024 $current = array_shift($scope_secondary);
2026 if ($isArguments && isset($current->arguments
)) {
2027 return array('list', ' ', $current->arguments
);
2030 if (isset($current->store
[$name])) {
2031 return $current->store
[$name];
2034 // has secondary scope?
2035 if (isset($current->storeParent
)) {
2036 $scope_secondary[] = $current->storeParent
;
2039 if (isset($current->parent
)) {
2040 $current = $current->parent
;
2047 $this->throwError("variable $name is undefined");
2050 // inject array of unparsed strings into environment as variables
2051 protected function injectVariables($args) {
2053 $parser = new lessc_parser($this, __METHOD__
);
2054 foreach ($args as $name => $strValue) {
2055 if ($name[0] != '@') $name = '@'.$name;
2057 $parser->buffer
= (string)$strValue;
2058 if (!$parser->propertyValue($value)) {
2059 throw new \
Exception("failed to parse passed in variable $name: $strValue");
2062 $this->set($name, $value);
2067 * Initialize any static state, can initialize parser for a file
2068 * $opts isn't used yet
2070 public function __construct($fname = null) {
2071 if ($fname !== null) {
2072 // used for deprecated parse method
2073 $this->_parseFile
= $fname;
2077 public function compile($string, $name = null) {
2078 $locale = setlocale(LC_NUMERIC
, 0);
2079 setlocale(LC_NUMERIC
, "C");
2081 $this->parser
= $this->makeParser($name);
2082 $root = $this->parser
->parse($string);
2085 $this->scope
= null;
2086 $this->allParsedFiles
= array();
2088 $this->formatter
= $this->newFormatter();
2090 if (!empty($this->registeredVars
)) {
2091 $this->injectVariables($this->registeredVars
);
2094 $this->sourceParser
= $this->parser
; // used for error messages
2095 $this->compileBlock($root);
2098 $this->formatter
->block($this->scope
);
2099 $out = ob_get_clean();
2100 setlocale(LC_NUMERIC
, $locale);
2104 public function compileFile($fname, $outFname = null) {
2105 if (!is_readable($fname)) {
2106 throw new \
Exception('load error: failed to find '.$fname);
2109 $pi = pathinfo($fname);
2111 $oldImport = $this->importDir
;
2113 $this->importDir
= (array)$this->importDir
;
2114 $this->importDir
[] = $pi['dirname'].'/';
2116 $this->addParsedFile($fname);
2118 $out = $this->compile(file_get_contents($fname), $fname);
2120 $this->importDir
= $oldImport;
2122 if ($outFname !== null) {
2123 return file_put_contents($outFname, $out);
2130 * Based on explicit input/output files does a full change check on cache before compiling.
2133 * @param string $out
2134 * @param boolean $force
2135 * @return string Compiled CSS results
2138 public function checkedCachedCompile($in, $out, $force = false) {
2139 if (!is_file($in) ||
!is_readable($in)) {
2140 throw new Exception('Invalid or unreadable input file specified.');
2142 if (is_dir($out) ||
!is_writable(file_exists($out) ?
$out : dirname($out))) {
2143 throw new Exception('Invalid or unwritable output file specified.');
2146 $outMeta = $out . '.meta';
2148 if (!$force && is_file($outMeta)) {
2149 $metadata = unserialize(file_get_contents($outMeta));
2152 $output = $this->cachedCompile($metadata ?
$metadata : $in);
2154 if (!$metadata ||
$metadata['updated'] != $output['updated']) {
2155 $css = $output['compiled'];
2156 unset($output['compiled']);
2157 file_put_contents($out, $css);
2158 file_put_contents($outMeta, serialize($output));
2160 $css = file_get_contents($out);
2166 // compile only if changed input has changed or output doesn't exist
2167 public function checkedCompile($in, $out) {
2168 if (!is_file($out) ||
filemtime($in) > filemtime($out)) {
2169 $this->compileFile($in, $out);
2176 * Execute lessphp on a .less file or a lessphp cache structure
2178 * The lessphp cache structure contains information about a specific
2179 * less file having been parsed. It can be used as a hint for future
2180 * calls to determine whether or not a rebuild is required.
2182 * The cache structure contains two important keys that may be used
2185 * compiled: The final compiled CSS
2186 * updated: The time (in seconds) the CSS was last compiled
2188 * The cache structure is a plain-ol' PHP associative array and can
2189 * be serialized and unserialized without a hitch.
2191 * @param mixed $in Input
2192 * @param bool $force Force rebuild?
2193 * @return array lessphp cache structure
2195 public function cachedCompile($in, $force = false) {
2199 if (is_string($in)) {
2201 } elseif (is_array($in) and isset($in['root'])) {
2202 if ($force or ! isset($in['files'])) {
2203 // If we are forcing a recompile or if for some reason the
2204 // structure does not contain any file information we should
2205 // specify the root to trigger a rebuild.
2206 $root = $in['root'];
2207 } elseif (isset($in['files']) and is_array($in['files'])) {
2208 foreach ($in['files'] as $fname => $ftime ) {
2209 if (!file_exists($fname) or filemtime($fname) > $ftime) {
2210 // One of the files we knew about previously has changed
2211 // so we should look at our incoming root again.
2212 $root = $in['root'];
2218 // TODO: Throw an exception? We got neither a string nor something
2219 // that looks like a compatible lessphp cache structure.
2223 if ($root !== null) {
2224 // If we have a root value which means we should rebuild.
2226 $out['root'] = $root;
2227 $out['compiled'] = $this->compileFile($root);
2228 $out['files'] = $this->allParsedFiles();
2229 $out['updated'] = time();
2232 // No changes, pass back the structure
2233 // we were given initially.
2239 // parse and compile buffer
2240 // This is deprecated
2241 public function parse($str = null, $initialVariables = null) {
2242 if (is_array($str)) {
2243 $initialVariables = $str;
2247 $oldVars = $this->registeredVars
;
2248 if ($initialVariables !== null) {
2249 $this->setVariables($initialVariables);
2253 if (empty($this->_parseFile
)) {
2254 throw new \
Exception("nothing to parse");
2257 $out = $this->compileFile($this->_parseFile
);
2259 $out = $this->compile($str);
2262 $this->registeredVars
= $oldVars;
2266 protected function makeParser($name) {
2267 $parser = new lessc_parser($this, $name);
2268 $parser->writeComments
= $this->preserveComments
;
2273 public function setFormatter($name) {
2274 $this->formatterName
= $name;
2277 protected function newFormatter() {
2278 $className = "lessc_formatter_lessjs";
2279 if (!empty($this->formatterName
)) {
2280 if (!is_string($this->formatterName
))
2281 return $this->formatterName
;
2282 $className = "lessc_formatter_$this->formatterName";
2285 return new $className;
2288 public function setPreserveComments($preserve) {
2289 $this->preserveComments
= $preserve;
2292 public function registerFunction($name, $func) {
2293 $this->libFunctions
[$name] = $func;
2296 public function unregisterFunction($name) {
2297 unset($this->libFunctions
[$name]);
2300 public function setVariables($variables) {
2301 $this->registeredVars
= array_merge($this->registeredVars
, $variables);
2304 public function unsetVariable($name) {
2305 unset($this->registeredVars
[$name]);
2308 public function setImportDir($dirs) {
2309 $this->importDir
= (array)$dirs;
2312 public function addImportDir($dir) {
2313 $this->importDir
= (array)$this->importDir
;
2314 $this->importDir
[] = $dir;
2317 public function allParsedFiles() {
2318 return $this->allParsedFiles
;
2321 public function addParsedFile($file) {
2322 $this->allParsedFiles
[realpath($file)] = filemtime($file);
2326 * Uses the current value of $this->count to show line and line number
2328 public function throwError($msg = null) {
2329 if ($this->sourceLoc
>= 0) {
2330 $this->sourceParser
->throwError($msg, $this->sourceLoc
);
2332 throw new \
Exception($msg);
2335 // compile file $in to file $out if $in is newer than $out
2336 // returns true when it compiles, false otherwise
2337 public static function ccompile($in, $out, $less = null) {
2338 if ($less === null) {
2341 return $less->checkedCompile($in, $out);
2344 public static function cexecute($in, $force = false, $less = null) {
2345 if ($less === null) {
2348 return $less->cachedCompile($in, $force);
2351 static protected $cssColors = array(
2352 'aliceblue' => '240,248,255',
2353 'antiquewhite' => '250,235,215',
2354 'aqua' => '0,255,255',
2355 'aquamarine' => '127,255,212',
2356 'azure' => '240,255,255',
2357 'beige' => '245,245,220',
2358 'bisque' => '255,228,196',
2360 'blanchedalmond' => '255,235,205',
2361 'blue' => '0,0,255',
2362 'blueviolet' => '138,43,226',
2363 'brown' => '165,42,42',
2364 'burlywood' => '222,184,135',
2365 'cadetblue' => '95,158,160',
2366 'chartreuse' => '127,255,0',
2367 'chocolate' => '210,105,30',
2368 'coral' => '255,127,80',
2369 'cornflowerblue' => '100,149,237',
2370 'cornsilk' => '255,248,220',
2371 'crimson' => '220,20,60',
2372 'cyan' => '0,255,255',
2373 'darkblue' => '0,0,139',
2374 'darkcyan' => '0,139,139',
2375 'darkgoldenrod' => '184,134,11',
2376 'darkgray' => '169,169,169',
2377 'darkgreen' => '0,100,0',
2378 'darkgrey' => '169,169,169',
2379 'darkkhaki' => '189,183,107',
2380 'darkmagenta' => '139,0,139',
2381 'darkolivegreen' => '85,107,47',
2382 'darkorange' => '255,140,0',
2383 'darkorchid' => '153,50,204',
2384 'darkred' => '139,0,0',
2385 'darksalmon' => '233,150,122',
2386 'darkseagreen' => '143,188,143',
2387 'darkslateblue' => '72,61,139',
2388 'darkslategray' => '47,79,79',
2389 'darkslategrey' => '47,79,79',
2390 'darkturquoise' => '0,206,209',
2391 'darkviolet' => '148,0,211',
2392 'deeppink' => '255,20,147',
2393 'deepskyblue' => '0,191,255',
2394 'dimgray' => '105,105,105',
2395 'dimgrey' => '105,105,105',
2396 'dodgerblue' => '30,144,255',
2397 'firebrick' => '178,34,34',
2398 'floralwhite' => '255,250,240',
2399 'forestgreen' => '34,139,34',
2400 'fuchsia' => '255,0,255',
2401 'gainsboro' => '220,220,220',
2402 'ghostwhite' => '248,248,255',
2403 'gold' => '255,215,0',
2404 'goldenrod' => '218,165,32',
2405 'gray' => '128,128,128',
2406 'green' => '0,128,0',
2407 'greenyellow' => '173,255,47',
2408 'grey' => '128,128,128',
2409 'honeydew' => '240,255,240',
2410 'hotpink' => '255,105,180',
2411 'indianred' => '205,92,92',
2412 'indigo' => '75,0,130',
2413 'ivory' => '255,255,240',
2414 'khaki' => '240,230,140',
2415 'lavender' => '230,230,250',
2416 'lavenderblush' => '255,240,245',
2417 'lawngreen' => '124,252,0',
2418 'lemonchiffon' => '255,250,205',
2419 'lightblue' => '173,216,230',
2420 'lightcoral' => '240,128,128',
2421 'lightcyan' => '224,255,255',
2422 'lightgoldenrodyellow' => '250,250,210',
2423 'lightgray' => '211,211,211',
2424 'lightgreen' => '144,238,144',
2425 'lightgrey' => '211,211,211',
2426 'lightpink' => '255,182,193',
2427 'lightsalmon' => '255,160,122',
2428 'lightseagreen' => '32,178,170',
2429 'lightskyblue' => '135,206,250',
2430 'lightslategray' => '119,136,153',
2431 'lightslategrey' => '119,136,153',
2432 'lightsteelblue' => '176,196,222',
2433 'lightyellow' => '255,255,224',
2434 'lime' => '0,255,0',
2435 'limegreen' => '50,205,50',
2436 'linen' => '250,240,230',
2437 'magenta' => '255,0,255',
2438 'maroon' => '128,0,0',
2439 'mediumaquamarine' => '102,205,170',
2440 'mediumblue' => '0,0,205',
2441 'mediumorchid' => '186,85,211',
2442 'mediumpurple' => '147,112,219',
2443 'mediumseagreen' => '60,179,113',
2444 'mediumslateblue' => '123,104,238',
2445 'mediumspringgreen' => '0,250,154',
2446 'mediumturquoise' => '72,209,204',
2447 'mediumvioletred' => '199,21,133',
2448 'midnightblue' => '25,25,112',
2449 'mintcream' => '245,255,250',
2450 'mistyrose' => '255,228,225',
2451 'moccasin' => '255,228,181',
2452 'navajowhite' => '255,222,173',
2453 'navy' => '0,0,128',
2454 'oldlace' => '253,245,230',
2455 'olive' => '128,128,0',
2456 'olivedrab' => '107,142,35',
2457 'orange' => '255,165,0',
2458 'orangered' => '255,69,0',
2459 'orchid' => '218,112,214',
2460 'palegoldenrod' => '238,232,170',
2461 'palegreen' => '152,251,152',
2462 'paleturquoise' => '175,238,238',
2463 'palevioletred' => '219,112,147',
2464 'papayawhip' => '255,239,213',
2465 'peachpuff' => '255,218,185',
2466 'peru' => '205,133,63',
2467 'pink' => '255,192,203',
2468 'plum' => '221,160,221',
2469 'powderblue' => '176,224,230',
2470 'purple' => '128,0,128',
2472 'rosybrown' => '188,143,143',
2473 'royalblue' => '65,105,225',
2474 'saddlebrown' => '139,69,19',
2475 'salmon' => '250,128,114',
2476 'sandybrown' => '244,164,96',
2477 'seagreen' => '46,139,87',
2478 'seashell' => '255,245,238',
2479 'sienna' => '160,82,45',
2480 'silver' => '192,192,192',
2481 'skyblue' => '135,206,235',
2482 'slateblue' => '106,90,205',
2483 'slategray' => '112,128,144',
2484 'slategrey' => '112,128,144',
2485 'snow' => '255,250,250',
2486 'springgreen' => '0,255,127',
2487 'steelblue' => '70,130,180',
2488 'tan' => '210,180,140',
2489 'teal' => '0,128,128',
2490 'thistle' => '216,191,216',
2491 'tomato' => '255,99,71',
2492 'transparent' => '0,0,0,0',
2493 'turquoise' => '64,224,208',
2494 'violet' => '238,130,238',
2495 'wheat' => '245,222,179',
2496 'white' => '255,255,255',
2497 'whitesmoke' => '245,245,245',
2498 'yellow' => '255,255,0',
2499 'yellowgreen' => '154,205,50'
2503 // responsible for taking a string of LESS code and converting it into a
2505 class lessc_parser
{
2506 static protected $nextBlockId = 0; // used to uniquely identify blocks
2508 static protected $precedence = array(
2522 static protected $whitePattern;
2523 static protected $commentMulti;
2525 static protected $commentSingle = "//";
2526 static protected $commentMultiLeft = "/*";
2527 static protected $commentMultiRight = "*/";
2529 // regex string to match any of the operators
2530 static protected $operatorString;
2532 // these properties will supress division unless it's inside parenthases
2533 static protected $supressDivisionProps =
2534 array('/border-radius$/i', '/^font$/i');
2536 protected $blockDirectives = array("font-face", "keyframes", "page", "-moz-document", "viewport", "-moz-viewport", "-o-viewport", "-ms-viewport");
2537 protected $lineDirectives = array("charset");
2540 * if we are in parens we can be more liberal with whitespace around
2541 * operators because it must evaluate to a single value and thus is less
2545 * property1: 10 -5; // is two numbers, 10 and -5
2546 * property2: (10 -5); // should evaluate to 5
2548 protected $inParens = false;
2550 // caches preg escaped literals
2551 static protected $literalCache = array();
2553 public function __construct($lessc, $sourceName = null) {
2554 $this->eatWhiteDefault
= true;
2555 // reference to less needed for vPrefix, mPrefix, and parentSelector
2556 $this->lessc
= $lessc;
2558 $this->sourceName
= $sourceName; // name used for error messages
2560 $this->writeComments
= false;
2562 if (!self
::$operatorString) {
2563 self
::$operatorString =
2564 '('.implode('|', array_map(array('lessc', 'preg_quote'),
2565 array_keys(self
::$precedence))).')';
2567 $commentSingle = lessc
::preg_quote(self
::$commentSingle);
2568 $commentMultiLeft = lessc
::preg_quote(self
::$commentMultiLeft);
2569 $commentMultiRight = lessc
::preg_quote(self
::$commentMultiRight);
2571 self
::$commentMulti = $commentMultiLeft.'.*?'.$commentMultiRight;
2572 self
::$whitePattern = '/'.$commentSingle.'[^\n]*\s*|('.self
::$commentMulti.')\s*|\s+/Ais';
2576 public function parse($buffer) {
2580 $this->env
= null; // block stack
2581 $this->buffer
= $this->writeComments ?
$buffer : $this->removeComments($buffer);
2582 $this->pushSpecialBlock("root");
2583 $this->eatWhiteDefault
= true;
2584 $this->seenComments
= array();
2586 // trim whitespace on head
2587 // if (preg_match('/^\s+/', $this->buffer, $m)) {
2588 // $this->line += substr_count($m[0], "\n");
2589 // $this->buffer = ltrim($this->buffer);
2591 $this->whitespace();
2593 // parse the entire file
2594 while (false !== $this->parseChunk());
2596 if ($this->count
!= strlen($this->buffer
))
2597 $this->throwError();
2599 // TODO report where the block was opened
2600 if ( !property_exists($this->env
, 'parent') ||
!is_null($this->env
->parent
) )
2601 throw new \
Exception('parse error: unclosed block');
2607 * Parse a single chunk off the head of the buffer and append it to the
2608 * current parse environment.
2609 * Returns false when the buffer is empty, or when there is an error.
2611 * This function is called repeatedly until the entire document is
2614 * This parser is most similar to a recursive descent parser. Single
2615 * functions represent discrete grammatical rules for the language, and
2616 * they are able to capture the text that represents those rules.
2618 * Consider the function lessc::keyword(). (all parse functions are
2619 * structured the same)
2621 * The function takes a single reference argument. When calling the
2622 * function it will attempt to match a keyword on the head of the buffer.
2623 * If it is successful, it will place the keyword in the referenced
2624 * argument, advance the position in the buffer, and return true. If it
2625 * fails then it won't advance the buffer and it will return false.
2627 * All of these parse functions are powered by lessc::match(), which behaves
2628 * the same way, but takes a literal regular expression. Sometimes it is
2629 * more convenient to use match instead of creating a new function.
2631 * Because of the format of the functions, to parse an entire string of
2632 * grammatical rules, you can chain them together using &&.
2634 * But, if some of the rules in the chain succeed before one fails, then
2635 * the buffer position will be left at an invalid state. In order to
2636 * avoid this, lessc::seek() is used to remember and set buffer positions.
2638 * Before parsing a chain, use $s = $this->seek() to remember the current
2639 * position into $s. Then if a chain fails, use $this->seek($s) to
2640 * go back where we started.
2642 protected function parseChunk() {
2643 if (empty($this->buffer
)) return false;
2646 if ($this->whitespace()) {
2650 // setting a property
2651 if ($this->keyword($key) && $this->assign() &&
2652 $this->propertyValue($value, $key) && $this->end())
2654 $this->append(array('assign', $key, $value), $s);
2661 // look for special css blocks
2662 if ($this->literal('@', false)) {
2666 if ($this->literal('@media')) {
2667 if (($this->mediaQueryList($mediaQueries) ||
true)
2668 && $this->literal('{'))
2670 $media = $this->pushSpecialBlock("media");
2671 $media->queries
= is_null($mediaQueries) ?
array() : $mediaQueries;
2679 if ($this->literal("@", false) && $this->keyword($dirName)) {
2680 if ($this->isDirective($dirName, $this->blockDirectives
)) {
2681 if (($this->openString("{", $dirValue, null, array(";")) ||
true) &&
2682 $this->literal("{"))
2684 $dir = $this->pushSpecialBlock("directive");
2685 $dir->name
= $dirName;
2686 if (isset($dirValue)) $dir->value
= $dirValue;
2689 } elseif ($this->isDirective($dirName, $this->lineDirectives
)) {
2690 if ($this->propertyValue($dirValue) && $this->end()) {
2691 $this->append(array("directive", $dirName, $dirValue));
2694 } elseif ($this->literal(":", true)) {
2695 //Ruleset Definition
2696 if (($this->openString("{", $dirValue, null, array(";")) ||
true) &&
2697 $this->literal("{"))
2699 $dir = $this->pushBlock($this->fixTags(array("@".$dirName)));
2700 $dir->name
= $dirName;
2701 if (isset($dirValue)) $dir->value
= $dirValue;
2710 // setting a variable
2711 if ($this->variable($var) && $this->assign() &&
2712 $this->propertyValue($value) && $this->end())
2714 $this->append(array('assign', $var, $value), $s);
2720 if ($this->import($importValue)) {
2721 $this->append($importValue, $s);
2725 // opening parametric mixin
2726 if ($this->tag($tag, true) && $this->argumentDef($args, $isVararg) &&
2727 ($this->guards($guards) ||
true) &&
2728 $this->literal('{'))
2730 $block = $this->pushBlock($this->fixTags(array($tag)));
2731 $block->args
= $args;
2732 $block->isVararg
= $isVararg;
2733 if (!empty($guards)) $block->guards
= $guards;
2739 // opening a simple block
2740 if ($this->tags($tags) && $this->literal('{', false)) {
2741 $tags = $this->fixTags($tags);
2742 $this->pushBlock($tags);
2749 if ($this->literal('}', false)) {
2751 $block = $this->pop();
2752 } catch (\Exception
$e) {
2754 $this->throwError($e->getMessage());
2758 if (is_null($block->type
)) {
2760 if (!isset($block->args
)) {
2761 foreach ($block->tags
as $tag) {
2762 if (!is_string($tag) ||
$tag[0] != $this->lessc
->mPrefix
) {
2769 foreach ($block->tags
as $tag) {
2770 if (is_string($tag)) {
2771 $this->env
->children
[$tag][] = $block;
2777 $this->append(array('block', $block), $s);
2780 // this is done here so comments aren't bundled into he block that
2782 $this->whitespace();
2787 if ($this->mixinTags($tags) &&
2788 ($this->argumentDef($argv, $isVararg) ||
true) &&
2789 ($this->keyword($suffix) ||
true) && $this->end())
2791 $tags = $this->fixTags($tags);
2792 $this->append(array('mixin', $tags, $argv, $suffix), $s);
2799 if ($this->literal(';')) return true;
2801 return false; // got nothing, throw error
2804 protected function isDirective($dirname, $directives) {
2805 // TODO: cache pattern in parser
2806 $pattern = implode("|",
2807 array_map(array("lessc", "preg_quote"), $directives));
2808 $pattern = '/^(-[a-z-]+-)?(' . $pattern . ')$/i';
2810 return preg_match($pattern, $dirname);
2813 protected function fixTags($tags) {
2814 // move @ tags out of variable namespace
2815 foreach ($tags as &$tag) {
2816 if ($tag[0] == $this->lessc
->vPrefix
)
2817 $tag[0] = $this->lessc
->mPrefix
;
2822 // a list of expressions
2823 protected function expressionList(&$exps) {
2826 while ($this->expression($exp)) {
2830 if (count($values) == 0) return false;
2832 $exps = lessc
::compressList($values, ' ');
2837 * Attempt to consume an expression.
2838 * @link http://en.wikipedia.org/wiki/Operator-precedence_parser#Pseudo-code
2840 protected function expression(&$out) {
2841 if ($this->value($lhs)) {
2842 $out = $this->expHelper($lhs, 0);
2844 // look for / shorthand
2845 if (!empty($this->env
->supressedDivision
)) {
2846 unset($this->env
->supressedDivision
);
2848 if ($this->literal("/") && $this->value($rhs)) {
2849 $out = array("list", "",
2850 array($out, array("keyword", "/"), $rhs));
2862 * recursively parse infix equation with $lhs at precedence $minP
2864 protected function expHelper($lhs, $minP) {
2865 $this->inExp
= true;
2866 $ss = $this->seek();
2869 $whiteBefore = isset($this->buffer
[$this->count
- 1]) &&
2870 ctype_space($this->buffer
[$this->count
- 1]);
2872 // If there is whitespace before the operator, then we require
2873 // whitespace after the operator for it to be an expression
2874 $needWhite = $whiteBefore && !$this->inParens
;
2876 if ($this->match(self
::$operatorString.($needWhite ?
'\s' : ''), $m) && self
::$precedence[$m[1]] >= $minP) {
2877 if (!$this->inParens
&& isset($this->env
->currentProperty
) && $m[1] == "/" && empty($this->env
->supressedDivision
)) {
2878 foreach (self
::$supressDivisionProps as $pattern) {
2879 if (preg_match($pattern, $this->env
->currentProperty
)) {
2880 $this->env
->supressedDivision
= true;
2887 $whiteAfter = isset($this->buffer
[$this->count
- 1]) &&
2888 ctype_space($this->buffer
[$this->count
- 1]);
2890 if (!$this->value($rhs)) break;
2892 // peek for next operator to see what to do with rhs
2893 if ($this->peek(self
::$operatorString, $next) && self
::$precedence[$next[1]] > self
::$precedence[$m[1]]) {
2894 $rhs = $this->expHelper($rhs, self
::$precedence[$next[1]]);
2897 $lhs = array('expression', $m[1], $lhs, $rhs, $whiteBefore, $whiteAfter);
2898 $ss = $this->seek();
2911 // consume a list of values for a property
2912 public function propertyValue(&$value, $keyName = null) {
2915 if ($keyName !== null) $this->env
->currentProperty
= $keyName;
2918 while ($this->expressionList($v)) {
2921 if (!$this->literal(',')) break;
2924 if ($s) $this->seek($s);
2926 if ($keyName !== null) unset($this->env
->currentProperty
);
2928 if (count($values) == 0) return false;
2930 $value = lessc
::compressList($values, ', ');
2934 protected function parenValue(&$out) {
2938 if (isset($this->buffer
[$this->count
]) && $this->buffer
[$this->count
] != "(") {
2942 $inParens = $this->inParens
;
2943 if ($this->literal("(") &&
2944 ($this->inParens
= true) && $this->expression($exp) &&
2945 $this->literal(")"))
2948 $this->inParens
= $inParens;
2951 $this->inParens
= $inParens;
2959 protected function value(&$value) {
2963 if (isset($this->buffer
[$this->count
]) && $this->buffer
[$this->count
] == "-") {
2965 if ($this->literal("-", false) &&
2966 (($this->variable($inner) && $inner = array("variable", $inner)) ||
2967 $this->unit($inner) ||
2968 $this->parenValue($inner)))
2970 $value = array("unary", "-", $inner);
2977 if ($this->parenValue($value)) return true;
2978 if ($this->unit($value)) return true;
2979 if ($this->color($value)) return true;
2980 if ($this->func($value)) return true;
2981 if ($this->stringValue($value)) return true;
2983 if ($this->keyword($word)) {
2984 $value = array('keyword', $word);
2989 if ($this->variable($var)) {
2990 $value = array('variable', $var);
2994 // unquote string (should this work on any type?
2995 if ($this->literal("~") && $this->stringValue($str)) {
2996 $value = array("escape", $str);
3003 if ($this->literal('\\') && $this->match('([0-9]+)', $m)) {
3004 $value = array('keyword', '\\'.$m[1]);
3013 // an import statement
3014 protected function import(&$out) {
3015 if (!$this->literal('@import')) return false;
3017 // @import "something.css" media;
3018 // @import url("something.css") media;
3019 // @import url(something.css) media;
3021 if ($this->propertyValue($value)) {
3022 $out = array("import", $value);
3027 protected function mediaQueryList(&$out) {
3028 if ($this->genericList($list, "mediaQuery", ",", false)) {
3035 protected function mediaQuery(&$out) {
3038 $expressions = null;
3041 if (($this->literal("only") && ($only = true) ||
$this->literal("not") && ($not = true) ||
true) && $this->keyword($mediaType)) {
3042 $prop = array("mediaType");
3043 if (isset($only)) $prop[] = "only";
3044 if (isset($not)) $prop[] = "not";
3045 $prop[] = $mediaType;
3052 if (!empty($mediaType) && !$this->literal("and")) {
3055 $this->genericList($expressions, "mediaExpression", "and", false);
3056 if (is_array($expressions)) $parts = array_merge($parts, $expressions[2]);
3059 if (count($parts) == 0) {
3068 protected function mediaExpression(&$out) {
3071 if ($this->literal("(") &&
3072 $this->keyword($feature) &&
3073 ($this->literal(":") && $this->expression($value) ||
true) &&
3074 $this->literal(")"))
3076 $out = array("mediaExp", $feature);
3077 if ($value) $out[] = $value;
3079 } elseif ($this->variable($variable)) {
3080 $out = array('variable', $variable);
3088 // an unbounded string stopped by $end
3089 protected function openString($end, &$out, $nestingOpen=null, $rejectStrs = null) {
3090 $oldWhite = $this->eatWhiteDefault
;
3091 $this->eatWhiteDefault
= false;
3093 $stop = array("'", '"', "@{", $end);
3094 $stop = array_map(array("lessc", "preg_quote"), $stop);
3095 // $stop[] = self::$commentMulti;
3097 if (!is_null($rejectStrs)) {
3098 $stop = array_merge($stop, $rejectStrs);
3101 $patt = '(.*?)('.implode("|", $stop).')';
3106 while ($this->match($patt, $m, false)) {
3107 if (!empty($m[1])) {
3110 $nestingLevel +
= substr_count($m[1], $nestingOpen);
3116 $this->count
-= strlen($tok);
3118 if ($nestingLevel == 0) {
3125 if (($tok == "'" ||
$tok == '"') && $this->stringValue($str)) {
3130 if ($tok == "@{" && $this->interpolation($inter)) {
3131 $content[] = $inter;
3135 if (!empty($rejectStrs) && in_array($tok, $rejectStrs)) {
3140 $this->count+
= strlen($tok);
3143 $this->eatWhiteDefault
= $oldWhite;
3145 if (count($content) == 0) return false;
3148 if (is_string(end($content))) {
3149 $content[count($content) - 1] = rtrim(end($content));
3152 $out = array("string", "", $content);
3156 protected function stringValue(&$out) {
3158 if ($this->literal('"', false)) {
3160 } elseif ($this->literal("'", false)) {
3168 // look for either ending delim , escape, or string interpolation
3169 $patt = '([^\n]*?)(@\{|\\\\|' .
3170 lessc
::preg_quote($delim).')';
3172 $oldWhite = $this->eatWhiteDefault
;
3173 $this->eatWhiteDefault
= false;
3175 while ($this->match($patt, $m, false)) {
3177 if ($m[2] == "@{") {
3178 $this->count
-= strlen($m[2]);
3179 if ($this->interpolation($inter, false)) {
3180 $content[] = $inter;
3182 $this->count +
= strlen($m[2]);
3183 $content[] = "@{"; // ignore it
3185 } elseif ($m[2] == '\\') {
3187 if ($this->literal($delim, false)) {
3188 $content[] = $delim;
3191 $this->count
-= strlen($delim);
3196 $this->eatWhiteDefault
= $oldWhite;
3198 if ($this->literal($delim)) {
3199 $out = array("string", $delim, $content);
3207 protected function interpolation(&$out) {
3208 $oldWhite = $this->eatWhiteDefault
;
3209 $this->eatWhiteDefault
= true;
3212 if ($this->literal("@{") &&
3213 $this->openString("}", $interp, null, array("'", '"', ";")) &&
3214 $this->literal("}", false))
3216 $out = array("interpolate", $interp);
3217 $this->eatWhiteDefault
= $oldWhite;
3218 if ($this->eatWhiteDefault
) $this->whitespace();
3222 $this->eatWhiteDefault
= $oldWhite;
3227 protected function unit(&$unit) {
3229 if (isset($this->buffer
[$this->count
])) {
3230 $char = $this->buffer
[$this->count
];
3231 if (!ctype_digit($char) && $char != ".") return false;
3234 if ($this->match('([0-9]+(?:\.[0-9]*)?|\.[0-9]+)([%a-zA-Z]+)?', $m)) {
3235 $unit = array("number", $m[1], empty($m[2]) ?
"" : $m[2]);
3242 protected function color(&$out) {
3243 if ($this->match('(#(?:[0-9a-f]{8}|[0-9a-f]{6}|[0-9a-f]{3}))', $m)) {
3244 if (strlen($m[1]) > 7) {
3245 $out = array("string", "", array($m[1]));
3247 $out = array("raw_color", $m[1]);
3255 // consume an argument definition list surrounded by ()
3256 // each argument is a variable name with optional value
3257 // or at the end a ... or a variable named followed by ...
3258 // arguments are separated by , unless a ; is in the list, then ; is the
3260 protected function argumentDef(&$args, &$isVararg) {
3262 if (!$this->literal('(')) return false;
3266 $method = "expressionList";
3270 if ($this->literal("...")) {
3275 if ($this->$method($value)) {
3276 if ($value[0] == "variable") {
3277 $arg = array("arg", $value[1]);
3278 $ss = $this->seek();
3280 if ($this->assign() && $this->$method($rhs)) {
3284 if ($this->literal("...")) {
3291 if ($isVararg) break;
3294 $values[] = array("lit", $value);
3299 if (!$this->literal($delim)) {
3300 if ($delim == "," && $this->literal(";")) {
3301 // found new delim, convert existing args
3303 $method = "propertyValue";
3305 // transform arg list
3306 if (isset($values[1])) { // 2 items
3308 foreach ($values as $i => $arg) {
3312 $this->throwError("Cannot mix ; and , as delimiter types");
3314 $newList[] = $arg[2];
3317 $newList[] = $arg[1];
3320 $this->throwError("Unexpected rest before semicolon");
3324 $newList = array("list", ", ", $newList);
3326 switch ($values[0][0]) {
3328 $newArg = array("arg", $values[0][1], $newList);
3331 $newArg = array("lit", $newList);
3335 } elseif ($values) { // 1 item
3336 $newArg = $values[0];
3340 $values = array($newArg);
3348 if (!$this->literal(')')) {
3358 // consume a list of tags
3359 // this accepts a hanging delimiter
3360 protected function tags(&$tags, $simple = false, $delim = ',') {
3362 while ($this->tag($tt, $simple)) {
3364 if (!$this->literal($delim)) break;
3366 if (count($tags) == 0) return false;
3371 // list of tags of specifying mixin path
3372 // optionally separated by > (lazy, accepts extra >)
3373 protected function mixinTags(&$tags) {
3375 while ($this->tag($tt, true)) {
3377 $this->literal(">");
3380 if (count($tags) == 0) return false;
3385 // a bracketed value (contained within in a tag definition)
3386 protected function tagBracket(&$parts, &$hasExpression) {
3388 if (isset($this->buffer
[$this->count
]) && $this->buffer
[$this->count
] != "[") {
3394 $hasInterpolation = false;
3396 if ($this->literal("[", false)) {
3397 $attrParts = array("[");
3398 // keyword, string, operator
3400 if ($this->literal("]", false)) {
3402 break; // get out early
3405 if ($this->match('\s+', $m)) {
3409 if ($this->stringValue($str)) {
3410 // escape parent selector, (yuck)
3411 foreach ($str[2] as &$chunk) {
3412 if (is_string($chunk)) {
3413 $chunk = str_replace($this->lessc
->parentSelector
, "$&$", $chunk);
3417 $attrParts[] = $str;
3418 $hasInterpolation = true;
3422 if ($this->keyword($word)) {
3423 $attrParts[] = $word;
3427 if ($this->interpolation($inter, false)) {
3428 $attrParts[] = $inter;
3429 $hasInterpolation = true;
3433 // operator, handles attr namespace too
3434 if ($this->match('[|-~\$\*\^=]+', $m)) {
3435 $attrParts[] = $m[0];
3442 if ($this->literal("]", false)) {
3444 foreach ($attrParts as $part) {
3447 $hasExpression = $hasExpression ||
$hasInterpolation;
3457 // a space separated list of selectors
3458 protected function tag(&$tag, $simple = false) {
3460 $chars = '^@,:;{}\][>\(\) "\'';
3462 $chars = '^@,;{}["\'';
3466 $hasExpression = false;
3468 while ($this->tagBracket($parts, $hasExpression));
3470 $oldWhite = $this->eatWhiteDefault
;
3471 $this->eatWhiteDefault
= false;
3474 if ($this->match('(['.$chars.'0-9]['.$chars.']*)', $m)) {
3478 while ($this->tagBracket($parts, $hasExpression));
3482 if (isset($this->buffer
[$this->count
]) && $this->buffer
[$this->count
] == "@") {
3483 if ($this->interpolation($interp)) {
3484 $hasExpression = true;
3485 $interp[2] = true; // don't unescape
3490 if ($this->literal("@")) {
3496 if ($this->unit($unit)) { // for keyframes
3497 $parts[] = $unit[1];
3498 $parts[] = $unit[2];
3505 $this->eatWhiteDefault
= $oldWhite;
3511 if ($hasExpression) {
3512 $tag = array("exp", array("string", "", $parts));
3514 $tag = trim(implode($parts));
3517 $this->whitespace();
3522 protected function func(&$func) {
3525 if ($this->match('(%|[\w\-_][\w\-_:\.]+|[\w_])', $m) && $this->literal('(')) {
3528 $sPreArgs = $this->seek();
3532 $ss = $this->seek();
3533 // this ugly nonsense is for ie filter properties
3534 if ($this->keyword($name) && $this->literal('=') && $this->expressionList($value)) {
3535 $args[] = array("string", "", array($name, "=", $value));
3538 if ($this->expressionList($value)) {
3543 if (!$this->literal(',')) break;
3545 $args = array('list', ',', $args);
3547 if ($this->literal(')')) {
3548 $func = array('function', $fname, $args);
3550 } elseif ($fname == 'url') {
3551 // couldn't parse and in url? treat as string
3552 $this->seek($sPreArgs);
3553 if ($this->openString(")", $string) && $this->literal(")")) {
3554 $func = array('function', $fname, $string);
3564 // consume a less variable
3565 protected function variable(&$name) {
3567 if ($this->literal($this->lessc
->vPrefix
, false) &&
3568 ($this->variable($sub) ||
$this->keyword($name)))
3571 $name = array('variable', $sub);
3573 $name = $this->lessc
->vPrefix
.$name;
3584 * Consume an assignment operator
3585 * Can optionally take a name that will be set to the current property name
3587 protected function assign($name = null) {
3588 if ($name) $this->currentProperty
= $name;
3589 return $this->literal(':') ||
$this->literal('=');
3592 // consume a keyword
3593 protected function keyword(&$word) {
3594 if ($this->match('([\w_\-\*!"][\w\-_"]*)', $m)) {
3601 // consume an end of statement delimiter
3602 protected function end() {
3603 if ($this->literal(';', false)) {
3605 } elseif ($this->count
== strlen($this->buffer
) ||
$this->buffer
[$this->count
] == '}') {
3606 // if there is end of file or a closing block next then we don't need a ;
3612 protected function guards(&$guards) {
3615 if (!$this->literal("when")) {
3622 while ($this->guardGroup($g)) {
3624 if (!$this->literal(",")) break;
3627 if (count($guards) == 0) {
3636 // a bunch of guards that are and'd together
3637 // TODO rename to guardGroup
3638 protected function guardGroup(&$guardGroup) {
3640 $guardGroup = array();
3641 while ($this->guard($guard)) {
3642 $guardGroup[] = $guard;
3643 if (!$this->literal("and")) break;
3646 if (count($guardGroup) == 0) {
3655 protected function guard(&$guard) {
3657 $negate = $this->literal("not");
3659 if ($this->literal("(") && $this->expression($exp) && $this->literal(")")) {
3661 if ($negate) $guard = array("negate", $guard);
3669 /* raw parsing functions */
3671 protected function literal($what, $eatWhitespace = null) {
3672 if ($eatWhitespace === null) $eatWhitespace = $this->eatWhiteDefault
;
3674 // shortcut on single letter
3675 if (!isset($what[1]) && isset($this->buffer
[$this->count
])) {
3676 if ($this->buffer
[$this->count
] == $what) {
3677 if (!$eatWhitespace) {
3687 if (!isset(self
::$literalCache[$what])) {
3688 self
::$literalCache[$what] = lessc
::preg_quote($what);
3691 return $this->match(self
::$literalCache[$what], $m, $eatWhitespace);
3694 protected function genericList(&$out, $parseItem, $delim="", $flatten=true) {
3697 while ($this->$parseItem($value)) {
3700 if (!$this->literal($delim)) break;
3704 if (count($items) == 0) {
3709 if ($flatten && count($items) == 1) {
3712 $out = array("list", $delim, $items);
3719 // advance counter to next occurrence of $what
3720 // $until - don't include $what in advance
3721 // $allowNewline, if string, will be used as valid char set
3722 protected function to($what, &$out, $until = false, $allowNewline = false) {
3723 if (is_string($allowNewline)) {
3724 $validChars = $allowNewline;
3726 $validChars = $allowNewline ?
"." : "[^\n]";
3728 if (!$this->match('('.$validChars.'*?)'.lessc
::preg_quote($what), $m, !$until)) return false;
3729 if ($until) $this->count
-= strlen($what); // give back $what
3734 // try to match something on head of buffer
3735 protected function match($regex, &$out, $eatWhitespace = null) {
3736 if ($eatWhitespace === null) $eatWhitespace = $this->eatWhiteDefault
;
3738 $r = '/'.$regex.($eatWhitespace && !$this->writeComments ?
'\s*' : '').'/Ais';
3739 if (preg_match($r, $this->buffer
, $out, null, $this->count
)) {
3740 $this->count +
= strlen($out[0]);
3741 if ($eatWhitespace && $this->writeComments
) $this->whitespace();
3747 // match some whitespace
3748 protected function whitespace() {
3749 if ($this->writeComments
) {
3751 while (preg_match(self
::$whitePattern, $this->buffer
, $m, null, $this->count
)) {
3752 if (isset($m[1]) && empty($this->seenComments
[$this->count
])) {
3753 $this->append(array("comment", $m[1]));
3754 $this->seenComments
[$this->count
] = true;
3756 $this->count +
= strlen($m[0]);
3761 $this->match("", $m);
3762 return strlen($m[0]) > 0;
3766 // match something without consuming it
3767 protected function peek($regex, &$out = null, $from=null) {
3768 if (is_null($from)) $from = $this->count
;
3769 $r = '/'.$regex.'/Ais';
3770 $result = preg_match($r, $this->buffer
, $out, null, $from);
3775 // seek to a spot in the buffer or return where we are on no argument
3776 protected function seek($where = null) {
3777 if ($where === null) return $this->count
;
3778 else $this->count
= $where;
3782 /* misc functions */
3784 public function throwError($msg = "parse error", $count = null) {
3785 $count = is_null($count) ?
$this->count
: $count;
3787 $line = $this->line +
3788 substr_count(substr($this->buffer
, 0, $count), "\n");
3790 if (!empty($this->sourceName
)) {
3791 $loc = "$this->sourceName on line $line";
3793 $loc = "line: $line";
3796 // TODO this depends on $this->count
3797 if ($this->peek("(.*?)(\n|$)", $m, $count)) {
3798 throw new \
Exception("$msg: failed at `$m[1]` $loc");
3800 throw new \
Exception("$msg: $loc");
3804 protected function pushBlock($selectors=null, $type=null) {
3805 $b = new \
stdClass();
3806 $b->parent
= $this->env
;
3809 $b->id
= self
::$nextBlockId++
;
3811 $b->isVararg
= false; // TODO: kill me from here
3812 $b->tags
= $selectors;
3814 $b->props
= array();
3815 $b->children
= array();
3817 // add a reference to the parser so
3818 // we can access the parser to throw errors
3819 // or retrieve the sourceName of this block.
3822 // so we know the position of this block
3823 $b->count
= $this->count
;
3829 // push a block that doesn't multiply tags
3830 protected function pushSpecialBlock($type) {
3831 return $this->pushBlock(null, $type);
3834 // append a property to the current block
3835 protected function append($prop, $pos = null) {
3836 if ($pos !== null) $prop[-1] = $pos;
3837 $this->env
->props
[] = $prop;
3840 // pop something off the stack
3841 protected function pop() {
3843 $this->env
= $this->env
->parent
;
3847 // remove comments from $text
3848 // todo: make it work for all functions, not just url
3849 protected function removeComments($text) {
3851 'url(', '//', '/*', '"', "'"
3857 // find the next item
3858 foreach ($look as $token) {
3859 $pos = strpos($text, $token);
3860 if ($pos !== false) {
3861 if (!isset($min) ||
$pos < $min[1]) $min = array($token, $pos);
3865 if (is_null($min)) break;
3872 if (preg_match('/url\(.*?\)/', $text, $m, 0, $count))
3873 $count +
= strlen($m[0]) - strlen($min[0]);
3877 if (preg_match('/'.$min[0].'.*?(?<!\\\\)'.$min[0].'/', $text, $m, 0, $count))
3878 $count +
= strlen($m[0]) - 1;
3881 $skip = strpos($text, "\n", $count);
3882 if ($skip === false) $skip = strlen($text) - $count;
3883 else $skip -= $count;
3886 if (preg_match('/\/\*.*?\*\//s', $text, $m, 0, $count)) {
3887 $skip = strlen($m[0]);
3888 $newlines = substr_count($m[0], "\n");
3893 if ($skip == 0) $count +
= strlen($min[0]);
3895 $out .= substr($text, 0, $count).str_repeat("\n", $newlines);
3896 $text = substr($text, $count +
$skip);
3906 class lessc_formatter_classic
{
3907 public $indentChar = " ";
3909 public $break = "\n";
3910 public $open = " {";
3911 public $close = "}";
3912 public $selectorSeparator = ", ";
3913 public $assignSeparator = ":";
3915 public $openSingle = " { ";
3916 public $closeSingle = " }";
3918 public $disableSingle = false;
3919 public $breakSelectors = false;
3921 public $compressColors = false;
3923 public function __construct() {
3924 $this->indentLevel
= 0;
3927 public function indentStr($n = 0) {
3928 return str_repeat($this->indentChar
, max($this->indentLevel +
$n, 0));
3931 public function property($name, $value) {
3932 return $name . $this->assignSeparator
. $value . ";";
3935 protected function isEmpty($block) {
3936 if (empty($block->lines
)) {
3937 foreach ($block->children
as $child) {
3938 if (!$this->isEmpty($child)) return false;
3946 public function block($block) {
3947 if ($this->isEmpty($block)) return;
3949 $inner = $pre = $this->indentStr();
3951 $isSingle = !$this->disableSingle
&&
3952 is_null($block->type
) && count($block->lines
) == 1;
3954 if (!empty($block->selectors
)) {
3955 $this->indentLevel++
;
3957 if ($this->breakSelectors
) {
3958 $selectorSeparator = $this->selectorSeparator
. $this->break . $pre;
3960 $selectorSeparator = $this->selectorSeparator
;
3964 implode($selectorSeparator, $block->selectors
);
3966 echo $this->openSingle
;
3969 echo $this->open
. $this->break;
3970 $inner = $this->indentStr();
3975 if (!empty($block->lines
)) {
3976 $glue = $this->break.$inner;
3977 echo $inner . implode($glue, $block->lines
);
3978 if (!$isSingle && !empty($block->children
)) {
3983 foreach ($block->children
as $child) {
3984 $this->block($child);
3987 if (!empty($block->selectors
)) {
3988 if (!$isSingle && empty($block->children
)) echo $this->break;
3991 echo $this->closeSingle
. $this->break;
3993 echo $pre . $this->close
. $this->break;
3996 $this->indentLevel
--;
4001 class lessc_formatter_compressed
extends lessc_formatter_classic
{
4002 public $disableSingle = true;
4004 public $selectorSeparator = ",";
4005 public $assignSeparator = ":";
4007 public $compressColors = true;
4009 public function indentStr($n = 0) {
4014 class lessc_formatter_lessjs
extends lessc_formatter_classic
{
4015 public $disableSingle = true;
4016 public $breakSelectors = true;
4017 public $assignSeparator = ": ";
4018 public $selectorSeparator = ",";