44 public static $VERSION =
"v0.8.0";
46 public static $TRUE = array(
"keyword",
"true");
47 public static $FALSE = array(
"keyword",
"false");
49 protected $libFunctions = array();
50 protected $registeredVars = array();
51 protected $preserveComments =
false;
53 public $vPrefix =
'@';
54 public $mPrefix =
'$';
55 public $parentSelector =
'&';
57 public $importDisabled =
false;
58 public $importDir =
'';
62 public $formatterName;
68 protected $numberPrecision =
null;
70 protected $allParsedFiles = array();
74 protected $sourceParser =
null;
75 protected $sourceLoc =
null;
77 protected static $nextImportId = 0;
80 protected function findImport($url)
82 foreach ((array) $this->importDir as $dir) {
83 $full = $dir.(substr($dir, -1) !=
'/' ?
'/' :
'').$url;
100 return is_file($name);
103 public static function compressList($items, $delim)
105 if (!isset($items[1]) && isset($items[0])) {
108 return array(
'list', $delim, $items);
112 public static function preg_quote($what)
114 return preg_quote($what,
'/');
117 protected function tryImport($importPath, $parentBlock, $out)
119 if ($importPath[0] ==
"function" && $importPath[1] ==
"url") {
120 $importPath = $this->flattenList($importPath[2]);
123 $str = $this->coerceString($importPath);
131 if (substr_compare($url,
'.css', -4, 4) === 0) {
135 $realPath = $this->findImport($url);
137 if ($realPath ===
null) {
141 if ($this->importDisabled) {
142 return array(
false,
"/* import disabled */");
145 if (isset($this->allParsedFiles[realpath($realPath)])) {
146 return array(
false,
null);
149 $this->addParsedFile($realPath);
150 $parser = $this->makeParser($realPath);
151 $root = $parser->parse(file_get_contents($realPath));
154 foreach ($root->props as $prop) {
155 if ($prop[0] ==
"block") {
156 $prop[1]->parent = $parentBlock;
163 foreach ($root->children as $childName => $child) {
164 if (isset($parentBlock->children[$childName])) {
165 $parentBlock->children[$childName] = array_merge(
166 $parentBlock->children[$childName],
170 $parentBlock->children[$childName] = $child;
174 $pi = pathinfo($realPath);
175 $dir = $pi[
"dirname"];
177 list($top, $bottom) = $this->sortProps($root->props,
true);
178 $this->compileImportedProps($top, $parentBlock, $out, $parser, $dir);
180 return array(
true, $bottom, $parser, $dir);
183 protected function compileImportedProps($props, $block, $out, $sourceParser, $importDir)
185 $oldSourceParser = $this->sourceParser;
187 $oldImport = $this->importDir;
190 $this->importDir = (array) $this->importDir;
191 array_unshift($this->importDir, $importDir);
193 foreach ($props as $prop) {
194 $this->compileProp($prop, $block, $out);
197 $this->importDir = $oldImport;
198 $this->sourceParser = $oldSourceParser;
224 switch ($block->type) {
226 $this->compileRoot($block);
229 $this->compileCSSBlock($block);
232 $this->compileMedia($block);
235 $name =
"@".$block->name;
236 if (!empty($block->value)) {
237 $name .=
" ".$this->compileValue($this->reduce($block->value));
240 $this->compileNestedBlock($block, array($name));
243 $this->
throwError(
"unknown block type: $block->type\n");
247 protected function compileCSSBlock($block)
249 $env = $this->pushEnv();
251 $selectors = $this->compileSelectors($block->tags);
252 $env->selectors = $this->multiplySelectors($selectors);
253 $out = $this->makeOutputBlock(
null, $env->selectors);
255 $this->scope->children[] = $out;
256 $this->compileProps($block, $out);
258 $block->scope = $env;
262 protected function compileMedia($media)
264 $env = $this->pushEnv($media);
265 $parentScope = $this->mediaParent($this->scope);
267 $query = $this->compileMediaQuery($this->multiplyMedia($env));
269 $this->scope = $this->makeOutputBlock($media->type, array($query));
270 $parentScope->children[] = $this->scope;
272 $this->compileProps($media, $this->scope);
274 if (count($this->scope->lines) > 0) {
275 $orphanSelelectors = $this->findClosestSelectors();
276 if (!is_null($orphanSelelectors)) {
277 $orphan = $this->makeOutputBlock(
null, $orphanSelelectors);
278 $orphan->lines = $this->scope->lines;
279 array_unshift($this->scope->children, $orphan);
280 $this->scope->lines = array();
284 $this->scope = $this->scope->parent;
288 protected function mediaParent($scope)
290 while (!empty($scope->parent)) {
291 if (!empty($scope->type) && $scope->type !=
"media") {
294 $scope = $scope->parent;
300 protected function compileNestedBlock($block, $selectors)
302 $this->pushEnv($block);
303 $this->scope = $this->makeOutputBlock($block->type, $selectors);
304 $this->scope->parent->children[] = $this->scope;
306 $this->compileProps($block, $this->scope);
308 $this->scope = $this->scope->parent;
312 protected function compileRoot($root)
315 $this->scope = $this->makeOutputBlock($root->type);
316 $this->compileProps($root, $this->scope);
320 protected function compileProps($block, $out)
322 foreach ($this->sortProps($block->props) as $prop) {
323 $this->compileProp($prop, $block, $out);
338 foreach ($lines as $line) {
339 if (strpos($line,
'/*') === 0) {
343 if (!in_array($line, $unique)) {
346 array_splice($unique, array_search($line, $unique), 0, $comments);
349 return array_merge($unique, $comments);
352 protected function sortProps($props, $split =
false)
359 foreach ($props as $prop) {
366 if (isset($prop[1][0]) && $prop[1][0] == $this->vPrefix) {
367 $vars = array_merge($vars, $stack);
369 $other = array_merge($other, $stack);
374 $id = self::$nextImportId++;
377 $imports = array_merge($imports, $stack);
378 $other[] = array(
"import_mixin",
$id);
383 $other = array_merge($other, $stack);
388 $other = array_merge($other, $stack);
391 return array(array_merge($imports, $vars), $other);
393 return array_merge($imports, $vars, $other);
397 protected function compileMediaQuery($queries)
399 $compiledQueries = array();
400 foreach ($queries as $query) {
402 foreach ($query as $q) {
405 $parts[] = implode(
" ", array_slice($q, 1));
409 $parts[] =
"($q[1]: ".
412 $parts[] =
"($q[1])";
421 if (count($parts) > 0) {
422 $compiledQueries[] = implode(
" and ", $parts);
427 if (!empty($parts)) {
429 implode($this->formatter->selectorSeparator, $compiledQueries);
434 protected function multiplyMedia($env, $childQueries =
null)
437 !empty($env->block->type) && $env->block->type !=
"media"
439 return $childQueries;
443 if (empty($env->block->type)) {
444 return $this->multiplyMedia($env->parent, $childQueries);
448 $queries = $env->block->queries;
449 if (is_null($childQueries)) {
452 foreach ($queries as $parent) {
453 foreach ($childQueries as $child) {
454 $out[] = array_merge($parent, $child);
459 return $this->multiplyMedia($env->parent, $out);
462 protected function expandParentSelectors(&$tag, $replace)
464 $parts = explode(
"$&$", $tag);
466 foreach ($parts as &$part) {
468 $part = str_replace($this->parentSelector, $replace, $part, $c);
471 $tag = implode($this->parentSelector, $parts);
475 protected function findClosestSelectors()
479 while ($env !==
null) {
480 if (isset($env->selectors)) {
481 $selectors = $env->selectors;
492 protected function multiplySelectors($selectors)
496 $parentSelectors = $this->findClosestSelectors();
497 if (is_null($parentSelectors)) {
499 foreach ($selectors as &$s) {
500 $this->expandParentSelectors($s,
"");
507 foreach ($parentSelectors as $parent) {
508 foreach ($selectors as $child) {
509 $count = $this->expandParentSelectors($child, $parent);
513 $out[] = trim($child);
515 $out[] = trim($parent.
' '.$child);
524 protected function compileSelectors($selectors)
528 foreach ($selectors as $s) {
531 $out[] = trim($this->
compileValue($this->reduce($value)));
540 protected function eq($left, $right)
542 return $left == $right;
545 protected function patternMatch($block, $orderedArgs, $keywordArgs)
549 if (!empty($block->guards)) {
550 $groupPassed =
false;
551 foreach ($block->guards as $guardGroup) {
552 foreach ($guardGroup as $guard) {
554 $this->zipSetArgs($block->args, $orderedArgs, $keywordArgs);
557 if ($guard[0] ==
"negate") {
562 $passed = $this->reduce($guard) == self::$TRUE;
572 $groupPassed =
false;
587 if (empty($block->args)) {
588 return $block->isVararg || empty($orderedArgs) && empty($keywordArgs);
591 $remainingArgs = $block->args;
593 $remainingArgs = array();
594 foreach ($block->args as $arg) {
595 if ($arg[0] ==
"arg" && isset($keywordArgs[$arg[1]])) {
599 $remainingArgs[] = $arg;
605 foreach ($remainingArgs as $i => $arg) {
608 if (empty($orderedArgs[$i]) || !$this->eq($arg[1], $orderedArgs[$i])) {
614 if (!isset($orderedArgs[$i]) && !isset($arg[2])) {
624 if ($block->isVararg) {
627 $numMatched = $i + 1;
629 return $numMatched >= count($orderedArgs);
633 protected function patternMatchAll($blocks, $orderedArgs, $keywordArgs, $skip = array())
636 foreach ($blocks as $block) {
638 if (isset($skip[$block->id]) && !isset($block->args)) {
642 if ($this->patternMatch($block, $orderedArgs, $keywordArgs)) {
651 protected function findBlocks($searchIn, $path, $orderedArgs, $keywordArgs, $seen = array())
653 if ($searchIn ==
null) {
656 if (isset($seen[$searchIn->id])) {
659 $seen[$searchIn->id] =
true;
663 if (isset($searchIn->children[$name])) {
664 $blocks = $searchIn->children[$name];
665 if (count($path) == 1) {
666 $matches = $this->patternMatchAll($blocks, $orderedArgs, $keywordArgs, $seen);
667 if (!empty($matches)) {
674 foreach ($blocks as $subBlock) {
675 $subMatches = $this->findBlocks(
677 array_slice($path, 1),
683 if (!is_null($subMatches)) {
684 foreach ($subMatches as $sm) {
690 return count($matches) > 0 ? $matches :
null;
693 if ($searchIn->parent === $searchIn) {
696 return $this->findBlocks($searchIn->parent, $path, $orderedArgs, $keywordArgs, $seen);
701 protected function zipSetArgs($args, $orderedValues, $keywordValues)
703 $assignedValues = array();
706 foreach ($args as $a) {
707 if ($a[0] ==
"arg") {
708 if (isset($keywordValues[$a[1]])) {
710 $value = $keywordValues[$a[1]];
711 } elseif (isset($orderedValues[$i])) {
713 $value = $orderedValues[$i];
715 } elseif (isset($a[2])) {
720 $this->
throwError(
"Failed to assign arg ".$a[1]);
723 $value = $this->reduce($value);
724 $this->
set($a[1], $value);
725 $assignedValues[] = $value;
734 if ($last && $last[0] ==
"rest") {
735 $rest = array_slice($orderedValues, count($args) - 1);
736 $this->
set($last[1], $this->reduce(array(
"list",
" ", $rest)));
740 $this->env->arguments = $assignedValues + $orderedValues;
744 protected function compileProp($prop, $block, $out)
747 $this->sourceLoc = isset($prop[-1]) ? $prop[-1] : -1;
751 list(, $name, $value) = $prop;
752 if ($name[0] == $this->vPrefix) {
753 $this->
set($name, $value);
755 $out->lines[] = $this->formatter->property(
762 list(, $child) = $prop;
766 list(, $path, $args, $suffix) = $prop;
768 $orderedArgs = array();
769 $keywordArgs = array();
770 foreach ((array) $args as $arg) {
774 if (!isset($arg[2])) {
775 $orderedArgs[] = $this->reduce(array(
"variable", $arg[1]));
777 $keywordArgs[$arg[1]] = $this->reduce($arg[2]);
782 $orderedArgs[] = $this->reduce($arg[1]);
785 $this->
throwError(
"Unknown arg type: ".$arg[0]);
789 $mixins = $this->findBlocks($block, $path, $orderedArgs, $keywordArgs);
791 if ($mixins ===
null) {
792 $this->
throwError(
"{$prop[1][0]} is undefined");
795 foreach ($mixins as $mixin) {
796 if ($mixin === $block && !$orderedArgs) {
801 if (isset($mixin->parent->scope)) {
803 $mixinParentEnv = $this->pushEnv();
804 $mixinParentEnv->storeParent = $mixin->parent->scope;
808 if (isset($mixin->args)) {
811 $this->zipSetArgs($mixin->args, $orderedArgs, $keywordArgs);
814 $oldParent = $mixin->parent;
815 if ($mixin != $block) {
816 $mixin->parent = $block;
819 foreach ($this->sortProps($mixin->props) as $subProp) {
820 if ($suffix !==
null &&
821 $subProp[0] ==
"assign" &&
822 is_string($subProp[1]) &&
823 $subProp[1][0] != $this->vPrefix
827 array($subProp[2], array(
'keyword', $suffix))
831 $this->compileProp($subProp, $mixin, $out);
834 $mixin->parent = $oldParent;
846 $out->lines[] = $prop[1];
849 list(, $name, $value) = $prop;
850 $out->lines[] =
"@$name ".$this->compileValue($this->reduce($value)).
';';
853 $out->lines[] = $prop[1];
856 list(, $importPath, $importId) = $prop;
857 $importPath = $this->reduce($importPath);
859 if (!isset($this->env->imports)) {
860 $this->env->imports = array();
863 $result = $this->tryImport($importPath, $block, $out);
865 $this->env->imports[$importId] = $result ===
false ?
866 array(
false,
"@import ".$this->
compileValue($importPath).
";") : $result;
870 list(, $importId) = $prop;
871 $import = $this->env->imports[$importId];
872 if ($import[0] ===
false) {
873 if (isset($import[1])) {
874 $out->lines[] = $import[1];
877 list(, $bottom, $parser, $importDir) = $import;
878 $this->compileImportedProps($bottom, $block, $out, $parser, $importDir);
883 $this->
throwError(
"unknown op: {$prop[0]}\n");
905 return implode($value[1], array_map(array($this,
'compileValue'), $value[2]));
907 if (!empty($this->formatter->compressColors)) {
915 list(, $num, $unit) = $value;
918 if ($this->numberPrecision !==
null) {
919 $num = round($num, $this->numberPrecision);
924 list(, $delim, $content) = $value;
925 foreach ($content as &$part) {
926 if (is_array($part)) {
930 return $delim.implode($content).$delim;
936 list(, $r, $g, $b) = $value;
941 if (count($value) == 5 && $value[4] != 1) {
942 return 'rgba('.$r.
','.$g.
','.$b.
','.$value[4].
')';
945 $h = sprintf(
"#%02x%02x%02x", $r, $g, $b);
947 if (!empty($this->formatter->compressColors)) {
949 if ($h[1] === $h[2] && $h[3] === $h[4] && $h[5] === $h[6]) {
950 $h =
'#'.$h[1].$h[3].$h[5];
957 list(, $name, $args) = $value;
960 $this->
throwError(
"unknown value type: $value[0]");
964 protected function lib_pow($args)
966 list($base, $exp) = $this->assertArgs($args, 2,
"pow");
967 return pow($this->assertNumber($base), $this->assertNumber($exp));
970 protected function lib_pi()
975 protected function lib_mod($args)
977 list($a, $b) = $this->assertArgs($args, 2,
"mod");
978 return $this->assertNumber($a) % $this->assertNumber($b);
981 protected function lib_tan($num)
983 return tan($this->assertNumber($num));
986 protected function lib_sin($num)
988 return sin($this->assertNumber($num));
991 protected function lib_cos($num)
993 return cos($this->assertNumber($num));
996 protected function lib_atan($num)
998 $num = atan($this->assertNumber($num));
999 return array(
"number", $num,
"rad");
1002 protected function lib_asin($num)
1004 $num = asin($this->assertNumber($num));
1005 return array(
"number", $num,
"rad");
1008 protected function lib_acos($num)
1010 $num = acos($this->assertNumber($num));
1011 return array(
"number", $num,
"rad");
1014 protected function lib_sqrt($num)
1016 return sqrt($this->assertNumber($num));
1019 protected function lib_extract($value)
1021 list($list, $idx) = $this->assertArgs($value, 2,
"extract");
1022 $idx = $this->assertNumber($idx);
1024 if ($list[0] ==
"list" && isset($list[2][$idx - 1])) {
1025 return $list[2][$idx - 1];
1030 protected function lib_isnumber($value)
1032 return $this->toBool($value[0] ==
"number");
1035 protected function lib_isstring($value)
1037 return $this->toBool($value[0] ==
"string");
1040 protected function lib_iscolor($value)
1042 return $this->toBool($this->coerceColor($value));
1045 protected function lib_iskeyword($value)
1047 return $this->toBool($value[0] ==
"keyword");
1050 protected function lib_ispixel($value)
1052 return $this->toBool($value[0] ==
"number" && $value[2] ==
"px");
1055 protected function lib_ispercentage($value)
1057 return $this->toBool($value[0] ==
"number" && $value[2] ==
"%");
1060 protected function lib_isem($value)
1062 return $this->toBool($value[0] ==
"number" && $value[2] ==
"em");
1065 protected function lib_isrem($value)
1067 return $this->toBool($value[0] ==
"number" && $value[2] ==
"rem");
1070 protected function lib_rgbahex($color)
1072 $color = $this->coerceColor($color);
1073 if (is_null($color)) {
1074 $this->
throwError(
"color expected for rgbahex");
1078 "#%02x%02x%02x%02x",
1079 isset($color[4]) ? $color[4] * 255 : 255,
1086 protected function lib_argb($color)
1088 return $this->lib_rgbahex($color);
1099 $mime = ($value[0] ===
'list') ? $value[2][0][2] :
null;
1100 $url = ($value[0] ===
'list') ? $value[2][1][2][0] : $value[2][0];
1102 $fullpath = $this->findImport($url);
1104 if ($fullpath && ($fsize = filesize($fullpath)) !==
false) {
1106 if ($fsize / 1024 < 32) {
1107 if (is_null($mime)) {
1108 if (class_exists(
'finfo')) {
1109 $finfo =
new finfo(FILEINFO_MIME);
1110 $mime = explode(
'; ', $finfo->file($fullpath));
1112 } elseif (function_exists(
'mime_content_type')) {
1113 $mime = mime_content_type($fullpath);
1117 if (!is_null($mime)) {
1118 $url = sprintf(
'data:%s;base64,%s', $mime, base64_encode(file_get_contents($fullpath)));
1123 return 'url("'.$url.
'")';
1127 protected function lib_e($arg)
1132 if (isset($items[0])) {
1133 return $this->lib_e($items[0]);
1147 protected function lib__sprintf($args)
1149 if ($args[0] !=
"list") {
1153 $string = array_shift($values);
1154 $template = $this->
compileValue($this->lib_e($string));
1158 if (preg_match_all(
'/%[dsa]/', $template, $m)) {
1159 foreach ($m[0] as $match) {
1160 $val = isset($values[$i]) ?
1161 $this->reduce($values[$i]) : array(
'keyword',
'');
1164 if ($color = $this->coerceColor($val)) {
1170 $template = preg_replace(
1171 '/'.self::preg_quote($match).
'/',
1179 $d = $string[0] ==
"string" ? $string[1] :
'"';
1180 return array(
"string", $d, array($template));
1183 protected function lib_floor($arg)
1185 $value = $this->assertNumber($arg);
1186 return array(
"number", floor($value), $arg[2]);
1189 protected function lib_ceil($arg)
1191 $value = $this->assertNumber($arg);
1192 return array(
"number", ceil($value), $arg[2]);
1195 protected function lib_round($arg)
1197 if ($arg[0] !=
"list") {
1198 $value = $this->assertNumber($arg);
1199 return array(
"number", round($value), $arg[2]);
1201 $value = $this->assertNumber($arg[2][0]);
1202 $precision = $this->assertNumber($arg[2][1]);
1203 return array(
"number", round($value, $precision), $arg[2][0][2]);
1207 protected function lib_unit($arg)
1209 if ($arg[0] ==
"list") {
1210 list($number, $newUnit) = $arg[2];
1211 return array(
"number", $this->assertNumber($number),
1214 return array(
"number", $this->assertNumber($arg),
"");
1224 if ($args[0] !=
'list' || count($args[2]) < 2) {
1225 return array(array(
'color', 0, 0, 0), 0);
1227 list($color, $delta) = $args[2];
1228 $color = $this->assertColor($color);
1229 $delta = (float) $delta[1];
1231 return array($color, $delta);
1234 protected function lib_darken($args)
1236 list($color, $delta) = $this->
colorArgs($args);
1238 $hsl = $this->toHSL($color);
1239 $hsl[3] = $this->clamp($hsl[3] - $delta, 100);
1240 return $this->
toRGB($hsl);
1243 protected function lib_lighten($args)
1245 list($color, $delta) = $this->
colorArgs($args);
1247 $hsl = $this->toHSL($color);
1248 $hsl[3] = $this->clamp($hsl[3] + $delta, 100);
1249 return $this->
toRGB($hsl);
1252 protected function lib_saturate($args)
1254 list($color, $delta) = $this->
colorArgs($args);
1256 $hsl = $this->toHSL($color);
1257 $hsl[2] = $this->clamp($hsl[2] + $delta, 100);
1258 return $this->
toRGB($hsl);
1261 protected function lib_desaturate($args)
1263 list($color, $delta) = $this->
colorArgs($args);
1265 $hsl = $this->toHSL($color);
1266 $hsl[2] = $this->clamp($hsl[2] - $delta, 100);
1267 return $this->
toRGB($hsl);
1270 protected function lib_spin($args)
1272 list($color, $delta) = $this->
colorArgs($args);
1274 $hsl = $this->toHSL($color);
1276 $hsl[1] = $hsl[1] + $delta % 360;
1281 return $this->
toRGB($hsl);
1284 protected function lib_fadeout($args)
1286 list($color, $delta) = $this->
colorArgs($args);
1287 $color[4] = $this->clamp((isset($color[4]) ? $color[4] : 1) - $delta / 100);
1291 protected function lib_fadein($args)
1293 list($color, $delta) = $this->
colorArgs($args);
1294 $color[4] = $this->clamp((isset($color[4]) ? $color[4] : 1) + $delta / 100);
1298 protected function lib_hue($color)
1300 $hsl = $this->toHSL($this->assertColor($color));
1301 return round($hsl[1]);
1304 protected function lib_saturation($color)
1306 $hsl = $this->toHSL($this->assertColor($color));
1307 return round($hsl[2]);
1310 protected function lib_lightness($color)
1312 $hsl = $this->toHSL($this->assertColor($color));
1313 return round($hsl[3]);
1318 protected function lib_alpha($value)
1320 if (!is_null($color = $this->coerceColor($value))) {
1321 return isset($color[4]) ? $color[4] : 1;
1327 protected function lib_fade($args)
1329 list($color, $alpha) = $this->
colorArgs($args);
1330 $color[4] = $this->clamp($alpha / 100.0);
1334 protected function lib_percentage($arg)
1336 $num = $this->assertNumber($arg);
1337 return array(
"number", $num * 100,
"%");
1353 $white = [
'color', 255, 255, 255];
1354 if ($args[0] ==
'color') {
1355 return $this->
lib_mix([
'list',
',', [$white, $args]]);
1356 } elseif ($args[0] ==
"list" && count($args[2]) == 2) {
1357 return $this->
lib_mix([$args[0], $args[1], [$white, $args[2][0], $args[2][1]]]);
1359 $this->
throwError(
"tint expects (color, weight)");
1377 $black = [
'color', 0, 0, 0];
1378 if ($args[0] ==
'color') {
1379 return $this->
lib_mix([
'list',
',', [$black, $args]]);
1380 } elseif ($args[0] ==
"list" && count($args[2]) == 2) {
1381 return $this->
lib_mix([$args[0], $args[1], [$black, $args[2][0], $args[2][1]]]);
1383 $this->
throwError(
"shade expects (color, weight)");
1399 if ($args[0] !=
"list" || count($args[2]) < 2) {
1400 $this->
throwError(
"mix expects (color1, color2, weight)");
1403 list($first, $second) = $args[2];
1404 $first = $this->assertColor($first);
1405 $second = $this->assertColor($second);
1407 $first_a = $this->lib_alpha($first);
1408 $second_a = $this->lib_alpha($second);
1410 if (isset($args[2][2])) {
1411 $weight = $args[2][2][1] / 100.0;
1416 $w = $weight * 2 - 1;
1417 $a = $first_a - $second_a;
1419 $w1 = (($w * $a == -1 ? $w : ($w + $a) / (1 + $w * $a)) + 1) / 2.0;
1422 $new = array(
'color',
1423 $w1 * $first[1] + $w2 * $second[1],
1424 $w1 * $first[2] + $w2 * $second[2],
1425 $w1 * $first[3] + $w2 * $second[3],
1428 if ($first_a != 1.0 || $second_a != 1.0) {
1429 $new[] = $first_a * $weight + $second_a * ($weight - 1);
1432 return $this->fixColor($new);
1443 $darkColor = array(
'color', 0, 0, 0);
1444 $lightColor = array(
'color', 255, 255, 255);
1447 if ($args[0] ==
'list') {
1448 $inputColor = (isset($args[2][0])) ? $this->assertColor($args[2][0]) : $lightColor;
1449 $darkColor = (isset($args[2][1])) ? $this->assertColor($args[2][1]) : $darkColor;
1450 $lightColor = (isset($args[2][2])) ? $this->assertColor($args[2][2]) : $lightColor;
1451 $threshold = (isset($args[2][3])) ? $this->assertNumber($args[2][3]) : $threshold;
1453 $inputColor = $this->assertColor($args);
1456 $inputColor = $this->coerceColor($inputColor);
1457 $darkColor = $this->coerceColor($darkColor);
1458 $lightColor = $this->coerceColor($lightColor);
1461 if ($this->toLuma($darkColor) > $this->toLuma($lightColor)) {
1463 $lightColor = $darkColor;
1467 $inputColor_alpha = $this->lib_alpha($inputColor);
1468 if (($this->toLuma($inputColor) * $inputColor_alpha) < $threshold) {
1474 private function toLuma($color)
1476 list(, $r, $g, $b) = $this->coerceColor($color);
1482 $r = ($r <= 0.03928) ? $r / 12.92 : pow((($r + 0.055) / 1.055), 2.4);
1483 $g = ($g <= 0.03928) ? $g / 12.92 : pow((($g + 0.055) / 1.055), 2.4);
1484 $b = ($b <= 0.03928) ? $b / 12.92 : pow((($b + 0.055) / 1.055), 2.4);
1486 return (0.2126 * $r) + (0.7152 * $g) + (0.0722 * $b);
1489 protected function lib_luma($color)
1491 return array(
"number", round($this->toLuma($color) * 100, 8),
"%");
1495 public function assertColor($value, $error =
"expected color value")
1497 $color = $this->coerceColor($value);
1498 if (is_null($color)) {
1504 public function assertNumber($value, $error =
"expecting number")
1506 if ($value[0] ==
"number") {
1512 public function assertArgs($value, $expectedArgs, $name =
"")
1514 if ($expectedArgs == 1) {
1517 if ($value[0] !==
"list" || $value[1] !=
",") {
1520 $values = $value[2];
1521 $numValues = count($values);
1522 if ($expectedArgs != $numValues) {
1527 $this->
throwError(
"{$name}expecting $expectedArgs arguments, got $numValues");
1534 protected function toHSL($color)
1536 if ($color[0] ===
'hsl') {
1540 $r = $color[1] / 255;
1541 $g = $color[2] / 255;
1542 $b = $color[3] / 255;
1544 $min = min($r, $g, $b);
1545 $max = max($r, $g, $b);
1547 $L = ($min + $max) / 2;
1552 $S = ($max - $min) / ($max + $min);
1554 $S = ($max - $min) / (2.0 - $max - $min);
1557 $H = ($g - $b) / ($max - $min);
1558 } elseif ($g == $max) {
1559 $H = 2.0 + ($b - $r) / ($max - $min);
1560 } elseif ($b == $max) {
1561 $H = 4.0 + ($r - $g) / ($max - $min);
1566 ($H < 0 ? $H + 6 : $H) * 60,
1571 if (count($color) > 4) {
1578 protected function toRGB_helper($comp, $temp1, $temp2)
1582 } elseif ($comp > 1) {
1586 if (6 * $comp < 1) {
1587 return $temp1 + ($temp2 - $temp1) * 6 * $comp;
1589 if (2 * $comp < 1) {
1592 if (3 * $comp < 2) {
1593 return $temp1 + ($temp2 - $temp1) * ((2 / 3) - $comp) * 6;
1605 if ($color[0] ===
'color') {
1609 $H = $color[1] / 360;
1610 $S = $color[2] / 100;
1611 $L = $color[3] / 100;
1617 $L * (1.0 + $S) : $L + $S - $L * $S;
1619 $temp1 = 2.0 * $L - $temp2;
1621 $r = $this->toRGB_helper($H + 1 / 3, $temp1, $temp2);
1622 $g = $this->toRGB_helper($H, $temp1, $temp2);
1623 $b = $this->toRGB_helper($H - 1 / 3, $temp1, $temp2);
1627 $out = array(
'color', $r * 255, $g * 255, $b * 255);
1628 if (count($color) > 4) {
1635 protected function clamp($v, $max = 1, $min = 0)
1637 return min($max, max($min, $v));
1647 if ($func[2][0] !=
'list') {
1651 $rawComponents = $func[2][2];
1653 if ($fname ==
'hsl' || $fname ==
'hsla') {
1654 $hsl = array(
'hsl');
1656 foreach ($rawComponents as $c) {
1657 $val = $this->reduce($c);
1658 $val = isset($val[1]) ? (float) $val[1] : 0;
1668 $hsl[] = $this->clamp($val, $clamp);
1672 while (count($hsl) < 4) {
1675 return $this->
toRGB($hsl);
1677 } elseif ($fname ==
'rgb' || $fname ==
'rgba') {
1678 $components = array();
1680 foreach ($rawComponents as $c) {
1681 $c = $this->reduce($c);
1683 if ($c[0] ==
"number" && $c[2] ==
"%") {
1684 $components[] = 255 * ($c[1] / 100);
1686 $components[] = (float) $c[1];
1688 } elseif ($i == 4) {
1689 if ($c[0] ==
"number" && $c[2] ==
"%") {
1690 $components[] = 1.0 * ($c[1] / 100);
1692 $components[] = (float) $c[1];
1700 while (count($components) < 3) {
1703 array_unshift($components,
'color');
1704 return $this->fixColor($components);
1710 protected function reduce($value, $forExpression =
false)
1712 switch ($value[0]) {
1714 $reduced = $this->reduce($value[1]);
1716 $res = $this->reduce(array(
"variable", $this->vPrefix.$var));
1718 if ($res[0] ==
"raw_color") {
1719 $res = $this->coerceColor($res);
1722 if (empty($value[2])) {
1723 $res = $this->lib_e($res);
1729 if (is_array($key)) {
1730 $key = $this->reduce($key);
1731 $key = $this->vPrefix.$this->compileValue($this->lib_e($key));
1734 $seen = & $this->env->seenNames;
1736 if (!empty($seen[$key])) {
1737 $this->
throwError(
"infinite loop detected: $key");
1741 $out = $this->reduce($this->
get($key));
1742 $seen[$key] =
false;
1745 foreach ($value[2] as &$item) {
1746 $item = $this->reduce($item, $forExpression);
1750 return $this->evaluate($value);
1752 foreach ($value[2] as &$part) {
1753 if (is_array($part)) {
1754 $strip = $part[0] ==
"variable";
1755 $part = $this->reduce($part);
1757 $part = $this->lib_e($part);
1763 list(, $inner) = $value;
1764 return $this->lib_e($this->reduce($inner));
1771 list(, $name, $args) = $value;
1776 $f = isset($this->libFunctions[$name]) ?
1777 $this->libFunctions[$name] : array($this,
'lib_'.str_replace(
'-',
'_', $name));
1779 if (is_callable($f)) {
1780 if ($args[0] ==
'list') {
1781 $args = self::compressList($args[2], $args[1]);
1784 $ret = call_user_func($f, $this->reduce($args,
true), $this);
1786 if (is_null($ret)) {
1787 return array(
"string",
"", array(
1788 $name,
"(", $args,
")"
1793 if (is_numeric($ret)) {
1794 $ret = array(
'number', $ret,
"");
1795 } elseif (!is_array($ret)) {
1796 $ret = array(
'keyword', $ret);
1803 $value[2] = $this->reduce($value[2]);
1806 list(, $op, $exp) = $value;
1807 $exp = $this->reduce($exp);
1809 if ($exp[0] ==
"number") {
1818 return array(
"string",
"", array($op, $exp));
1821 if ($forExpression) {
1822 switch ($value[0]) {
1824 if ($color = $this->coerceColor($value)) {
1829 return $this->coerceColor($value);
1838 protected function coerceColor($value)
1840 switch ($value[0]) {
1844 $c = array(
"color", 0, 0, 0);
1845 $colorStr = substr($value[1], 1);
1846 $num = hexdec($colorStr);
1847 $width = strlen($colorStr) == 3 ? 16 : 256;
1849 for ($i = 3; $i > 0; $i--) {
1850 $t = intval($num) % $width;
1853 $c[$i] = $t * (256 / $width) + $t * floor(16/$width);
1859 if (isset(self::$cssColors[$name])) {
1860 $rgba = explode(
',', self::$cssColors[$name]);
1862 if (isset($rgba[3])) {
1863 return array(
'color', $rgba[0], $rgba[1], $rgba[2], $rgba[3]);
1865 return array(
'color', $rgba[0], $rgba[1], $rgba[2]);
1873 protected function coerceString($value)
1875 switch ($value[0]) {
1879 return array(
"string",
"", array($value[1]));
1885 protected function flattenList($value)
1887 if ($value[0] ==
"list" && count($value[2]) == 1) {
1888 return $this->flattenList($value[2][0]);
1893 public function toBool($a)
1895 return $a ? self::$TRUE : self::$FALSE;
1899 protected function evaluate($exp)
1901 list(, $op, $left, $right, $whiteBefore, $whiteAfter) = $exp;
1903 $left = $this->reduce($left,
true);
1904 $right = $this->reduce($right,
true);
1906 if ($leftColor = $this->coerceColor($left)) {
1910 if ($rightColor = $this->coerceColor($right)) {
1911 $right = $rightColor;
1919 return $this->toBool($left == self::$TRUE && $right == self::$TRUE);
1923 return $this->toBool($this->eq($left, $right));
1926 if ($op ==
"+" && !is_null($str = $this->stringConcatenate($left, $right))) {
1931 $fname =
"op_{$ltype}_{$rtype}";
1932 if (is_callable(array($this, $fname))) {
1933 $out = $this->$fname($op, $left, $right);
1934 if (!is_null($out)) {
1942 $paddedOp =
" ".$paddedOp;
1948 return array(
"string",
"", array($left, $paddedOp, $right));
1951 protected function stringConcatenate($left, $right)
1953 if ($strLeft = $this->coerceString($left)) {
1954 if ($right[0] ==
"string") {
1957 $strLeft[2][] = $right;
1961 if ($strRight = $this->coerceString($right)) {
1962 array_unshift($strRight[2], $left);
1970 protected function fixColor($c)
1972 foreach (range(1, 3) as $i) {
1984 protected function op_number_color($op, $lft, $rgt)
1986 if ($op ==
'+' || $op ==
'*') {
1987 return $this->op_color_number($op, $rgt, $lft);
1992 protected function op_color_number($op, $lft, $rgt)
1994 if ($rgt[0] ==
'%') {
1998 return $this->op_color_color(
2001 array_fill(1, count($lft) - 1, $rgt[1])
2005 protected function op_color_color($op, $left, $right)
2007 $out = array(
'color');
2008 $max = count($left) > count($right) ? count($left) : count($right);
2009 foreach (range(1, $max - 1) as $i) {
2010 $lval = isset($left[$i]) ? $left[$i] : 0;
2011 $rval = isset($right[$i]) ? $right[$i] : 0;
2014 $out[] = $lval + $rval;
2017 $out[] = $lval - $rval;
2020 $out[] = $lval * $rval;
2023 $out[] = $lval % $rval;
2027 $this->
throwError(
"evaluate error: can't divide by zero");
2029 $out[] = $lval / $rval;
2032 $this->
throwError(
'evaluate error: color op number failed on op '.$op);
2035 return $this->fixColor($out);
2038 public function lib_red($color)
2040 $color = $this->coerceColor($color);
2041 if (is_null($color)) {
2042 $this->
throwError(
'color expected for red()');
2048 public function lib_green($color)
2050 $color = $this->coerceColor($color);
2051 if (is_null($color)) {
2052 $this->
throwError(
'color expected for green()');
2058 public function lib_blue($color)
2060 $color = $this->coerceColor($color);
2061 if (is_null($color)) {
2062 $this->
throwError(
'color expected for blue()');
2070 protected function op_number_number($op, $left, $right)
2072 $unit = empty($left[2]) ? $right[2] : $left[2];
2077 $value = $left[1] + $right[1];
2080 $value = $left[1] * $right[1];
2083 $value = $left[1] - $right[1];
2086 $value = $left[1] % $right[1];
2089 if ($right[1] == 0) {
2090 $this->
throwError(
'parse error: divide by zero');
2092 $value = $left[1] / $right[1];
2095 return $this->toBool($left[1] < $right[1]);
2097 return $this->toBool($left[1] > $right[1]);
2099 return $this->toBool($left[1] >= $right[1]);
2101 return $this->toBool($left[1] <= $right[1]);
2103 $this->
throwError(
'parse error: unknown number operator: '.$op);
2106 return array(
"number", $value, $unit);
2112 protected function makeOutputBlock($type, $selectors =
null)
2114 $b =
new stdclass();
2115 $b->lines = array();
2116 $b->children = array();
2117 $b->selectors = $selectors;
2119 $b->parent = $this->scope;
2124 protected function pushEnv($block =
null)
2126 $e =
new stdclass();
2127 $e->parent = $this->env;
2128 $e->store = array();
2136 protected function popEnv()
2139 $this->env = $this->env->parent;
2144 protected function set($name, $value)
2146 $this->env->store[$name] = $value;
2151 protected function get($name)
2153 $current = $this->env;
2155 $isArguments = $name == $this->vPrefix.
'arguments';
2157 if ($isArguments && isset($current->arguments)) {
2158 return array(
'list',
' ', $current->arguments);
2161 if (isset($current->store[$name])) {
2162 return $current->store[$name];
2165 $current = isset($current->storeParent) ?
2166 $current->storeParent : $current->parent;
2169 $this->
throwError(
"variable $name is undefined");
2173 protected function injectVariables($args)
2178 foreach ($args as $name => $strValue) {
2179 if ($name[0] !==
'@') {
2183 $parser->buffer = (string) $strValue;
2184 if (!$parser->propertyValue($value)) {
2185 throw new Exception(
"failed to parse passed in variable $name: $strValue");
2188 $this->
set($name, $value);
2198 if ($fname !==
null) {
2200 $this->_parseFile = $fname;
2204 public function compile($string, $name =
null)
2206 $locale = setlocale(LC_NUMERIC,
'0');
2207 setlocale(LC_NUMERIC,
"C");
2209 $this->parser = $this->makeParser($name);
2210 $root = $this->parser->parse($string);
2213 $this->scope =
null;
2215 $this->formatter = $this->newFormatter();
2217 if (!empty($this->registeredVars)) {
2218 $this->injectVariables($this->registeredVars);
2221 $this->sourceParser = $this->parser;
2225 $this->formatter->block($this->scope);
2226 $out = ob_get_clean();
2227 setlocale(LC_NUMERIC, $locale);
2231 public function compileFile($fname, $outFname =
null)
2233 if (!is_readable($fname)) {
2234 throw new Exception(
'load error: failed to find '.$fname);
2237 $pi = pathinfo($fname);
2239 $oldImport = $this->importDir;
2241 $this->importDir = (array) $this->importDir;
2242 $this->importDir[] = $pi[
'dirname'].
'/';
2244 $this->addParsedFile($fname);
2246 $out = $this->compile(file_get_contents($fname), $fname);
2248 $this->importDir = $oldImport;
2250 if ($outFname !==
null) {
2251 return file_put_contents($outFname, $out);
2258 public function checkedCompile($in, $out)
2260 if (!is_file($out) || filemtime($in) > filemtime($out)) {
2261 $this->compileFile($in, $out);
2292 if (is_string($in)) {
2294 } elseif (is_array($in) && isset($in[
'root'])) {
2295 if ($force || !isset($in[
'files'])) {
2299 $root = $in[
'root'];
2300 } elseif (isset($in[
'files']) && is_array($in[
'files'])) {
2301 foreach ($in[
'files'] as $fname => $ftime) {
2302 if (!file_exists($fname) || filemtime($fname) > $ftime) {
2305 $root = $in[
'root'];
2316 if ($root !==
null) {
2319 $out[
'root'] = $root;
2320 $out[
'compiled'] = $this->compileFile($root);
2321 $out[
'files'] = $this->allParsedFiles();
2322 $out[
'updated'] = time();
2333 public function parse($str =
null, $initialVariables =
null)
2335 if (is_array($str)) {
2336 $initialVariables = $str;
2340 $oldVars = $this->registeredVars;
2341 if ($initialVariables !==
null) {
2342 $this->setVariables($initialVariables);
2346 if (empty($this->_parseFile)) {
2347 throw new exception(
"nothing to parse");
2350 $out = $this->compileFile($this->_parseFile);
2352 $out = $this->compile($str);
2355 $this->registeredVars = $oldVars;
2359 protected function makeParser($name)
2362 $parser->writeComments = $this->preserveComments;
2367 public function setFormatter($name)
2369 $this->formatterName = $name;
2372 protected function newFormatter()
2374 $className =
"lessc_formatter_lessjs";
2375 if (!empty($this->formatterName)) {
2376 if (!is_string($this->formatterName)) {
2377 return $this->formatterName;
2379 $className =
"lessc_formatter_$this->formatterName";
2382 return new $className();
2385 public function setPreserveComments($preserve)
2387 $this->preserveComments = $preserve;
2390 public function registerFunction($name, $func)
2392 $this->libFunctions[$name] = $func;
2395 public function unregisterFunction($name)
2397 unset($this->libFunctions[$name]);
2400 public function setVariables($variables)
2402 $this->registeredVars = array_merge($this->registeredVars, $variables);
2405 public function unsetVariable($name)
2407 unset($this->registeredVars[$name]);
2410 public function setImportDir($dirs)
2412 $this->importDir = (array) $dirs;
2415 public function addImportDir($dir)
2417 $this->importDir = (array) $this->importDir;
2418 $this->importDir[] = $dir;
2421 public function allParsedFiles()
2423 return $this->allParsedFiles;
2426 public function addParsedFile($file)
2428 $this->allParsedFiles[realpath($file)] = filemtime($file);
2436 if ($this->sourceLoc >= 0) {
2437 $this->sourceParser->throwError($msg, $this->sourceLoc);
2439 throw new exception($msg);
2444 public static function ccompile($in, $out, $less =
null)
2446 if ($less ===
null) {
2449 return $less->checkedCompile($in, $out);
2452 public static function cexecute($in, $force =
false, $less =
null)
2454 if ($less ===
null) {
2457 return $less->cachedCompile($in, $force);
2460 protected static $cssColors = array(
2461 'aliceblue' =>
'240,248,255',
2462 'antiquewhite' =>
'250,235,215',
2463 'aqua' =>
'0,255,255',
2464 'aquamarine' =>
'127,255,212',
2465 'azure' =>
'240,255,255',
2466 'beige' =>
'245,245,220',
2467 'bisque' =>
'255,228,196',
2469 'blanchedalmond' =>
'255,235,205',
2470 'blue' =>
'0,0,255',
2471 'blueviolet' =>
'138,43,226',
2472 'brown' =>
'165,42,42',
2473 'burlywood' =>
'222,184,135',
2474 'cadetblue' =>
'95,158,160',
2475 'chartreuse' =>
'127,255,0',
2476 'chocolate' =>
'210,105,30',
2477 'coral' =>
'255,127,80',
2478 'cornflowerblue' =>
'100,149,237',
2479 'cornsilk' =>
'255,248,220',
2480 'crimson' =>
'220,20,60',
2481 'cyan' =>
'0,255,255',
2482 'darkblue' =>
'0,0,139',
2483 'darkcyan' =>
'0,139,139',
2484 'darkgoldenrod' =>
'184,134,11',
2485 'darkgray' =>
'169,169,169',
2486 'darkgreen' =>
'0,100,0',
2487 'darkgrey' =>
'169,169,169',
2488 'darkkhaki' =>
'189,183,107',
2489 'darkmagenta' =>
'139,0,139',
2490 'darkolivegreen' =>
'85,107,47',
2491 'darkorange' =>
'255,140,0',
2492 'darkorchid' =>
'153,50,204',
2493 'darkred' =>
'139,0,0',
2494 'darksalmon' =>
'233,150,122',
2495 'darkseagreen' =>
'143,188,143',
2496 'darkslateblue' =>
'72,61,139',
2497 'darkslategray' =>
'47,79,79',
2498 'darkslategrey' =>
'47,79,79',
2499 'darkturquoise' =>
'0,206,209',
2500 'darkviolet' =>
'148,0,211',
2501 'deeppink' =>
'255,20,147',
2502 'deepskyblue' =>
'0,191,255',
2503 'dimgray' =>
'105,105,105',
2504 'dimgrey' =>
'105,105,105',
2505 'dodgerblue' =>
'30,144,255',
2506 'firebrick' =>
'178,34,34',
2507 'floralwhite' =>
'255,250,240',
2508 'forestgreen' =>
'34,139,34',
2509 'fuchsia' =>
'255,0,255',
2510 'gainsboro' =>
'220,220,220',
2511 'ghostwhite' =>
'248,248,255',
2512 'gold' =>
'255,215,0',
2513 'goldenrod' =>
'218,165,32',
2514 'gray' =>
'128,128,128',
2515 'green' =>
'0,128,0',
2516 'greenyellow' =>
'173,255,47',
2517 'grey' =>
'128,128,128',
2518 'honeydew' =>
'240,255,240',
2519 'hotpink' =>
'255,105,180',
2520 'indianred' =>
'205,92,92',
2521 'indigo' =>
'75,0,130',
2522 'ivory' =>
'255,255,240',
2523 'khaki' =>
'240,230,140',
2524 'lavender' =>
'230,230,250',
2525 'lavenderblush' =>
'255,240,245',
2526 'lawngreen' =>
'124,252,0',
2527 'lemonchiffon' =>
'255,250,205',
2528 'lightblue' =>
'173,216,230',
2529 'lightcoral' =>
'240,128,128',
2530 'lightcyan' =>
'224,255,255',
2531 'lightgoldenrodyellow' =>
'250,250,210',
2532 'lightgray' =>
'211,211,211',
2533 'lightgreen' =>
'144,238,144',
2534 'lightgrey' =>
'211,211,211',
2535 'lightpink' =>
'255,182,193',
2536 'lightsalmon' =>
'255,160,122',
2537 'lightseagreen' =>
'32,178,170',
2538 'lightskyblue' =>
'135,206,250',
2539 'lightslategray' =>
'119,136,153',
2540 'lightslategrey' =>
'119,136,153',
2541 'lightsteelblue' =>
'176,196,222',
2542 'lightyellow' =>
'255,255,224',
2543 'lime' =>
'0,255,0',
2544 'limegreen' =>
'50,205,50',
2545 'linen' =>
'250,240,230',
2546 'magenta' =>
'255,0,255',
2547 'maroon' =>
'128,0,0',
2548 'mediumaquamarine' =>
'102,205,170',
2549 'mediumblue' =>
'0,0,205',
2550 'mediumorchid' =>
'186,85,211',
2551 'mediumpurple' =>
'147,112,219',
2552 'mediumseagreen' =>
'60,179,113',
2553 'mediumslateblue' =>
'123,104,238',
2554 'mediumspringgreen' =>
'0,250,154',
2555 'mediumturquoise' =>
'72,209,204',
2556 'mediumvioletred' =>
'199,21,133',
2557 'midnightblue' =>
'25,25,112',
2558 'mintcream' =>
'245,255,250',
2559 'mistyrose' =>
'255,228,225',
2560 'moccasin' =>
'255,228,181',
2561 'navajowhite' =>
'255,222,173',
2562 'navy' =>
'0,0,128',
2563 'oldlace' =>
'253,245,230',
2564 'olive' =>
'128,128,0',
2565 'olivedrab' =>
'107,142,35',
2566 'orange' =>
'255,165,0',
2567 'orangered' =>
'255,69,0',
2568 'orchid' =>
'218,112,214',
2569 'palegoldenrod' =>
'238,232,170',
2570 'palegreen' =>
'152,251,152',
2571 'paleturquoise' =>
'175,238,238',
2572 'palevioletred' =>
'219,112,147',
2573 'papayawhip' =>
'255,239,213',
2574 'peachpuff' =>
'255,218,185',
2575 'peru' =>
'205,133,63',
2576 'pink' =>
'255,192,203',
2577 'plum' =>
'221,160,221',
2578 'powderblue' =>
'176,224,230',
2579 'purple' =>
'128,0,128',
2581 'rosybrown' =>
'188,143,143',
2582 'royalblue' =>
'65,105,225',
2583 'saddlebrown' =>
'139,69,19',
2584 'salmon' =>
'250,128,114',
2585 'sandybrown' =>
'244,164,96',
2586 'seagreen' =>
'46,139,87',
2587 'seashell' =>
'255,245,238',
2588 'sienna' =>
'160,82,45',
2589 'silver' =>
'192,192,192',
2590 'skyblue' =>
'135,206,235',
2591 'slateblue' =>
'106,90,205',
2592 'slategray' =>
'112,128,144',
2593 'slategrey' =>
'112,128,144',
2594 'snow' =>
'255,250,250',
2595 'springgreen' =>
'0,255,127',
2596 'steelblue' =>
'70,130,180',
2597 'tan' =>
'210,180,140',
2598 'teal' =>
'0,128,128',
2599 'thistle' =>
'216,191,216',
2600 'tomato' =>
'255,99,71',
2601 'transparent' =>
'0,0,0,0',
2602 'turquoise' =>
'64,224,208',
2603 'violet' =>
'238,130,238',
2604 'wheat' =>
'245,222,179',
2605 'white' =>
'255,255,255',
2606 'whitesmoke' =>
'245,245,245',
2607 'yellow' =>
'255,255,0',
2608 'yellowgreen' =>
'154,205,50'
2616 protected static $nextBlockId = 0;
2618 protected static $precedence = array(
2632 protected static $whitePattern;
2633 protected static $commentMulti;
2635 protected static $commentSingle =
"//";
2636 protected static $commentMultiLeft =
"/*";
2637 protected static $commentMultiRight =
"*/";
2640 protected static $operatorString;
2643 protected static $supressDivisionProps =
2644 array(
'/border-radius$/i',
'/^font$/i');
2646 protected $blockDirectives = array(
"font-face",
"keyframes",
"page",
"-moz-document",
"viewport",
"-moz-viewport",
"-o-viewport",
"-ms-viewport");
2647 protected $lineDirectives = array(
"charset");
2661 protected static $literalCache = array();
2667 public $eatWhiteDefault;
2670 public $writeComments;
2671 public $seenComments;
2672 public $currentProperty;
2676 public function __construct($lessc, $sourceName =
null)
2678 $this->eatWhiteDefault =
true;
2680 $this->lessc = $lessc;
2682 $this->sourceName = $sourceName;
2684 $this->writeComments =
false;
2686 if (!self::$operatorString) {
2687 self::$operatorString =
2688 '('.implode(
'|', array_map(
2689 array(
'lessc',
'preg_quote'),
2690 array_keys(self::$precedence)
2693 $commentSingle = Lessc::preg_quote(self::$commentSingle);
2694 $commentMultiLeft = Lessc::preg_quote(self::$commentMultiLeft);
2695 $commentMultiRight = Lessc::preg_quote(self::$commentMultiRight);
2697 self::$commentMulti = $commentMultiLeft.
'.*?'.$commentMultiRight;
2698 self::$whitePattern =
'/'.$commentSingle.
'[^\n]*\s*|('.self::$commentMulti.
')\s*|\s+/Ais';
2715 $this->buffer = $this->writeComments ? $buffer : $this->removeComments($buffer);
2716 $this->pushSpecialBlock(
"root");
2717 $this->eatWhiteDefault =
true;
2718 $this->seenComments = array();
2725 $this->whitespace();
2730 if ($this->count != strlen($this->buffer)) {
2731 $this->throwError(
'parse error count '.$this->count.
' != len buffer '.strlen($this->buffer));
2736 if (!property_exists($this->env,
'parent') || !is_null($this->env->parent)) {
2737 throw new exception(
'parse error: unclosed block');
2781 if (empty($this->buffer)) {
2786 if ($this->whitespace()) {
2792 $mediaQueries =
null;
2795 $importValue =
null;
2806 if ($this->keyword($key) && $this->
assign() &&
2807 $this->propertyValue($value, $key) && $this->end()
2809 $this->append(array(
'assign', $key, $value), $s);
2817 if ($this->literal(
'@',
false)) {
2821 if ($this->literal(
'@media')) {
2822 if ($this->mediaQueryList($mediaQueries)
2823 && $this->literal(
'{')
2825 $media = $this->pushSpecialBlock(
"media");
2826 $media->queries = is_null($mediaQueries) ? array() : $mediaQueries;
2834 if ($this->literal(
"@",
false) && $this->keyword($dirName)) {
2835 if ($this->isDirective($dirName, $this->blockDirectives)) {
2836 if ($this->openString(
"{", $dirValue,
null, array(
";")) &&
2839 $dir = $this->pushSpecialBlock(
"directive");
2840 $dir->name = $dirName;
2841 if (isset($dirValue)) {
2842 $dir->value = $dirValue;
2846 } elseif ($this->isDirective($dirName, $this->lineDirectives)) {
2847 if ($this->propertyValue($dirValue) && $this->end()) {
2848 $this->append(array(
"directive", $dirName, $dirValue));
2858 if ($this->variable($var) && $this->
assign() &&
2859 $this->propertyValue($value) && $this->end()
2861 $this->append(array(
'assign', $var, $value), $s);
2867 if ($this->
import($importValue)) {
2868 $this->append($importValue, $s);
2873 if ($this->tag($tag,
true) && $this->argumentDef($args, $isVararg) &&
2874 $this->guards($guards) &&
2877 $block = $this->pushBlock($this->fixTags(array($tag)));
2878 $block->args = $args;
2879 $block->isVararg = $isVararg;
2880 if (!empty($guards)) {
2881 $block->guards = $guards;
2889 if ($this->tags($tags) && $this->literal(
'{',
false)) {
2890 $tags = $this->fixTags($tags);
2891 $this->pushBlock($tags);
2898 if ($this->literal(
'}',
false)) {
2900 $block = $this->pop();
2901 }
catch (exception $e) {
2903 $this->throwError($e->getMessage());
2907 if (is_null($block->type)) {
2909 if (!isset($block->args)) {
2910 foreach ($block->tags as $tag) {
2911 if (!is_string($tag) || $tag[0] != $this->lessc->mPrefix) {
2918 foreach ($block->tags as $tag) {
2919 if (is_string($tag)) {
2920 $this->env->children[$tag][] = $block;
2926 $this->append(array(
'block', $block), $s);
2931 $this->whitespace();
2936 if ($this->mixinTags($tags) &&
2937 $this->argumentDef($argv, $isVararg) &&
2938 $this->keyword($suffix) && $this->end()
2940 $tags = $this->fixTags($tags);
2941 $this->append(array(
'mixin', $tags, $argv, $suffix), $s);
2948 if ($this->literal(
';')) {
2955 protected function isDirective($dirname, $directives)
2960 array_map(array(
"lessc",
"preg_quote"), $directives)
2962 $pattern =
'/^(-[a-z-]+-)?('.$pattern.
')$/i';
2964 return preg_match($pattern, $dirname);
2967 protected function fixTags($tags)
2970 foreach ($tags as &$tag) {
2971 if ($tag[0] == $this->lessc->vPrefix) {
2972 $tag[0] = $this->lessc->mPrefix;
2979 protected function expressionList(&$exps)
2989 if (count($values) == 0) {
2993 $exps = Lessc::compressList($values,
' ');
3006 if ($this->value($lhs)) {
3010 if (!empty($this->env->supressedDivision)) {
3011 unset($this->env->supressedDivision);
3013 if ($this->literal(
"/") && $this->value($rhs)) {
3014 $out = array(
"list",
"",
3015 array($out, array(
"keyword",
"/"), $rhs));
3034 $this->inExp =
true;
3035 $ss = $this->seek();
3038 $whiteBefore = isset($this->buffer[$this->count - 1]) &&
3039 ctype_space($this->buffer[$this->count - 1]);
3046 if ($this->match(self::$operatorString.($needWhite ?
'\s' :
''), $m) && self::$precedence[$m[1]] >= $minP) {
3047 if (!$this->inParens && isset($this->env->currentProperty) && $m[1] ==
"/" && empty($this->env->supressedDivision)) {
3048 foreach (self::$supressDivisionProps as $pattern) {
3049 if (preg_match($pattern, $this->env->currentProperty)) {
3050 $this->env->supressedDivision =
true;
3057 $whiteAfter = isset($this->buffer[$this->count - 1]) &&
3058 ctype_space($this->buffer[$this->count - 1]);
3060 if (!$this->value($rhs)) {
3065 if ($this->peek(self::$operatorString, $next) && self::$precedence[$next[1]] > self::$precedence[$m[1]]) {
3066 $rhs = $this->
expHelper($rhs, self::$precedence[$next[1]]);
3069 $lhs = array(
'expression', $m[1], $lhs, $rhs, $whiteBefore, $whiteAfter);
3070 $ss = $this->seek();
3084 public function propertyValue(&$value, $keyName =
null)
3089 if ($keyName !==
null) {
3090 $this->env->currentProperty = $keyName;
3094 while ($this->expressionList($v)) {
3097 if (!$this->literal(
',')) {
3106 if ($keyName !==
null) {
3107 unset($this->env->currentProperty);
3110 if (count($values) == 0) {
3114 $value = Lessc::compressList($values,
', ');
3118 protected function parenValue(&$out)
3125 if (isset($this->buffer[$this->count]) && $this->buffer[$this->count] !=
"(") {
3130 if ($this->literal(
"(") &&
3131 ($this->inParens =
true) && $this->
expression($exp) &&
3146 protected function value(&$value)
3156 if (isset($this->buffer[$this->count]) && $this->buffer[$this->count] ==
"-") {
3158 if ($this->literal(
"-",
false) &&
3159 (($this->variable($inner) && $inner = array(
"variable", $inner)) ||
3160 $this->unit($inner) ||
3161 $this->parenValue($inner))
3163 $value = array(
"unary",
"-", $inner);
3170 if ($this->parenValue($value)) {
3173 if ($this->unit($value)) {
3176 if ($this->color($value)) {
3179 if ($this->func($value)) {
3182 if ($this->
string($value)) {
3186 if ($this->keyword($word)) {
3187 $value = array(
'keyword', $word);
3192 if ($this->variable($var)) {
3193 $value = array(
'variable', $var);
3198 if ($this->literal(
"~") && $this->
string($str)) {
3199 $value = array(
"escape", $str);
3207 if ($this->literal(
'\\') && $this->match(
'([0-9]+)', $m)) {
3208 $value = array(
'keyword',
'\\'.$m[1]);
3218 protected function import(&$out, $value =
'')
3220 if (!$this->literal(
'@import')) {
3228 if ($this->propertyValue($value)) {
3229 $out = array(
"import", $value);
3236 protected function mediaQueryList(&$out)
3240 if ($this->genericList($list,
"mediaQuery",
",",
false)) {
3247 protected function mediaQuery(&$out)
3253 $expressions =
null;
3256 if ((($this->literal(
"only") && ($only =
true)) || ($this->literal(
"not") && ($not =
true))) && $this->keyword($mediaType)) {
3257 $prop = array(
"mediaType");
3264 $prop[] = $mediaType;
3271 if (!empty($mediaType) && !$this->literal(
"and")) {
3274 $this->genericList($expressions,
"mediaExpression",
"and",
false);
3275 if (is_array($expressions)) {
3276 $parts = array_merge($parts, $expressions[2]);
3280 if (count($parts) == 0) {
3289 protected function mediaExpression(&$out)
3296 if ($this->literal(
"(") &&
3297 $this->keyword($feature) &&
3298 ($this->literal(
":") && $this->
expression($value)) &&
3301 $out = array(
"mediaExp", $feature);
3306 } elseif ($this->variable($variable)) {
3307 $out = array(
'variable', $variable);
3316 protected function openString($end, &$out, $nestingOpen =
null, $rejectStrs =
null)
3321 $oldWhite = $this->eatWhiteDefault;
3322 $this->eatWhiteDefault =
false;
3324 $stop = array(
"'",
'"',
"@{", $end);
3325 $stop = array_map(array(
"lessc",
"preg_quote"), $stop);
3328 if (!is_null($rejectStrs)) {
3329 $stop = array_merge($stop, $rejectStrs);
3332 $patt =
'(.*?)('.implode(
"|", $stop).
')';
3338 while ($this->match($patt, $m,
false)) {
3339 if (!empty($m[1])) {
3342 $nestingLevel += substr_count($m[1], $nestingOpen);
3348 $this->count -= strlen($tok);
3350 if ($nestingLevel == 0) {
3357 if (($tok ==
"'" || $tok ==
'"') && $this->
string($str)) {
3362 if ($tok ==
"@{" && $this->interpolation($inter)) {
3363 $content[] = $inter;
3367 if (!empty($rejectStrs) && in_array($tok, $rejectStrs)) {
3372 $this->count += strlen($tok);
3375 $this->eatWhiteDefault = $oldWhite;
3377 if (count($content) == 0) {
3382 if (is_string(end($content))) {
3383 $content[count($content) - 1] = rtrim(end($content));
3386 $out = array(
"string",
"", $content);
3390 protected function string(&$out)
3395 if ($this->literal(
'"',
false)) {
3397 } elseif ($this->literal(
"'",
false)) {
3406 $patt =
'([^\n]*?)(@\{|\\\\|'.
3407 Lessc::preg_quote($delim).
')';
3409 $oldWhite = $this->eatWhiteDefault;
3410 $this->eatWhiteDefault =
false;
3413 while ($this->match($patt, $m,
false)) {
3415 if ($m[2] ==
"@{") {
3416 $this->count -= strlen($m[2]);
3417 if ($this->interpolation($inter)) {
3418 $content[] = $inter;
3420 $this->count += strlen($m[2]);
3423 } elseif ($m[2] ==
'\\') {
3425 if ($this->literal($delim,
false)) {
3426 $content[] = $delim;
3429 $this->count -= strlen($delim);
3434 $this->eatWhiteDefault = $oldWhite;
3436 if ($this->literal($delim)) {
3437 $out = array(
"string", $delim, $content);
3445 protected function interpolation(&$out)
3449 $oldWhite = $this->eatWhiteDefault;
3450 $this->eatWhiteDefault =
true;
3453 if ($this->literal(
"@{") &&
3454 $this->openString(
"}", $interp,
null, array(
"'",
'"',
";")) &&
3455 $this->literal(
"}",
false)
3457 $out = array(
"interpolate", $interp);
3458 $this->eatWhiteDefault = $oldWhite;
3459 if ($this->eatWhiteDefault) {
3460 $this->whitespace();
3465 $this->eatWhiteDefault = $oldWhite;
3470 protected function unit(&$unit)
3475 if (isset($this->buffer[$this->count])) {
3476 $char = $this->buffer[$this->count];
3477 if (!ctype_digit($char) && $char !=
".") {
3482 if ($this->match(
'([0-9]+(?:\.[0-9]*)?|\.[0-9]+)([%a-zA-Z]+)?', $m)) {
3483 $unit = array(
"number", $m[1], empty($m[2]) ?
"" : $m[2]);
3490 protected function color(&$out)
3494 if ($this->match(
'(#(?:[0-9a-f]{8}|[0-9a-f]{6}|[0-9a-f]{3}))', $m)) {
3495 if (strlen($m[1]) > 7) {
3496 $out = array(
"string",
"", array($m[1]));
3498 $out = array(
"raw_color", $m[1]);
3511 protected function argumentDef(&$args, &$isVararg)
3517 if (!$this->literal(
'(')) {
3523 $method =
"expressionList";
3527 if ($this->literal(
"...")) {
3532 if ($this->$method($value)) {
3533 if ($value[0] ==
"variable") {
3534 $arg = array(
"arg", $value[1]);
3535 $ss = $this->seek();
3537 if ($this->
assign() && $this->$method($rhs)) {
3541 if ($this->literal(
"...")) {
3553 $values[] = array(
"lit", $value);
3558 if (!$this->literal($delim)) {
3559 if ($delim ==
"," && $this->literal(
";")) {
3562 $method =
"propertyValue";
3565 if (isset($values[1])) {
3567 foreach ($values as $i => $arg) {
3571 $this->throwError(
"Cannot mix ; and , as delimiter types");
3573 $newList[] = $arg[2];
3576 $newList[] = $arg[1];
3579 $this->throwError(
"Unexpected rest before semicolon");
3583 $newList = array(
"list",
", ", $newList);
3585 switch ($values[0][0]) {
3587 $newArg = array(
"arg", $values[0][1], $newList);
3590 $newArg = array(
"lit", $newList);
3594 } elseif ($values) {
3595 $newArg = $values[0];
3599 $values = array($newArg);
3607 if (!$this->literal(
')')) {
3619 protected function tags(&$tags, $simple =
false, $delim =
',')
3624 while ($this->tag($tt, $simple)) {
3626 if (!$this->literal($delim)) {
3630 if (count($tags) == 0) {
3639 protected function mixinTags(&$tags)
3644 while ($this->tag($tt,
true)) {
3646 $this->literal(
">");
3657 protected function tagBracket(&$parts, &$hasExpression)
3664 if (isset($this->buffer[$this->count]) && $this->buffer[$this->count] !=
"[") {
3670 $hasInterpolation =
false;
3672 if ($this->literal(
"[",
false)) {
3673 $attrParts = array(
"[");
3676 if ($this->literal(
"]",
false)) {
3682 if ($this->match(
'\s+', $m)) {
3686 if ($this->
string($str)) {
3688 foreach ($str[2] as &$chunk) {
3689 $chunk = str_replace($this->lessc->parentSelector,
"$&$", $chunk);
3692 $attrParts[] = $str;
3693 $hasInterpolation =
true;
3697 if ($this->keyword($word)) {
3698 $attrParts[] = $word;
3702 if ($this->interpolation($inter)) {
3703 $attrParts[] = $inter;
3704 $hasInterpolation =
true;
3709 if ($this->match(
'[|-~\$\*\^=]+', $m)) {
3710 $attrParts[] = $m[0];
3717 if ($this->literal(
"]",
false)) {
3719 foreach ($attrParts as $part) {
3722 $hasExpression = $hasExpression || $hasInterpolation;
3733 protected function tag(&$tag, $simple =
false)
3739 $chars =
'^@,:;{}\][>\(\) "\'';
3741 $chars =
'^@,;{}["\'';
3745 $hasExpression =
false;
3747 while ($this->tagBracket($parts, $hasExpression));
3749 $oldWhite = $this->eatWhiteDefault;
3750 $this->eatWhiteDefault =
false;
3754 if ($this->match(
'(['.$chars.
'0-9]['.$chars.
']*)', $m)) {
3760 while ($this->tagBracket($parts, $hasExpression));
3764 if (isset($this->buffer[$this->count]) && $this->buffer[$this->count] ==
"@") {
3765 if ($this->interpolation($interp)) {
3766 $hasExpression =
true;
3772 if ($this->literal(
"@")) {
3778 if ($this->unit($unit)) {
3779 $parts[] = $unit[1];
3780 $parts[] = $unit[2];
3787 $this->eatWhiteDefault = $oldWhite;
3793 if ($hasExpression) {
3794 $tag = array(
"exp", array(
"string",
"", $parts));
3796 $tag = trim(implode($parts));
3799 $this->whitespace();
3804 protected function func(&$func)
3813 if ($this->match(
'(%|[\w\-_][\w\-_:\.]+|[\w_])', $m) && $this->literal(
'(')) {
3816 $sPreArgs = $this->seek();
3820 $ss = $this->seek();
3822 if ($this->keyword($name) && $this->literal(
'=') && $this->expressionList($value)) {
3823 $args[] = array(
"string",
"", array($name,
"=", $value));
3826 if ($this->expressionList($value)) {
3831 if (!$this->literal(
',')) {
3835 $args = array(
'list',
',', $args);
3837 if ($this->literal(
')')) {
3838 $func = array(
'function', $fname, $args);
3840 } elseif ($fname ==
'url') {
3842 $this->seek($sPreArgs);
3843 if ($this->openString(
")", $string) && $this->literal(
")")) {
3844 $func = array(
'function', $fname, $string);
3855 protected function variable(&$name)
3861 if ($this->literal($this->lessc->vPrefix,
false) &&
3862 ($this->variable($sub) || $this->keyword($name))
3865 $name = array(
'variable', $sub);
3867 $name = $this->lessc->vPrefix.$name;
3884 $this->currentProperty = $name;
3886 return $this->literal(
':') || $this->literal(
'=');
3890 protected function keyword(&$word)
3893 if ($this->match(
'([\w_\-\*!"][\w\-_"]*)', $m)) {
3901 protected function end()
3903 if ($this->literal(
';',
false)) {
3905 } elseif ($this->count == strlen($this->buffer) || $this->buffer[$this->count] ==
'}') {
3912 protected function guards(&$guards)
3918 if (!$this->literal(
"when")) {
3925 while ($this->guardGroup($g)) {
3927 if (!$this->literal(
",")) {
3932 if (count($guards) == 0) {
3943 protected function guardGroup(&$guardGroup)
3948 $guardGroup = array();
3949 while ($this->guard($guard)) {
3950 $guardGroup[] = $guard;
3951 if (!$this->literal(
"and")) {
3956 if (count($guardGroup) == 0) {
3965 protected function guard(&$guard)
3970 $negate = $this->literal(
"not");
3972 if ($this->literal(
"(") && $this->
expression($exp) && $this->literal(
")")) {
3975 $guard = array(
"negate", $guard);
3986 protected function literal($what, $eatWhitespace =
null)
3988 if ($eatWhitespace ===
null) {
3989 $eatWhitespace = $this->eatWhiteDefault;
3993 if (!isset($what[1]) && isset($this->buffer[$this->count])) {
3994 if ($this->buffer[$this->count] == $what) {
3995 if (!$eatWhitespace) {
4005 if (!isset(self::$literalCache[$what])) {
4006 self::$literalCache[$what] = Lessc::preg_quote($what);
4010 return $this->match(self::$literalCache[$what], $m, $eatWhitespace);
4013 protected function genericList(&$out, $parseItem, $delim =
"", $flatten =
true)
4019 while ($this->$parseItem($value)) {
4022 if (!$this->literal($delim)) {
4028 if (count($items) == 0) {
4033 if ($flatten && count($items) == 1) {
4036 $out = array(
"list", $delim, $items);
4046 protected function to($what, &$out, $until =
false, $allowNewline =
false)
4048 if (is_string($allowNewline)) {
4049 $validChars = $allowNewline;
4051 $validChars = $allowNewline ?
"." :
"[^\n]";
4054 if (!$this->match(
'('.$validChars.
'*?)'.Lessc::preg_quote($what), $m, !$until)) {
4058 $this->count -= strlen($what);
4065 protected function match($regex, &$out, $eatWhitespace =
null)
4067 if ($eatWhitespace ===
null) {
4068 $eatWhitespace = $this->eatWhiteDefault;
4071 $r =
'/'.$regex.($eatWhitespace && !$this->writeComments ?
'\s*' :
'').
'/Ais';
4072 if (preg_match($r, $this->buffer, $out, 0, $this->count)) {
4073 $this->count += strlen($out[0]);
4074 if ($eatWhitespace && $this->writeComments) {
4075 $this->whitespace();
4083 protected function whitespace()
4085 if ($this->writeComments) {
4088 while (preg_match(self::$whitePattern, $this->buffer, $m, 0, $this->count)) {
4089 if (isset($m[1]) && empty($this->seenComments[$this->count])) {
4090 $this->append(array(
"comment", $m[1]));
4091 $this->seenComments[$this->count] =
true;
4093 $this->count += strlen($m[0]);
4098 $this->match(
"", $m);
4099 return strlen($m[0]) > 0;
4104 protected function peek($regex, &$out =
null, $from =
null)
4106 if (is_null($from)) {
4107 $from = $this->count;
4109 $r =
'/'.$regex.
'/Ais';
4110 $result = preg_match($r, $this->buffer, $out, 0, $from);
4116 protected function seek($where =
null)
4118 if ($where ===
null) {
4119 return $this->count;
4121 $this->count = $where;
4128 public function throwError($msg =
"parse error", $count =
null)
4130 $count = is_null($count) ? $this->count : $count;
4132 $line = $this->line +
4133 substr_count(substr($this->buffer, 0, $count),
"\n");
4135 if (!empty($this->sourceName)) {
4136 $loc =
"$this->sourceName on line $line";
4138 $loc =
"line: $line";
4143 if ($this->peek(
"(.*?)(\n|$)", $m, $count)) {
4144 throw new exception(
"$msg: failed at `$m[1]` $loc");
4146 throw new exception(
"$msg: $loc");
4150 protected function pushBlock($selectors =
null, $type =
null)
4152 $b =
new stdclass();
4153 $b->parent = $this->env;
4156 $b->id = self::$nextBlockId++;
4158 $b->isVararg =
false;
4159 $b->tags = $selectors;
4161 $b->props = array();
4162 $b->children = array();
4169 protected function pushSpecialBlock($type)
4171 return $this->pushBlock(
null, $type);
4175 protected function append($prop, $pos =
null)
4177 if ($pos !==
null) {
4180 $this->env->props[] = $prop;
4184 protected function pop()
4187 $this->env = $this->env->parent;
4193 protected function removeComments($text)
4196 'url(',
'//',
'/*',
'"',
"'"
4203 foreach ($look as $token) {
4204 $pos = strpos($text, $token);
4205 if ($pos !==
false) {
4206 if (!isset($min) || $pos < $min[1]) {
4207 $min = array($token, $pos);
4212 if (is_null($min)) {
4222 if (preg_match(
'/url\(.*?\)/', $text, $m, 0, $count)) {
4223 $count += strlen($m[0]) - strlen($min[0]);
4229 if (preg_match(
'/'.$min[0].
'.*?(?<!\\\\)'.$min[0].
'/', $text, $m, 0, $count)) {
4230 $count += strlen($m[0]) - 1;
4234 $skip = strpos($text,
"\n", $count);
4235 if ($skip ===
false) {
4236 $skip = strlen($text) - $count;
4243 if (preg_match(
'/\/\*.*?\*\//s', $text, $m, 0, $count)) {
4244 $skip = strlen($m[0]);
4245 $newlines = substr_count($m[0],
"\n");
4251 $count += strlen($min[0]);
4254 $out .= substr($text, 0, $count).str_repeat(
"\n", $newlines);
4255 $text = substr($text, $count + $skip);