41 public static $VERSION =
"v0.5.0";
43 public static $TRUE = array(
"keyword",
"true");
44 public static $FALSE = array(
"keyword",
"false");
46 protected $libFunctions = array();
47 protected $registeredVars = array();
48 protected $preserveComments =
false;
50 public $vPrefix =
'@';
51 public $mPrefix =
'$';
52 public $parentSelector =
'&';
54 public $importDisabled =
false;
55 public $importDir =
'';
62 protected $numberPrecision =
null;
64 protected $allParsedFiles = array();
68 protected $sourceParser =
null;
69 protected $sourceLoc =
null;
71 protected static $nextImportId = 0;
74 protected function findImport($url)
76 foreach ((array) $this->importDir as $dir) {
77 $full = $dir.(substr($dir, -1) !=
'/' ?
'/' :
'').$url;
94 return is_file($name);
97 public static function compressList($items, $delim)
99 if (!isset($items[1]) && isset($items[0])) {
102 return array(
'list', $delim, $items);
106 public static function preg_quote($what)
108 return preg_quote($what,
'/');
111 protected function tryImport($importPath, $parentBlock, $out)
113 if ($importPath[0] ==
"function" && $importPath[1] ==
"url") {
114 $importPath = $this->flattenList($importPath[2]);
117 $str = $this->coerceString($importPath);
125 if (substr_compare($url,
'.css', -4, 4) === 0) {
129 $realPath = $this->findImport($url);
131 if ($realPath ===
null) {
135 if ($this->importDisabled) {
136 return array(
false,
"/* import disabled */");
139 if (isset($this->allParsedFiles[realpath($realPath)])) {
140 return array(
false,
null);
143 $this->addParsedFile($realPath);
144 $parser = $this->makeParser($realPath);
145 $root = $parser->parse(file_get_contents($realPath));
148 foreach ($root->props as $prop) {
149 if ($prop[0] ==
"block") {
150 $prop[1]->parent = $parentBlock;
157 foreach ($root->children as $childName => $child) {
158 if (isset($parentBlock->children[$childName])) {
159 $parentBlock->children[$childName] = array_merge(
160 $parentBlock->children[$childName],
164 $parentBlock->children[$childName] = $child;
168 $pi = pathinfo($realPath);
169 $dir = $pi[
"dirname"];
171 list($top, $bottom) = $this->sortProps($root->props,
true);
172 $this->compileImportedProps($top, $parentBlock, $out, $parser, $dir);
174 return array(
true, $bottom, $parser, $dir);
177 protected function compileImportedProps($props, $block, $out, $sourceParser, $importDir)
179 $oldSourceParser = $this->sourceParser;
181 $oldImport = $this->importDir;
184 $this->importDir = (array) $this->importDir;
185 array_unshift($this->importDir, $importDir);
187 foreach ($props as $prop) {
188 $this->compileProp($prop, $block, $out);
191 $this->importDir = $oldImport;
192 $this->sourceParser = $oldSourceParser;
218 switch ($block->type) {
220 $this->compileRoot($block);
223 $this->compileCSSBlock($block);
226 $this->compileMedia($block);
229 $name =
"@".$block->name;
230 if (!empty($block->value)) {
231 $name .=
" ".$this->compileValue($this->reduce($block->value));
234 $this->compileNestedBlock($block, array($name));
237 $this->
throwError(
"unknown block type: $block->type\n");
241 protected function compileCSSBlock($block)
243 $env = $this->pushEnv();
245 $selectors = $this->compileSelectors($block->tags);
246 $env->selectors = $this->multiplySelectors($selectors);
247 $out = $this->makeOutputBlock(
null, $env->selectors);
249 $this->scope->children[] = $out;
250 $this->compileProps($block, $out);
252 $block->scope = $env;
256 protected function compileMedia($media)
258 $env = $this->pushEnv($media);
259 $parentScope = $this->mediaParent($this->scope);
261 $query = $this->compileMediaQuery($this->multiplyMedia($env));
263 $this->scope = $this->makeOutputBlock($media->type, array($query));
264 $parentScope->children[] = $this->scope;
266 $this->compileProps($media, $this->scope);
268 if (count($this->scope->lines) > 0) {
269 $orphanSelelectors = $this->findClosestSelectors();
270 if (!is_null($orphanSelelectors)) {
271 $orphan = $this->makeOutputBlock(
null, $orphanSelelectors);
272 $orphan->lines = $this->scope->lines;
273 array_unshift($this->scope->children, $orphan);
274 $this->scope->lines = array();
278 $this->scope = $this->scope->parent;
282 protected function mediaParent($scope)
284 while (!empty($scope->parent)) {
285 if (!empty($scope->type) && $scope->type !=
"media") {
288 $scope = $scope->parent;
294 protected function compileNestedBlock($block, $selectors)
296 $this->pushEnv($block);
297 $this->scope = $this->makeOutputBlock($block->type, $selectors);
298 $this->scope->parent->children[] = $this->scope;
300 $this->compileProps($block, $this->scope);
302 $this->scope = $this->scope->parent;
306 protected function compileRoot($root)
309 $this->scope = $this->makeOutputBlock($root->type);
310 $this->compileProps($root, $this->scope);
314 protected function compileProps($block, $out)
316 foreach ($this->sortProps($block->props) as $prop) {
317 $this->compileProp($prop, $block, $out);
332 foreach ($lines as $line) {
333 if (strpos($line,
'/*') === 0) {
337 if (!in_array($line, $unique)) {
340 array_splice($unique, array_search($line, $unique), 0, $comments);
343 return array_merge($unique, $comments);
346 protected function sortProps($props, $split =
false)
353 foreach ($props as $prop) {
360 if (isset($prop[1][0]) && $prop[1][0] == $this->vPrefix) {
361 $vars = array_merge($vars, $stack);
363 $other = array_merge($other, $stack);
368 $id = self::$nextImportId++;
371 $imports = array_merge($imports, $stack);
372 $other[] = array(
"import_mixin", $id);
377 $other = array_merge($other, $stack);
382 $other = array_merge($other, $stack);
385 return array(array_merge($imports, $vars), $other);
387 return array_merge($imports, $vars, $other);
391 protected function compileMediaQuery($queries)
393 $compiledQueries = array();
394 foreach ($queries as $query) {
396 foreach ($query as $q) {
399 $parts[] = implode(
" ", array_slice($q, 1));
403 $parts[] =
"($q[1]: ".
406 $parts[] =
"($q[1])";
415 if (count($parts) > 0) {
416 $compiledQueries[] = implode(
" and ", $parts);
421 if (!empty($parts)) {
423 implode($this->formatter->selectorSeparator, $compiledQueries);
428 protected function multiplyMedia($env, $childQueries =
null)
431 !empty($env->block->type) && $env->block->type !=
"media"
433 return $childQueries;
437 if (empty($env->block->type)) {
438 return $this->multiplyMedia($env->parent, $childQueries);
442 $queries = $env->block->queries;
443 if (is_null($childQueries)) {
446 foreach ($queries as $parent) {
447 foreach ($childQueries as $child) {
448 $out[] = array_merge($parent, $child);
453 return $this->multiplyMedia($env->parent, $out);
456 protected function expandParentSelectors(&$tag, $replace)
458 $parts = explode(
"$&$", $tag);
460 foreach ($parts as &$part) {
462 $part = str_replace($this->parentSelector, $replace, $part, $c);
465 $tag = implode($this->parentSelector, $parts);
469 protected function findClosestSelectors()
473 while ($env !==
null) {
474 if (isset($env->selectors)) {
475 $selectors = $env->selectors;
486 protected function multiplySelectors($selectors)
490 $parentSelectors = $this->findClosestSelectors();
491 if (is_null($parentSelectors)) {
493 foreach ($selectors as &$s) {
494 $this->expandParentSelectors($s,
"");
501 foreach ($parentSelectors as $parent) {
502 foreach ($selectors as $child) {
503 $count = $this->expandParentSelectors($child, $parent);
507 $out[] = trim($child);
509 $out[] = trim($parent.
' '.$child);
518 protected function compileSelectors($selectors)
522 foreach ($selectors as $s) {
525 $out[] = trim($this->
compileValue($this->reduce($value)));
534 protected function eq($left, $right)
536 return $left == $right;
539 protected function patternMatch($block, $orderedArgs, $keywordArgs)
543 if (!empty($block->guards)) {
544 $groupPassed =
false;
545 foreach ($block->guards as $guardGroup) {
546 foreach ($guardGroup as $guard) {
548 $this->zipSetArgs($block->args, $orderedArgs, $keywordArgs);
551 if ($guard[0] ==
"negate") {
556 $passed = $this->reduce($guard) == self::$TRUE;
566 $groupPassed =
false;
581 if (empty($block->args)) {
582 return $block->isVararg || empty($orderedArgs) && empty($keywordArgs);
585 $remainingArgs = $block->args;
587 $remainingArgs = array();
588 foreach ($block->args as $arg) {
589 if ($arg[0] ==
"arg" && isset($keywordArgs[$arg[1]])) {
593 $remainingArgs[] = $arg;
599 foreach ($remainingArgs as $i => $arg) {
602 if (empty($orderedArgs[$i]) || !$this->eq($arg[1], $orderedArgs[$i])) {
608 if (!isset($orderedArgs[$i]) && !isset($arg[2])) {
618 if ($block->isVararg) {
621 $numMatched = $i + 1;
623 return $numMatched >= count($orderedArgs);
627 protected function patternMatchAll($blocks, $orderedArgs, $keywordArgs, $skip = array())
630 foreach ($blocks as $block) {
632 if (isset($skip[$block->id]) && !isset($block->args)) {
636 if ($this->patternMatch($block, $orderedArgs, $keywordArgs)) {
645 protected function findBlocks($searchIn, $path, $orderedArgs, $keywordArgs, $seen = array())
647 if ($searchIn ==
null) {
650 if (isset($seen[$searchIn->id])) {
653 $seen[$searchIn->id] =
true;
657 if (isset($searchIn->children[$name])) {
658 $blocks = $searchIn->children[$name];
659 if (count($path) == 1) {
660 $matches = $this->patternMatchAll($blocks, $orderedArgs, $keywordArgs, $seen);
661 if (!empty($matches)) {
668 foreach ($blocks as $subBlock) {
669 $subMatches = $this->findBlocks(
671 array_slice($path, 1),
677 if (!is_null($subMatches)) {
678 foreach ($subMatches as $sm) {
684 return count($matches) > 0 ? $matches :
null;
687 if ($searchIn->parent === $searchIn) {
690 return $this->findBlocks($searchIn->parent, $path, $orderedArgs, $keywordArgs, $seen);
695 protected function zipSetArgs($args, $orderedValues, $keywordValues)
697 $assignedValues = array();
700 foreach ($args as $a) {
701 if ($a[0] ==
"arg") {
702 if (isset($keywordValues[$a[1]])) {
704 $value = $keywordValues[$a[1]];
705 } elseif (isset($orderedValues[$i])) {
707 $value = $orderedValues[$i];
709 } elseif (isset($a[2])) {
714 $this->
throwError(
"Failed to assign arg ".$a[1]);
717 $value = $this->reduce($value);
718 $this->
set($a[1], $value);
719 $assignedValues[] = $value;
728 if ($last[0] ==
"rest") {
729 $rest = array_slice($orderedValues, count($args) - 1);
730 $this->
set($last[1], $this->reduce(array(
"list",
" ", $rest)));
734 $this->env->arguments = $assignedValues + $orderedValues;
738 protected function compileProp($prop, $block, $out)
741 $this->sourceLoc = isset($prop[-1]) ? $prop[-1] : -1;
745 list(, $name, $value) = $prop;
746 if ($name[0] == $this->vPrefix) {
747 $this->
set($name, $value);
749 $out->lines[] = $this->formatter->property(
756 list(, $child) = $prop;
760 list(, $path, $args, $suffix) = $prop;
762 $orderedArgs = array();
763 $keywordArgs = array();
764 foreach ((array) $args as $arg) {
767 if (!isset($arg[2])) {
768 $orderedArgs[] = $this->reduce(array(
"variable", $arg[1]));
770 $keywordArgs[$arg[1]] = $this->reduce($arg[2]);
775 $orderedArgs[] = $this->reduce($arg[1]);
778 $this->
throwError(
"Unknown arg type: ".$arg[0]);
782 $mixins = $this->findBlocks($block, $path, $orderedArgs, $keywordArgs);
784 if ($mixins ===
null) {
785 $this->
throwError(
"{$prop[1][0]} is undefined");
788 foreach ($mixins as $mixin) {
789 if ($mixin === $block && !$orderedArgs) {
794 if (isset($mixin->parent->scope)) {
796 $mixinParentEnv = $this->pushEnv();
797 $mixinParentEnv->storeParent = $mixin->parent->scope;
801 if (isset($mixin->args)) {
804 $this->zipSetArgs($mixin->args, $orderedArgs, $keywordArgs);
807 $oldParent = $mixin->parent;
808 if ($mixin != $block) {
809 $mixin->parent = $block;
812 foreach ($this->sortProps($mixin->props) as $subProp) {
813 if ($suffix !==
null &&
814 $subProp[0] ==
"assign" &&
815 is_string($subProp[1]) &&
816 $subProp[1][0] != $this->vPrefix
820 array($subProp[2], array(
'keyword', $suffix))
824 $this->compileProp($subProp, $mixin, $out);
827 $mixin->parent = $oldParent;
839 $out->lines[] = $prop[1];
842 list(, $name, $value) = $prop;
843 $out->lines[] =
"@$name ".$this->compileValue($this->reduce($value)).
';';
846 $out->lines[] = $prop[1];
849 list(, $importPath, $importId) = $prop;
850 $importPath = $this->reduce($importPath);
852 if (!isset($this->env->imports)) {
853 $this->env->imports = array();
856 $result = $this->tryImport($importPath, $block, $out);
858 $this->env->imports[$importId] = $result ===
false ?
859 array(
false,
"@import ".$this->
compileValue($importPath).
";") : $result;
863 list(,$importId) = $prop;
864 $import = $this->env->imports[$importId];
865 if ($import[0] ===
false) {
866 if (isset($import[1])) {
867 $out->lines[] = $import[1];
870 list(, $bottom, $parser, $importDir) = $import;
871 $this->compileImportedProps($bottom, $block, $out, $parser, $importDir);
898 return implode($value[1], array_map(array($this,
'compileValue'), $value[2]));
900 if (!empty($this->formatter->compressColors)) {
908 list(, $num, $unit) = $value;
911 if ($this->numberPrecision !==
null) {
912 $num = round($num, $this->numberPrecision);
917 list(, $delim, $content) = $value;
918 foreach ($content as &$part) {
919 if (is_array($part)) {
923 return $delim.implode($content).$delim;
929 list(, $r, $g, $b) = $value;
934 if (count($value) == 5 && $value[4] != 1) {
935 return 'rgba('.$r.
','.$g.
','.$b.
','.$value[4].
')';
938 $h = sprintf(
"#%02x%02x%02x", $r, $g, $b);
940 if (!empty($this->formatter->compressColors)) {
942 if ($h[1] === $h[2] && $h[3] === $h[4] && $h[5] === $h[6]) {
943 $h =
'#'.$h[1].$h[3].$h[5];
950 list(, $name, $args) = $value;
953 $this->
throwError(
"unknown value type: $value[0]");
957 protected function lib_pow($args)
959 list($base, $exp) = $this->assertArgs($args, 2,
"pow");
960 return pow($this->assertNumber($base), $this->assertNumber($exp));
963 protected function lib_pi()
968 protected function lib_mod($args)
970 list($a, $b) = $this->assertArgs($args, 2,
"mod");
971 return $this->assertNumber($a) % $this->assertNumber($b);
974 protected function lib_tan($num)
976 return tan($this->assertNumber($num));
979 protected function lib_sin($num)
981 return sin($this->assertNumber($num));
984 protected function lib_cos($num)
986 return cos($this->assertNumber($num));
989 protected function lib_atan($num)
991 $num = atan($this->assertNumber($num));
992 return array(
"number", $num,
"rad");
995 protected function lib_asin($num)
997 $num = asin($this->assertNumber($num));
998 return array(
"number", $num,
"rad");
1001 protected function lib_acos($num)
1003 $num = acos($this->assertNumber($num));
1004 return array(
"number", $num,
"rad");
1007 protected function lib_sqrt($num)
1009 return sqrt($this->assertNumber($num));
1012 protected function lib_extract($value)
1014 list($list, $idx) = $this->assertArgs($value, 2,
"extract");
1015 $idx = $this->assertNumber($idx);
1017 if ($list[0] ==
"list" && isset($list[2][$idx - 1])) {
1018 return $list[2][$idx - 1];
1023 protected function lib_isnumber($value)
1025 return $this->toBool($value[0] ==
"number");
1028 protected function lib_isstring($value)
1030 return $this->toBool($value[0] ==
"string");
1033 protected function lib_iscolor($value)
1035 return $this->toBool($this->coerceColor($value));
1038 protected function lib_iskeyword($value)
1040 return $this->toBool($value[0] ==
"keyword");
1043 protected function lib_ispixel($value)
1045 return $this->toBool($value[0] ==
"number" && $value[2] ==
"px");
1048 protected function lib_ispercentage($value)
1050 return $this->toBool($value[0] ==
"number" && $value[2] ==
"%");
1053 protected function lib_isem($value)
1055 return $this->toBool($value[0] ==
"number" && $value[2] ==
"em");
1058 protected function lib_isrem($value)
1060 return $this->toBool($value[0] ==
"number" && $value[2] ==
"rem");
1063 protected function lib_rgbahex($color)
1065 $color = $this->coerceColor($color);
1066 if (is_null($color)) {
1067 $this->
throwError(
"color expected for rgbahex");
1071 "#%02x%02x%02x%02x",
1072 isset($color[4]) ? $color[4] * 255 : 255,
1079 protected function lib_argb($color)
1081 return $this->lib_rgbahex($color);
1092 $mime = ($value[0] ===
'list') ? $value[2][0][2] :
null;
1093 $url = ($value[0] ===
'list') ? $value[2][1][2][0] : $value[2][0];
1095 $fullpath = $this->findImport($url);
1097 if ($fullpath && ($fsize = filesize($fullpath)) !==
false) {
1099 if ($fsize / 1024 < 32) {
1100 if (is_null($mime)) {
1101 if (class_exists(
'finfo')) {
1102 $finfo =
new finfo(FILEINFO_MIME);
1103 $mime = explode(
'; ', $finfo->file($fullpath));
1105 } elseif (function_exists(
'mime_content_type')) {
1106 $mime = mime_content_type($fullpath);
1110 if (!is_null($mime)) {
1111 $url = sprintf(
'data:%s;base64,%s', $mime, base64_encode(file_get_contents($fullpath)));
1116 return 'url("'.$url.
'")';
1120 protected function lib_e($arg)
1125 if (isset($items[0])) {
1126 return $this->lib_e($items[0]);
1139 protected function lib__sprintf($args)
1141 if ($args[0] !=
"list") {
1145 $string = array_shift($values);
1146 $template = $this->
compileValue($this->lib_e($string));
1150 if (preg_match_all(
'/%[dsa]/', $template, $m)) {
1151 foreach ($m[0] as $match) {
1152 $val = isset($values[$i]) ?
1153 $this->reduce($values[$i]) : array(
'keyword',
'');
1156 if ($color = $this->coerceColor($val)) {
1162 $template = preg_replace(
1163 '/'.self::preg_quote($match).
'/',
1171 $d = $string[0] ==
"string" ? $string[1] :
'"';
1172 return array(
"string", $d, array($template));
1175 protected function lib_floor($arg)
1177 $value = $this->assertNumber($arg);
1178 return array(
"number", floor($value), $arg[2]);
1181 protected function lib_ceil($arg)
1183 $value = $this->assertNumber($arg);
1184 return array(
"number", ceil($value), $arg[2]);
1187 protected function lib_round($arg)
1189 if ($arg[0] !=
"list") {
1190 $value = $this->assertNumber($arg);
1191 return array(
"number", round($value), $arg[2]);
1193 $value = $this->assertNumber($arg[2][0]);
1194 $precision = $this->assertNumber($arg[2][1]);
1195 return array(
"number", round($value, $precision), $arg[2][0][2]);
1199 protected function lib_unit($arg)
1201 if ($arg[0] ==
"list") {
1202 list($number, $newUnit) = $arg[2];
1203 return array(
"number", $this->assertNumber($number),
1206 return array(
"number", $this->assertNumber($arg),
"");
1216 if ($args[0] !=
'list' || count($args[2]) < 2) {
1217 return array(array(
'color', 0, 0, 0), 0);
1219 list($color, $delta) = $args[2];
1220 $color = $this->assertColor($color);
1221 $delta = floatval($delta[1]);
1223 return array($color, $delta);
1226 protected function lib_darken($args)
1228 list($color, $delta) = $this->
colorArgs($args);
1230 $hsl = $this->toHSL($color);
1231 $hsl[3] = $this->clamp($hsl[3] - $delta, 100);
1232 return $this->
toRGB($hsl);
1235 protected function lib_lighten($args)
1237 list($color, $delta) = $this->
colorArgs($args);
1239 $hsl = $this->toHSL($color);
1240 $hsl[3] = $this->clamp($hsl[3] + $delta, 100);
1241 return $this->
toRGB($hsl);
1244 protected function lib_saturate($args)
1246 list($color, $delta) = $this->
colorArgs($args);
1248 $hsl = $this->toHSL($color);
1249 $hsl[2] = $this->clamp($hsl[2] + $delta, 100);
1250 return $this->
toRGB($hsl);
1253 protected function lib_desaturate($args)
1255 list($color, $delta) = $this->
colorArgs($args);
1257 $hsl = $this->toHSL($color);
1258 $hsl[2] = $this->clamp($hsl[2] - $delta, 100);
1259 return $this->
toRGB($hsl);
1262 protected function lib_spin($args)
1264 list($color, $delta) = $this->
colorArgs($args);
1266 $hsl = $this->toHSL($color);
1268 $hsl[1] = $hsl[1] + $delta % 360;
1273 return $this->
toRGB($hsl);
1276 protected function lib_fadeout($args)
1278 list($color, $delta) = $this->
colorArgs($args);
1279 $color[4] = $this->clamp((isset($color[4]) ? $color[4] : 1) - $delta / 100);
1283 protected function lib_fadein($args)
1285 list($color, $delta) = $this->
colorArgs($args);
1286 $color[4] = $this->clamp((isset($color[4]) ? $color[4] : 1) + $delta / 100);
1290 protected function lib_hue($color)
1292 $hsl = $this->toHSL($this->assertColor($color));
1293 return round($hsl[1]);
1296 protected function lib_saturation($color)
1298 $hsl = $this->toHSL($this->assertColor($color));
1299 return round($hsl[2]);
1302 protected function lib_lightness($color)
1304 $hsl = $this->toHSL($this->assertColor($color));
1305 return round($hsl[3]);
1310 protected function lib_alpha($value)
1312 if (!is_null($color = $this->coerceColor($value))) {
1313 return isset($color[4]) ? $color[4] : 1;
1319 protected function lib_fade($args)
1321 list($color, $alpha) = $this->
colorArgs($args);
1322 $color[4] = $this->clamp($alpha / 100.0);
1326 protected function lib_percentage($arg)
1328 $num = $this->assertNumber($arg);
1329 return array(
"number", $num * 100,
"%");
1345 $white = [
'color', 255, 255, 255];
1346 if ($args[0] ==
'color') {
1347 return $this->lib_mix([
'list',
',', [$white, $args]]);
1348 } elseif ($args[0] ==
"list" && count($args[2]) == 2) {
1349 return $this->lib_mix([$args[0], $args[1], [$white, $args[2][0], $args[2][1]]]);
1351 $this->
throwError(
"tint expects (color, weight)");
1368 $black = [
'color', 0, 0, 0];
1369 if ($args[0] ==
'color') {
1370 return $this->lib_mix([
'list',
',', [$black, $args]]);
1371 } elseif ($args[0] ==
"list" && count($args[2]) == 2) {
1372 return $this->lib_mix([$args[0], $args[1], [$black, $args[2][0], $args[2][1]]]);
1374 $this->
throwError(
"shade expects (color, weight)");
1381 protected function lib_mix($args)
1383 if ($args[0] !=
"list" || count($args[2]) < 2) {
1384 $this->
throwError(
"mix expects (color1, color2, weight)");
1387 list($first, $second) = $args[2];
1388 $first = $this->assertColor($first);
1389 $second = $this->assertColor($second);
1391 $first_a = $this->lib_alpha($first);
1392 $second_a = $this->lib_alpha($second);
1394 if (isset($args[2][2])) {
1395 $weight = $args[2][2][1] / 100.0;
1400 $w = $weight * 2 - 1;
1401 $a = $first_a - $second_a;
1403 $w1 = (($w * $a == -1 ? $w : ($w + $a) / (1 + $w * $a)) + 1) / 2.0;
1406 $new = array(
'color',
1407 $w1 * $first[1] + $w2 * $second[1],
1408 $w1 * $first[2] + $w2 * $second[2],
1409 $w1 * $first[3] + $w2 * $second[3],
1412 if ($first_a != 1.0 || $second_a != 1.0) {
1413 $new[] = $first_a * $weight + $second_a * ($weight - 1);
1416 return $this->fixColor($new);
1419 protected function lib_contrast($args)
1421 $darkColor = array(
'color', 0, 0, 0);
1422 $lightColor = array(
'color', 255, 255, 255);
1425 if ($args[0] ==
'list') {
1426 $inputColor = (isset($args[2][0])) ? $this->assertColor($args[2][0]) : $lightColor;
1427 $darkColor = (isset($args[2][1])) ? $this->assertColor($args[2][1]) : $darkColor;
1428 $lightColor = (isset($args[2][2])) ? $this->assertColor($args[2][2]) : $lightColor;
1429 $threshold = (isset($args[2][3])) ? $this->assertNumber($args[2][3]) : $threshold;
1431 $inputColor = $this->assertColor($args);
1434 $inputColor = $this->coerceColor($inputColor);
1435 $darkColor = $this->coerceColor($darkColor);
1436 $lightColor = $this->coerceColor($lightColor);
1439 if ($this->toLuma($darkColor) > $this->toLuma($lightColor)) {
1441 $lightColor = $darkColor;
1445 $inputColor_alpha = $this->lib_alpha($inputColor);
1446 if (($this->toLuma($inputColor) * $inputColor_alpha) < $threshold) {
1452 private function toLuma($color)
1454 list(, $r, $g, $b) = $this->coerceColor($color);
1460 $r = ($r <= 0.03928) ? $r / 12.92 : pow((($r + 0.055) / 1.055), 2.4);
1461 $g = ($g <= 0.03928) ? $g / 12.92 : pow((($g + 0.055) / 1.055), 2.4);
1462 $b = ($b <= 0.03928) ? $b / 12.92 : pow((($b + 0.055) / 1.055), 2.4);
1464 return (0.2126 * $r) + (0.7152 * $g) + (0.0722 * $b);
1467 protected function lib_luma($color)
1469 return array(
"number", round($this->toLuma($color) * 100, 8),
"%");
1473 public function assertColor($value, $error =
"expected color value")
1475 $color = $this->coerceColor($value);
1476 if (is_null($color)) {
1482 public function assertNumber($value, $error =
"expecting number")
1484 if ($value[0] ==
"number") {
1490 public function assertArgs($value, $expectedArgs, $name =
"")
1492 if ($expectedArgs == 1) {
1495 if ($value[0] !==
"list" || $value[1] !=
",") {
1498 $values = $value[2];
1499 $numValues = count($values);
1500 if ($expectedArgs != $numValues) {
1505 $this->
throwError(
"${name}expecting $expectedArgs arguments, got $numValues");
1512 protected function toHSL($color)
1514 if ($color[0] ===
'hsl') {
1518 $r = $color[1] / 255;
1519 $g = $color[2] / 255;
1520 $b = $color[3] / 255;
1522 $min = min($r, $g, $b);
1523 $max = max($r, $g, $b);
1525 $L = ($min + $max) / 2;
1530 $S = ($max - $min) / ($max + $min);
1532 $S = ($max - $min) / (2.0 - $max - $min);
1535 $H = ($g - $b) / ($max - $min);
1536 } elseif ($g == $max) {
1537 $H = 2.0 + ($b - $r) / ($max - $min);
1538 } elseif ($b == $max) {
1539 $H = 4.0 + ($r - $g) / ($max - $min);
1544 ($H < 0 ? $H + 6 : $H) * 60,
1549 if (count($color) > 4) {
1556 protected function toRGB_helper($comp, $temp1, $temp2)
1560 } elseif ($comp > 1) {
1564 if (6 * $comp < 1) {
1565 return $temp1 + ($temp2 - $temp1) * 6 * $comp;
1567 if (2 * $comp < 1) {
1570 if (3 * $comp < 2) {
1571 return $temp1 + ($temp2 - $temp1) * ((2 / 3) - $comp) * 6;
1583 if ($color[0] ===
'color') {
1587 $H = $color[1] / 360;
1588 $S = $color[2] / 100;
1589 $L = $color[3] / 100;
1595 $L * (1.0 + $S) : $L + $S - $L * $S;
1597 $temp1 = 2.0 * $L - $temp2;
1599 $r = $this->toRGB_helper($H + 1 / 3, $temp1, $temp2);
1600 $g = $this->toRGB_helper($H, $temp1, $temp2);
1601 $b = $this->toRGB_helper($H - 1 / 3, $temp1, $temp2);
1605 $out = array(
'color', $r * 255, $g * 255, $b * 255);
1606 if (count($color) > 4) {
1613 protected function clamp($v, $max = 1, $min = 0)
1615 return min($max, max($min, $v));
1625 if ($func[2][0] !=
'list') {
1629 $rawComponents = $func[2][2];
1631 if ($fname ==
'hsl' || $fname ==
'hsla') {
1632 $hsl = array(
'hsl');
1634 foreach ($rawComponents as $c) {
1635 $val = $this->reduce($c);
1636 $val = isset($val[1]) ? floatval($val[1]) : 0;
1646 $hsl[] = $this->clamp($val, $clamp);
1650 while (count($hsl) < 4) {
1653 return $this->
toRGB($hsl);
1654 } elseif ($fname ==
'rgb' || $fname ==
'rgba') {
1655 $components = array();
1657 foreach ($rawComponents as $c) {
1658 $c = $this->reduce($c);
1660 if ($c[0] ==
"number" && $c[2] ==
"%") {
1661 $components[] = 255 * ($c[1] / 100);
1663 $components[] = floatval($c[1]);
1665 } elseif ($i == 4) {
1666 if ($c[0] ==
"number" && $c[2] ==
"%") {
1667 $components[] = 1.0 * ($c[1] / 100);
1669 $components[] = floatval($c[1]);
1677 while (count($components) < 3) {
1680 array_unshift($components,
'color');
1681 return $this->fixColor($components);
1687 protected function reduce($value, $forExpression =
false)
1689 switch ($value[0]) {
1691 $reduced = $this->reduce($value[1]);
1693 $res = $this->reduce(array(
"variable", $this->vPrefix.$var));
1695 if ($res[0] ==
"raw_color") {
1696 $res = $this->coerceColor($res);
1699 if (empty($value[2])) {
1700 $res = $this->lib_e($res);
1706 if (is_array($key)) {
1707 $key = $this->reduce($key);
1708 $key = $this->vPrefix.$this->compileValue($this->lib_e($key));
1711 $seen = & $this->env->seenNames;
1713 if (!empty($seen[$key])) {
1714 $this->
throwError(
"infinite loop detected: $key");
1718 $out = $this->reduce($this->
get($key));
1719 $seen[$key] =
false;
1722 foreach ($value[2] as &$item) {
1723 $item = $this->reduce($item, $forExpression);
1727 return $this->evaluate($value);
1729 foreach ($value[2] as &$part) {
1730 if (is_array($part)) {
1731 $strip = $part[0] ==
"variable";
1732 $part = $this->reduce($part);
1734 $part = $this->lib_e($part);
1740 list(,$inner) = $value;
1741 return $this->lib_e($this->reduce($inner));
1748 list(, $name, $args) = $value;
1753 $f = isset($this->libFunctions[$name]) ?
1754 $this->libFunctions[$name] : array($this,
'lib_'.str_replace(
'-',
'_', $name));
1756 if (is_callable($f)) {
1757 if ($args[0] ==
'list') {
1758 $args = self::compressList($args[2], $args[1]);
1761 $ret = call_user_func($f, $this->reduce($args,
true), $this);
1763 if (is_null($ret)) {
1764 return array(
"string",
"", array(
1765 $name,
"(", $args,
")"
1770 if (is_numeric($ret)) {
1771 $ret = array(
'number', $ret,
"");
1772 } elseif (!is_array($ret)) {
1773 $ret = array(
'keyword', $ret);
1780 $value[2] = $this->reduce($value[2]);
1783 list(, $op, $exp) = $value;
1784 $exp = $this->reduce($exp);
1786 if ($exp[0] ==
"number") {
1795 return array(
"string",
"", array($op, $exp));
1798 if ($forExpression) {
1799 switch ($value[0]) {
1801 if ($color = $this->coerceColor($value)) {
1806 return $this->coerceColor($value);
1815 protected function coerceColor($value)
1817 switch ($value[0]) {
1821 $c = array(
"color", 0, 0, 0);
1822 $colorStr = substr($value[1], 1);
1823 $num = hexdec($colorStr);
1824 $width = strlen($colorStr) == 3 ? 16 : 256;
1826 for ($i = 3; $i > 0; $i--) {
1830 $c[$i] = $t * (256 / $width) + $t * floor(16 / $width);
1836 if (isset(self::$cssColors[$name])) {
1837 $rgba = explode(
',', self::$cssColors[$name]);
1839 if (isset($rgba[3])) {
1840 return array(
'color', $rgba[0], $rgba[1], $rgba[2], $rgba[3]);
1842 return array(
'color', $rgba[0], $rgba[1], $rgba[2]);
1850 protected function coerceString($value)
1852 switch ($value[0]) {
1856 return array(
"string",
"", array($value[1]));
1862 protected function flattenList($value)
1864 if ($value[0] ==
"list" && count($value[2]) == 1) {
1865 return $this->flattenList($value[2][0]);
1870 public function toBool($a)
1872 return $a ? self::$TRUE : self::$FALSE;
1876 protected function evaluate($exp)
1878 list(, $op, $left, $right, $whiteBefore, $whiteAfter) = $exp;
1880 $left = $this->reduce($left,
true);
1881 $right = $this->reduce($right,
true);
1883 if ($leftColor = $this->coerceColor($left)) {
1887 if ($rightColor = $this->coerceColor($right)) {
1888 $right = $rightColor;
1896 return $this->toBool($left == self::$TRUE && $right == self::$TRUE);
1900 return $this->toBool($this->eq($left, $right));
1903 if ($op ==
"+" && !is_null($str = $this->stringConcatenate($left, $right))) {
1908 $fname =
"op_".$ltype.
"_".$rtype;
1909 if (is_callable(array($this, $fname))) {
1910 $out = $this->$fname($op, $left, $right);
1911 if (!is_null($out)) {
1919 $paddedOp =
" ".$paddedOp;
1925 return array(
"string",
"", array($left, $paddedOp, $right));
1928 protected function stringConcatenate($left, $right)
1930 if ($strLeft = $this->coerceString($left)) {
1931 if ($right[0] ==
"string") {
1934 $strLeft[2][] = $right;
1938 if ($strRight = $this->coerceString($right)) {
1939 array_unshift($strRight[2], $left);
1948 protected function fixColor($c)
1950 foreach (range(1, 3) as $i) {
1962 protected function op_number_color($op, $lft, $rgt)
1964 if ($op ==
'+' || $op ==
'*') {
1965 return $this->op_color_number($op, $rgt, $lft);
1971 protected function op_color_number($op, $lft, $rgt)
1973 if ($rgt[0] ==
'%') {
1977 return $this->op_color_color(
1980 array_fill(1, count($lft) - 1, $rgt[1])
1984 protected function op_color_color($op, $left, $right)
1986 $out = array(
'color');
1987 $max = count($left) > count($right) ? count($left) : count($right);
1988 foreach (range(1, $max - 1) as $i) {
1989 $lval = isset($left[$i]) ? $left[$i] : 0;
1990 $rval = isset($right[$i]) ? $right[$i] : 0;
1993 $out[] = $lval + $rval;
1996 $out[] = $lval - $rval;
1999 $out[] = $lval * $rval;
2002 $out[] = $lval % $rval;
2006 $this->
throwError(
"evaluate error: can't divide by zero");
2008 $out[] = $lval / $rval;
2011 $this->
throwError(
'evaluate error: color op number failed on op '.$op);
2014 return $this->fixColor($out);
2017 public function lib_red($color)
2019 $color = $this->coerceColor($color);
2020 if (is_null($color)) {
2021 $this->
throwError(
'color expected for red()');
2027 public function lib_green($color)
2029 $color = $this->coerceColor($color);
2030 if (is_null($color)) {
2031 $this->
throwError(
'color expected for green()');
2037 public function lib_blue($color)
2039 $color = $this->coerceColor($color);
2040 if (is_null($color)) {
2041 $this->
throwError(
'color expected for blue()');
2049 protected function op_number_number($op, $left, $right)
2051 $unit = empty($left[2]) ? $right[2] : $left[2];
2056 $value = $left[1] + $right[1];
2059 $value = $left[1] * $right[1];
2062 $value = $left[1] - $right[1];
2065 $value = $left[1] % $right[1];
2068 if ($right[1] == 0) {
2069 $this->
throwError(
'parse error: divide by zero');
2071 $value = $left[1] / $right[1];
2074 return $this->toBool($left[1] < $right[1]);
2076 return $this->toBool($left[1] > $right[1]);
2078 return $this->toBool($left[1] >= $right[1]);
2080 return $this->toBool($left[1] <= $right[1]);
2082 $this->
throwError(
'parse error: unknown number operator: '.$op);
2085 return array(
"number", $value, $unit);
2091 protected function makeOutputBlock($type, $selectors =
null)
2094 $b->lines = array();
2095 $b->children = array();
2096 $b->selectors = $selectors;
2098 $b->parent = $this->scope;
2103 protected function pushEnv($block =
null)
2106 $e->parent = $this->env;
2107 $e->store = array();
2115 protected function popEnv()
2118 $this->env = $this->env->parent;
2123 protected function set($name, $value)
2125 $this->env->store[$name] = $value;
2130 protected function get($name)
2132 $current = $this->env;
2134 $isArguments = $name == $this->vPrefix.
'arguments';
2136 if ($isArguments && isset($current->arguments)) {
2137 return array(
'list',
' ', $current->arguments);
2140 if (isset($current->store[$name])) {
2141 return $current->store[$name];
2144 $current = isset($current->storeParent) ?
2145 $current->storeParent : $current->parent;
2148 $this->
throwError(
"variable $name is undefined");
2152 protected function injectVariables($args)
2157 foreach ($args as $name => $strValue) {
2158 if ($name[0] !==
'@') {
2162 $parser->buffer = (string) $strValue;
2163 if (!$parser->propertyValue($value)) {
2164 throw new Exception(
"failed to parse passed in variable $name: $strValue");
2167 $this->
set($name, $value);
2177 if ($fname !==
null) {
2179 $this->_parseFile = $fname;
2183 public function compile($string, $name =
null)
2185 $locale = setlocale(LC_NUMERIC, 0);
2186 setlocale(LC_NUMERIC,
"C");
2188 $this->parser = $this->makeParser($name);
2189 $root = $this->parser->parse($string);
2192 $this->scope =
null;
2194 $this->formatter = $this->newFormatter();
2196 if (!empty($this->registeredVars)) {
2197 $this->injectVariables($this->registeredVars);
2200 $this->sourceParser = $this->parser;
2204 $this->formatter->block($this->scope);
2205 $out = ob_get_clean();
2206 setlocale(LC_NUMERIC, $locale);
2210 public function compileFile($fname, $outFname =
null)
2212 if (!is_readable($fname)) {
2213 throw new Exception(
'load error: failed to find '.$fname);
2216 $pi = pathinfo($fname);
2218 $oldImport = $this->importDir;
2220 $this->importDir = (array) $this->importDir;
2221 $this->importDir[] = $pi[
'dirname'].
'/';
2223 $this->addParsedFile($fname);
2225 $out = $this->compile(file_get_contents($fname), $fname);
2227 $this->importDir = $oldImport;
2229 if ($outFname !==
null) {
2230 return file_put_contents($outFname, $out);
2237 public function checkedCompile($in, $out)
2239 if (!is_file($out) || filemtime($in) > filemtime($out)) {
2240 $this->compileFile($in, $out);
2271 if (is_string($in)) {
2273 } elseif (is_array($in) && isset($in[
'root'])) {
2274 if ($force || !isset($in[
'files'])) {
2278 $root = $in[
'root'];
2279 } elseif (isset($in[
'files']) && is_array($in[
'files'])) {
2280 foreach ($in[
'files'] as $fname => $ftime) {
2281 if (!file_exists($fname) || filemtime($fname) > $ftime) {
2284 $root = $in[
'root'];
2295 if ($root !==
null) {
2298 $out[
'root'] = $root;
2299 $out[
'compiled'] = $this->compileFile($root);
2300 $out[
'files'] = $this->allParsedFiles();
2301 $out[
'updated'] = time();
2312 public function parse($str =
null, $initialVariables =
null)
2314 if (is_array($str)) {
2315 $initialVariables = $str;
2319 $oldVars = $this->registeredVars;
2320 if ($initialVariables !==
null) {
2321 $this->setVariables($initialVariables);
2325 if (empty($this->_parseFile)) {
2326 throw new exception(
"nothing to parse");
2329 $out = $this->compileFile($this->_parseFile);
2331 $out = $this->compile($str);
2334 $this->registeredVars = $oldVars;
2338 protected function makeParser($name)
2341 $parser->writeComments = $this->preserveComments;
2346 public function setFormatter($name)
2348 $this->formatterName = $name;
2351 protected function newFormatter()
2353 $className =
"lessc_formatter_lessjs";
2354 if (!empty($this->formatterName)) {
2355 if (!is_string($this->formatterName)) {
2356 return $this->formatterName;
2358 $className =
"lessc_formatter_$this->formatterName";
2361 return new $className;
2364 public function setPreserveComments($preserve)
2366 $this->preserveComments = $preserve;
2369 public function registerFunction($name, $func)
2371 $this->libFunctions[$name] = $func;
2374 public function unregisterFunction($name)
2376 unset($this->libFunctions[$name]);
2379 public function setVariables($variables)
2381 $this->registeredVars = array_merge($this->registeredVars, $variables);
2384 public function unsetVariable($name)
2386 unset($this->registeredVars[$name]);
2389 public function setImportDir($dirs)
2391 $this->importDir = (array) $dirs;
2394 public function addImportDir($dir)
2396 $this->importDir = (array) $this->importDir;
2397 $this->importDir[] = $dir;
2400 public function allParsedFiles()
2402 return $this->allParsedFiles;
2405 public function addParsedFile($file)
2407 $this->allParsedFiles[realpath($file)] = filemtime($file);
2415 if ($this->sourceLoc >= 0) {
2416 $this->sourceParser->throwError($msg, $this->sourceLoc);
2418 throw new exception($msg);
2423 public static function ccompile($in, $out, $less =
null)
2425 if ($less ===
null) {
2428 return $less->checkedCompile($in, $out);
2431 public static function cexecute($in, $force =
false, $less =
null)
2433 if ($less ===
null) {
2436 return $less->cachedCompile($in, $force);
2439 protected static $cssColors = array(
2440 'aliceblue' =>
'240,248,255',
2441 'antiquewhite' =>
'250,235,215',
2442 'aqua' =>
'0,255,255',
2443 'aquamarine' =>
'127,255,212',
2444 'azure' =>
'240,255,255',
2445 'beige' =>
'245,245,220',
2446 'bisque' =>
'255,228,196',
2448 'blanchedalmond' =>
'255,235,205',
2449 'blue' =>
'0,0,255',
2450 'blueviolet' =>
'138,43,226',
2451 'brown' =>
'165,42,42',
2452 'burlywood' =>
'222,184,135',
2453 'cadetblue' =>
'95,158,160',
2454 'chartreuse' =>
'127,255,0',
2455 'chocolate' =>
'210,105,30',
2456 'coral' =>
'255,127,80',
2457 'cornflowerblue' =>
'100,149,237',
2458 'cornsilk' =>
'255,248,220',
2459 'crimson' =>
'220,20,60',
2460 'cyan' =>
'0,255,255',
2461 'darkblue' =>
'0,0,139',
2462 'darkcyan' =>
'0,139,139',
2463 'darkgoldenrod' =>
'184,134,11',
2464 'darkgray' =>
'169,169,169',
2465 'darkgreen' =>
'0,100,0',
2466 'darkgrey' =>
'169,169,169',
2467 'darkkhaki' =>
'189,183,107',
2468 'darkmagenta' =>
'139,0,139',
2469 'darkolivegreen' =>
'85,107,47',
2470 'darkorange' =>
'255,140,0',
2471 'darkorchid' =>
'153,50,204',
2472 'darkred' =>
'139,0,0',
2473 'darksalmon' =>
'233,150,122',
2474 'darkseagreen' =>
'143,188,143',
2475 'darkslateblue' =>
'72,61,139',
2476 'darkslategray' =>
'47,79,79',
2477 'darkslategrey' =>
'47,79,79',
2478 'darkturquoise' =>
'0,206,209',
2479 'darkviolet' =>
'148,0,211',
2480 'deeppink' =>
'255,20,147',
2481 'deepskyblue' =>
'0,191,255',
2482 'dimgray' =>
'105,105,105',
2483 'dimgrey' =>
'105,105,105',
2484 'dodgerblue' =>
'30,144,255',
2485 'firebrick' =>
'178,34,34',
2486 'floralwhite' =>
'255,250,240',
2487 'forestgreen' =>
'34,139,34',
2488 'fuchsia' =>
'255,0,255',
2489 'gainsboro' =>
'220,220,220',
2490 'ghostwhite' =>
'248,248,255',
2491 'gold' =>
'255,215,0',
2492 'goldenrod' =>
'218,165,32',
2493 'gray' =>
'128,128,128',
2494 'green' =>
'0,128,0',
2495 'greenyellow' =>
'173,255,47',
2496 'grey' =>
'128,128,128',
2497 'honeydew' =>
'240,255,240',
2498 'hotpink' =>
'255,105,180',
2499 'indianred' =>
'205,92,92',
2500 'indigo' =>
'75,0,130',
2501 'ivory' =>
'255,255,240',
2502 'khaki' =>
'240,230,140',
2503 'lavender' =>
'230,230,250',
2504 'lavenderblush' =>
'255,240,245',
2505 'lawngreen' =>
'124,252,0',
2506 'lemonchiffon' =>
'255,250,205',
2507 'lightblue' =>
'173,216,230',
2508 'lightcoral' =>
'240,128,128',
2509 'lightcyan' =>
'224,255,255',
2510 'lightgoldenrodyellow' =>
'250,250,210',
2511 'lightgray' =>
'211,211,211',
2512 'lightgreen' =>
'144,238,144',
2513 'lightgrey' =>
'211,211,211',
2514 'lightpink' =>
'255,182,193',
2515 'lightsalmon' =>
'255,160,122',
2516 'lightseagreen' =>
'32,178,170',
2517 'lightskyblue' =>
'135,206,250',
2518 'lightslategray' =>
'119,136,153',
2519 'lightslategrey' =>
'119,136,153',
2520 'lightsteelblue' =>
'176,196,222',
2521 'lightyellow' =>
'255,255,224',
2522 'lime' =>
'0,255,0',
2523 'limegreen' =>
'50,205,50',
2524 'linen' =>
'250,240,230',
2525 'magenta' =>
'255,0,255',
2526 'maroon' =>
'128,0,0',
2527 'mediumaquamarine' =>
'102,205,170',
2528 'mediumblue' =>
'0,0,205',
2529 'mediumorchid' =>
'186,85,211',
2530 'mediumpurple' =>
'147,112,219',
2531 'mediumseagreen' =>
'60,179,113',
2532 'mediumslateblue' =>
'123,104,238',
2533 'mediumspringgreen' =>
'0,250,154',
2534 'mediumturquoise' =>
'72,209,204',
2535 'mediumvioletred' =>
'199,21,133',
2536 'midnightblue' =>
'25,25,112',
2537 'mintcream' =>
'245,255,250',
2538 'mistyrose' =>
'255,228,225',
2539 'moccasin' =>
'255,228,181',
2540 'navajowhite' =>
'255,222,173',
2541 'navy' =>
'0,0,128',
2542 'oldlace' =>
'253,245,230',
2543 'olive' =>
'128,128,0',
2544 'olivedrab' =>
'107,142,35',
2545 'orange' =>
'255,165,0',
2546 'orangered' =>
'255,69,0',
2547 'orchid' =>
'218,112,214',
2548 'palegoldenrod' =>
'238,232,170',
2549 'palegreen' =>
'152,251,152',
2550 'paleturquoise' =>
'175,238,238',
2551 'palevioletred' =>
'219,112,147',
2552 'papayawhip' =>
'255,239,213',
2553 'peachpuff' =>
'255,218,185',
2554 'peru' =>
'205,133,63',
2555 'pink' =>
'255,192,203',
2556 'plum' =>
'221,160,221',
2557 'powderblue' =>
'176,224,230',
2558 'purple' =>
'128,0,128',
2560 'rosybrown' =>
'188,143,143',
2561 'royalblue' =>
'65,105,225',
2562 'saddlebrown' =>
'139,69,19',
2563 'salmon' =>
'250,128,114',
2564 'sandybrown' =>
'244,164,96',
2565 'seagreen' =>
'46,139,87',
2566 'seashell' =>
'255,245,238',
2567 'sienna' =>
'160,82,45',
2568 'silver' =>
'192,192,192',
2569 'skyblue' =>
'135,206,235',
2570 'slateblue' =>
'106,90,205',
2571 'slategray' =>
'112,128,144',
2572 'slategrey' =>
'112,128,144',
2573 'snow' =>
'255,250,250',
2574 'springgreen' =>
'0,255,127',
2575 'steelblue' =>
'70,130,180',
2576 'tan' =>
'210,180,140',
2577 'teal' =>
'0,128,128',
2578 'thistle' =>
'216,191,216',
2579 'tomato' =>
'255,99,71',
2580 'transparent' =>
'0,0,0,0',
2581 'turquoise' =>
'64,224,208',
2582 'violet' =>
'238,130,238',
2583 'wheat' =>
'245,222,179',
2584 'white' =>
'255,255,255',
2585 'whitesmoke' =>
'245,245,245',
2586 'yellow' =>
'255,255,0',
2587 'yellowgreen' =>
'154,205,50'
2595 protected static $nextBlockId = 0;
2597 protected static $precedence = array(
2611 protected static $whitePattern;
2612 protected static $commentMulti;
2614 protected static $commentSingle =
"//";
2615 protected static $commentMultiLeft =
"/*";
2616 protected static $commentMultiRight =
"*/";
2619 protected static $operatorString;
2622 protected static $supressDivisionProps =
2623 array(
'/border-radius$/i',
'/^font$/i');
2625 protected $blockDirectives = array(
"font-face",
"keyframes",
"page",
"-moz-document",
"viewport",
"-moz-viewport",
"-o-viewport",
"-ms-viewport");
2626 protected $lineDirectives = array(
"charset");
2640 protected static $literalCache = array();
2646 public function __construct($lessc, $sourceName =
null)
2648 $this->eatWhiteDefault =
true;
2650 $this->lessc = $lessc;
2652 $this->sourceName = $sourceName;
2654 $this->writeComments =
false;
2656 if (!self::$operatorString) {
2657 self::$operatorString =
2658 '('.implode(
'|', array_map(
2659 array(
'lessc',
'preg_quote'),
2660 array_keys(self::$precedence)
2663 $commentSingle = Lessc::preg_quote(self::$commentSingle);
2664 $commentMultiLeft = Lessc::preg_quote(self::$commentMultiLeft);
2665 $commentMultiRight = Lessc::preg_quote(self::$commentMultiRight);
2667 self::$commentMulti = $commentMultiLeft.
'.*?'.$commentMultiRight;
2668 self::$whitePattern =
'/'.$commentSingle.
'[^\n]*\s*|('.self::$commentMulti.
')\s*|\s+/Ais';
2685 $this->buffer = $this->writeComments ? $buffer : $this->removeComments($buffer);
2686 $this->pushSpecialBlock(
"root");
2687 $this->eatWhiteDefault =
true;
2688 $this->seenComments = array();
2695 $this->whitespace();
2700 if ($this->count != strlen($this->buffer)) {
2701 $this->throwError(
'parse error count '.$this->count.
' != len buffer '.strlen($this->buffer));
2705 if (!property_exists($this->env,
'parent') || !is_null($this->env->parent)) {
2706 throw new exception(
'parse error: unclosed block');
2750 if (empty($this->buffer)) {
2755 if ($this->whitespace()) {
2761 $mediaQueries =
null;
2764 $importValue =
null;
2775 if ($this->keyword($key) && $this->
assign() &&
2776 $this->propertyValue($value, $key) && $this->end()
2778 $this->append(array(
'assign', $key, $value), $s);
2786 if ($this->literal(
'@',
false)) {
2790 if ($this->literal(
'@media')) {
2791 if ($this->mediaQueryList($mediaQueries)
2792 && $this->literal(
'{')
2794 $media = $this->pushSpecialBlock(
"media");
2795 $media->queries = is_null($mediaQueries) ? array() : $mediaQueries;
2803 if ($this->literal(
"@",
false) && $this->keyword($dirName)) {
2804 if ($this->isDirective($dirName, $this->blockDirectives)) {
2805 if ($this->openString(
"{", $dirValue,
null, array(
";")) &&
2808 $dir = $this->pushSpecialBlock(
"directive");
2809 $dir->name = $dirName;
2810 if (isset($dirValue)) {
2811 $dir->value = $dirValue;
2815 } elseif ($this->isDirective($dirName, $this->lineDirectives)) {
2816 if ($this->propertyValue($dirValue) && $this->end()) {
2817 $this->append(array(
"directive", $dirName, $dirValue));
2827 if ($this->variable($var) && $this->
assign() &&
2828 $this->propertyValue($value) && $this->end()
2830 $this->append(array(
'assign', $var, $value), $s);
2836 if ($this->
import($importValue)) {
2837 $this->append($importValue, $s);
2842 if ($this->tag($tag,
true) && $this->argumentDef($args, $isVararg) &&
2843 $this->guards($guards) &&
2846 $block = $this->pushBlock($this->fixTags(array($tag)));
2847 $block->args = $args;
2848 $block->isVararg = $isVararg;
2849 if (!empty($guards)) {
2850 $block->guards = $guards;
2858 if ($this->tags($tags) && $this->literal(
'{',
false)) {
2859 $tags = $this->fixTags($tags);
2860 $this->pushBlock($tags);
2867 if ($this->literal(
'}',
false)) {
2869 $block = $this->pop();
2870 }
catch (exception $e) {
2872 $this->throwError($e->getMessage());
2876 if (is_null($block->type)) {
2878 if (!isset($block->args)) {
2879 foreach ($block->tags as $tag) {
2880 if (!is_string($tag) || $tag[0] != $this->lessc->mPrefix) {
2887 foreach ($block->tags as $tag) {
2888 if (is_string($tag)) {
2889 $this->env->children[$tag][] = $block;
2895 $this->append(array(
'block', $block), $s);
2900 $this->whitespace();
2905 if ($this->mixinTags($tags) &&
2906 $this->argumentDef($argv, $isVararg) &&
2907 $this->keyword($suffix) && $this->end()
2909 $tags = $this->fixTags($tags);
2910 $this->append(array(
'mixin', $tags, $argv, $suffix), $s);
2917 if ($this->literal(
';')) {
2924 protected function isDirective($dirname, $directives)
2929 array_map(array(
"lessc",
"preg_quote"), $directives)
2931 $pattern =
'/^(-[a-z-]+-)?('.$pattern.
')$/i';
2933 return preg_match($pattern, $dirname);
2936 protected function fixTags($tags)
2939 foreach ($tags as &$tag) {
2940 if ($tag[0] == $this->lessc->vPrefix) {
2941 $tag[0] = $this->lessc->mPrefix;
2948 protected function expressionList(&$exps)
2958 if (count($values) == 0) {
2962 $exps = Lessc::compressList($values,
' ');
2975 if ($this->value($lhs)) {
2979 if (!empty($this->env->supressedDivision)) {
2980 unset($this->env->supressedDivision);
2982 if ($this->literal(
"/") && $this->value($rhs)) {
2983 $out = array(
"list",
"",
2984 array($out, array(
"keyword",
"/"), $rhs));
3003 $this->inExp =
true;
3004 $ss = $this->seek();
3007 $whiteBefore = isset($this->buffer[$this->count - 1]) &&
3008 ctype_space($this->buffer[$this->count - 1]);
3015 if ($this->match(self::$operatorString.($needWhite ?
'\s' :
''), $m) && self::$precedence[$m[1]] >= $minP) {
3016 if (!$this->inParens && isset($this->env->currentProperty) && $m[1] ==
"/" && empty($this->env->supressedDivision)) {
3017 foreach (self::$supressDivisionProps as $pattern) {
3018 if (preg_match($pattern, $this->env->currentProperty)) {
3019 $this->env->supressedDivision =
true;
3026 $whiteAfter = isset($this->buffer[$this->count - 1]) &&
3027 ctype_space($this->buffer[$this->count - 1]);
3029 if (!$this->value($rhs)) {
3034 if ($this->peek(self::$operatorString, $next) && self::$precedence[$next[1]] > self::$precedence[$m[1]]) {
3035 $rhs = $this->
expHelper($rhs, self::$precedence[$next[1]]);
3038 $lhs = array(
'expression', $m[1], $lhs, $rhs, $whiteBefore, $whiteAfter);
3039 $ss = $this->seek();
3053 public function propertyValue(&$value, $keyName =
null)
3058 if ($keyName !==
null) {
3059 $this->env->currentProperty = $keyName;
3063 while ($this->expressionList($v)) {
3066 if (!$this->literal(
',')) {
3075 if ($keyName !==
null) {
3076 unset($this->env->currentProperty);
3079 if (count($values) == 0) {
3083 $value = Lessc::compressList($values,
', ');
3087 protected function parenValue(&$out)
3094 if (isset($this->buffer[$this->count]) && $this->buffer[$this->count] !=
"(") {
3099 if ($this->literal(
"(") &&
3100 ($this->inParens =
true) && $this->
expression($exp) &&
3115 protected function value(&$value)
3125 if (isset($this->buffer[$this->count]) && $this->buffer[$this->count] ==
"-") {
3127 if ($this->literal(
"-",
false) &&
3128 (($this->variable($inner) && $inner = array(
"variable", $inner)) ||
3129 $this->unit($inner) ||
3130 $this->parenValue($inner))
3132 $value = array(
"unary",
"-", $inner);
3139 if ($this->parenValue($value)) {
3142 if ($this->unit($value)) {
3145 if ($this->color($value)) {
3148 if ($this->func($value)) {
3151 if ($this->
string($value)) {
3155 if ($this->keyword($word)) {
3156 $value = array(
'keyword', $word);
3161 if ($this->variable($var)) {
3162 $value = array(
'variable', $var);
3167 if ($this->literal(
"~") && $this->
string($str)) {
3168 $value = array(
"escape", $str);
3176 if ($this->literal(
'\\') && $this->match(
'([0-9]+)', $m)) {
3177 $value = array(
'keyword',
'\\'.$m[1]);
3187 protected function import(&$out, $value =
'')
3189 if (!$this->literal(
'@import')) {
3197 if ($this->propertyValue($value)) {
3198 $out = array(
"import", $value);
3205 protected function mediaQueryList(&$out)
3209 if ($this->genericList($list,
"mediaQuery",
",",
false)) {
3216 protected function mediaQuery(&$out)
3222 $expressions =
null;
3225 if ((($this->literal(
"only") && ($only =
true)) || ($this->literal(
"not") && ($not =
true))) && $this->keyword($mediaType)) {
3226 $prop = array(
"mediaType");
3233 $prop[] = $mediaType;
3240 if (!empty($mediaType) && !$this->literal(
"and")) {
3243 $this->genericList($expressions,
"mediaExpression",
"and",
false);
3244 if (is_array($expressions)) {
3245 $parts = array_merge($parts, $expressions[2]);
3249 if (count($parts) == 0) {
3258 protected function mediaExpression(&$out)
3265 if ($this->literal(
"(") &&
3266 $this->keyword($feature) &&
3267 ($this->literal(
":") && $this->
expression($value)) &&
3270 $out = array(
"mediaExp", $feature);
3275 } elseif ($this->variable($variable)) {
3276 $out = array(
'variable', $variable);
3285 protected function openString($end, &$out, $nestingOpen =
null, $rejectStrs =
null)
3290 $oldWhite = $this->eatWhiteDefault;
3291 $this->eatWhiteDefault =
false;
3293 $stop = array(
"'",
'"',
"@{", $end);
3294 $stop = array_map(array(
"lessc",
"preg_quote"), $stop);
3297 if (!is_null($rejectStrs)) {
3298 $stop = array_merge($stop, $rejectStrs);
3301 $patt =
'(.*?)('.implode(
"|", $stop).
')';
3307 while ($this->match($patt, $m,
false)) {
3308 if (!empty($m[1])) {
3311 $nestingLevel += substr_count($m[1], $nestingOpen);
3317 $this->count -= strlen($tok);
3319 if ($nestingLevel == 0) {
3326 if (($tok ==
"'" || $tok ==
'"') && $this->
string($str)) {
3331 if ($tok ==
"@{" && $this->interpolation($inter)) {
3332 $content[] = $inter;
3336 if (!empty($rejectStrs) && in_array($tok, $rejectStrs)) {
3341 $this->count += strlen($tok);
3344 $this->eatWhiteDefault = $oldWhite;
3346 if (count($content) == 0) {
3351 if (is_string(end($content))) {
3352 $content[count($content) - 1] = rtrim(end($content));
3355 $out = array(
"string",
"", $content);
3359 protected function string(&$out)
3364 if ($this->literal(
'"',
false)) {
3366 } elseif ($this->literal(
"'",
false)) {
3375 $patt =
'([^\n]*?)(@\{|\\\\|'.
3376 Lessc::preg_quote($delim).
')';
3378 $oldWhite = $this->eatWhiteDefault;
3379 $this->eatWhiteDefault =
false;
3382 while ($this->match($patt, $m,
false)) {
3384 if ($m[2] ==
"@{") {
3385 $this->count -= strlen($m[2]);
3386 if ($this->interpolation($inter)) {
3387 $content[] = $inter;
3389 $this->count += strlen($m[2]);
3392 } elseif ($m[2] ==
'\\') {
3394 if ($this->literal($delim,
false)) {
3395 $content[] = $delim;
3398 $this->count -= strlen($delim);
3403 $this->eatWhiteDefault = $oldWhite;
3405 if ($this->literal($delim)) {
3406 $out = array(
"string", $delim, $content);
3414 protected function interpolation(&$out)
3418 $oldWhite = $this->eatWhiteDefault;
3419 $this->eatWhiteDefault =
true;
3422 if ($this->literal(
"@{") &&
3423 $this->openString(
"}", $interp,
null, array(
"'",
'"',
";")) &&
3424 $this->literal(
"}",
false)
3426 $out = array(
"interpolate", $interp);
3427 $this->eatWhiteDefault = $oldWhite;
3428 if ($this->eatWhiteDefault) {
3429 $this->whitespace();
3434 $this->eatWhiteDefault = $oldWhite;
3439 protected function unit(&$unit)
3444 if (isset($this->buffer[$this->count])) {
3445 $char = $this->buffer[$this->count];
3446 if (!ctype_digit($char) && $char !=
".") {
3451 if ($this->match(
'([0-9]+(?:\.[0-9]*)?|\.[0-9]+)([%a-zA-Z]+)?', $m)) {
3452 $unit = array(
"number", $m[1], empty($m[2]) ?
"" : $m[2]);
3459 protected function color(&$out)
3463 if ($this->match(
'(#(?:[0-9a-f]{8}|[0-9a-f]{6}|[0-9a-f]{3}))', $m)) {
3464 if (strlen($m[1]) > 7) {
3465 $out = array(
"string",
"", array($m[1]));
3467 $out = array(
"raw_color", $m[1]);
3480 protected function argumentDef(&$args, &$isVararg)
3486 if (!$this->literal(
'(')) {
3492 $method =
"expressionList";
3496 if ($this->literal(
"...")) {
3501 if ($this->$method($value)) {
3502 if ($value[0] ==
"variable") {
3503 $arg = array(
"arg", $value[1]);
3504 $ss = $this->seek();
3506 if ($this->
assign() && $this->$method($rhs)) {
3510 if ($this->literal(
"...")) {
3522 $values[] = array(
"lit", $value);
3527 if (!$this->literal($delim)) {
3528 if ($delim ==
"," && $this->literal(
";")) {
3531 $method =
"propertyValue";
3534 if (isset($values[1])) {
3536 foreach ($values as $i => $arg) {
3540 $this->throwError(
"Cannot mix ; and , as delimiter types");
3542 $newList[] = $arg[2];
3545 $newList[] = $arg[1];
3548 $this->throwError(
"Unexpected rest before semicolon");
3552 $newList = array(
"list",
", ", $newList);
3554 switch ($values[0][0]) {
3556 $newArg = array(
"arg", $values[0][1], $newList);
3559 $newArg = array(
"lit", $newList);
3562 } elseif ($values) {
3563 $newArg = $values[0];
3567 $values = array($newArg);
3575 if (!$this->literal(
')')) {
3587 protected function tags(&$tags, $simple =
false, $delim =
',')
3592 while ($this->tag($tt, $simple)) {
3594 if (!$this->literal($delim)) {
3598 if (count($tags) == 0) {
3607 protected function mixinTags(&$tags)
3612 while ($this->tag($tt,
true)) {
3614 $this->literal(
">");
3625 protected function tagBracket(&$parts, &$hasExpression)
3632 if (isset($this->buffer[$this->count]) && $this->buffer[$this->count] !=
"[") {
3638 $hasInterpolation =
false;
3640 if ($this->literal(
"[",
false)) {
3641 $attrParts = array(
"[");
3644 if ($this->literal(
"]",
false)) {
3650 if ($this->match(
'\s+', $m)) {
3654 if ($this->
string($str)) {
3656 foreach ($str[2] as &$chunk) {
3657 $chunk = str_replace($this->lessc->parentSelector,
"$&$", $chunk);
3660 $attrParts[] = $str;
3661 $hasInterpolation =
true;
3665 if ($this->keyword($word)) {
3666 $attrParts[] = $word;
3670 if ($this->interpolation($inter)) {
3671 $attrParts[] = $inter;
3672 $hasInterpolation =
true;
3677 if ($this->match(
'[|-~\$\*\^=]+', $m)) {
3678 $attrParts[] = $m[0];
3685 if ($this->literal(
"]",
false)) {
3687 foreach ($attrParts as $part) {
3690 $hasExpression = $hasExpression || $hasInterpolation;
3701 protected function tag(&$tag, $simple =
false)
3707 $chars =
'^@,:;{}\][>\(\) "\'';
3709 $chars =
'^@,;{}["\'';
3713 $hasExpression =
false;
3715 while ($this->tagBracket($parts, $hasExpression));
3717 $oldWhite = $this->eatWhiteDefault;
3718 $this->eatWhiteDefault =
false;
3722 if ($this->match(
'(['.$chars.
'0-9]['.$chars.
']*)', $m)) {
3728 while ($this->tagBracket($parts, $hasExpression));
3732 if (isset($this->buffer[$this->count]) && $this->buffer[$this->count] ==
"@") {
3733 if ($this->interpolation($interp)) {
3734 $hasExpression =
true;
3740 if ($this->literal(
"@")) {
3746 if ($this->unit($unit)) {
3747 $parts[] = $unit[1];
3748 $parts[] = $unit[2];
3755 $this->eatWhiteDefault = $oldWhite;
3761 if ($hasExpression) {
3762 $tag = array(
"exp", array(
"string",
"", $parts));
3764 $tag = trim(implode($parts));
3767 $this->whitespace();
3772 protected function func(&$func)
3781 if ($this->match(
'(%|[\w\-_][\w\-_:\.]+|[\w_])', $m) && $this->literal(
'(')) {
3784 $sPreArgs = $this->seek();
3788 $ss = $this->seek();
3790 if ($this->keyword($name) && $this->literal(
'=') && $this->expressionList($value)) {
3791 $args[] = array(
"string",
"", array($name,
"=", $value));
3794 if ($this->expressionList($value)) {
3799 if (!$this->literal(
',')) {
3803 $args = array(
'list',
',', $args);
3805 if ($this->literal(
')')) {
3806 $func = array(
'function', $fname, $args);
3808 } elseif ($fname ==
'url') {
3810 $this->seek($sPreArgs);
3811 if ($this->openString(
")", $string) && $this->literal(
")")) {
3812 $func = array(
'function', $fname, $string);
3823 protected function variable(&$name)
3829 if ($this->literal($this->lessc->vPrefix,
false) &&
3830 ($this->variable($sub) || $this->keyword($name))
3833 $name = array(
'variable', $sub);
3835 $name = $this->lessc->vPrefix.$name;
3852 $this->currentProperty = $name;
3854 return $this->literal(
':') || $this->literal(
'=');
3858 protected function keyword(&$word)
3861 if ($this->match(
'([\w_\-\*!"][\w\-_"]*)', $m)) {
3869 protected function end()
3871 if ($this->literal(
';',
false)) {
3873 } elseif ($this->count == strlen($this->buffer) || $this->buffer[$this->count] ==
'}') {
3880 protected function guards(&$guards)
3886 if (!$this->literal(
"when")) {
3893 while ($this->guardGroup($g)) {
3895 if (!$this->literal(
",")) {
3900 if (count($guards) == 0) {
3911 protected function guardGroup(&$guardGroup)
3916 $guardGroup = array();
3917 while ($this->guard($guard)) {
3918 $guardGroup[] = $guard;
3919 if (!$this->literal(
"and")) {
3924 if (count($guardGroup) == 0) {
3933 protected function guard(&$guard)
3938 $negate = $this->literal(
"not");
3940 if ($this->literal(
"(") && $this->
expression($exp) && $this->literal(
")")) {
3943 $guard = array(
"negate", $guard);
3954 protected function literal($what, $eatWhitespace =
null)
3956 if ($eatWhitespace ===
null) {
3957 $eatWhitespace = $this->eatWhiteDefault;
3961 if (!isset($what[1]) && isset($this->buffer[$this->count])) {
3962 if ($this->buffer[$this->count] == $what) {
3963 if (!$eatWhitespace) {
3973 if (!isset(self::$literalCache[$what])) {
3974 self::$literalCache[$what] = Lessc::preg_quote($what);
3978 return $this->match(self::$literalCache[$what], $m, $eatWhitespace);
3981 protected function genericList(&$out, $parseItem, $delim =
"", $flatten =
true)
3987 while ($this->$parseItem($value)) {
3990 if (!$this->literal($delim)) {
3996 if (count($items) == 0) {
4001 if ($flatten && count($items) == 1) {
4004 $out = array(
"list", $delim, $items);
4014 protected function to($what, &$out, $until =
false, $allowNewline =
false)
4016 if (is_string($allowNewline)) {
4017 $validChars = $allowNewline;
4019 $validChars = $allowNewline ?
"." :
"[^\n]";
4022 if (!$this->match(
'('.$validChars.
'*?)'.Lessc::preg_quote($what), $m, !$until)) {
4026 $this->count -= strlen($what);
4033 protected function match($regex, &$out, $eatWhitespace =
null)
4035 if ($eatWhitespace ===
null) {
4036 $eatWhitespace = $this->eatWhiteDefault;
4039 $r =
'/'.$regex.($eatWhitespace && !$this->writeComments ?
'\s*' :
'').
'/Ais';
4040 if (preg_match($r, $this->buffer, $out,
null, $this->count)) {
4041 $this->count += strlen($out[0]);
4042 if ($eatWhitespace && $this->writeComments) {
4043 $this->whitespace();
4051 protected function whitespace()
4053 if ($this->writeComments) {
4056 while (preg_match(self::$whitePattern, $this->buffer, $m,
null, $this->count)) {
4057 if (isset($m[1]) && empty($this->seenComments[$this->count])) {
4058 $this->append(array(
"comment", $m[1]));
4059 $this->seenComments[$this->count] =
true;
4061 $this->count += strlen($m[0]);
4066 $this->match(
"", $m);
4067 return strlen($m[0]) > 0;
4072 protected function peek($regex, &$out =
null, $from =
null)
4074 if (is_null($from)) {
4075 $from = $this->count;
4077 $r =
'/'.$regex.
'/Ais';
4078 $result = preg_match($r, $this->buffer, $out,
null, $from);
4084 protected function seek($where =
null)
4086 if ($where ===
null) {
4087 return $this->count;
4089 $this->count = $where;
4096 public function throwError($msg =
"parse error", $count =
null)
4098 $count = is_null($count) ? $this->count : $count;
4100 $line = $this->line +
4101 substr_count(substr($this->buffer, 0, $count),
"\n");
4103 if (!empty($this->sourceName)) {
4104 $loc =
"$this->sourceName on line $line";
4106 $loc =
"line: $line";
4111 if ($this->peek(
"(.*?)(\n|$)", $m, $count)) {
4112 throw new exception(
"$msg: failed at `$m[1]` $loc");
4114 throw new exception(
"$msg: $loc");
4118 protected function pushBlock($selectors =
null, $type =
null)
4121 $b->parent = $this->env;
4124 $b->id = self::$nextBlockId++;
4126 $b->isVararg =
false;
4127 $b->tags = $selectors;
4129 $b->props = array();
4130 $b->children = array();
4137 protected function pushSpecialBlock($type)
4139 return $this->pushBlock(
null, $type);
4143 protected function append($prop, $pos =
null)
4145 if ($pos !==
null) {
4148 $this->env->props[] = $prop;
4152 protected function pop()
4155 $this->env = $this->env->parent;
4161 protected function removeComments($text)
4164 'url(',
'//',
'/*',
'"',
"'"
4171 foreach ($look as $token) {
4172 $pos = strpos($text, $token);
4173 if ($pos !==
false) {
4174 if (!isset($min) || $pos < $min[1]) {
4175 $min = array($token, $pos);
4180 if (is_null($min)) {
4190 if (preg_match(
'/url\(.*?\)/', $text, $m, 0, $count)) {
4191 $count += strlen($m[0]) - strlen($min[0]);
4196 if (preg_match(
'/'.$min[0].
'.*?(?<!\\\\)'.$min[0].
'/', $text, $m, 0, $count)) {
4197 $count += strlen($m[0]) - 1;
4201 $skip = strpos($text,
"\n", $count);
4202 if ($skip ===
false) {
4203 $skip = strlen($text) - $count;
4210 if (preg_match(
'/\/\*.*?\*\//s', $text, $m, 0, $count)) {
4211 $skip = strlen($m[0]);
4212 $newlines = substr_count($m[0],
"\n");
4218 $count += strlen($min[0]);
4221 $out .= substr($text, 0, $count).str_repeat(
"\n", $newlines);
4222 $text = substr($text, $count + $skip);