43 public static $VERSION =
"v0.8.0";
45 public static $TRUE = array(
"keyword",
"true");
46 public static $FALSE = array(
"keyword",
"false");
48 protected $libFunctions = array();
49 protected $registeredVars = array();
50 protected $preserveComments =
false;
52 public $vPrefix =
'@';
53 public $mPrefix =
'$';
54 public $parentSelector =
'&';
56 public $importDisabled =
false;
57 public $importDir =
'';
61 public $formatterName;
67 protected $numberPrecision =
null;
69 protected $allParsedFiles = array();
73 protected $sourceParser =
null;
74 protected $sourceLoc =
null;
76 protected static $nextImportId = 0;
79 protected function findImport($url)
81 foreach ((array) $this->importDir as $dir) {
82 $full = $dir.(substr($dir, -1) !=
'/' ?
'/' :
'').$url;
99 return is_file($name);
102 public static function compressList($items, $delim)
104 if (!isset($items[1]) && isset($items[0])) {
107 return array(
'list', $delim, $items);
111 public static function preg_quote($what)
113 return preg_quote($what,
'/');
116 protected function tryImport($importPath, $parentBlock, $out)
118 if ($importPath[0] ==
"function" && $importPath[1] ==
"url") {
119 $importPath = $this->flattenList($importPath[2]);
122 $str = $this->coerceString($importPath);
130 if (substr_compare($url,
'.css', -4, 4) === 0) {
134 $realPath = $this->findImport($url);
136 if ($realPath ===
null) {
140 if ($this->importDisabled) {
141 return array(
false,
"/* import disabled */");
144 if (isset($this->allParsedFiles[realpath($realPath)])) {
145 return array(
false,
null);
148 $this->addParsedFile($realPath);
149 $parser = $this->makeParser($realPath);
150 $root = $parser->parse(file_get_contents($realPath));
153 foreach ($root->props as $prop) {
154 if ($prop[0] ==
"block") {
155 $prop[1]->parent = $parentBlock;
162 foreach ($root->children as $childName => $child) {
163 if (isset($parentBlock->children[$childName])) {
164 $parentBlock->children[$childName] = array_merge(
165 $parentBlock->children[$childName],
169 $parentBlock->children[$childName] = $child;
173 $pi = pathinfo($realPath);
174 $dir = $pi[
"dirname"];
176 list($top, $bottom) = $this->sortProps($root->props,
true);
177 $this->compileImportedProps($top, $parentBlock, $out, $parser, $dir);
179 return array(
true, $bottom, $parser, $dir);
182 protected function compileImportedProps($props, $block, $out, $sourceParser, $importDir)
184 $oldSourceParser = $this->sourceParser;
186 $oldImport = $this->importDir;
189 $this->importDir = (array) $this->importDir;
190 array_unshift($this->importDir, $importDir);
192 foreach ($props as $prop) {
193 $this->compileProp($prop, $block, $out);
196 $this->importDir = $oldImport;
197 $this->sourceParser = $oldSourceParser;
223 switch ($block->type) {
225 $this->compileRoot($block);
228 $this->compileCSSBlock($block);
231 $this->compileMedia($block);
234 $name =
"@".$block->name;
235 if (!empty($block->value)) {
236 $name .=
" ".$this->compileValue($this->reduce($block->value));
239 $this->compileNestedBlock($block, array($name));
242 $this->
throwError(
"unknown block type: $block->type\n");
246 protected function compileCSSBlock($block)
248 $env = $this->pushEnv();
250 $selectors = $this->compileSelectors($block->tags);
251 $env->selectors = $this->multiplySelectors($selectors);
252 $out = $this->makeOutputBlock(
null, $env->selectors);
254 $this->scope->children[] = $out;
255 $this->compileProps($block, $out);
257 $block->scope = $env;
261 protected function compileMedia($media)
263 $env = $this->pushEnv($media);
264 $parentScope = $this->mediaParent($this->scope);
266 $query = $this->compileMediaQuery($this->multiplyMedia($env));
268 $this->scope = $this->makeOutputBlock($media->type, array($query));
269 $parentScope->children[] = $this->scope;
271 $this->compileProps($media, $this->scope);
273 if (count($this->scope->lines) > 0) {
274 $orphanSelelectors = $this->findClosestSelectors();
275 if (!is_null($orphanSelelectors)) {
276 $orphan = $this->makeOutputBlock(
null, $orphanSelelectors);
277 $orphan->lines = $this->scope->lines;
278 array_unshift($this->scope->children, $orphan);
279 $this->scope->lines = array();
283 $this->scope = $this->scope->parent;
287 protected function mediaParent($scope)
289 while (!empty($scope->parent)) {
290 if (!empty($scope->type) && $scope->type !=
"media") {
293 $scope = $scope->parent;
299 protected function compileNestedBlock($block, $selectors)
301 $this->pushEnv($block);
302 $this->scope = $this->makeOutputBlock($block->type, $selectors);
303 $this->scope->parent->children[] = $this->scope;
305 $this->compileProps($block, $this->scope);
307 $this->scope = $this->scope->parent;
311 protected function compileRoot($root)
314 $this->scope = $this->makeOutputBlock($root->type);
315 $this->compileProps($root, $this->scope);
319 protected function compileProps($block, $out)
321 foreach ($this->sortProps($block->props) as $prop) {
322 $this->compileProp($prop, $block, $out);
337 foreach ($lines as $line) {
338 if (strpos($line,
'/*') === 0) {
342 if (!in_array($line, $unique)) {
345 array_splice($unique, array_search($line, $unique), 0, $comments);
348 return array_merge($unique, $comments);
351 protected function sortProps($props, $split =
false)
358 foreach ($props as $prop) {
365 if (isset($prop[1][0]) && $prop[1][0] == $this->vPrefix) {
366 $vars = array_merge($vars, $stack);
368 $other = array_merge($other, $stack);
373 $id = self::$nextImportId++;
376 $imports = array_merge($imports, $stack);
377 $other[] = array(
"import_mixin", $id);
382 $other = array_merge($other, $stack);
387 $other = array_merge($other, $stack);
390 return array(array_merge($imports, $vars), $other);
392 return array_merge($imports, $vars, $other);
396 protected function compileMediaQuery($queries)
398 $compiledQueries = array();
399 foreach ($queries as $query) {
401 foreach ($query as $q) {
404 $parts[] = implode(
" ", array_slice($q, 1));
408 $parts[] =
"($q[1]: ".
411 $parts[] =
"($q[1])";
420 if (count($parts) > 0) {
421 $compiledQueries[] = implode(
" and ", $parts);
426 if (!empty($parts)) {
428 implode($this->formatter->selectorSeparator, $compiledQueries);
433 protected function multiplyMedia($env, $childQueries =
null)
436 !empty($env->block->type) && $env->block->type !=
"media"
438 return $childQueries;
442 if (empty($env->block->type)) {
443 return $this->multiplyMedia($env->parent, $childQueries);
447 $queries = $env->block->queries;
448 if (is_null($childQueries)) {
451 foreach ($queries as $parent) {
452 foreach ($childQueries as $child) {
453 $out[] = array_merge($parent, $child);
458 return $this->multiplyMedia($env->parent, $out);
461 protected function expandParentSelectors(&$tag, $replace)
463 $parts = explode(
"$&$", $tag);
465 foreach ($parts as &$part) {
467 $part = str_replace($this->parentSelector, $replace, $part, $c);
470 $tag = implode($this->parentSelector, $parts);
474 protected function findClosestSelectors()
478 while ($env !==
null) {
479 if (isset($env->selectors)) {
480 $selectors = $env->selectors;
491 protected function multiplySelectors($selectors)
495 $parentSelectors = $this->findClosestSelectors();
496 if (is_null($parentSelectors)) {
498 foreach ($selectors as &$s) {
499 $this->expandParentSelectors($s,
"");
506 foreach ($parentSelectors as $parent) {
507 foreach ($selectors as $child) {
508 $count = $this->expandParentSelectors($child, $parent);
512 $out[] = trim($child);
514 $out[] = trim($parent.
' '.$child);
523 protected function compileSelectors($selectors)
527 foreach ($selectors as $s) {
530 $out[] = trim($this->
compileValue($this->reduce($value)));
539 protected function eq($left, $right)
541 return $left == $right;
544 protected function patternMatch($block, $orderedArgs, $keywordArgs)
548 if (!empty($block->guards)) {
549 $groupPassed =
false;
550 foreach ($block->guards as $guardGroup) {
551 foreach ($guardGroup as $guard) {
553 $this->zipSetArgs($block->args, $orderedArgs, $keywordArgs);
556 if ($guard[0] ==
"negate") {
561 $passed = $this->reduce($guard) == self::$TRUE;
571 $groupPassed =
false;
586 if (empty($block->args)) {
587 return $block->isVararg || empty($orderedArgs) && empty($keywordArgs);
590 $remainingArgs = $block->args;
592 $remainingArgs = array();
593 foreach ($block->args as $arg) {
594 if ($arg[0] ==
"arg" && isset($keywordArgs[$arg[1]])) {
598 $remainingArgs[] = $arg;
604 foreach ($remainingArgs as $i => $arg) {
607 if (empty($orderedArgs[$i]) || !$this->eq($arg[1], $orderedArgs[$i])) {
613 if (!isset($orderedArgs[$i]) && !isset($arg[2])) {
623 if ($block->isVararg) {
626 $numMatched = $i + 1;
628 return $numMatched >= count($orderedArgs);
632 protected function patternMatchAll($blocks, $orderedArgs, $keywordArgs, $skip = array())
635 foreach ($blocks as $block) {
637 if (isset($skip[$block->id]) && !isset($block->args)) {
641 if ($this->patternMatch($block, $orderedArgs, $keywordArgs)) {
650 protected function findBlocks($searchIn, $path, $orderedArgs, $keywordArgs, $seen = array())
652 if ($searchIn ==
null) {
655 if (isset($seen[$searchIn->id])) {
658 $seen[$searchIn->id] =
true;
662 if (isset($searchIn->children[$name])) {
663 $blocks = $searchIn->children[$name];
664 if (count($path) == 1) {
665 $matches = $this->patternMatchAll($blocks, $orderedArgs, $keywordArgs, $seen);
666 if (!empty($matches)) {
673 foreach ($blocks as $subBlock) {
674 $subMatches = $this->findBlocks(
676 array_slice($path, 1),
682 if (!is_null($subMatches)) {
683 foreach ($subMatches as $sm) {
689 return count($matches) > 0 ? $matches :
null;
692 if ($searchIn->parent === $searchIn) {
695 return $this->findBlocks($searchIn->parent, $path, $orderedArgs, $keywordArgs, $seen);
700 protected function zipSetArgs($args, $orderedValues, $keywordValues)
702 $assignedValues = array();
705 foreach ($args as $a) {
706 if ($a[0] ==
"arg") {
707 if (isset($keywordValues[$a[1]])) {
709 $value = $keywordValues[$a[1]];
710 } elseif (isset($orderedValues[$i])) {
712 $value = $orderedValues[$i];
714 } elseif (isset($a[2])) {
719 $this->
throwError(
"Failed to assign arg ".$a[1]);
722 $value = $this->reduce($value);
723 $this->
set($a[1], $value);
724 $assignedValues[] = $value;
733 if ($last && $last[0] ==
"rest") {
734 $rest = array_slice($orderedValues, count($args) - 1);
735 $this->
set($last[1], $this->reduce(array(
"list",
" ", $rest)));
739 $this->env->arguments = $assignedValues + $orderedValues;
743 protected function compileProp($prop, $block, $out)
746 $this->sourceLoc = isset($prop[-1]) ? $prop[-1] : -1;
750 list(, $name, $value) = $prop;
751 if ($name[0] == $this->vPrefix) {
752 $this->
set($name, $value);
754 $out->lines[] = $this->formatter->property(
761 list(, $child) = $prop;
765 list(, $path, $args, $suffix) = $prop;
767 $orderedArgs = array();
768 $keywordArgs = array();
769 foreach ((array) $args as $arg) {
773 if (!isset($arg[2])) {
774 $orderedArgs[] = $this->reduce(array(
"variable", $arg[1]));
776 $keywordArgs[$arg[1]] = $this->reduce($arg[2]);
781 $orderedArgs[] = $this->reduce($arg[1]);
784 $this->
throwError(
"Unknown arg type: ".$arg[0]);
788 $mixins = $this->findBlocks($block, $path, $orderedArgs, $keywordArgs);
790 if ($mixins ===
null) {
791 $this->
throwError(
"{$prop[1][0]} is undefined");
794 foreach ($mixins as $mixin) {
795 if ($mixin === $block && !$orderedArgs) {
800 if (isset($mixin->parent->scope)) {
802 $mixinParentEnv = $this->pushEnv();
803 $mixinParentEnv->storeParent = $mixin->parent->scope;
807 if (isset($mixin->args)) {
810 $this->zipSetArgs($mixin->args, $orderedArgs, $keywordArgs);
813 $oldParent = $mixin->parent;
814 if ($mixin != $block) {
815 $mixin->parent = $block;
818 foreach ($this->sortProps($mixin->props) as $subProp) {
819 if ($suffix !==
null &&
820 $subProp[0] ==
"assign" &&
821 is_string($subProp[1]) &&
822 $subProp[1][0] != $this->vPrefix
826 array($subProp[2], array(
'keyword', $suffix))
830 $this->compileProp($subProp, $mixin, $out);
833 $mixin->parent = $oldParent;
845 $out->lines[] = $prop[1];
848 list(, $name, $value) = $prop;
849 $out->lines[] =
"@$name ".$this->compileValue($this->reduce($value)).
';';
852 $out->lines[] = $prop[1];
855 list(, $importPath, $importId) = $prop;
856 $importPath = $this->reduce($importPath);
858 if (!isset($this->env->imports)) {
859 $this->env->imports = array();
862 $result = $this->tryImport($importPath, $block, $out);
864 $this->env->imports[$importId] = $result ===
false ?
865 array(
false,
"@import ".$this->
compileValue($importPath).
";") : $result;
869 list(, $importId) = $prop;
870 $import = $this->env->imports[$importId];
871 if ($import[0] ===
false) {
872 if (isset($import[1])) {
873 $out->lines[] = $import[1];
876 list(, $bottom, $parser, $importDir) = $import;
877 $this->compileImportedProps($bottom, $block, $out, $parser, $importDir);
882 $this->
throwError(
"unknown op: {$prop[0]}\n");
904 return implode($value[1], array_map(array($this,
'compileValue'), $value[2]));
906 if (!empty($this->formatter->compressColors)) {
914 list(, $num, $unit) = $value;
917 if ($this->numberPrecision !==
null) {
918 $num = round($num, $this->numberPrecision);
923 list(, $delim, $content) = $value;
924 foreach ($content as &$part) {
925 if (is_array($part)) {
929 return $delim.implode($content).$delim;
935 list(, $r, $g, $b) = $value;
940 if (count($value) == 5 && $value[4] != 1) {
941 return 'rgba('.$r.
','.$g.
','.$b.
','.$value[4].
')';
944 $h = sprintf(
"#%02x%02x%02x", $r, $g, $b);
946 if (!empty($this->formatter->compressColors)) {
948 if ($h[1] === $h[2] && $h[3] === $h[4] && $h[5] === $h[6]) {
949 $h =
'#'.$h[1].$h[3].$h[5];
956 list(, $name, $args) = $value;
959 $this->
throwError(
"unknown value type: $value[0]");
963 protected function lib_pow($args)
965 list($base, $exp) = $this->assertArgs($args, 2,
"pow");
966 return pow($this->assertNumber($base), $this->assertNumber($exp));
969 protected function lib_pi()
974 protected function lib_mod($args)
976 list($a, $b) = $this->assertArgs($args, 2,
"mod");
977 return $this->assertNumber($a) % $this->assertNumber($b);
980 protected function lib_tan($num)
982 return tan($this->assertNumber($num));
985 protected function lib_sin($num)
987 return sin($this->assertNumber($num));
990 protected function lib_cos($num)
992 return cos($this->assertNumber($num));
995 protected function lib_atan($num)
997 $num = atan($this->assertNumber($num));
998 return array(
"number", $num,
"rad");
1001 protected function lib_asin($num)
1003 $num = asin($this->assertNumber($num));
1004 return array(
"number", $num,
"rad");
1007 protected function lib_acos($num)
1009 $num = acos($this->assertNumber($num));
1010 return array(
"number", $num,
"rad");
1013 protected function lib_sqrt($num)
1015 return sqrt($this->assertNumber($num));
1018 protected function lib_extract($value)
1020 list($list, $idx) = $this->assertArgs($value, 2,
"extract");
1021 $idx = $this->assertNumber($idx);
1023 if ($list[0] ==
"list" && isset($list[2][$idx - 1])) {
1024 return $list[2][$idx - 1];
1029 protected function lib_isnumber($value)
1031 return $this->toBool($value[0] ==
"number");
1034 protected function lib_isstring($value)
1036 return $this->toBool($value[0] ==
"string");
1039 protected function lib_iscolor($value)
1041 return $this->toBool($this->coerceColor($value));
1044 protected function lib_iskeyword($value)
1046 return $this->toBool($value[0] ==
"keyword");
1049 protected function lib_ispixel($value)
1051 return $this->toBool($value[0] ==
"number" && $value[2] ==
"px");
1054 protected function lib_ispercentage($value)
1056 return $this->toBool($value[0] ==
"number" && $value[2] ==
"%");
1059 protected function lib_isem($value)
1061 return $this->toBool($value[0] ==
"number" && $value[2] ==
"em");
1064 protected function lib_isrem($value)
1066 return $this->toBool($value[0] ==
"number" && $value[2] ==
"rem");
1069 protected function lib_rgbahex($color)
1071 $color = $this->coerceColor($color);
1072 if (is_null($color)) {
1073 $this->
throwError(
"color expected for rgbahex");
1077 "#%02x%02x%02x%02x",
1078 isset($color[4]) ? $color[4] * 255 : 255,
1085 protected function lib_argb($color)
1087 return $this->lib_rgbahex($color);
1098 $mime = ($value[0] ===
'list') ? $value[2][0][2] :
null;
1099 $url = ($value[0] ===
'list') ? $value[2][1][2][0] : $value[2][0];
1101 $fullpath = $this->findImport($url);
1103 if ($fullpath && ($fsize = filesize($fullpath)) !==
false) {
1105 if ($fsize / 1024 < 32) {
1106 if (is_null($mime)) {
1107 if (class_exists(
'finfo')) {
1108 $finfo =
new finfo(FILEINFO_MIME);
1109 $mime = explode(
'; ', $finfo->file($fullpath));
1111 } elseif (function_exists(
'mime_content_type')) {
1112 $mime = mime_content_type($fullpath);
1116 if (!is_null($mime)) {
1117 $url = sprintf(
'data:%s;base64,%s', $mime, base64_encode(file_get_contents($fullpath)));
1122 return 'url("'.$url.
'")';
1126 protected function lib_e($arg)
1131 if (isset($items[0])) {
1132 return $this->lib_e($items[0]);
1146 protected function lib__sprintf($args)
1148 if ($args[0] !=
"list") {
1152 $string = array_shift($values);
1153 $template = $this->
compileValue($this->lib_e($string));
1157 if (preg_match_all(
'/%[dsa]/', $template, $m)) {
1158 foreach ($m[0] as $match) {
1159 $val = isset($values[$i]) ?
1160 $this->reduce($values[$i]) : array(
'keyword',
'');
1163 if ($color = $this->coerceColor($val)) {
1169 $template = preg_replace(
1170 '/'.self::preg_quote($match).
'/',
1178 $d = $string[0] ==
"string" ? $string[1] :
'"';
1179 return array(
"string", $d, array($template));
1182 protected function lib_floor($arg)
1184 $value = $this->assertNumber($arg);
1185 return array(
"number", floor($value), $arg[2]);
1188 protected function lib_ceil($arg)
1190 $value = $this->assertNumber($arg);
1191 return array(
"number", ceil($value), $arg[2]);
1194 protected function lib_round($arg)
1196 if ($arg[0] !=
"list") {
1197 $value = $this->assertNumber($arg);
1198 return array(
"number", round($value), $arg[2]);
1200 $value = $this->assertNumber($arg[2][0]);
1201 $precision = $this->assertNumber($arg[2][1]);
1202 return array(
"number", round($value, $precision), $arg[2][0][2]);
1206 protected function lib_unit($arg)
1208 if ($arg[0] ==
"list") {
1209 list($number, $newUnit) = $arg[2];
1210 return array(
"number", $this->assertNumber($number),
1213 return array(
"number", $this->assertNumber($arg),
"");
1223 if ($args[0] !=
'list' || count($args[2]) < 2) {
1224 return array(array(
'color', 0, 0, 0), 0);
1226 list($color, $delta) = $args[2];
1227 $color = $this->assertColor($color);
1228 $delta = (float) $delta[1];
1230 return array($color, $delta);
1233 protected function lib_darken($args)
1235 list($color, $delta) = $this->
colorArgs($args);
1237 $hsl = $this->toHSL($color);
1238 $hsl[3] = $this->clamp($hsl[3] - $delta, 100);
1239 return $this->
toRGB($hsl);
1242 protected function lib_lighten($args)
1244 list($color, $delta) = $this->
colorArgs($args);
1246 $hsl = $this->toHSL($color);
1247 $hsl[3] = $this->clamp($hsl[3] + $delta, 100);
1248 return $this->
toRGB($hsl);
1251 protected function lib_saturate($args)
1253 list($color, $delta) = $this->
colorArgs($args);
1255 $hsl = $this->toHSL($color);
1256 $hsl[2] = $this->clamp($hsl[2] + $delta, 100);
1257 return $this->
toRGB($hsl);
1260 protected function lib_desaturate($args)
1262 list($color, $delta) = $this->
colorArgs($args);
1264 $hsl = $this->toHSL($color);
1265 $hsl[2] = $this->clamp($hsl[2] - $delta, 100);
1266 return $this->
toRGB($hsl);
1269 protected function lib_spin($args)
1271 list($color, $delta) = $this->
colorArgs($args);
1273 $hsl = $this->toHSL($color);
1275 $hsl[1] = $hsl[1] + $delta % 360;
1280 return $this->
toRGB($hsl);
1283 protected function lib_fadeout($args)
1285 list($color, $delta) = $this->
colorArgs($args);
1286 $color[4] = $this->clamp((isset($color[4]) ? $color[4] : 1) - $delta / 100);
1290 protected function lib_fadein($args)
1292 list($color, $delta) = $this->
colorArgs($args);
1293 $color[4] = $this->clamp((isset($color[4]) ? $color[4] : 1) + $delta / 100);
1297 protected function lib_hue($color)
1299 $hsl = $this->toHSL($this->assertColor($color));
1300 return round($hsl[1]);
1303 protected function lib_saturation($color)
1305 $hsl = $this->toHSL($this->assertColor($color));
1306 return round($hsl[2]);
1309 protected function lib_lightness($color)
1311 $hsl = $this->toHSL($this->assertColor($color));
1312 return round($hsl[3]);
1317 protected function lib_alpha($value)
1319 if (!is_null($color = $this->coerceColor($value))) {
1320 return isset($color[4]) ? $color[4] : 1;
1326 protected function lib_fade($args)
1328 list($color, $alpha) = $this->
colorArgs($args);
1329 $color[4] = $this->clamp($alpha / 100.0);
1333 protected function lib_percentage($arg)
1335 $num = $this->assertNumber($arg);
1336 return array(
"number", $num * 100,
"%");
1352 $white = [
'color', 255, 255, 255];
1353 if ($args[0] ==
'color') {
1354 return $this->
lib_mix([
'list',
',', [$white, $args]]);
1355 } elseif ($args[0] ==
"list" && count($args[2]) == 2) {
1356 return $this->
lib_mix([$args[0], $args[1], [$white, $args[2][0], $args[2][1]]]);
1358 $this->
throwError(
"tint expects (color, weight)");
1376 $black = [
'color', 0, 0, 0];
1377 if ($args[0] ==
'color') {
1378 return $this->
lib_mix([
'list',
',', [$black, $args]]);
1379 } elseif ($args[0] ==
"list" && count($args[2]) == 2) {
1380 return $this->
lib_mix([$args[0], $args[1], [$black, $args[2][0], $args[2][1]]]);
1382 $this->
throwError(
"shade expects (color, weight)");
1398 if ($args[0] !=
"list" || count($args[2]) < 2) {
1399 $this->
throwError(
"mix expects (color1, color2, weight)");
1402 list($first, $second) = $args[2];
1403 $first = $this->assertColor($first);
1404 $second = $this->assertColor($second);
1406 $first_a = $this->lib_alpha($first);
1407 $second_a = $this->lib_alpha($second);
1409 if (isset($args[2][2])) {
1410 $weight = $args[2][2][1] / 100.0;
1415 $w = $weight * 2 - 1;
1416 $a = $first_a - $second_a;
1418 $w1 = (($w * $a == -1 ? $w : ($w + $a) / (1 + $w * $a)) + 1) / 2.0;
1421 $new = array(
'color',
1422 $w1 * $first[1] + $w2 * $second[1],
1423 $w1 * $first[2] + $w2 * $second[2],
1424 $w1 * $first[3] + $w2 * $second[3],
1427 if ($first_a != 1.0 || $second_a != 1.0) {
1428 $new[] = $first_a * $weight + $second_a * ($weight - 1);
1431 return $this->fixColor($new);
1442 $darkColor = array(
'color', 0, 0, 0);
1443 $lightColor = array(
'color', 255, 255, 255);
1446 if ($args[0] ==
'list') {
1447 $inputColor = (isset($args[2][0])) ? $this->assertColor($args[2][0]) : $lightColor;
1448 $darkColor = (isset($args[2][1])) ? $this->assertColor($args[2][1]) : $darkColor;
1449 $lightColor = (isset($args[2][2])) ? $this->assertColor($args[2][2]) : $lightColor;
1450 $threshold = (isset($args[2][3])) ? $this->assertNumber($args[2][3]) : $threshold;
1452 $inputColor = $this->assertColor($args);
1455 $inputColor = $this->coerceColor($inputColor);
1456 $darkColor = $this->coerceColor($darkColor);
1457 $lightColor = $this->coerceColor($lightColor);
1460 if ($this->toLuma($darkColor) > $this->toLuma($lightColor)) {
1462 $lightColor = $darkColor;
1466 $inputColor_alpha = $this->lib_alpha($inputColor);
1467 if (($this->toLuma($inputColor) * $inputColor_alpha) < $threshold) {
1473 private function toLuma($color)
1475 list(, $r, $g, $b) = $this->coerceColor($color);
1481 $r = ($r <= 0.03928) ? $r / 12.92 : pow((($r + 0.055) / 1.055), 2.4);
1482 $g = ($g <= 0.03928) ? $g / 12.92 : pow((($g + 0.055) / 1.055), 2.4);
1483 $b = ($b <= 0.03928) ? $b / 12.92 : pow((($b + 0.055) / 1.055), 2.4);
1485 return (0.2126 * $r) + (0.7152 * $g) + (0.0722 * $b);
1488 protected function lib_luma($color)
1490 return array(
"number", round($this->toLuma($color) * 100, 8),
"%");
1494 public function assertColor($value, $error =
"expected color value")
1496 $color = $this->coerceColor($value);
1497 if (is_null($color)) {
1503 public function assertNumber($value, $error =
"expecting number")
1505 if ($value[0] ==
"number") {
1511 public function assertArgs($value, $expectedArgs, $name =
"")
1513 if ($expectedArgs == 1) {
1516 if ($value[0] !==
"list" || $value[1] !=
",") {
1519 $values = $value[2];
1520 $numValues = count($values);
1521 if ($expectedArgs != $numValues) {
1526 $this->
throwError(
"{$name}expecting $expectedArgs arguments, got $numValues");
1533 protected function toHSL($color)
1535 if ($color[0] ===
'hsl') {
1539 $r = $color[1] / 255;
1540 $g = $color[2] / 255;
1541 $b = $color[3] / 255;
1543 $min = min($r, $g, $b);
1544 $max = max($r, $g, $b);
1546 $L = ($min + $max) / 2;
1551 $S = ($max - $min) / ($max + $min);
1553 $S = ($max - $min) / (2.0 - $max - $min);
1556 $H = ($g - $b) / ($max - $min);
1557 } elseif ($g == $max) {
1558 $H = 2.0 + ($b - $r) / ($max - $min);
1559 } elseif ($b == $max) {
1560 $H = 4.0 + ($r - $g) / ($max - $min);
1565 ($H < 0 ? $H + 6 : $H) * 60,
1570 if (count($color) > 4) {
1577 protected function toRGB_helper($comp, $temp1, $temp2)
1581 } elseif ($comp > 1) {
1585 if (6 * $comp < 1) {
1586 return $temp1 + ($temp2 - $temp1) * 6 * $comp;
1588 if (2 * $comp < 1) {
1591 if (3 * $comp < 2) {
1592 return $temp1 + ($temp2 - $temp1) * ((2 / 3) - $comp) * 6;
1604 if ($color[0] ===
'color') {
1608 $H = $color[1] / 360;
1609 $S = $color[2] / 100;
1610 $L = $color[3] / 100;
1616 $L * (1.0 + $S) : $L + $S - $L * $S;
1618 $temp1 = 2.0 * $L - $temp2;
1620 $r = $this->toRGB_helper($H + 1 / 3, $temp1, $temp2);
1621 $g = $this->toRGB_helper($H, $temp1, $temp2);
1622 $b = $this->toRGB_helper($H - 1 / 3, $temp1, $temp2);
1626 $out = array(
'color', $r * 255, $g * 255, $b * 255);
1627 if (count($color) > 4) {
1634 protected function clamp($v, $max = 1, $min = 0)
1636 return min($max, max($min, $v));
1646 if ($func[2][0] !=
'list') {
1650 $rawComponents = $func[2][2];
1652 if ($fname ==
'hsl' || $fname ==
'hsla') {
1653 $hsl = array(
'hsl');
1655 foreach ($rawComponents as $c) {
1656 $val = $this->reduce($c);
1657 $val = isset($val[1]) ? (float) $val[1] : 0;
1667 $hsl[] = $this->clamp($val, $clamp);
1671 while (count($hsl) < 4) {
1674 return $this->
toRGB($hsl);
1676 } elseif ($fname ==
'rgb' || $fname ==
'rgba') {
1677 $components = array();
1679 foreach ($rawComponents as $c) {
1680 $c = $this->reduce($c);
1682 if ($c[0] ==
"number" && $c[2] ==
"%") {
1683 $components[] = 255 * ($c[1] / 100);
1685 $components[] = (float) $c[1];
1687 } elseif ($i == 4) {
1688 if ($c[0] ==
"number" && $c[2] ==
"%") {
1689 $components[] = 1.0 * ($c[1] / 100);
1691 $components[] = (float) $c[1];
1699 while (count($components) < 3) {
1702 array_unshift($components,
'color');
1703 return $this->fixColor($components);
1709 protected function reduce($value, $forExpression =
false)
1711 switch ($value[0]) {
1713 $reduced = $this->reduce($value[1]);
1715 $res = $this->reduce(array(
"variable", $this->vPrefix.$var));
1717 if ($res[0] ==
"raw_color") {
1718 $res = $this->coerceColor($res);
1721 if (empty($value[2])) {
1722 $res = $this->lib_e($res);
1728 if (is_array($key)) {
1729 $key = $this->reduce($key);
1730 $key = $this->vPrefix.$this->compileValue($this->lib_e($key));
1733 $seen = & $this->env->seenNames;
1735 if (!empty($seen[$key])) {
1736 $this->
throwError(
"infinite loop detected: $key");
1740 $out = $this->reduce($this->
get($key));
1741 $seen[$key] =
false;
1744 foreach ($value[2] as &$item) {
1745 $item = $this->reduce($item, $forExpression);
1749 return $this->evaluate($value);
1751 foreach ($value[2] as &$part) {
1752 if (is_array($part)) {
1753 $strip = $part[0] ==
"variable";
1754 $part = $this->reduce($part);
1756 $part = $this->lib_e($part);
1762 list(, $inner) = $value;
1763 return $this->lib_e($this->reduce($inner));
1770 list(, $name, $args) = $value;
1775 $f = isset($this->libFunctions[$name]) ?
1776 $this->libFunctions[$name] : array($this,
'lib_'.str_replace(
'-',
'_', $name));
1778 if (is_callable($f)) {
1779 if ($args[0] ==
'list') {
1780 $args = self::compressList($args[2], $args[1]);
1783 $ret = call_user_func($f, $this->reduce($args,
true), $this);
1785 if (is_null($ret)) {
1786 return array(
"string",
"", array(
1787 $name,
"(", $args,
")"
1792 if (is_numeric($ret)) {
1793 $ret = array(
'number', $ret,
"");
1794 } elseif (!is_array($ret)) {
1795 $ret = array(
'keyword', $ret);
1802 $value[2] = $this->reduce($value[2]);
1805 list(, $op, $exp) = $value;
1806 $exp = $this->reduce($exp);
1808 if ($exp[0] ==
"number") {
1817 return array(
"string",
"", array($op, $exp));
1820 if ($forExpression) {
1821 switch ($value[0]) {
1823 if ($color = $this->coerceColor($value)) {
1828 return $this->coerceColor($value);
1837 protected function coerceColor($value)
1839 switch ($value[0]) {
1843 $c = array(
"color", 0, 0, 0);
1844 $colorStr = substr($value[1], 1);
1845 $num = hexdec($colorStr);
1846 $width = strlen($colorStr) == 3 ? 16 : 256;
1848 for ($i = 3; $i > 0; $i--) {
1849 $t = intval($num) % $width;
1852 $c[$i] = $t * (256 / $width) + $t * floor(16/$width);
1858 if (isset(self::$cssColors[$name])) {
1859 $rgba = explode(
',', self::$cssColors[$name]);
1861 if (isset($rgba[3])) {
1862 return array(
'color', $rgba[0], $rgba[1], $rgba[2], $rgba[3]);
1864 return array(
'color', $rgba[0], $rgba[1], $rgba[2]);
1872 protected function coerceString($value)
1874 switch ($value[0]) {
1878 return array(
"string",
"", array($value[1]));
1884 protected function flattenList($value)
1886 if ($value[0] ==
"list" && count($value[2]) == 1) {
1887 return $this->flattenList($value[2][0]);
1892 public function toBool($a)
1894 return $a ? self::$TRUE : self::$FALSE;
1898 protected function evaluate($exp)
1900 list(, $op, $left, $right, $whiteBefore, $whiteAfter) = $exp;
1902 $left = $this->reduce($left,
true);
1903 $right = $this->reduce($right,
true);
1905 if ($leftColor = $this->coerceColor($left)) {
1909 if ($rightColor = $this->coerceColor($right)) {
1910 $right = $rightColor;
1918 return $this->toBool($left == self::$TRUE && $right == self::$TRUE);
1922 return $this->toBool($this->eq($left, $right));
1925 if ($op ==
"+" && !is_null($str = $this->stringConcatenate($left, $right))) {
1930 $fname =
"op_{$ltype}_{$rtype}";
1931 if (is_callable(array($this, $fname))) {
1932 $out = $this->$fname($op, $left, $right);
1933 if (!is_null($out)) {
1941 $paddedOp =
" ".$paddedOp;
1947 return array(
"string",
"", array($left, $paddedOp, $right));
1950 protected function stringConcatenate($left, $right)
1952 if ($strLeft = $this->coerceString($left)) {
1953 if ($right[0] ==
"string") {
1956 $strLeft[2][] = $right;
1960 if ($strRight = $this->coerceString($right)) {
1961 array_unshift($strRight[2], $left);
1969 protected function fixColor($c)
1971 foreach (range(1, 3) as $i) {
1983 protected function op_number_color($op, $lft, $rgt)
1985 if ($op ==
'+' || $op ==
'*') {
1986 return $this->op_color_number($op, $rgt, $lft);
1991 protected function op_color_number($op, $lft, $rgt)
1993 if ($rgt[0] ==
'%') {
1997 return $this->op_color_color(
2000 array_fill(1, count($lft) - 1, $rgt[1])
2004 protected function op_color_color($op, $left, $right)
2006 $out = array(
'color');
2007 $max = count($left) > count($right) ? count($left) : count($right);
2008 foreach (range(1, $max - 1) as $i) {
2009 $lval = isset($left[$i]) ? $left[$i] : 0;
2010 $rval = isset($right[$i]) ? $right[$i] : 0;
2013 $out[] = $lval + $rval;
2016 $out[] = $lval - $rval;
2019 $out[] = $lval * $rval;
2022 $out[] = $lval % $rval;
2026 $this->
throwError(
"evaluate error: can't divide by zero");
2028 $out[] = $lval / $rval;
2031 $this->
throwError(
'evaluate error: color op number failed on op '.$op);
2034 return $this->fixColor($out);
2037 public function lib_red($color)
2039 $color = $this->coerceColor($color);
2040 if (is_null($color)) {
2041 $this->
throwError(
'color expected for red()');
2047 public function lib_green($color)
2049 $color = $this->coerceColor($color);
2050 if (is_null($color)) {
2051 $this->
throwError(
'color expected for green()');
2057 public function lib_blue($color)
2059 $color = $this->coerceColor($color);
2060 if (is_null($color)) {
2061 $this->
throwError(
'color expected for blue()');
2069 protected function op_number_number($op, $left, $right)
2071 $unit = empty($left[2]) ? $right[2] : $left[2];
2076 $value = $left[1] + $right[1];
2079 $value = $left[1] * $right[1];
2082 $value = $left[1] - $right[1];
2085 $value = $left[1] % $right[1];
2088 if ($right[1] == 0) {
2089 $this->
throwError(
'parse error: divide by zero');
2091 $value = $left[1] / $right[1];
2094 return $this->toBool($left[1] < $right[1]);
2096 return $this->toBool($left[1] > $right[1]);
2098 return $this->toBool($left[1] >= $right[1]);
2100 return $this->toBool($left[1] <= $right[1]);
2102 $this->
throwError(
'parse error: unknown number operator: '.$op);
2105 return array(
"number", $value, $unit);
2111 protected function makeOutputBlock($type, $selectors =
null)
2113 $b =
new stdclass();
2114 $b->lines = array();
2115 $b->children = array();
2116 $b->selectors = $selectors;
2118 $b->parent = $this->scope;
2123 protected function pushEnv($block =
null)
2125 $e =
new stdclass();
2126 $e->parent = $this->env;
2127 $e->store = array();
2135 protected function popEnv()
2138 $this->env = $this->env->parent;
2143 protected function set($name, $value)
2145 $this->env->store[$name] = $value;
2150 protected function get($name)
2152 $current = $this->env;
2154 $isArguments = $name == $this->vPrefix.
'arguments';
2156 if ($isArguments && isset($current->arguments)) {
2157 return array(
'list',
' ', $current->arguments);
2160 if (isset($current->store[$name])) {
2161 return $current->store[$name];
2164 $current = isset($current->storeParent) ?
2165 $current->storeParent : $current->parent;
2168 $this->
throwError(
"variable $name is undefined");
2172 protected function injectVariables($args)
2177 foreach ($args as $name => $strValue) {
2178 if ($name[0] !==
'@') {
2182 $parser->buffer = (string) $strValue;
2183 if (!$parser->propertyValue($value)) {
2184 throw new Exception(
"failed to parse passed in variable $name: $strValue");
2187 $this->
set($name, $value);
2197 if ($fname !==
null) {
2199 $this->_parseFile = $fname;
2203 public function compile($string, $name =
null)
2205 $locale = setlocale(LC_NUMERIC, 0);
2206 setlocale(LC_NUMERIC,
"C");
2208 $this->parser = $this->makeParser($name);
2209 $root = $this->parser->parse($string);
2212 $this->scope =
null;
2214 $this->formatter = $this->newFormatter();
2216 if (!empty($this->registeredVars)) {
2217 $this->injectVariables($this->registeredVars);
2220 $this->sourceParser = $this->parser;
2224 $this->formatter->block($this->scope);
2225 $out = ob_get_clean();
2226 setlocale(LC_NUMERIC, $locale);
2230 public function compileFile($fname, $outFname =
null)
2232 if (!is_readable($fname)) {
2233 throw new Exception(
'load error: failed to find '.$fname);
2236 $pi = pathinfo($fname);
2238 $oldImport = $this->importDir;
2240 $this->importDir = (array) $this->importDir;
2241 $this->importDir[] = $pi[
'dirname'].
'/';
2243 $this->addParsedFile($fname);
2245 $out = $this->compile(file_get_contents($fname), $fname);
2247 $this->importDir = $oldImport;
2249 if ($outFname !==
null) {
2250 return file_put_contents($outFname, $out);
2257 public function checkedCompile($in, $out)
2259 if (!is_file($out) || filemtime($in) > filemtime($out)) {
2260 $this->compileFile($in, $out);
2291 if (is_string($in)) {
2293 } elseif (is_array($in) && isset($in[
'root'])) {
2294 if ($force || !isset($in[
'files'])) {
2298 $root = $in[
'root'];
2299 } elseif (isset($in[
'files']) && is_array($in[
'files'])) {
2300 foreach ($in[
'files'] as $fname => $ftime) {
2301 if (!file_exists($fname) || filemtime($fname) > $ftime) {
2304 $root = $in[
'root'];
2315 if ($root !==
null) {
2318 $out[
'root'] = $root;
2319 $out[
'compiled'] = $this->compileFile($root);
2320 $out[
'files'] = $this->allParsedFiles();
2321 $out[
'updated'] = time();
2332 public function parse($str =
null, $initialVariables =
null)
2334 if (is_array($str)) {
2335 $initialVariables = $str;
2339 $oldVars = $this->registeredVars;
2340 if ($initialVariables !==
null) {
2341 $this->setVariables($initialVariables);
2345 if (empty($this->_parseFile)) {
2346 throw new exception(
"nothing to parse");
2349 $out = $this->compileFile($this->_parseFile);
2351 $out = $this->compile($str);
2354 $this->registeredVars = $oldVars;
2358 protected function makeParser($name)
2361 $parser->writeComments = $this->preserveComments;
2366 public function setFormatter($name)
2368 $this->formatterName = $name;
2371 protected function newFormatter()
2373 $className =
"lessc_formatter_lessjs";
2374 if (!empty($this->formatterName)) {
2375 if (!is_string($this->formatterName)) {
2376 return $this->formatterName;
2378 $className =
"lessc_formatter_$this->formatterName";
2381 return new $className();
2384 public function setPreserveComments($preserve)
2386 $this->preserveComments = $preserve;
2389 public function registerFunction($name, $func)
2391 $this->libFunctions[$name] = $func;
2394 public function unregisterFunction($name)
2396 unset($this->libFunctions[$name]);
2399 public function setVariables($variables)
2401 $this->registeredVars = array_merge($this->registeredVars, $variables);
2404 public function unsetVariable($name)
2406 unset($this->registeredVars[$name]);
2409 public function setImportDir($dirs)
2411 $this->importDir = (array) $dirs;
2414 public function addImportDir($dir)
2416 $this->importDir = (array) $this->importDir;
2417 $this->importDir[] = $dir;
2420 public function allParsedFiles()
2422 return $this->allParsedFiles;
2425 public function addParsedFile($file)
2427 $this->allParsedFiles[realpath($file)] = filemtime($file);
2435 if ($this->sourceLoc >= 0) {
2436 $this->sourceParser->throwError($msg, $this->sourceLoc);
2438 throw new exception($msg);
2443 public static function ccompile($in, $out, $less =
null)
2445 if ($less ===
null) {
2448 return $less->checkedCompile($in, $out);
2451 public static function cexecute($in, $force =
false, $less =
null)
2453 if ($less ===
null) {
2456 return $less->cachedCompile($in, $force);
2459 protected static $cssColors = array(
2460 'aliceblue' =>
'240,248,255',
2461 'antiquewhite' =>
'250,235,215',
2462 'aqua' =>
'0,255,255',
2463 'aquamarine' =>
'127,255,212',
2464 'azure' =>
'240,255,255',
2465 'beige' =>
'245,245,220',
2466 'bisque' =>
'255,228,196',
2468 'blanchedalmond' =>
'255,235,205',
2469 'blue' =>
'0,0,255',
2470 'blueviolet' =>
'138,43,226',
2471 'brown' =>
'165,42,42',
2472 'burlywood' =>
'222,184,135',
2473 'cadetblue' =>
'95,158,160',
2474 'chartreuse' =>
'127,255,0',
2475 'chocolate' =>
'210,105,30',
2476 'coral' =>
'255,127,80',
2477 'cornflowerblue' =>
'100,149,237',
2478 'cornsilk' =>
'255,248,220',
2479 'crimson' =>
'220,20,60',
2480 'cyan' =>
'0,255,255',
2481 'darkblue' =>
'0,0,139',
2482 'darkcyan' =>
'0,139,139',
2483 'darkgoldenrod' =>
'184,134,11',
2484 'darkgray' =>
'169,169,169',
2485 'darkgreen' =>
'0,100,0',
2486 'darkgrey' =>
'169,169,169',
2487 'darkkhaki' =>
'189,183,107',
2488 'darkmagenta' =>
'139,0,139',
2489 'darkolivegreen' =>
'85,107,47',
2490 'darkorange' =>
'255,140,0',
2491 'darkorchid' =>
'153,50,204',
2492 'darkred' =>
'139,0,0',
2493 'darksalmon' =>
'233,150,122',
2494 'darkseagreen' =>
'143,188,143',
2495 'darkslateblue' =>
'72,61,139',
2496 'darkslategray' =>
'47,79,79',
2497 'darkslategrey' =>
'47,79,79',
2498 'darkturquoise' =>
'0,206,209',
2499 'darkviolet' =>
'148,0,211',
2500 'deeppink' =>
'255,20,147',
2501 'deepskyblue' =>
'0,191,255',
2502 'dimgray' =>
'105,105,105',
2503 'dimgrey' =>
'105,105,105',
2504 'dodgerblue' =>
'30,144,255',
2505 'firebrick' =>
'178,34,34',
2506 'floralwhite' =>
'255,250,240',
2507 'forestgreen' =>
'34,139,34',
2508 'fuchsia' =>
'255,0,255',
2509 'gainsboro' =>
'220,220,220',
2510 'ghostwhite' =>
'248,248,255',
2511 'gold' =>
'255,215,0',
2512 'goldenrod' =>
'218,165,32',
2513 'gray' =>
'128,128,128',
2514 'green' =>
'0,128,0',
2515 'greenyellow' =>
'173,255,47',
2516 'grey' =>
'128,128,128',
2517 'honeydew' =>
'240,255,240',
2518 'hotpink' =>
'255,105,180',
2519 'indianred' =>
'205,92,92',
2520 'indigo' =>
'75,0,130',
2521 'ivory' =>
'255,255,240',
2522 'khaki' =>
'240,230,140',
2523 'lavender' =>
'230,230,250',
2524 'lavenderblush' =>
'255,240,245',
2525 'lawngreen' =>
'124,252,0',
2526 'lemonchiffon' =>
'255,250,205',
2527 'lightblue' =>
'173,216,230',
2528 'lightcoral' =>
'240,128,128',
2529 'lightcyan' =>
'224,255,255',
2530 'lightgoldenrodyellow' =>
'250,250,210',
2531 'lightgray' =>
'211,211,211',
2532 'lightgreen' =>
'144,238,144',
2533 'lightgrey' =>
'211,211,211',
2534 'lightpink' =>
'255,182,193',
2535 'lightsalmon' =>
'255,160,122',
2536 'lightseagreen' =>
'32,178,170',
2537 'lightskyblue' =>
'135,206,250',
2538 'lightslategray' =>
'119,136,153',
2539 'lightslategrey' =>
'119,136,153',
2540 'lightsteelblue' =>
'176,196,222',
2541 'lightyellow' =>
'255,255,224',
2542 'lime' =>
'0,255,0',
2543 'limegreen' =>
'50,205,50',
2544 'linen' =>
'250,240,230',
2545 'magenta' =>
'255,0,255',
2546 'maroon' =>
'128,0,0',
2547 'mediumaquamarine' =>
'102,205,170',
2548 'mediumblue' =>
'0,0,205',
2549 'mediumorchid' =>
'186,85,211',
2550 'mediumpurple' =>
'147,112,219',
2551 'mediumseagreen' =>
'60,179,113',
2552 'mediumslateblue' =>
'123,104,238',
2553 'mediumspringgreen' =>
'0,250,154',
2554 'mediumturquoise' =>
'72,209,204',
2555 'mediumvioletred' =>
'199,21,133',
2556 'midnightblue' =>
'25,25,112',
2557 'mintcream' =>
'245,255,250',
2558 'mistyrose' =>
'255,228,225',
2559 'moccasin' =>
'255,228,181',
2560 'navajowhite' =>
'255,222,173',
2561 'navy' =>
'0,0,128',
2562 'oldlace' =>
'253,245,230',
2563 'olive' =>
'128,128,0',
2564 'olivedrab' =>
'107,142,35',
2565 'orange' =>
'255,165,0',
2566 'orangered' =>
'255,69,0',
2567 'orchid' =>
'218,112,214',
2568 'palegoldenrod' =>
'238,232,170',
2569 'palegreen' =>
'152,251,152',
2570 'paleturquoise' =>
'175,238,238',
2571 'palevioletred' =>
'219,112,147',
2572 'papayawhip' =>
'255,239,213',
2573 'peachpuff' =>
'255,218,185',
2574 'peru' =>
'205,133,63',
2575 'pink' =>
'255,192,203',
2576 'plum' =>
'221,160,221',
2577 'powderblue' =>
'176,224,230',
2578 'purple' =>
'128,0,128',
2580 'rosybrown' =>
'188,143,143',
2581 'royalblue' =>
'65,105,225',
2582 'saddlebrown' =>
'139,69,19',
2583 'salmon' =>
'250,128,114',
2584 'sandybrown' =>
'244,164,96',
2585 'seagreen' =>
'46,139,87',
2586 'seashell' =>
'255,245,238',
2587 'sienna' =>
'160,82,45',
2588 'silver' =>
'192,192,192',
2589 'skyblue' =>
'135,206,235',
2590 'slateblue' =>
'106,90,205',
2591 'slategray' =>
'112,128,144',
2592 'slategrey' =>
'112,128,144',
2593 'snow' =>
'255,250,250',
2594 'springgreen' =>
'0,255,127',
2595 'steelblue' =>
'70,130,180',
2596 'tan' =>
'210,180,140',
2597 'teal' =>
'0,128,128',
2598 'thistle' =>
'216,191,216',
2599 'tomato' =>
'255,99,71',
2600 'transparent' =>
'0,0,0,0',
2601 'turquoise' =>
'64,224,208',
2602 'violet' =>
'238,130,238',
2603 'wheat' =>
'245,222,179',
2604 'white' =>
'255,255,255',
2605 'whitesmoke' =>
'245,245,245',
2606 'yellow' =>
'255,255,0',
2607 'yellowgreen' =>
'154,205,50'
2615 protected static $nextBlockId = 0;
2617 protected static $precedence = array(
2631 protected static $whitePattern;
2632 protected static $commentMulti;
2634 protected static $commentSingle =
"//";
2635 protected static $commentMultiLeft =
"/*";
2636 protected static $commentMultiRight =
"*/";
2639 protected static $operatorString;
2642 protected static $supressDivisionProps =
2643 array(
'/border-radius$/i',
'/^font$/i');
2645 protected $blockDirectives = array(
"font-face",
"keyframes",
"page",
"-moz-document",
"viewport",
"-moz-viewport",
"-o-viewport",
"-ms-viewport");
2646 protected $lineDirectives = array(
"charset");
2660 protected static $literalCache = array();
2666 public $eatWhiteDefault;
2669 public $writeComments;
2670 public $seenComments;
2671 public $currentProperty;
2675 public function __construct($lessc, $sourceName =
null)
2677 $this->eatWhiteDefault =
true;
2679 $this->lessc = $lessc;
2681 $this->sourceName = $sourceName;
2683 $this->writeComments =
false;
2685 if (!self::$operatorString) {
2686 self::$operatorString =
2687 '('.implode(
'|', array_map(
2688 array(
'lessc',
'preg_quote'),
2689 array_keys(self::$precedence)
2692 $commentSingle = Lessc::preg_quote(self::$commentSingle);
2693 $commentMultiLeft = Lessc::preg_quote(self::$commentMultiLeft);
2694 $commentMultiRight = Lessc::preg_quote(self::$commentMultiRight);
2696 self::$commentMulti = $commentMultiLeft.
'.*?'.$commentMultiRight;
2697 self::$whitePattern =
'/'.$commentSingle.
'[^\n]*\s*|('.self::$commentMulti.
')\s*|\s+/Ais';
2714 $this->buffer = $this->writeComments ? $buffer : $this->removeComments($buffer);
2715 $this->pushSpecialBlock(
"root");
2716 $this->eatWhiteDefault =
true;
2717 $this->seenComments = array();
2724 $this->whitespace();
2729 if ($this->count != strlen($this->buffer)) {
2730 $this->throwError(
'parse error count '.$this->count.
' != len buffer '.strlen($this->buffer));
2735 if (!property_exists($this->env,
'parent') || !is_null($this->env->parent)) {
2736 throw new exception(
'parse error: unclosed block');
2780 if (empty($this->buffer)) {
2785 if ($this->whitespace()) {
2791 $mediaQueries =
null;
2794 $importValue =
null;
2805 if ($this->keyword($key) && $this->
assign() &&
2806 $this->propertyValue($value, $key) && $this->end()
2808 $this->append(array(
'assign', $key, $value), $s);
2816 if ($this->literal(
'@',
false)) {
2820 if ($this->literal(
'@media')) {
2821 if ($this->mediaQueryList($mediaQueries)
2822 && $this->literal(
'{')
2824 $media = $this->pushSpecialBlock(
"media");
2825 $media->queries = is_null($mediaQueries) ? array() : $mediaQueries;
2833 if ($this->literal(
"@",
false) && $this->keyword($dirName)) {
2834 if ($this->isDirective($dirName, $this->blockDirectives)) {
2835 if ($this->openString(
"{", $dirValue,
null, array(
";")) &&
2838 $dir = $this->pushSpecialBlock(
"directive");
2839 $dir->name = $dirName;
2840 if (isset($dirValue)) {
2841 $dir->value = $dirValue;
2845 } elseif ($this->isDirective($dirName, $this->lineDirectives)) {
2846 if ($this->propertyValue($dirValue) && $this->end()) {
2847 $this->append(array(
"directive", $dirName, $dirValue));
2857 if ($this->variable($var) && $this->
assign() &&
2858 $this->propertyValue($value) && $this->end()
2860 $this->append(array(
'assign', $var, $value), $s);
2866 if ($this->
import($importValue)) {
2867 $this->append($importValue, $s);
2872 if ($this->tag($tag,
true) && $this->argumentDef($args, $isVararg) &&
2873 $this->guards($guards) &&
2876 $block = $this->pushBlock($this->fixTags(array($tag)));
2877 $block->args = $args;
2878 $block->isVararg = $isVararg;
2879 if (!empty($guards)) {
2880 $block->guards = $guards;
2888 if ($this->tags($tags) && $this->literal(
'{',
false)) {
2889 $tags = $this->fixTags($tags);
2890 $this->pushBlock($tags);
2897 if ($this->literal(
'}',
false)) {
2899 $block = $this->pop();
2900 }
catch (exception $e) {
2902 $this->throwError($e->getMessage());
2906 if (is_null($block->type)) {
2908 if (!isset($block->args)) {
2909 foreach ($block->tags as $tag) {
2910 if (!is_string($tag) || $tag[0] != $this->lessc->mPrefix) {
2917 foreach ($block->tags as $tag) {
2918 if (is_string($tag)) {
2919 $this->env->children[$tag][] = $block;
2925 $this->append(array(
'block', $block), $s);
2930 $this->whitespace();
2935 if ($this->mixinTags($tags) &&
2936 $this->argumentDef($argv, $isVararg) &&
2937 $this->keyword($suffix) && $this->end()
2939 $tags = $this->fixTags($tags);
2940 $this->append(array(
'mixin', $tags, $argv, $suffix), $s);
2947 if ($this->literal(
';')) {
2954 protected function isDirective($dirname, $directives)
2959 array_map(array(
"lessc",
"preg_quote"), $directives)
2961 $pattern =
'/^(-[a-z-]+-)?('.$pattern.
')$/i';
2963 return preg_match($pattern, $dirname);
2966 protected function fixTags($tags)
2969 foreach ($tags as &$tag) {
2970 if ($tag[0] == $this->lessc->vPrefix) {
2971 $tag[0] = $this->lessc->mPrefix;
2978 protected function expressionList(&$exps)
2988 if (count($values) == 0) {
2992 $exps = Lessc::compressList($values,
' ');
3005 if ($this->value($lhs)) {
3009 if (!empty($this->env->supressedDivision)) {
3010 unset($this->env->supressedDivision);
3012 if ($this->literal(
"/") && $this->value($rhs)) {
3013 $out = array(
"list",
"",
3014 array($out, array(
"keyword",
"/"), $rhs));
3033 $this->inExp =
true;
3034 $ss = $this->seek();
3037 $whiteBefore = isset($this->buffer[$this->count - 1]) &&
3038 ctype_space($this->buffer[$this->count - 1]);
3045 if ($this->match(self::$operatorString.($needWhite ?
'\s' :
''), $m) && self::$precedence[$m[1]] >= $minP) {
3046 if (!$this->inParens && isset($this->env->currentProperty) && $m[1] ==
"/" && empty($this->env->supressedDivision)) {
3047 foreach (self::$supressDivisionProps as $pattern) {
3048 if (preg_match($pattern, $this->env->currentProperty)) {
3049 $this->env->supressedDivision =
true;
3056 $whiteAfter = isset($this->buffer[$this->count - 1]) &&
3057 ctype_space($this->buffer[$this->count - 1]);
3059 if (!$this->value($rhs)) {
3064 if ($this->peek(self::$operatorString, $next) && self::$precedence[$next[1]] > self::$precedence[$m[1]]) {
3065 $rhs = $this->
expHelper($rhs, self::$precedence[$next[1]]);
3068 $lhs = array(
'expression', $m[1], $lhs, $rhs, $whiteBefore, $whiteAfter);
3069 $ss = $this->seek();
3083 public function propertyValue(&$value, $keyName =
null)
3088 if ($keyName !==
null) {
3089 $this->env->currentProperty = $keyName;
3093 while ($this->expressionList($v)) {
3096 if (!$this->literal(
',')) {
3105 if ($keyName !==
null) {
3106 unset($this->env->currentProperty);
3109 if (count($values) == 0) {
3113 $value = Lessc::compressList($values,
', ');
3117 protected function parenValue(&$out)
3124 if (isset($this->buffer[$this->count]) && $this->buffer[$this->count] !=
"(") {
3129 if ($this->literal(
"(") &&
3130 ($this->inParens =
true) && $this->
expression($exp) &&
3145 protected function value(&$value)
3155 if (isset($this->buffer[$this->count]) && $this->buffer[$this->count] ==
"-") {
3157 if ($this->literal(
"-",
false) &&
3158 (($this->variable($inner) && $inner = array(
"variable", $inner)) ||
3159 $this->unit($inner) ||
3160 $this->parenValue($inner))
3162 $value = array(
"unary",
"-", $inner);
3169 if ($this->parenValue($value)) {
3172 if ($this->unit($value)) {
3175 if ($this->color($value)) {
3178 if ($this->func($value)) {
3181 if ($this->
string($value)) {
3185 if ($this->keyword($word)) {
3186 $value = array(
'keyword', $word);
3191 if ($this->variable($var)) {
3192 $value = array(
'variable', $var);
3197 if ($this->literal(
"~") && $this->
string($str)) {
3198 $value = array(
"escape", $str);
3206 if ($this->literal(
'\\') && $this->match(
'([0-9]+)', $m)) {
3207 $value = array(
'keyword',
'\\'.$m[1]);
3217 protected function import(&$out, $value =
'')
3219 if (!$this->literal(
'@import')) {
3227 if ($this->propertyValue($value)) {
3228 $out = array(
"import", $value);
3235 protected function mediaQueryList(&$out)
3239 if ($this->genericList($list,
"mediaQuery",
",",
false)) {
3246 protected function mediaQuery(&$out)
3252 $expressions =
null;
3255 if ((($this->literal(
"only") && ($only =
true)) || ($this->literal(
"not") && ($not =
true))) && $this->keyword($mediaType)) {
3256 $prop = array(
"mediaType");
3263 $prop[] = $mediaType;
3270 if (!empty($mediaType) && !$this->literal(
"and")) {
3273 $this->genericList($expressions,
"mediaExpression",
"and",
false);
3274 if (is_array($expressions)) {
3275 $parts = array_merge($parts, $expressions[2]);
3279 if (count($parts) == 0) {
3288 protected function mediaExpression(&$out)
3295 if ($this->literal(
"(") &&
3296 $this->keyword($feature) &&
3297 ($this->literal(
":") && $this->
expression($value)) &&
3300 $out = array(
"mediaExp", $feature);
3305 } elseif ($this->variable($variable)) {
3306 $out = array(
'variable', $variable);
3315 protected function openString($end, &$out, $nestingOpen =
null, $rejectStrs =
null)
3320 $oldWhite = $this->eatWhiteDefault;
3321 $this->eatWhiteDefault =
false;
3323 $stop = array(
"'",
'"',
"@{", $end);
3324 $stop = array_map(array(
"lessc",
"preg_quote"), $stop);
3327 if (!is_null($rejectStrs)) {
3328 $stop = array_merge($stop, $rejectStrs);
3331 $patt =
'(.*?)('.implode(
"|", $stop).
')';
3337 while ($this->match($patt, $m,
false)) {
3338 if (!empty($m[1])) {
3341 $nestingLevel += substr_count($m[1], $nestingOpen);
3347 $this->count -= strlen($tok);
3349 if ($nestingLevel == 0) {
3356 if (($tok ==
"'" || $tok ==
'"') && $this->
string($str)) {
3361 if ($tok ==
"@{" && $this->interpolation($inter)) {
3362 $content[] = $inter;
3366 if (!empty($rejectStrs) && in_array($tok, $rejectStrs)) {
3371 $this->count += strlen($tok);
3374 $this->eatWhiteDefault = $oldWhite;
3376 if (count($content) == 0) {
3381 if (is_string(end($content))) {
3382 $content[count($content) - 1] = rtrim(end($content));
3385 $out = array(
"string",
"", $content);
3389 protected function string(&$out)
3394 if ($this->literal(
'"',
false)) {
3396 } elseif ($this->literal(
"'",
false)) {
3405 $patt =
'([^\n]*?)(@\{|\\\\|'.
3406 Lessc::preg_quote($delim).
')';
3408 $oldWhite = $this->eatWhiteDefault;
3409 $this->eatWhiteDefault =
false;
3412 while ($this->match($patt, $m,
false)) {
3414 if ($m[2] ==
"@{") {
3415 $this->count -= strlen($m[2]);
3416 if ($this->interpolation($inter)) {
3417 $content[] = $inter;
3419 $this->count += strlen($m[2]);
3422 } elseif ($m[2] ==
'\\') {
3424 if ($this->literal($delim,
false)) {
3425 $content[] = $delim;
3428 $this->count -= strlen($delim);
3433 $this->eatWhiteDefault = $oldWhite;
3435 if ($this->literal($delim)) {
3436 $out = array(
"string", $delim, $content);
3444 protected function interpolation(&$out)
3448 $oldWhite = $this->eatWhiteDefault;
3449 $this->eatWhiteDefault =
true;
3452 if ($this->literal(
"@{") &&
3453 $this->openString(
"}", $interp,
null, array(
"'",
'"',
";")) &&
3454 $this->literal(
"}",
false)
3456 $out = array(
"interpolate", $interp);
3457 $this->eatWhiteDefault = $oldWhite;
3458 if ($this->eatWhiteDefault) {
3459 $this->whitespace();
3464 $this->eatWhiteDefault = $oldWhite;
3469 protected function unit(&$unit)
3474 if (isset($this->buffer[$this->count])) {
3475 $char = $this->buffer[$this->count];
3476 if (!ctype_digit($char) && $char !=
".") {
3481 if ($this->match(
'([0-9]+(?:\.[0-9]*)?|\.[0-9]+)([%a-zA-Z]+)?', $m)) {
3482 $unit = array(
"number", $m[1], empty($m[2]) ?
"" : $m[2]);
3489 protected function color(&$out)
3493 if ($this->match(
'(#(?:[0-9a-f]{8}|[0-9a-f]{6}|[0-9a-f]{3}))', $m)) {
3494 if (strlen($m[1]) > 7) {
3495 $out = array(
"string",
"", array($m[1]));
3497 $out = array(
"raw_color", $m[1]);
3510 protected function argumentDef(&$args, &$isVararg)
3516 if (!$this->literal(
'(')) {
3522 $method =
"expressionList";
3526 if ($this->literal(
"...")) {
3531 if ($this->$method($value)) {
3532 if ($value[0] ==
"variable") {
3533 $arg = array(
"arg", $value[1]);
3534 $ss = $this->seek();
3536 if ($this->
assign() && $this->$method($rhs)) {
3540 if ($this->literal(
"...")) {
3552 $values[] = array(
"lit", $value);
3557 if (!$this->literal($delim)) {
3558 if ($delim ==
"," && $this->literal(
";")) {
3561 $method =
"propertyValue";
3564 if (isset($values[1])) {
3566 foreach ($values as $i => $arg) {
3570 $this->throwError(
"Cannot mix ; and , as delimiter types");
3572 $newList[] = $arg[2];
3575 $newList[] = $arg[1];
3578 $this->throwError(
"Unexpected rest before semicolon");
3582 $newList = array(
"list",
", ", $newList);
3584 switch ($values[0][0]) {
3586 $newArg = array(
"arg", $values[0][1], $newList);
3589 $newArg = array(
"lit", $newList);
3593 } elseif ($values) {
3594 $newArg = $values[0];
3598 $values = array($newArg);
3606 if (!$this->literal(
')')) {
3618 protected function tags(&$tags, $simple =
false, $delim =
',')
3623 while ($this->tag($tt, $simple)) {
3625 if (!$this->literal($delim)) {
3629 if (count($tags) == 0) {
3638 protected function mixinTags(&$tags)
3643 while ($this->tag($tt,
true)) {
3645 $this->literal(
">");
3656 protected function tagBracket(&$parts, &$hasExpression)
3663 if (isset($this->buffer[$this->count]) && $this->buffer[$this->count] !=
"[") {
3669 $hasInterpolation =
false;
3671 if ($this->literal(
"[",
false)) {
3672 $attrParts = array(
"[");
3675 if ($this->literal(
"]",
false)) {
3681 if ($this->match(
'\s+', $m)) {
3685 if ($this->
string($str)) {
3687 foreach ($str[2] as &$chunk) {
3688 $chunk = str_replace($this->lessc->parentSelector,
"$&$", $chunk);
3691 $attrParts[] = $str;
3692 $hasInterpolation =
true;
3696 if ($this->keyword($word)) {
3697 $attrParts[] = $word;
3701 if ($this->interpolation($inter)) {
3702 $attrParts[] = $inter;
3703 $hasInterpolation =
true;
3708 if ($this->match(
'[|-~\$\*\^=]+', $m)) {
3709 $attrParts[] = $m[0];
3716 if ($this->literal(
"]",
false)) {
3718 foreach ($attrParts as $part) {
3721 $hasExpression = $hasExpression || $hasInterpolation;
3732 protected function tag(&$tag, $simple =
false)
3738 $chars =
'^@,:;{}\][>\(\) "\'';
3740 $chars =
'^@,;{}["\'';
3744 $hasExpression =
false;
3746 while ($this->tagBracket($parts, $hasExpression));
3748 $oldWhite = $this->eatWhiteDefault;
3749 $this->eatWhiteDefault =
false;
3753 if ($this->match(
'(['.$chars.
'0-9]['.$chars.
']*)', $m)) {
3759 while ($this->tagBracket($parts, $hasExpression));
3763 if (isset($this->buffer[$this->count]) && $this->buffer[$this->count] ==
"@") {
3764 if ($this->interpolation($interp)) {
3765 $hasExpression =
true;
3771 if ($this->literal(
"@")) {
3777 if ($this->unit($unit)) {
3778 $parts[] = $unit[1];
3779 $parts[] = $unit[2];
3786 $this->eatWhiteDefault = $oldWhite;
3792 if ($hasExpression) {
3793 $tag = array(
"exp", array(
"string",
"", $parts));
3795 $tag = trim(implode($parts));
3798 $this->whitespace();
3803 protected function func(&$func)
3812 if ($this->match(
'(%|[\w\-_][\w\-_:\.]+|[\w_])', $m) && $this->literal(
'(')) {
3815 $sPreArgs = $this->seek();
3819 $ss = $this->seek();
3821 if ($this->keyword($name) && $this->literal(
'=') && $this->expressionList($value)) {
3822 $args[] = array(
"string",
"", array($name,
"=", $value));
3825 if ($this->expressionList($value)) {
3830 if (!$this->literal(
',')) {
3834 $args = array(
'list',
',', $args);
3836 if ($this->literal(
')')) {
3837 $func = array(
'function', $fname, $args);
3839 } elseif ($fname ==
'url') {
3841 $this->seek($sPreArgs);
3842 if ($this->openString(
")", $string) && $this->literal(
")")) {
3843 $func = array(
'function', $fname, $string);
3854 protected function variable(&$name)
3860 if ($this->literal($this->lessc->vPrefix,
false) &&
3861 ($this->variable($sub) || $this->keyword($name))
3864 $name = array(
'variable', $sub);
3866 $name = $this->lessc->vPrefix.$name;
3883 $this->currentProperty = $name;
3885 return $this->literal(
':') || $this->literal(
'=');
3889 protected function keyword(&$word)
3892 if ($this->match(
'([\w_\-\*!"][\w\-_"]*)', $m)) {
3900 protected function end()
3902 if ($this->literal(
';',
false)) {
3904 } elseif ($this->count == strlen($this->buffer) || $this->buffer[$this->count] ==
'}') {
3911 protected function guards(&$guards)
3917 if (!$this->literal(
"when")) {
3924 while ($this->guardGroup($g)) {
3926 if (!$this->literal(
",")) {
3931 if (count($guards) == 0) {
3942 protected function guardGroup(&$guardGroup)
3947 $guardGroup = array();
3948 while ($this->guard($guard)) {
3949 $guardGroup[] = $guard;
3950 if (!$this->literal(
"and")) {
3955 if (count($guardGroup) == 0) {
3964 protected function guard(&$guard)
3969 $negate = $this->literal(
"not");
3971 if ($this->literal(
"(") && $this->
expression($exp) && $this->literal(
")")) {
3974 $guard = array(
"negate", $guard);
3985 protected function literal($what, $eatWhitespace =
null)
3987 if ($eatWhitespace ===
null) {
3988 $eatWhitespace = $this->eatWhiteDefault;
3992 if (!isset($what[1]) && isset($this->buffer[$this->count])) {
3993 if ($this->buffer[$this->count] == $what) {
3994 if (!$eatWhitespace) {
4004 if (!isset(self::$literalCache[$what])) {
4005 self::$literalCache[$what] = Lessc::preg_quote($what);
4009 return $this->match(self::$literalCache[$what], $m, $eatWhitespace);
4012 protected function genericList(&$out, $parseItem, $delim =
"", $flatten =
true)
4018 while ($this->$parseItem($value)) {
4021 if (!$this->literal($delim)) {
4027 if (count($items) == 0) {
4032 if ($flatten && count($items) == 1) {
4035 $out = array(
"list", $delim, $items);
4045 protected function to($what, &$out, $until =
false, $allowNewline =
false)
4047 if (is_string($allowNewline)) {
4048 $validChars = $allowNewline;
4050 $validChars = $allowNewline ?
"." :
"[^\n]";
4053 if (!$this->match(
'('.$validChars.
'*?)'.Lessc::preg_quote($what), $m, !$until)) {
4057 $this->count -= strlen($what);
4064 protected function match($regex, &$out, $eatWhitespace =
null)
4066 if ($eatWhitespace ===
null) {
4067 $eatWhitespace = $this->eatWhiteDefault;
4070 $r =
'/'.$regex.($eatWhitespace && !$this->writeComments ?
'\s*' :
'').
'/Ais';
4071 if (preg_match($r, $this->buffer, $out, 0, $this->count)) {
4072 $this->count += strlen($out[0]);
4073 if ($eatWhitespace && $this->writeComments) {
4074 $this->whitespace();
4082 protected function whitespace()
4084 if ($this->writeComments) {
4087 while (preg_match(self::$whitePattern, $this->buffer, $m, 0, $this->count)) {
4088 if (isset($m[1]) && empty($this->seenComments[$this->count])) {
4089 $this->append(array(
"comment", $m[1]));
4090 $this->seenComments[$this->count] =
true;
4092 $this->count += strlen($m[0]);
4097 $this->match(
"", $m);
4098 return strlen($m[0]) > 0;
4103 protected function peek($regex, &$out =
null, $from =
null)
4105 if (is_null($from)) {
4106 $from = $this->count;
4108 $r =
'/'.$regex.
'/Ais';
4109 $result = preg_match($r, $this->buffer, $out, 0, $from);
4115 protected function seek($where =
null)
4117 if ($where ===
null) {
4118 return $this->count;
4120 $this->count = $where;
4127 public function throwError($msg =
"parse error", $count =
null)
4129 $count = is_null($count) ? $this->count : $count;
4131 $line = $this->line +
4132 substr_count(substr($this->buffer, 0, $count),
"\n");
4134 if (!empty($this->sourceName)) {
4135 $loc =
"$this->sourceName on line $line";
4137 $loc =
"line: $line";
4142 if ($this->peek(
"(.*?)(\n|$)", $m, $count)) {
4143 throw new exception(
"$msg: failed at `$m[1]` $loc");
4145 throw new exception(
"$msg: $loc");
4149 protected function pushBlock($selectors =
null, $type =
null)
4151 $b =
new stdclass();
4152 $b->parent = $this->env;
4155 $b->id = self::$nextBlockId++;
4157 $b->isVararg =
false;
4158 $b->tags = $selectors;
4160 $b->props = array();
4161 $b->children = array();
4168 protected function pushSpecialBlock($type)
4170 return $this->pushBlock(
null, $type);
4174 protected function append($prop, $pos =
null)
4176 if ($pos !==
null) {
4179 $this->env->props[] = $prop;
4183 protected function pop()
4186 $this->env = $this->env->parent;
4192 protected function removeComments($text)
4195 'url(',
'//',
'/*',
'"',
"'"
4202 foreach ($look as $token) {
4203 $pos = strpos($text, $token);
4204 if ($pos !==
false) {
4205 if (!isset($min) || $pos < $min[1]) {
4206 $min = array($token, $pos);
4211 if (is_null($min)) {
4221 if (preg_match(
'/url\(.*?\)/', $text, $m, 0, $count)) {
4222 $count += strlen($m[0]) - strlen($min[0]);
4228 if (preg_match(
'/'.$min[0].
'.*?(?<!\\\\)'.$min[0].
'/', $text, $m, 0, $count)) {
4229 $count += strlen($m[0]) - 1;
4233 $skip = strpos($text,
"\n", $count);
4234 if ($skip ===
false) {
4235 $skip = strlen($text) - $count;
4242 if (preg_match(
'/\/\*.*?\*\//s', $text, $m, 0, $count)) {
4243 $skip = strlen($m[0]);
4244 $newlines = substr_count($m[0],
"\n");
4250 $count += strlen($min[0]);
4253 $out .= substr($text, 0, $count).str_repeat(
"\n", $newlines);
4254 $text = substr($text, $count + $skip);