42 public static $VERSION =
"v0.8.0";
44 public static $TRUE = array(
"keyword",
"true");
45 public static $FALSE = array(
"keyword",
"false");
47 protected $libFunctions = array();
48 protected $registeredVars = array();
49 protected $preserveComments =
false;
51 public $vPrefix =
'@';
52 public $mPrefix =
'$';
53 public $parentSelector =
'&';
55 public $importDisabled =
false;
56 public $importDir =
'';
60 public $formatterName;
66 protected $numberPrecision =
null;
68 protected $allParsedFiles = array();
72 protected $sourceParser =
null;
73 protected $sourceLoc =
null;
75 protected static $nextImportId = 0;
78 protected function findImport($url)
80 foreach ((array) $this->importDir as $dir) {
81 $full = $dir.(substr($dir, -1) !=
'/' ?
'/' :
'').$url;
98 return is_file($name);
101 public static function compressList($items, $delim)
103 if (!isset($items[1]) && isset($items[0])) {
106 return array(
'list', $delim, $items);
110 public static function preg_quote($what)
112 return preg_quote($what,
'/');
115 protected function tryImport($importPath, $parentBlock, $out)
117 if ($importPath[0] ==
"function" && $importPath[1] ==
"url") {
118 $importPath = $this->flattenList($importPath[2]);
121 $str = $this->coerceString($importPath);
129 if (substr_compare($url,
'.css', -4, 4) === 0) {
133 $realPath = $this->findImport($url);
135 if ($realPath ===
null) {
139 if ($this->importDisabled) {
140 return array(
false,
"/* import disabled */");
143 if (isset($this->allParsedFiles[realpath($realPath)])) {
144 return array(
false,
null);
147 $this->addParsedFile($realPath);
148 $parser = $this->makeParser($realPath);
149 $root = $parser->parse(file_get_contents($realPath));
152 foreach ($root->props as $prop) {
153 if ($prop[0] ==
"block") {
154 $prop[1]->parent = $parentBlock;
161 foreach ($root->children as $childName => $child) {
162 if (isset($parentBlock->children[$childName])) {
163 $parentBlock->children[$childName] = array_merge(
164 $parentBlock->children[$childName],
168 $parentBlock->children[$childName] = $child;
172 $pi = pathinfo($realPath);
173 $dir = $pi[
"dirname"];
175 list($top, $bottom) = $this->sortProps($root->props,
true);
176 $this->compileImportedProps($top, $parentBlock, $out, $parser, $dir);
178 return array(
true, $bottom, $parser, $dir);
181 protected function compileImportedProps($props, $block, $out, $sourceParser, $importDir)
183 $oldSourceParser = $this->sourceParser;
185 $oldImport = $this->importDir;
188 $this->importDir = (array) $this->importDir;
189 array_unshift($this->importDir, $importDir);
191 foreach ($props as $prop) {
192 $this->compileProp($prop, $block, $out);
195 $this->importDir = $oldImport;
196 $this->sourceParser = $oldSourceParser;
222 switch ($block->type) {
224 $this->compileRoot($block);
227 $this->compileCSSBlock($block);
230 $this->compileMedia($block);
233 $name =
"@".$block->name;
234 if (!empty($block->value)) {
235 $name .=
" ".$this->compileValue($this->reduce($block->value));
238 $this->compileNestedBlock($block, array($name));
241 $this->
throwError(
"unknown block type: $block->type\n");
245 protected function compileCSSBlock($block)
247 $env = $this->pushEnv();
249 $selectors = $this->compileSelectors($block->tags);
250 $env->selectors = $this->multiplySelectors($selectors);
251 $out = $this->makeOutputBlock(
null, $env->selectors);
253 $this->scope->children[] = $out;
254 $this->compileProps($block, $out);
256 $block->scope = $env;
260 protected function compileMedia($media)
262 $env = $this->pushEnv($media);
263 $parentScope = $this->mediaParent($this->scope);
265 $query = $this->compileMediaQuery($this->multiplyMedia($env));
267 $this->scope = $this->makeOutputBlock($media->type, array($query));
268 $parentScope->children[] = $this->scope;
270 $this->compileProps($media, $this->scope);
272 if (count($this->scope->lines) > 0) {
273 $orphanSelelectors = $this->findClosestSelectors();
274 if (!is_null($orphanSelelectors)) {
275 $orphan = $this->makeOutputBlock(
null, $orphanSelelectors);
276 $orphan->lines = $this->scope->lines;
277 array_unshift($this->scope->children, $orphan);
278 $this->scope->lines = array();
282 $this->scope = $this->scope->parent;
286 protected function mediaParent($scope)
288 while (!empty($scope->parent)) {
289 if (!empty($scope->type) && $scope->type !=
"media") {
292 $scope = $scope->parent;
298 protected function compileNestedBlock($block, $selectors)
300 $this->pushEnv($block);
301 $this->scope = $this->makeOutputBlock($block->type, $selectors);
302 $this->scope->parent->children[] = $this->scope;
304 $this->compileProps($block, $this->scope);
306 $this->scope = $this->scope->parent;
310 protected function compileRoot($root)
313 $this->scope = $this->makeOutputBlock($root->type);
314 $this->compileProps($root, $this->scope);
318 protected function compileProps($block, $out)
320 foreach ($this->sortProps($block->props) as $prop) {
321 $this->compileProp($prop, $block, $out);
336 foreach ($lines as $line) {
337 if (strpos($line,
'/*') === 0) {
341 if (!in_array($line, $unique)) {
344 array_splice($unique, array_search($line, $unique), 0, $comments);
347 return array_merge($unique, $comments);
350 protected function sortProps($props, $split =
false)
357 foreach ($props as $prop) {
364 if (isset($prop[1][0]) && $prop[1][0] == $this->vPrefix) {
365 $vars = array_merge($vars, $stack);
367 $other = array_merge($other, $stack);
372 $id = self::$nextImportId++;
375 $imports = array_merge($imports, $stack);
376 $other[] = array(
"import_mixin",
$id);
381 $other = array_merge($other, $stack);
386 $other = array_merge($other, $stack);
389 return array(array_merge($imports, $vars), $other);
391 return array_merge($imports, $vars, $other);
395 protected function compileMediaQuery($queries)
397 $compiledQueries = array();
398 foreach ($queries as $query) {
400 foreach ($query as $q) {
403 $parts[] = implode(
" ", array_slice($q, 1));
407 $parts[] =
"($q[1]: ".
410 $parts[] =
"($q[1])";
419 if (count($parts) > 0) {
420 $compiledQueries[] = implode(
" and ", $parts);
425 if (!empty($parts)) {
427 implode($this->formatter->selectorSeparator, $compiledQueries);
432 protected function multiplyMedia($env, $childQueries =
null)
435 !empty($env->block->type) && $env->block->type !=
"media"
437 return $childQueries;
441 if (empty($env->block->type)) {
442 return $this->multiplyMedia($env->parent, $childQueries);
446 $queries = $env->block->queries;
447 if (is_null($childQueries)) {
450 foreach ($queries as $parent) {
451 foreach ($childQueries as $child) {
452 $out[] = array_merge($parent, $child);
457 return $this->multiplyMedia($env->parent, $out);
460 protected function expandParentSelectors(&$tag, $replace)
462 $parts = explode(
"$&$", $tag);
464 foreach ($parts as &$part) {
466 $part = str_replace($this->parentSelector, $replace, $part,
$c);
469 $tag = implode($this->parentSelector, $parts);
473 protected function findClosestSelectors()
477 while ($env !==
null) {
478 if (isset($env->selectors)) {
479 $selectors = $env->selectors;
490 protected function multiplySelectors($selectors)
494 $parentSelectors = $this->findClosestSelectors();
495 if (is_null($parentSelectors)) {
497 foreach ($selectors as &$s) {
498 $this->expandParentSelectors($s,
"");
505 foreach ($parentSelectors as $parent) {
506 foreach ($selectors as $child) {
507 $count = $this->expandParentSelectors($child, $parent);
511 $out[] = trim($child);
513 $out[] = trim($parent.
' '.$child);
522 protected function compileSelectors($selectors)
526 foreach ($selectors as $s) {
529 $out[] = trim($this->
compileValue($this->reduce($value)));
538 protected function eq($left, $right)
540 return $left == $right;
543 protected function patternMatch($block, $orderedArgs, $keywordArgs)
547 if (!empty($block->guards)) {
548 $groupPassed =
false;
549 foreach ($block->guards as $guardGroup) {
550 foreach ($guardGroup as $guard) {
552 $this->zipSetArgs($block->args, $orderedArgs, $keywordArgs);
555 if ($guard[0] ==
"negate") {
560 $passed = $this->reduce($guard) == self::$TRUE;
570 $groupPassed =
false;
585 if (empty($block->args)) {
586 return $block->isVararg || empty($orderedArgs) && empty($keywordArgs);
589 $remainingArgs = $block->args;
591 $remainingArgs = array();
592 foreach ($block->args as $arg) {
593 if ($arg[0] ==
"arg" && isset($keywordArgs[$arg[1]])) {
597 $remainingArgs[] = $arg;
603 foreach ($remainingArgs as $i => $arg) {
606 if (empty($orderedArgs[$i]) || !$this->eq($arg[1], $orderedArgs[$i])) {
612 if (!isset($orderedArgs[$i]) && !isset($arg[2])) {
622 if ($block->isVararg) {
625 $numMatched = $i + 1;
627 return $numMatched >= count($orderedArgs);
631 protected function patternMatchAll($blocks, $orderedArgs, $keywordArgs, $skip = array())
634 foreach ($blocks as $block) {
636 if (isset($skip[$block->id]) && !isset($block->args)) {
640 if ($this->patternMatch($block, $orderedArgs, $keywordArgs)) {
649 protected function findBlocks($searchIn, $path, $orderedArgs, $keywordArgs, $seen = array())
651 if ($searchIn ==
null) {
654 if (isset($seen[$searchIn->id])) {
657 $seen[$searchIn->id] =
true;
661 if (isset($searchIn->children[$name])) {
662 $blocks = $searchIn->children[$name];
663 if (count($path) == 1) {
664 $matches = $this->patternMatchAll($blocks, $orderedArgs, $keywordArgs, $seen);
665 if (!empty($matches)) {
672 foreach ($blocks as $subBlock) {
673 $subMatches = $this->findBlocks(
675 array_slice($path, 1),
681 if (!is_null($subMatches)) {
682 foreach ($subMatches as $sm) {
688 return count($matches) > 0 ? $matches :
null;
691 if ($searchIn->parent === $searchIn) {
694 return $this->findBlocks($searchIn->parent, $path, $orderedArgs, $keywordArgs, $seen);
699 protected function zipSetArgs($args, $orderedValues, $keywordValues)
701 $assignedValues = array();
704 foreach ($args as $a) {
705 if ($a[0] ==
"arg") {
706 if (isset($keywordValues[$a[1]])) {
708 $value = $keywordValues[$a[1]];
709 } elseif (isset($orderedValues[$i])) {
711 $value = $orderedValues[$i];
713 } elseif (isset($a[2])) {
718 $this->
throwError(
"Failed to assign arg ".$a[1]);
721 $value = $this->reduce($value);
722 $this->
set($a[1], $value);
723 $assignedValues[] = $value;
732 if ($last && $last[0] ==
"rest") {
733 $rest = array_slice($orderedValues, count($args) - 1);
734 $this->
set($last[1], $this->reduce(array(
"list",
" ", $rest)));
738 $this->env->arguments = $assignedValues + $orderedValues;
742 protected function compileProp($prop, $block, $out)
745 $this->sourceLoc = isset($prop[-1]) ? $prop[-1] : -1;
749 list(, $name, $value) = $prop;
750 if ($name[0] == $this->vPrefix) {
751 $this->
set($name, $value);
753 $out->lines[] = $this->formatter->property(
760 list(, $child) = $prop;
764 list(, $path, $args, $suffix) = $prop;
766 $orderedArgs = array();
767 $keywordArgs = array();
768 foreach ((array) $args as $arg) {
772 if (!isset($arg[2])) {
773 $orderedArgs[] = $this->reduce(array(
"variable", $arg[1]));
775 $keywordArgs[$arg[1]] = $this->reduce($arg[2]);
780 $orderedArgs[] = $this->reduce($arg[1]);
783 $this->
throwError(
"Unknown arg type: ".$arg[0]);
787 $mixins = $this->findBlocks($block, $path, $orderedArgs, $keywordArgs);
789 if ($mixins ===
null) {
790 $this->
throwError(
"{$prop[1][0]} is undefined");
793 foreach ($mixins as $mixin) {
794 if ($mixin === $block && !$orderedArgs) {
799 if (isset($mixin->parent->scope)) {
801 $mixinParentEnv = $this->pushEnv();
802 $mixinParentEnv->storeParent = $mixin->parent->scope;
806 if (isset($mixin->args)) {
809 $this->zipSetArgs($mixin->args, $orderedArgs, $keywordArgs);
812 $oldParent = $mixin->parent;
813 if ($mixin != $block) {
814 $mixin->parent = $block;
817 foreach ($this->sortProps($mixin->props) as $subProp) {
818 if ($suffix !==
null &&
819 $subProp[0] ==
"assign" &&
820 is_string($subProp[1]) &&
821 $subProp[1][0] != $this->vPrefix
825 array($subProp[2], array(
'keyword', $suffix))
829 $this->compileProp($subProp, $mixin, $out);
832 $mixin->parent = $oldParent;
844 $out->lines[] = $prop[1];
847 list(, $name, $value) = $prop;
848 $out->lines[] =
"@$name ".$this->compileValue($this->reduce($value)).
';';
851 $out->lines[] = $prop[1];
854 list(, $importPath, $importId) = $prop;
855 $importPath = $this->reduce($importPath);
857 if (!isset($this->env->imports)) {
858 $this->env->imports = array();
861 $result = $this->tryImport($importPath, $block, $out);
863 $this->env->imports[$importId] = $result ===
false ?
864 array(
false,
"@import ".$this->
compileValue($importPath).
";") : $result;
868 list(, $importId) = $prop;
869 $import = $this->env->imports[$importId];
870 if ($import[0] ===
false) {
871 if (isset($import[1])) {
872 $out->lines[] = $import[1];
875 list(, $bottom, $parser, $importDir) = $import;
876 $this->compileImportedProps($bottom, $block, $out, $parser, $importDir);
881 $this->
throwError(
"unknown op: {$prop[0]}\n");
903 return implode($value[1], array_map(array($this,
'compileValue'), $value[2]));
905 if (!empty($this->formatter->compressColors)) {
913 list(, $num, $unit) = $value;
916 if ($this->numberPrecision !==
null) {
917 $num = round($num, $this->numberPrecision);
922 list(, $delim, $content) = $value;
923 foreach ($content as &$part) {
924 if (is_array($part)) {
928 return $delim.implode($content).$delim;
934 list(, $r, $g, $b) = $value;
939 if (count($value) == 5 && $value[4] != 1) {
940 return 'rgba('.$r.
','.$g.
','.$b.
','.$value[4].
')';
943 $h = sprintf(
"#%02x%02x%02x", $r, $g, $b);
945 if (!empty($this->formatter->compressColors)) {
947 if ($h[1] === $h[2] && $h[3] === $h[4] && $h[5] === $h[6]) {
948 $h =
'#'.$h[1].$h[3].$h[5];
955 list(, $name, $args) = $value;
958 $this->
throwError(
"unknown value type: $value[0]");
962 protected function lib_pow($args)
964 list($base, $exp) = $this->assertArgs($args, 2,
"pow");
965 return pow($this->assertNumber($base), $this->assertNumber($exp));
968 protected function lib_pi()
973 protected function lib_mod($args)
975 list($a, $b) = $this->assertArgs($args, 2,
"mod");
976 return $this->assertNumber($a) % $this->assertNumber($b);
979 protected function lib_tan($num)
981 return tan($this->assertNumber($num));
984 protected function lib_sin($num)
986 return sin($this->assertNumber($num));
989 protected function lib_cos($num)
991 return cos($this->assertNumber($num));
994 protected function lib_atan($num)
996 $num = atan($this->assertNumber($num));
997 return array(
"number", $num,
"rad");
1000 protected function lib_asin($num)
1002 $num = asin($this->assertNumber($num));
1003 return array(
"number", $num,
"rad");
1006 protected function lib_acos($num)
1008 $num = acos($this->assertNumber($num));
1009 return array(
"number", $num,
"rad");
1012 protected function lib_sqrt($num)
1014 return sqrt($this->assertNumber($num));
1017 protected function lib_extract($value)
1019 list($list, $idx) = $this->assertArgs($value, 2,
"extract");
1020 $idx = $this->assertNumber($idx);
1022 if ($list[0] ==
"list" && isset($list[2][$idx - 1])) {
1023 return $list[2][$idx - 1];
1028 protected function lib_isnumber($value)
1030 return $this->toBool($value[0] ==
"number");
1033 protected function lib_isstring($value)
1035 return $this->toBool($value[0] ==
"string");
1038 protected function lib_iscolor($value)
1040 return $this->toBool($this->coerceColor($value));
1043 protected function lib_iskeyword($value)
1045 return $this->toBool($value[0] ==
"keyword");
1048 protected function lib_ispixel($value)
1050 return $this->toBool($value[0] ==
"number" && $value[2] ==
"px");
1053 protected function lib_ispercentage($value)
1055 return $this->toBool($value[0] ==
"number" && $value[2] ==
"%");
1058 protected function lib_isem($value)
1060 return $this->toBool($value[0] ==
"number" && $value[2] ==
"em");
1063 protected function lib_isrem($value)
1065 return $this->toBool($value[0] ==
"number" && $value[2] ==
"rem");
1068 protected function lib_rgbahex($color)
1070 $color = $this->coerceColor($color);
1071 if (is_null($color)) {
1072 $this->
throwError(
"color expected for rgbahex");
1076 "#%02x%02x%02x%02x",
1077 isset($color[4]) ? $color[4] * 255 : 255,
1084 protected function lib_argb($color)
1086 return $this->lib_rgbahex($color);
1097 $mime = ($value[0] ===
'list') ? $value[2][0][2] :
null;
1098 $url = ($value[0] ===
'list') ? $value[2][1][2][0] : $value[2][0];
1100 $fullpath = $this->findImport($url);
1102 if ($fullpath && ($fsize = filesize($fullpath)) !==
false) {
1104 if ($fsize / 1024 < 32) {
1105 if (is_null($mime)) {
1106 if (class_exists(
'finfo')) {
1107 $finfo =
new finfo(FILEINFO_MIME);
1108 $mime = explode(
'; ', $finfo->file($fullpath));
1110 } elseif (function_exists(
'mime_content_type')) {
1111 $mime = mime_content_type($fullpath);
1115 if (!is_null($mime)) {
1116 $url = sprintf(
'data:%s;base64,%s', $mime, base64_encode(file_get_contents($fullpath)));
1121 return 'url("'.$url.
'")';
1125 protected function lib_e($arg)
1130 if (isset($items[0])) {
1131 return $this->lib_e($items[0]);
1145 protected function lib__sprintf($args)
1147 if ($args[0] !=
"list") {
1151 $string = array_shift($values);
1152 $template = $this->
compileValue($this->lib_e($string));
1156 if (preg_match_all(
'/%[dsa]/', $template, $m)) {
1157 foreach ($m[0] as $match) {
1158 $val = isset($values[$i]) ?
1159 $this->reduce($values[$i]) : array(
'keyword',
'');
1162 if ($color = $this->coerceColor($val)) {
1168 $template = preg_replace(
1169 '/'.self::preg_quote($match).
'/',
1177 $d = $string[0] ==
"string" ? $string[1] :
'"';
1178 return array(
"string", $d, array($template));
1181 protected function lib_floor($arg)
1183 $value = $this->assertNumber($arg);
1184 return array(
"number", floor($value), $arg[2]);
1187 protected function lib_ceil($arg)
1189 $value = $this->assertNumber($arg);
1190 return array(
"number", ceil($value), $arg[2]);
1193 protected function lib_round($arg)
1195 if ($arg[0] !=
"list") {
1196 $value = $this->assertNumber($arg);
1197 return array(
"number", round($value), $arg[2]);
1199 $value = $this->assertNumber($arg[2][0]);
1200 $precision = $this->assertNumber($arg[2][1]);
1201 return array(
"number", round($value, $precision), $arg[2][0][2]);
1205 protected function lib_unit($arg)
1207 if ($arg[0] ==
"list") {
1208 list($number, $newUnit) = $arg[2];
1209 return array(
"number", $this->assertNumber($number),
1212 return array(
"number", $this->assertNumber($arg),
"");
1222 if ($args[0] !=
'list' || count($args[2]) < 2) {
1223 return array(array(
'color', 0, 0, 0), 0);
1225 list($color, $delta) = $args[2];
1226 $color = $this->assertColor($color);
1227 $delta = (float) $delta[1];
1229 return array($color, $delta);
1232 protected function lib_darken($args)
1234 list($color, $delta) = $this->
colorArgs($args);
1236 $hsl = $this->toHSL($color);
1237 $hsl[3] = $this->clamp($hsl[3] - $delta, 100);
1238 return $this->
toRGB($hsl);
1241 protected function lib_lighten($args)
1243 list($color, $delta) = $this->
colorArgs($args);
1245 $hsl = $this->toHSL($color);
1246 $hsl[3] = $this->clamp($hsl[3] + $delta, 100);
1247 return $this->
toRGB($hsl);
1250 protected function lib_saturate($args)
1252 list($color, $delta) = $this->
colorArgs($args);
1254 $hsl = $this->toHSL($color);
1255 $hsl[2] = $this->clamp($hsl[2] + $delta, 100);
1256 return $this->
toRGB($hsl);
1259 protected function lib_desaturate($args)
1261 list($color, $delta) = $this->
colorArgs($args);
1263 $hsl = $this->toHSL($color);
1264 $hsl[2] = $this->clamp($hsl[2] - $delta, 100);
1265 return $this->
toRGB($hsl);
1268 protected function lib_spin($args)
1270 list($color, $delta) = $this->
colorArgs($args);
1272 $hsl = $this->toHSL($color);
1274 $hsl[1] = $hsl[1] + $delta % 360;
1279 return $this->
toRGB($hsl);
1282 protected function lib_fadeout($args)
1284 list($color, $delta) = $this->
colorArgs($args);
1285 $color[4] = $this->clamp((isset($color[4]) ? $color[4] : 1) - $delta / 100);
1289 protected function lib_fadein($args)
1291 list($color, $delta) = $this->
colorArgs($args);
1292 $color[4] = $this->clamp((isset($color[4]) ? $color[4] : 1) + $delta / 100);
1296 protected function lib_hue($color)
1298 $hsl = $this->toHSL($this->assertColor($color));
1299 return round($hsl[1]);
1302 protected function lib_saturation($color)
1304 $hsl = $this->toHSL($this->assertColor($color));
1305 return round($hsl[2]);
1308 protected function lib_lightness($color)
1310 $hsl = $this->toHSL($this->assertColor($color));
1311 return round($hsl[3]);
1316 protected function lib_alpha($value)
1318 if (!is_null($color = $this->coerceColor($value))) {
1319 return isset($color[4]) ? $color[4] : 1;
1325 protected function lib_fade($args)
1327 list($color, $alpha) = $this->
colorArgs($args);
1328 $color[4] = $this->clamp($alpha / 100.0);
1332 protected function lib_percentage($arg)
1334 $num = $this->assertNumber($arg);
1335 return array(
"number", $num * 100,
"%");
1351 $white = [
'color', 255, 255, 255];
1352 if ($args[0] ==
'color') {
1353 return $this->
lib_mix([
'list',
',', [$white, $args]]);
1354 } elseif ($args[0] ==
"list" && count($args[2]) == 2) {
1355 return $this->
lib_mix([$args[0], $args[1], [$white, $args[2][0], $args[2][1]]]);
1357 $this->
throwError(
"tint expects (color, weight)");
1375 $black = [
'color', 0, 0, 0];
1376 if ($args[0] ==
'color') {
1377 return $this->
lib_mix([
'list',
',', [$black, $args]]);
1378 } elseif ($args[0] ==
"list" && count($args[2]) == 2) {
1379 return $this->
lib_mix([$args[0], $args[1], [$black, $args[2][0], $args[2][1]]]);
1381 $this->
throwError(
"shade expects (color, weight)");
1397 if ($args[0] !=
"list" || count($args[2]) < 2) {
1398 $this->
throwError(
"mix expects (color1, color2, weight)");
1401 list($first, $second) = $args[2];
1402 $first = $this->assertColor($first);
1403 $second = $this->assertColor($second);
1405 $first_a = $this->lib_alpha($first);
1406 $second_a = $this->lib_alpha($second);
1408 if (isset($args[2][2])) {
1409 $weight = $args[2][2][1] / 100.0;
1414 $w = $weight * 2 - 1;
1415 $a = $first_a - $second_a;
1417 $w1 = (($w * $a == -1 ? $w : ($w + $a) / (1 + $w * $a)) + 1) / 2.0;
1420 $new = array(
'color',
1421 $w1 * $first[1] + $w2 * $second[1],
1422 $w1 * $first[2] + $w2 * $second[2],
1423 $w1 * $first[3] + $w2 * $second[3],
1426 if ($first_a != 1.0 || $second_a != 1.0) {
1427 $new[] = $first_a * $weight + $second_a * ($weight - 1);
1430 return $this->fixColor($new);
1441 $darkColor = array(
'color', 0, 0, 0);
1442 $lightColor = array(
'color', 255, 255, 255);
1445 if ($args[0] ==
'list') {
1446 $inputColor = (isset($args[2][0])) ? $this->assertColor($args[2][0]) : $lightColor;
1447 $darkColor = (isset($args[2][1])) ? $this->assertColor($args[2][1]) : $darkColor;
1448 $lightColor = (isset($args[2][2])) ? $this->assertColor($args[2][2]) : $lightColor;
1449 $threshold = (isset($args[2][3])) ? $this->assertNumber($args[2][3]) : $threshold;
1451 $inputColor = $this->assertColor($args);
1454 $inputColor = $this->coerceColor($inputColor);
1455 $darkColor = $this->coerceColor($darkColor);
1456 $lightColor = $this->coerceColor($lightColor);
1459 if ($this->toLuma($darkColor) > $this->toLuma($lightColor)) {
1461 $lightColor = $darkColor;
1465 $inputColor_alpha = $this->lib_alpha($inputColor);
1466 if (($this->toLuma($inputColor) * $inputColor_alpha) < $threshold) {
1472 private function toLuma($color)
1474 list(, $r, $g, $b) = $this->coerceColor($color);
1480 $r = ($r <= 0.03928) ? $r / 12.92 : pow((($r + 0.055) / 1.055), 2.4);
1481 $g = ($g <= 0.03928) ? $g / 12.92 : pow((($g + 0.055) / 1.055), 2.4);
1482 $b = ($b <= 0.03928) ? $b / 12.92 : pow((($b + 0.055) / 1.055), 2.4);
1484 return (0.2126 * $r) + (0.7152 * $g) + (0.0722 * $b);
1487 protected function lib_luma($color)
1489 return array(
"number", round($this->toLuma($color) * 100, 8),
"%");
1493 public function assertColor($value, $error =
"expected color value")
1495 $color = $this->coerceColor($value);
1496 if (is_null($color)) {
1502 public function assertNumber($value, $error =
"expecting number")
1504 if ($value[0] ==
"number") {
1510 public function assertArgs($value, $expectedArgs, $name =
"")
1512 if ($expectedArgs == 1) {
1515 if ($value[0] !==
"list" || $value[1] !=
",") {
1518 $values = $value[2];
1519 $numValues = count($values);
1520 if ($expectedArgs != $numValues) {
1525 $this->
throwError(
"{$name}expecting $expectedArgs arguments, got $numValues");
1532 protected function toHSL($color)
1534 if ($color[0] ===
'hsl') {
1538 $r = $color[1] / 255;
1539 $g = $color[2] / 255;
1540 $b = $color[3] / 255;
1542 $min = min($r, $g, $b);
1543 $max = max($r, $g, $b);
1545 $L = ($min + $max) / 2;
1550 $S = ($max - $min) / ($max + $min);
1552 $S = ($max - $min) / (2.0 - $max - $min);
1555 $H = ($g - $b) / ($max - $min);
1556 } elseif ($g == $max) {
1557 $H = 2.0 + ($b - $r) / ($max - $min);
1558 } elseif ($b == $max) {
1559 $H = 4.0 + ($r - $g) / ($max - $min);
1564 ($H < 0 ? $H + 6 : $H) * 60,
1569 if (count($color) > 4) {
1576 protected function toRGB_helper($comp, $temp1, $temp2)
1580 } elseif ($comp > 1) {
1584 if (6 * $comp < 1) {
1585 return $temp1 + ($temp2 - $temp1) * 6 * $comp;
1587 if (2 * $comp < 1) {
1590 if (3 * $comp < 2) {
1591 return $temp1 + ($temp2 - $temp1) * ((2 / 3) - $comp) * 6;
1603 if ($color[0] ===
'color') {
1607 $H = $color[1] / 360;
1608 $S = $color[2] / 100;
1609 $L = $color[3] / 100;
1615 $L * (1.0 + $S) : $L + $S - $L * $S;
1617 $temp1 = 2.0 * $L - $temp2;
1619 $r = $this->toRGB_helper($H + 1 / 3, $temp1, $temp2);
1620 $g = $this->toRGB_helper($H, $temp1, $temp2);
1621 $b = $this->toRGB_helper($H - 1 / 3, $temp1, $temp2);
1625 $out = array(
'color', $r * 255, $g * 255, $b * 255);
1626 if (count($color) > 4) {
1633 protected function clamp($v, $max = 1, $min = 0)
1635 return min($max, max($min, $v));
1645 if ($func[2][0] !=
'list') {
1649 $rawComponents = $func[2][2];
1651 if ($fname ==
'hsl' || $fname ==
'hsla') {
1652 $hsl = array(
'hsl');
1654 foreach ($rawComponents as
$c) {
1655 $val = $this->reduce(
$c);
1656 $val = isset($val[1]) ? (float) $val[1] : 0;
1666 $hsl[] = $this->clamp($val, $clamp);
1670 while (count($hsl) < 4) {
1673 return $this->
toRGB($hsl);
1675 } elseif ($fname ==
'rgb' || $fname ==
'rgba') {
1676 $components = array();
1678 foreach ($rawComponents as
$c) {
1679 $c = $this->reduce(
$c);
1681 if (
$c[0] ==
"number" &&
$c[2] ==
"%") {
1682 $components[] = 255 * (
$c[1] / 100);
1684 $components[] = (float)
$c[1];
1686 } elseif ($i == 4) {
1687 if (
$c[0] ==
"number" &&
$c[2] ==
"%") {
1688 $components[] = 1.0 * (
$c[1] / 100);
1690 $components[] = (float)
$c[1];
1698 while (count($components) < 3) {
1701 array_unshift($components,
'color');
1702 return $this->fixColor($components);
1708 protected function reduce($value, $forExpression =
false)
1710 switch ($value[0]) {
1712 $reduced = $this->reduce($value[1]);
1714 $res = $this->reduce(array(
"variable", $this->vPrefix.$var));
1716 if ($res[0] ==
"raw_color") {
1717 $res = $this->coerceColor($res);
1720 if (empty($value[2])) {
1721 $res = $this->lib_e($res);
1727 if (is_array($key)) {
1728 $key = $this->reduce($key);
1729 $key = $this->vPrefix.$this->compileValue($this->lib_e($key));
1732 $seen = & $this->env->seenNames;
1734 if (!empty($seen[$key])) {
1735 $this->
throwError(
"infinite loop detected: $key");
1739 $out = $this->reduce($this->
get($key));
1740 $seen[$key] =
false;
1743 foreach ($value[2] as &$item) {
1744 $item = $this->reduce($item, $forExpression);
1748 return $this->evaluate($value);
1750 foreach ($value[2] as &$part) {
1751 if (is_array($part)) {
1752 $strip = $part[0] ==
"variable";
1753 $part = $this->reduce($part);
1755 $part = $this->lib_e($part);
1761 list(, $inner) = $value;
1762 return $this->lib_e($this->reduce($inner));
1769 list(, $name, $args) = $value;
1774 $f = isset($this->libFunctions[$name]) ?
1775 $this->libFunctions[$name] : array($this,
'lib_'.str_replace(
'-',
'_', $name));
1777 if (is_callable($f)) {
1778 if ($args[0] ==
'list') {
1779 $args = self::compressList($args[2], $args[1]);
1782 $ret = call_user_func($f, $this->reduce($args,
true), $this);
1784 if (is_null($ret)) {
1785 return array(
"string",
"", array(
1786 $name,
"(", $args,
")"
1791 if (is_numeric($ret)) {
1792 $ret = array(
'number', $ret,
"");
1793 } elseif (!is_array($ret)) {
1794 $ret = array(
'keyword', $ret);
1801 $value[2] = $this->reduce($value[2]);
1804 list(, $op, $exp) = $value;
1805 $exp = $this->reduce($exp);
1807 if ($exp[0] ==
"number") {
1816 return array(
"string",
"", array($op, $exp));
1819 if ($forExpression) {
1820 switch ($value[0]) {
1822 if ($color = $this->coerceColor($value)) {
1827 return $this->coerceColor($value);
1836 protected function coerceColor($value)
1838 switch ($value[0]) {
1842 $c = array(
"color", 0, 0, 0);
1843 $colorStr = substr($value[1], 1);
1844 $num = hexdec($colorStr);
1845 $width = strlen($colorStr) == 3 ? 16 : 256;
1847 for ($i = 3; $i > 0; $i--) {
1848 $t = intval($num) % $width;
1851 $c[$i] = $t * (256 / $width) + $t * floor(16/$width);
1857 if (isset(self::$cssColors[$name])) {
1858 $rgba = explode(
',', self::$cssColors[$name]);
1860 if (isset($rgba[3])) {
1861 return array(
'color', $rgba[0], $rgba[1], $rgba[2], $rgba[3]);
1863 return array(
'color', $rgba[0], $rgba[1], $rgba[2]);
1871 protected function coerceString($value)
1873 switch ($value[0]) {
1877 return array(
"string",
"", array($value[1]));
1883 protected function flattenList($value)
1885 if ($value[0] ==
"list" && count($value[2]) == 1) {
1886 return $this->flattenList($value[2][0]);
1891 public function toBool($a)
1893 return $a ? self::$TRUE : self::$FALSE;
1897 protected function evaluate($exp)
1899 list(, $op, $left, $right, $whiteBefore, $whiteAfter) = $exp;
1901 $left = $this->reduce($left,
true);
1902 $right = $this->reduce($right,
true);
1904 if ($leftColor = $this->coerceColor($left)) {
1908 if ($rightColor = $this->coerceColor($right)) {
1909 $right = $rightColor;
1917 return $this->toBool($left == self::$TRUE && $right == self::$TRUE);
1921 return $this->toBool($this->eq($left, $right));
1924 if ($op ==
"+" && !is_null($str = $this->stringConcatenate($left, $right))) {
1929 $fname =
"op_{$ltype}_{$rtype}";
1930 if (is_callable(array($this, $fname))) {
1931 $out = $this->$fname($op, $left, $right);
1932 if (!is_null($out)) {
1940 $paddedOp =
" ".$paddedOp;
1946 return array(
"string",
"", array($left, $paddedOp, $right));
1949 protected function stringConcatenate($left, $right)
1951 if ($strLeft = $this->coerceString($left)) {
1952 if ($right[0] ==
"string") {
1955 $strLeft[2][] = $right;
1959 if ($strRight = $this->coerceString($right)) {
1960 array_unshift($strRight[2], $left);
1968 protected function fixColor(
$c)
1970 foreach (range(1, 3) as $i) {
1982 protected function op_number_color($op, $lft, $rgt)
1984 if ($op ==
'+' || $op ==
'*') {
1985 return $this->op_color_number($op, $rgt, $lft);
1990 protected function op_color_number($op, $lft, $rgt)
1992 if ($rgt[0] ==
'%') {
1996 return $this->op_color_color(
1999 array_fill(1, count($lft) - 1, $rgt[1])
2003 protected function op_color_color($op, $left, $right)
2005 $out = array(
'color');
2006 $max = count($left) > count($right) ? count($left) : count($right);
2007 foreach (range(1, $max - 1) as $i) {
2008 $lval = isset($left[$i]) ? $left[$i] : 0;
2009 $rval = isset($right[$i]) ? $right[$i] : 0;
2012 $out[] = $lval + $rval;
2015 $out[] = $lval - $rval;
2018 $out[] = $lval * $rval;
2021 $out[] = $lval % $rval;
2025 $this->
throwError(
"evaluate error: can't divide by zero");
2027 $out[] = $lval / $rval;
2030 $this->
throwError(
'evaluate error: color op number failed on op '.$op);
2033 return $this->fixColor($out);
2036 public function lib_red($color)
2038 $color = $this->coerceColor($color);
2039 if (is_null($color)) {
2040 $this->
throwError(
'color expected for red()');
2046 public function lib_green($color)
2048 $color = $this->coerceColor($color);
2049 if (is_null($color)) {
2050 $this->
throwError(
'color expected for green()');
2056 public function lib_blue($color)
2058 $color = $this->coerceColor($color);
2059 if (is_null($color)) {
2060 $this->
throwError(
'color expected for blue()');
2068 protected function op_number_number($op, $left, $right)
2070 $unit = empty($left[2]) ? $right[2] : $left[2];
2075 $value = $left[1] + $right[1];
2078 $value = $left[1] * $right[1];
2081 $value = $left[1] - $right[1];
2084 $value = $left[1] % $right[1];
2087 if ($right[1] == 0) {
2088 $this->
throwError(
'parse error: divide by zero');
2090 $value = $left[1] / $right[1];
2093 return $this->toBool($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 $this->
throwError(
'parse error: unknown number operator: '.$op);
2104 return array(
"number", $value, $unit);
2110 protected function makeOutputBlock($type, $selectors =
null)
2112 $b =
new stdclass();
2113 $b->lines = array();
2114 $b->children = array();
2115 $b->selectors = $selectors;
2117 $b->parent = $this->scope;
2122 protected function pushEnv($block =
null)
2124 $e =
new stdclass();
2125 $e->parent = $this->env;
2126 $e->store = array();
2134 protected function popEnv()
2137 $this->env = $this->env->parent;
2142 protected function set($name, $value)
2144 $this->env->store[$name] = $value;
2149 protected function get($name)
2151 $current = $this->env;
2153 $isArguments = $name == $this->vPrefix.
'arguments';
2155 if ($isArguments && isset($current->arguments)) {
2156 return array(
'list',
' ', $current->arguments);
2159 if (isset($current->store[$name])) {
2160 return $current->store[$name];
2163 $current = isset($current->storeParent) ?
2164 $current->storeParent : $current->parent;
2167 $this->
throwError(
"variable $name is undefined");
2171 protected function injectVariables($args)
2176 foreach ($args as $name => $strValue) {
2177 if ($name[0] !==
'@') {
2181 $parser->buffer = (
string) $strValue;
2182 if (!$parser->propertyValue($value)) {
2183 throw new Exception(
"failed to parse passed in variable $name: $strValue");
2186 $this->
set($name, $value);
2196 if ($fname !==
null) {
2198 $this->_parseFile = $fname;
2202 public function compile($string, $name =
null)
2204 $locale = setlocale(LC_NUMERIC,
'0');
2205 setlocale(LC_NUMERIC,
"C");
2207 $this->parser = $this->makeParser($name);
2208 $root = $this->parser->parse($string);
2211 $this->scope =
null;
2213 $this->formatter = $this->newFormatter();
2215 if (!empty($this->registeredVars)) {
2216 $this->injectVariables($this->registeredVars);
2219 $this->sourceParser = $this->parser;
2223 $this->formatter->block($this->scope);
2224 $out = ob_get_clean();
2225 setlocale(LC_NUMERIC, $locale);
2229 public function compileFile($fname, $outFname =
null)
2231 if (!is_readable($fname)) {
2232 throw new Exception(
'load error: failed to find '.$fname);
2235 $pi = pathinfo($fname);
2237 $oldImport = $this->importDir;
2239 $this->importDir = (array) $this->importDir;
2240 $this->importDir[] = $pi[
'dirname'].
'/';
2242 $this->addParsedFile($fname);
2244 $out = $this->compile(file_get_contents($fname), $fname);
2246 $this->importDir = $oldImport;
2248 if ($outFname !==
null) {
2249 $res = 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);