Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like lessc often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use lessc, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class lessc { |
||
| 41 | static public $VERSION = "v0.5.0"; |
||
| 42 | |||
| 43 | static public $TRUE = array("keyword", "true"); |
||
| 44 | static public $FALSE = array("keyword", "false"); |
||
| 45 | |||
| 46 | protected $libFunctions = array(); |
||
| 47 | protected $registeredVars = array(); |
||
| 48 | protected $preserveComments = false; |
||
| 49 | |||
| 50 | public $vPrefix = '@'; // prefix of abstract properties |
||
| 51 | public $mPrefix = '$'; // prefix of abstract blocks |
||
| 52 | public $parentSelector = '&'; |
||
| 53 | |||
| 54 | public $importDisabled = false; |
||
| 55 | public $importDir = ''; |
||
| 56 | |||
| 57 | protected $numberPrecision = null; |
||
| 58 | |||
| 59 | protected $allParsedFiles = array(); |
||
| 60 | |||
| 61 | // set to the parser that generated the current line when compiling |
||
| 62 | // so we know how to create error messages |
||
| 63 | protected $sourceParser = null; |
||
| 64 | protected $sourceLoc = null; |
||
| 65 | |||
| 66 | static protected $nextImportId = 0; // uniquely identify imports |
||
| 67 | |||
| 68 | // attempts to find the path of an import url, returns null for css files |
||
| 69 | protected function findImport($url) { |
||
| 70 | foreach ((array)$this->importDir as $dir) { |
||
| 71 | $full = $dir.(substr($dir, -1) != '/' ? '/' : '').$url; |
||
| 72 | if ($this->fileExists($file = $full.'.less') || $this->fileExists($file = $full)) { |
||
| 73 | return $file; |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | return null; |
||
| 78 | } |
||
| 79 | |||
| 80 | protected function fileExists($name) { |
||
| 83 | |||
| 84 | static public function compressList($items, $delim) { |
||
| 88 | |||
| 89 | static public function preg_quote($what) { |
||
| 92 | |||
| 93 | protected function tryImport($importPath, $parentBlock, $out) { |
||
| 94 | if ($importPath[0] == "function" && $importPath[1] == "url") { |
||
| 95 | $importPath = $this->flattenList($importPath[2]); |
||
| 96 | } |
||
| 97 | |||
| 98 | $str = $this->coerceString($importPath); |
||
| 99 | if ($str === null) return false; |
||
| 100 | |||
| 101 | $url = $this->compileValue($this->lib_e($str)); |
||
| 102 | |||
| 103 | // don't import if it ends in css |
||
| 104 | if (substr_compare($url, '.css', -4, 4) === 0) return false; |
||
| 105 | |||
| 106 | $realPath = $this->findImport($url); |
||
| 107 | |||
| 108 | if ($realPath === null) return false; |
||
| 109 | |||
| 110 | if ($this->importDisabled) { |
||
| 111 | return array(false, "/* import disabled */"); |
||
| 112 | } |
||
| 113 | |||
| 114 | if (isset($this->allParsedFiles[realpath($realPath)])) { |
||
| 115 | return array(false, null); |
||
| 116 | } |
||
| 117 | |||
| 118 | $this->addParsedFile($realPath); |
||
| 119 | $parser = $this->makeParser($realPath); |
||
| 120 | $root = $parser->parse(file_get_contents($realPath)); |
||
| 121 | |||
| 122 | // set the parents of all the block props |
||
| 123 | foreach ($root->props as $prop) { |
||
| 124 | if ($prop[0] == "block") { |
||
| 125 | $prop[1]->parent = $parentBlock; |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | // copy mixins into scope, set their parents |
||
| 130 | // bring blocks from import into current block |
||
| 131 | // TODO: need to mark the source parser these came from this file |
||
| 132 | foreach ($root->children as $childName => $child) { |
||
| 133 | if (isset($parentBlock->children[$childName])) { |
||
| 134 | $parentBlock->children[$childName] = array_merge( |
||
| 135 | $parentBlock->children[$childName], |
||
| 136 | $child); |
||
| 137 | } else { |
||
| 138 | $parentBlock->children[$childName] = $child; |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | $pi = pathinfo($realPath); |
||
| 143 | $dir = $pi["dirname"]; |
||
| 144 | |||
| 145 | list($top, $bottom) = $this->sortProps($root->props, true); |
||
| 146 | $this->compileImportedProps($top, $parentBlock, $out, $parser, $dir); |
||
| 147 | |||
| 148 | return array(true, $bottom, $parser, $dir); |
||
| 149 | } |
||
| 150 | |||
| 151 | protected function compileImportedProps($props, $block, $out, $sourceParser, $importDir) { |
||
| 152 | $oldSourceParser = $this->sourceParser; |
||
| 153 | |||
| 154 | $oldImport = $this->importDir; |
||
| 155 | |||
| 156 | // TODO: this is because the importDir api is stupid |
||
| 157 | $this->importDir = (array)$this->importDir; |
||
| 158 | array_unshift($this->importDir, $importDir); |
||
| 159 | |||
| 160 | foreach ($props as $prop) { |
||
| 161 | $this->compileProp($prop, $block, $out); |
||
| 162 | } |
||
| 163 | |||
| 164 | $this->importDir = $oldImport; |
||
| 165 | $this->sourceParser = $oldSourceParser; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Recursively compiles a block. |
||
| 170 | * |
||
| 171 | * A block is analogous to a CSS block in most cases. A single LESS document |
||
| 172 | * is encapsulated in a block when parsed, but it does not have parent tags |
||
| 173 | * so all of it's children appear on the root level when compiled. |
||
| 174 | * |
||
| 175 | * Blocks are made up of props and children. |
||
| 176 | * |
||
| 177 | * Props are property instructions, array tuples which describe an action |
||
| 178 | * to be taken, eg. write a property, set a variable, mixin a block. |
||
| 179 | * |
||
| 180 | * The children of a block are just all the blocks that are defined within. |
||
| 181 | * This is used to look up mixins when performing a mixin. |
||
| 182 | * |
||
| 183 | * Compiling the block involves pushing a fresh environment on the stack, |
||
| 184 | * and iterating through the props, compiling each one. |
||
| 185 | * |
||
| 186 | * See lessc::compileProp() |
||
| 187 | * |
||
| 188 | */ |
||
| 189 | protected function compileBlock($block) { |
||
| 190 | switch ($block->type) { |
||
| 191 | case "root": |
||
| 192 | $this->compileRoot($block); |
||
| 193 | break; |
||
| 194 | case null: |
||
| 195 | $this->compileCSSBlock($block); |
||
| 196 | break; |
||
| 197 | case "media": |
||
| 198 | $this->compileMedia($block); |
||
| 199 | break; |
||
| 200 | View Code Duplication | case "directive": |
|
| 201 | $name = "@" . $block->name; |
||
| 202 | if (!empty($block->value)) { |
||
| 203 | $name .= " " . $this->compileValue($this->reduce($block->value)); |
||
| 204 | } |
||
| 205 | |||
| 206 | $this->compileNestedBlock($block, array($name)); |
||
| 207 | break; |
||
| 208 | default: |
||
| 209 | $this->throwError("unknown block type: $block->type\n"); |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | protected function compileCSSBlock($block) { |
||
| 214 | $env = $this->pushEnv(); |
||
| 215 | |||
| 216 | $selectors = $this->compileSelectors($block->tags); |
||
| 217 | $env->selectors = $this->multiplySelectors($selectors); |
||
| 218 | $out = $this->makeOutputBlock(null, $env->selectors); |
||
| 219 | |||
| 220 | $this->scope->children[] = $out; |
||
| 221 | $this->compileProps($block, $out); |
||
| 222 | |||
| 223 | $block->scope = $env; // mixins carry scope with them! |
||
| 224 | $this->popEnv(); |
||
| 225 | } |
||
| 226 | |||
| 227 | protected function compileMedia($media) { |
||
| 228 | $env = $this->pushEnv($media); |
||
| 229 | $parentScope = $this->mediaParent($this->scope); |
||
| 230 | |||
| 231 | $query = $this->compileMediaQuery($this->multiplyMedia($env)); |
||
| 232 | |||
| 233 | $this->scope = $this->makeOutputBlock($media->type, array($query)); |
||
| 234 | $parentScope->children[] = $this->scope; |
||
| 235 | |||
| 236 | $this->compileProps($media, $this->scope); |
||
| 237 | |||
| 238 | if (count($this->scope->lines) > 0) { |
||
| 239 | $orphanSelelectors = $this->findClosestSelectors(); |
||
| 240 | if (!is_null($orphanSelelectors)) { |
||
| 241 | $orphan = $this->makeOutputBlock(null, $orphanSelelectors); |
||
| 242 | $orphan->lines = $this->scope->lines; |
||
| 243 | array_unshift($this->scope->children, $orphan); |
||
| 244 | $this->scope->lines = array(); |
||
| 245 | } |
||
| 246 | } |
||
| 247 | |||
| 248 | $this->scope = $this->scope->parent; |
||
| 249 | $this->popEnv(); |
||
| 250 | } |
||
| 251 | |||
| 252 | View Code Duplication | protected function mediaParent($scope) { |
|
| 262 | |||
| 263 | View Code Duplication | protected function compileNestedBlock($block, $selectors) { |
|
| 264 | $this->pushEnv($block); |
||
| 265 | $this->scope = $this->makeOutputBlock($block->type, $selectors); |
||
| 266 | $this->scope->parent->children[] = $this->scope; |
||
| 267 | |||
| 268 | $this->compileProps($block, $this->scope); |
||
| 269 | |||
| 270 | $this->scope = $this->scope->parent; |
||
| 271 | $this->popEnv(); |
||
| 272 | } |
||
| 273 | |||
| 274 | protected function compileRoot($root) { |
||
| 275 | $this->pushEnv(); |
||
| 276 | $this->scope = $this->makeOutputBlock($root->type); |
||
| 277 | $this->compileProps($root, $this->scope); |
||
| 278 | $this->popEnv(); |
||
| 279 | } |
||
| 280 | |||
| 281 | protected function compileProps($block, $out) { |
||
| 282 | foreach ($this->sortProps($block->props) as $prop) { |
||
| 283 | $this->compileProp($prop, $block, $out); |
||
| 284 | } |
||
| 285 | $out->lines = $this->deduplicate($out->lines); |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Deduplicate lines in a block. Comments are not deduplicated. If a |
||
| 290 | * duplicate rule is detected, the comments immediately preceding each |
||
| 291 | * occurence are consolidated. |
||
| 292 | */ |
||
| 293 | protected function deduplicate($lines) { |
||
| 310 | |||
| 311 | protected function sortProps($props, $split = false) { |
||
| 312 | $vars = array(); |
||
| 313 | $imports = array(); |
||
| 314 | $other = array(); |
||
| 315 | $stack = array(); |
||
| 316 | |||
| 317 | foreach ($props as $prop) { |
||
| 318 | switch ($prop[0]) { |
||
| 319 | case "comment": |
||
| 320 | $stack[] = $prop; |
||
| 321 | break; |
||
| 322 | case "assign": |
||
| 323 | $stack[] = $prop; |
||
| 324 | if (isset($prop[1][0]) && $prop[1][0] == $this->vPrefix) { |
||
| 325 | $vars = array_merge($vars, $stack); |
||
| 326 | } else { |
||
| 327 | $other = array_merge($other, $stack); |
||
| 328 | } |
||
| 329 | $stack = array(); |
||
| 330 | break; |
||
| 331 | case "import": |
||
| 332 | $id = self::$nextImportId++; |
||
| 333 | $prop[] = $id; |
||
| 334 | $stack[] = $prop; |
||
| 335 | $imports = array_merge($imports, $stack); |
||
| 336 | $other[] = array("import_mixin", $id); |
||
| 337 | $stack = array(); |
||
| 338 | break; |
||
| 339 | default: |
||
| 340 | $stack[] = $prop; |
||
| 341 | $other = array_merge($other, $stack); |
||
| 342 | $stack = array(); |
||
| 343 | break; |
||
| 344 | } |
||
| 345 | } |
||
| 346 | $other = array_merge($other, $stack); |
||
| 347 | |||
| 348 | if ($split) { |
||
| 349 | return array(array_merge($imports, $vars), $other); |
||
| 350 | } else { |
||
| 351 | return array_merge($imports, $vars, $other); |
||
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | protected function compileMediaQuery($queries) { |
||
| 356 | $compiledQueries = array(); |
||
| 357 | foreach ($queries as $query) { |
||
| 358 | $parts = array(); |
||
| 359 | foreach ($query as $q) { |
||
| 360 | switch ($q[0]) { |
||
| 361 | case "mediaType": |
||
| 362 | $parts[] = implode(" ", array_slice($q, 1)); |
||
| 363 | break; |
||
| 364 | case "mediaExp": |
||
| 365 | if (isset($q[2])) { |
||
| 366 | $parts[] = "($q[1]: " . |
||
| 367 | $this->compileValue($this->reduce($q[2])) . ")"; |
||
| 368 | } else { |
||
| 369 | $parts[] = "($q[1])"; |
||
| 370 | } |
||
| 371 | break; |
||
| 372 | case "variable": |
||
| 373 | $parts[] = $this->compileValue($this->reduce($q)); |
||
| 374 | break; |
||
| 375 | } |
||
| 376 | } |
||
| 377 | |||
| 378 | if (count($parts) > 0) { |
||
| 379 | $compiledQueries[] = implode(" and ", $parts); |
||
| 380 | } |
||
| 381 | } |
||
| 382 | |||
| 383 | $out = "@media"; |
||
| 384 | if (!empty($parts)) { |
||
| 385 | $out .= " " . |
||
| 386 | implode($this->formatter->selectorSeparator, $compiledQueries); |
||
| 387 | } |
||
| 388 | return $out; |
||
| 389 | } |
||
| 390 | |||
| 391 | View Code Duplication | protected function multiplyMedia($env, $childQueries = null) { |
|
| 392 | if (is_null($env) || |
||
| 393 | !empty($env->block->type) && $env->block->type != "media") |
||
| 394 | { |
||
| 395 | return $childQueries; |
||
| 396 | } |
||
| 397 | |||
| 398 | // plain old block, skip |
||
| 399 | if (empty($env->block->type)) { |
||
| 400 | return $this->multiplyMedia($env->parent, $childQueries); |
||
| 401 | } |
||
| 402 | |||
| 403 | $out = array(); |
||
| 404 | $queries = $env->block->queries; |
||
| 405 | if (is_null($childQueries)) { |
||
| 406 | $out = $queries; |
||
| 407 | } else { |
||
| 408 | foreach ($queries as $parent) { |
||
| 409 | foreach ($childQueries as $child) { |
||
| 410 | $out[] = array_merge($parent, $child); |
||
| 411 | } |
||
| 412 | } |
||
| 413 | } |
||
| 414 | |||
| 415 | return $this->multiplyMedia($env->parent, $out); |
||
| 416 | } |
||
| 417 | |||
| 418 | protected function expandParentSelectors(&$tag, $replace) { |
||
| 419 | $parts = explode("$&$", $tag); |
||
| 420 | $count = 0; |
||
| 421 | foreach ($parts as &$part) { |
||
| 422 | $part = str_replace($this->parentSelector, $replace, $part, $c); |
||
| 423 | $count += $c; |
||
| 424 | } |
||
| 425 | $tag = implode($this->parentSelector, $parts); |
||
| 426 | return $count; |
||
| 427 | } |
||
| 428 | |||
| 429 | protected function findClosestSelectors() { |
||
| 430 | $env = $this->env; |
||
| 431 | $selectors = null; |
||
| 432 | while ($env !== null) { |
||
| 433 | if (isset($env->selectors)) { |
||
| 434 | $selectors = $env->selectors; |
||
| 435 | break; |
||
| 436 | } |
||
| 437 | $env = $env->parent; |
||
| 438 | } |
||
| 439 | |||
| 440 | return $selectors; |
||
| 441 | } |
||
| 442 | |||
| 443 | |||
| 444 | // multiply $selectors against the nearest selectors in env |
||
| 445 | protected function multiplySelectors($selectors) { |
||
| 446 | // find parent selectors |
||
| 447 | |||
| 448 | $parentSelectors = $this->findClosestSelectors(); |
||
| 449 | if (is_null($parentSelectors)) { |
||
| 450 | // kill parent reference in top level selector |
||
| 451 | foreach ($selectors as &$s) { |
||
| 452 | $this->expandParentSelectors($s, ""); |
||
| 453 | } |
||
| 454 | |||
| 455 | return $selectors; |
||
| 456 | } |
||
| 457 | |||
| 458 | $out = array(); |
||
| 459 | foreach ($parentSelectors as $parent) { |
||
| 460 | foreach ($selectors as $child) { |
||
| 461 | $count = $this->expandParentSelectors($child, $parent); |
||
| 462 | |||
| 463 | // don't prepend the parent tag if & was used |
||
| 464 | if ($count > 0) { |
||
| 465 | $out[] = trim($child); |
||
| 466 | } else { |
||
| 467 | $out[] = trim($parent . ' ' . $child); |
||
| 468 | } |
||
| 469 | } |
||
| 470 | } |
||
| 471 | |||
| 472 | return $out; |
||
| 473 | } |
||
| 474 | |||
| 475 | // reduces selector expressions |
||
| 476 | protected function compileSelectors($selectors) { |
||
| 477 | $out = array(); |
||
| 478 | |||
| 479 | foreach ($selectors as $s) { |
||
| 480 | if (is_array($s)) { |
||
| 481 | list(, $value) = $s; |
||
| 482 | $out[] = trim($this->compileValue($this->reduce($value))); |
||
| 483 | } else { |
||
| 484 | $out[] = $s; |
||
| 485 | } |
||
| 486 | } |
||
| 487 | |||
| 488 | return $out; |
||
| 489 | } |
||
| 490 | |||
| 491 | protected function eq($left, $right) { |
||
| 494 | |||
| 495 | protected function patternMatch($block, $orderedArgs, $keywordArgs) { |
||
| 496 | // match the guards if it has them |
||
| 497 | // any one of the groups must have all its guards pass for a match |
||
| 498 | if (!empty($block->guards)) { |
||
| 499 | $groupPassed = false; |
||
| 500 | foreach ($block->guards as $guardGroup) { |
||
| 501 | foreach ($guardGroup as $guard) { |
||
| 502 | $this->pushEnv(); |
||
| 503 | $this->zipSetArgs($block->args, $orderedArgs, $keywordArgs); |
||
| 504 | |||
| 505 | $negate = false; |
||
| 506 | if ($guard[0] == "negate") { |
||
| 507 | $guard = $guard[1]; |
||
| 508 | $negate = true; |
||
| 509 | } |
||
| 510 | |||
| 511 | $passed = $this->reduce($guard) == self::$TRUE; |
||
| 512 | if ($negate) $passed = !$passed; |
||
| 513 | |||
| 514 | $this->popEnv(); |
||
| 515 | |||
| 516 | if ($passed) { |
||
| 517 | $groupPassed = true; |
||
| 518 | } else { |
||
| 519 | $groupPassed = false; |
||
| 520 | break; |
||
| 521 | } |
||
| 522 | } |
||
| 523 | |||
| 524 | if ($groupPassed) break; |
||
| 525 | } |
||
| 526 | |||
| 527 | if (!$groupPassed) { |
||
| 528 | return false; |
||
| 529 | } |
||
| 530 | } |
||
| 531 | |||
| 532 | if (empty($block->args)) { |
||
| 533 | return $block->isVararg || empty($orderedArgs) && empty($keywordArgs); |
||
| 534 | } |
||
| 535 | |||
| 536 | $remainingArgs = $block->args; |
||
| 537 | if ($keywordArgs) { |
||
| 538 | $remainingArgs = array(); |
||
| 539 | foreach ($block->args as $arg) { |
||
| 540 | if ($arg[0] == "arg" && isset($keywordArgs[$arg[1]])) { |
||
| 541 | continue; |
||
| 542 | } |
||
| 543 | |||
| 544 | $remainingArgs[] = $arg; |
||
| 545 | } |
||
| 546 | } |
||
| 547 | |||
| 548 | $i = -1; // no args |
||
| 549 | // try to match by arity or by argument literal |
||
| 550 | foreach ($remainingArgs as $i => $arg) { |
||
| 551 | switch ($arg[0]) { |
||
| 552 | case "lit": |
||
| 553 | if (empty($orderedArgs[$i]) || !$this->eq($arg[1], $orderedArgs[$i])) { |
||
| 554 | return false; |
||
| 555 | } |
||
| 556 | break; |
||
| 557 | case "arg": |
||
| 558 | // no arg and no default value |
||
| 559 | if (!isset($orderedArgs[$i]) && !isset($arg[2])) { |
||
| 560 | return false; |
||
| 561 | } |
||
| 562 | break; |
||
| 563 | case "rest": |
||
| 564 | $i--; // rest can be empty |
||
| 565 | break 2; |
||
| 566 | } |
||
| 567 | } |
||
| 568 | |||
| 569 | if ($block->isVararg) { |
||
| 570 | return true; // not having enough is handled above |
||
| 571 | } else { |
||
| 572 | $numMatched = $i + 1; |
||
| 573 | // greater than becuase default values always match |
||
| 574 | return $numMatched >= count($orderedArgs); |
||
| 575 | } |
||
| 576 | } |
||
| 577 | |||
| 578 | protected function patternMatchAll($blocks, $orderedArgs, $keywordArgs, $skip=array()) { |
||
| 579 | $matches = null; |
||
| 580 | foreach ($blocks as $block) { |
||
| 581 | // skip seen blocks that don't have arguments |
||
| 582 | if (isset($skip[$block->id]) && !isset($block->args)) { |
||
| 583 | continue; |
||
| 584 | } |
||
| 585 | |||
| 586 | if ($this->patternMatch($block, $orderedArgs, $keywordArgs)) { |
||
| 587 | $matches[] = $block; |
||
| 588 | } |
||
| 589 | } |
||
| 590 | |||
| 591 | return $matches; |
||
| 592 | } |
||
| 593 | |||
| 594 | // attempt to find blocks matched by path and args |
||
| 595 | protected function findBlocks($searchIn, $path, $orderedArgs, $keywordArgs, $seen=array()) { |
||
| 596 | if ($searchIn == null) return null; |
||
| 597 | if (isset($seen[$searchIn->id])) return null; |
||
| 598 | $seen[$searchIn->id] = true; |
||
| 599 | |||
| 600 | $name = $path[0]; |
||
| 601 | |||
| 602 | if (isset($searchIn->children[$name])) { |
||
| 603 | $blocks = $searchIn->children[$name]; |
||
| 604 | if (count($path) == 1) { |
||
| 605 | $matches = $this->patternMatchAll($blocks, $orderedArgs, $keywordArgs, $seen); |
||
| 606 | if (!empty($matches)) { |
||
| 607 | // This will return all blocks that match in the closest |
||
| 608 | // scope that has any matching block, like lessjs |
||
| 609 | return $matches; |
||
| 610 | } |
||
| 611 | } else { |
||
| 612 | $matches = array(); |
||
| 613 | foreach ($blocks as $subBlock) { |
||
| 614 | $subMatches = $this->findBlocks($subBlock, |
||
| 615 | array_slice($path, 1), $orderedArgs, $keywordArgs, $seen); |
||
| 616 | |||
| 617 | if (!is_null($subMatches)) { |
||
| 618 | foreach ($subMatches as $sm) { |
||
| 619 | $matches[] = $sm; |
||
| 620 | } |
||
| 621 | } |
||
| 622 | } |
||
| 623 | |||
| 624 | return count($matches) > 0 ? $matches : null; |
||
| 625 | } |
||
| 626 | } |
||
| 627 | if ($searchIn->parent === $searchIn) return null; |
||
| 628 | return $this->findBlocks($searchIn->parent, $path, $orderedArgs, $keywordArgs, $seen); |
||
| 629 | } |
||
| 630 | |||
| 631 | // sets all argument names in $args to either the default value |
||
| 632 | // or the one passed in through $values |
||
| 633 | protected function zipSetArgs($args, $orderedValues, $keywordValues) { |
||
| 634 | $assignedValues = array(); |
||
| 635 | |||
| 636 | $i = 0; |
||
| 637 | foreach ($args as $a) { |
||
| 638 | if ($a[0] == "arg") { |
||
| 639 | if (isset($keywordValues[$a[1]])) { |
||
| 640 | // has keyword arg |
||
| 641 | $value = $keywordValues[$a[1]]; |
||
| 642 | } elseif (isset($orderedValues[$i])) { |
||
| 643 | // has ordered arg |
||
| 644 | $value = $orderedValues[$i]; |
||
| 645 | $i++; |
||
| 646 | } elseif (isset($a[2])) { |
||
| 647 | // has default value |
||
| 648 | $value = $a[2]; |
||
| 649 | } else { |
||
| 650 | $this->throwError("Failed to assign arg " . $a[1]); |
||
| 651 | $value = null; // :( |
||
| 652 | } |
||
| 653 | |||
| 654 | $value = $this->reduce($value); |
||
| 655 | $this->set($a[1], $value); |
||
| 656 | $assignedValues[] = $value; |
||
| 657 | } else { |
||
| 658 | // a lit |
||
| 659 | $i++; |
||
| 660 | } |
||
| 661 | } |
||
| 662 | |||
| 663 | // check for a rest |
||
| 664 | $last = end($args); |
||
| 665 | if ($last[0] == "rest") { |
||
| 666 | $rest = array_slice($orderedValues, count($args) - 1); |
||
| 667 | $this->set($last[1], $this->reduce(array("list", " ", $rest))); |
||
| 668 | } |
||
| 669 | |||
| 670 | // wow is this the only true use of PHP's + operator for arrays? |
||
| 671 | $this->env->arguments = $assignedValues + $orderedValues; |
||
| 672 | } |
||
| 673 | |||
| 674 | // compile a prop and update $lines or $blocks appropriately |
||
| 675 | protected function compileProp($prop, $block, $out) { |
||
| 809 | |||
| 810 | |||
| 811 | /** |
||
| 812 | * Compiles a primitive value into a CSS property value. |
||
| 813 | * |
||
| 814 | * Values in lessphp are typed by being wrapped in arrays, their format is |
||
| 815 | * typically: |
||
| 816 | * |
||
| 817 | * array(type, contents [, additional_contents]*) |
||
| 818 | * |
||
| 819 | * The input is expected to be reduced. This function will not work on |
||
| 820 | * things like expressions and variables. |
||
| 821 | */ |
||
| 822 | public function compileValue($value) { |
||
| 823 | switch ($value[0]) { |
||
| 824 | case 'list': |
||
| 825 | // [1] - delimiter |
||
| 826 | // [2] - array of values |
||
| 827 | return implode($value[1], array_map(array($this, 'compileValue'), $value[2])); |
||
| 828 | case 'raw_color': |
||
| 829 | if (!empty($this->formatter->compressColors)) { |
||
| 830 | return $this->compileValue($this->coerceColor($value)); |
||
| 831 | } |
||
| 832 | return $value[1]; |
||
| 833 | case 'keyword': |
||
| 834 | // [1] - the keyword |
||
| 835 | return $value[1]; |
||
| 836 | case 'number': |
||
| 837 | list(, $num, $unit) = $value; |
||
| 838 | // [1] - the number |
||
| 839 | // [2] - the unit |
||
| 840 | if ($this->numberPrecision !== null) { |
||
| 841 | $num = round($num, $this->numberPrecision); |
||
| 842 | } |
||
| 843 | return $num . $unit; |
||
| 844 | case 'string': |
||
| 845 | // [1] - contents of string (includes quotes) |
||
| 846 | list(, $delim, $content) = $value; |
||
| 847 | foreach ($content as &$part) { |
||
| 848 | if (is_array($part)) { |
||
| 849 | $part = $this->compileValue($part); |
||
| 850 | } |
||
| 851 | } |
||
| 852 | return $delim . implode($content) . $delim; |
||
| 853 | case 'color': |
||
| 854 | // [1] - red component (either number or a %) |
||
| 855 | // [2] - green component |
||
| 856 | // [3] - blue component |
||
| 857 | // [4] - optional alpha component |
||
| 858 | list(, $r, $g, $b) = $value; |
||
| 859 | $r = round($r); |
||
| 860 | $g = round($g); |
||
| 861 | $b = round($b); |
||
| 862 | |||
| 863 | View Code Duplication | if (count($value) == 5 && $value[4] != 1) { // rgba |
|
| 864 | return 'rgba('.$r.','.$g.','.$b.','.$value[4].')'; |
||
| 865 | } |
||
| 866 | |||
| 867 | $h = sprintf("#%02x%02x%02x", $r, $g, $b); |
||
| 868 | |||
| 869 | View Code Duplication | if (!empty($this->formatter->compressColors)) { |
|
| 870 | // Converting hex color to short notation (e.g. #003399 to #039) |
||
| 871 | if ($h[1] === $h[2] && $h[3] === $h[4] && $h[5] === $h[6]) { |
||
| 872 | $h = '#' . $h[1] . $h[3] . $h[5]; |
||
| 873 | } |
||
| 874 | } |
||
| 875 | |||
| 876 | return $h; |
||
| 877 | |||
| 878 | case 'function': |
||
| 879 | list(, $name, $args) = $value; |
||
| 880 | return $name.'('.$this->compileValue($args).')'; |
||
| 881 | default: // assumed to be unit |
||
| 882 | $this->throwError("unknown value type: $value[0]"); |
||
| 883 | } |
||
| 884 | } |
||
| 885 | |||
| 886 | protected function lib_pow($args) { |
||
| 890 | |||
| 891 | protected function lib_pi() { |
||
| 894 | |||
| 895 | protected function lib_mod($args) { |
||
| 899 | |||
| 900 | protected function lib_tan($num) { |
||
| 903 | |||
| 904 | protected function lib_sin($num) { |
||
| 907 | |||
| 908 | protected function lib_cos($num) { |
||
| 911 | |||
| 912 | protected function lib_atan($num) { |
||
| 916 | |||
| 917 | protected function lib_asin($num) { |
||
| 921 | |||
| 922 | protected function lib_acos($num) { |
||
| 926 | |||
| 927 | protected function lib_sqrt($num) { |
||
| 930 | |||
| 931 | protected function lib_extract($value) { |
||
| 932 | list($list, $idx) = $this->assertArgs($value, 2, "extract"); |
||
| 933 | $idx = $this->assertNumber($idx); |
||
| 934 | // 1 indexed |
||
| 935 | if ($list[0] == "list" && isset($list[2][$idx - 1])) { |
||
| 936 | return $list[2][$idx - 1]; |
||
| 937 | } |
||
| 938 | } |
||
| 939 | |||
| 940 | protected function lib_isnumber($value) { |
||
| 943 | |||
| 944 | protected function lib_isstring($value) { |
||
| 947 | |||
| 948 | protected function lib_iscolor($value) { |
||
| 951 | |||
| 952 | protected function lib_iskeyword($value) { |
||
| 955 | |||
| 956 | protected function lib_ispixel($value) { |
||
| 959 | |||
| 960 | protected function lib_ispercentage($value) { |
||
| 963 | |||
| 964 | protected function lib_isem($value) { |
||
| 967 | |||
| 968 | protected function lib_isrem($value) { |
||
| 971 | |||
| 972 | protected function lib_rgbahex($color) { |
||
| 973 | $color = $this->coerceColor($color); |
||
| 974 | if (is_null($color)) |
||
| 975 | $this->throwError("color expected for rgbahex"); |
||
| 976 | |||
| 977 | return sprintf("#%02x%02x%02x%02x", |
||
| 978 | isset($color[4]) ? $color[4]*255 : 255, |
||
| 979 | $color[1],$color[2], $color[3]); |
||
| 980 | } |
||
| 981 | |||
| 982 | protected function lib_argb($color){ |
||
| 985 | |||
| 986 | /** |
||
| 987 | * Given an url, decide whether to output a regular link or the base64-encoded contents of the file |
||
| 988 | * |
||
| 989 | * @param array $value either an argument list (two strings) or a single string |
||
| 990 | * @return string formatted url(), either as a link or base64-encoded |
||
| 991 | */ |
||
| 992 | protected function lib_data_uri($value) { |
||
| 993 | $mime = ($value[0] === 'list') ? $value[2][0][2] : null; |
||
| 994 | $url = ($value[0] === 'list') ? $value[2][1][2][0] : $value[2][0]; |
||
| 995 | |||
| 996 | $fullpath = $this->findImport($url); |
||
| 997 | |||
| 998 | if($fullpath && ($fsize = filesize($fullpath)) !== false) { |
||
| 999 | // IE8 can't handle data uris larger than 32KB |
||
| 1000 | if($fsize/1024 < 32) { |
||
| 1001 | View Code Duplication | if(is_null($mime)) { |
|
| 1002 | if(class_exists('finfo')) { // php 5.3+ |
||
| 1003 | $finfo = new finfo(FILEINFO_MIME); |
||
| 1004 | $mime = explode('; ', $finfo->file($fullpath)); |
||
| 1005 | $mime = $mime[0]; |
||
| 1006 | } elseif(function_exists('mime_content_type')) { // PHP 5.2 |
||
| 1007 | $mime = mime_content_type($fullpath); |
||
| 1008 | } |
||
| 1009 | } |
||
| 1010 | |||
| 1011 | if(!is_null($mime)) // fallback if the mime type is still unknown |
||
| 1012 | $url = sprintf('data:%s;base64,%s', $mime, base64_encode(file_get_contents($fullpath))); |
||
| 1013 | } |
||
| 1014 | } |
||
| 1015 | |||
| 1016 | return 'url("'.$url.'")'; |
||
| 1017 | } |
||
| 1018 | |||
| 1019 | // utility func to unquote a string |
||
| 1020 | protected function lib_e($arg) { |
||
| 1021 | switch ($arg[0]) { |
||
| 1022 | case "list": |
||
| 1023 | $items = $arg[2]; |
||
| 1024 | if (isset($items[0])) { |
||
| 1025 | return $this->lib_e($items[0]); |
||
| 1026 | } |
||
| 1027 | $this->throwError("unrecognised input"); |
||
| 1028 | case "string": |
||
| 1029 | $arg[1] = ""; |
||
| 1030 | return $arg; |
||
| 1031 | case "keyword": |
||
| 1032 | return $arg; |
||
| 1033 | default: |
||
| 1034 | return array("keyword", $this->compileValue($arg)); |
||
| 1035 | } |
||
| 1036 | } |
||
| 1037 | |||
| 1038 | protected function lib__sprintf($args) { |
||
| 1039 | if ($args[0] != "list") return $args; |
||
| 1040 | $values = $args[2]; |
||
| 1041 | $string = array_shift($values); |
||
| 1042 | $template = $this->compileValue($this->lib_e($string)); |
||
| 1043 | |||
| 1044 | $i = 0; |
||
| 1045 | if (preg_match_all('/%[dsa]/', $template, $m)) { |
||
| 1046 | foreach ($m[0] as $match) { |
||
| 1047 | $val = isset($values[$i]) ? |
||
| 1048 | $this->reduce($values[$i]) : array('keyword', ''); |
||
| 1049 | |||
| 1050 | // lessjs compat, renders fully expanded color, not raw color |
||
| 1051 | if ($color = $this->coerceColor($val)) { |
||
| 1052 | $val = $color; |
||
| 1053 | } |
||
| 1054 | |||
| 1055 | $i++; |
||
| 1056 | $rep = $this->compileValue($this->lib_e($val)); |
||
| 1057 | $template = preg_replace('/'.self::preg_quote($match).'/', |
||
| 1058 | $rep, $template, 1); |
||
| 1059 | } |
||
| 1060 | } |
||
| 1061 | |||
| 1062 | $d = $string[0] == "string" ? $string[1] : '"'; |
||
| 1063 | return array("string", $d, array($template)); |
||
| 1064 | } |
||
| 1065 | |||
| 1066 | protected function lib_floor($arg) { |
||
| 1070 | |||
| 1071 | protected function lib_ceil($arg) { |
||
| 1075 | |||
| 1076 | protected function lib_round($arg) { |
||
| 1077 | if($arg[0] != "list") { |
||
| 1078 | $value = $this->assertNumber($arg); |
||
| 1079 | return array("number", round($value), $arg[2]); |
||
| 1080 | } else { |
||
| 1081 | $value = $this->assertNumber($arg[2][0]); |
||
| 1082 | $precision = $this->assertNumber($arg[2][1]); |
||
| 1083 | return array("number", round($value, $precision), $arg[2][0][2]); |
||
| 1084 | } |
||
| 1085 | } |
||
| 1086 | |||
| 1087 | protected function lib_unit($arg) { |
||
| 1088 | if ($arg[0] == "list") { |
||
| 1089 | list($number, $newUnit) = $arg[2]; |
||
| 1090 | return array("number", $this->assertNumber($number), |
||
| 1091 | $this->compileValue($this->lib_e($newUnit))); |
||
| 1092 | } else { |
||
| 1093 | return array("number", $this->assertNumber($arg), ""); |
||
| 1094 | } |
||
| 1095 | } |
||
| 1096 | |||
| 1097 | /** |
||
| 1098 | * Helper function to get arguments for color manipulation functions. |
||
| 1099 | * takes a list that contains a color like thing and a percentage |
||
| 1100 | */ |
||
| 1101 | public function colorArgs($args) { |
||
| 1102 | if ($args[0] != 'list' || count($args[2]) < 2) { |
||
| 1103 | return array(array('color', 0, 0, 0), 0); |
||
| 1104 | } |
||
| 1105 | list($color, $delta) = $args[2]; |
||
| 1106 | $color = $this->assertColor($color); |
||
| 1107 | $delta = floatval($delta[1]); |
||
| 1108 | |||
| 1109 | return array($color, $delta); |
||
| 1110 | } |
||
| 1111 | |||
| 1112 | View Code Duplication | protected function lib_darken($args) { |
|
| 1113 | list($color, $delta) = $this->colorArgs($args); |
||
| 1114 | |||
| 1115 | $hsl = $this->toHSL($color); |
||
| 1116 | $hsl[3] = $this->clamp($hsl[3] - $delta, 100); |
||
| 1117 | return $this->toRGB($hsl); |
||
| 1118 | } |
||
| 1119 | |||
| 1120 | View Code Duplication | protected function lib_lighten($args) { |
|
| 1121 | list($color, $delta) = $this->colorArgs($args); |
||
| 1122 | |||
| 1123 | $hsl = $this->toHSL($color); |
||
| 1124 | $hsl[3] = $this->clamp($hsl[3] + $delta, 100); |
||
| 1125 | return $this->toRGB($hsl); |
||
| 1126 | } |
||
| 1127 | |||
| 1128 | View Code Duplication | protected function lib_saturate($args) { |
|
| 1129 | list($color, $delta) = $this->colorArgs($args); |
||
| 1130 | |||
| 1131 | $hsl = $this->toHSL($color); |
||
| 1132 | $hsl[2] = $this->clamp($hsl[2] + $delta, 100); |
||
| 1133 | return $this->toRGB($hsl); |
||
| 1134 | } |
||
| 1135 | |||
| 1136 | View Code Duplication | protected function lib_desaturate($args) { |
|
| 1137 | list($color, $delta) = $this->colorArgs($args); |
||
| 1138 | |||
| 1139 | $hsl = $this->toHSL($color); |
||
| 1140 | $hsl[2] = $this->clamp($hsl[2] - $delta, 100); |
||
| 1141 | return $this->toRGB($hsl); |
||
| 1142 | } |
||
| 1143 | |||
| 1144 | protected function lib_spin($args) { |
||
| 1145 | list($color, $delta) = $this->colorArgs($args); |
||
| 1146 | |||
| 1147 | $hsl = $this->toHSL($color); |
||
| 1148 | |||
| 1149 | $hsl[1] = $hsl[1] + $delta % 360; |
||
| 1150 | if ($hsl[1] < 0) $hsl[1] += 360; |
||
| 1151 | |||
| 1152 | return $this->toRGB($hsl); |
||
| 1153 | } |
||
| 1154 | |||
| 1155 | protected function lib_fadeout($args) { |
||
| 1156 | list($color, $delta) = $this->colorArgs($args); |
||
| 1157 | $color[4] = $this->clamp((isset($color[4]) ? $color[4] : 1) - $delta/100); |
||
| 1158 | return $color; |
||
| 1159 | } |
||
| 1160 | |||
| 1161 | protected function lib_fadein($args) { |
||
| 1162 | list($color, $delta) = $this->colorArgs($args); |
||
| 1163 | $color[4] = $this->clamp((isset($color[4]) ? $color[4] : 1) + $delta/100); |
||
| 1164 | return $color; |
||
| 1165 | } |
||
| 1166 | |||
| 1167 | protected function lib_hue($color) { |
||
| 1171 | |||
| 1172 | protected function lib_saturation($color) { |
||
| 1176 | |||
| 1177 | protected function lib_lightness($color) { |
||
| 1181 | |||
| 1182 | // get the alpha of a color |
||
| 1183 | // defaults to 1 for non-colors or colors without an alpha |
||
| 1184 | protected function lib_alpha($value) { |
||
| 1185 | if (!is_null($color = $this->coerceColor($value))) { |
||
| 1186 | return isset($color[4]) ? $color[4] : 1; |
||
| 1187 | } |
||
| 1188 | } |
||
| 1189 | |||
| 1190 | // set the alpha of the color |
||
| 1191 | protected function lib_fade($args) { |
||
| 1192 | list($color, $alpha) = $this->colorArgs($args); |
||
| 1193 | $color[4] = $this->clamp($alpha / 100.0); |
||
| 1194 | return $color; |
||
| 1195 | } |
||
| 1196 | |||
| 1197 | protected function lib_percentage($arg) { |
||
| 1201 | |||
| 1202 | // mixes two colors by weight |
||
| 1203 | // mix(@color1, @color2, [@weight: 50%]); |
||
| 1204 | // http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html#mix-instance_method |
||
| 1205 | protected function lib_mix($args) { |
||
| 1206 | if ($args[0] != "list" || count($args[2]) < 2) |
||
| 1207 | $this->throwError("mix expects (color1, color2, weight)"); |
||
| 1208 | |||
| 1209 | list($first, $second) = $args[2]; |
||
| 1210 | $first = $this->assertColor($first); |
||
| 1211 | $second = $this->assertColor($second); |
||
| 1212 | |||
| 1213 | $first_a = $this->lib_alpha($first); |
||
| 1214 | $second_a = $this->lib_alpha($second); |
||
| 1215 | |||
| 1216 | if (isset($args[2][2])) { |
||
| 1217 | $weight = $args[2][2][1] / 100.0; |
||
| 1218 | } else { |
||
| 1219 | $weight = 0.5; |
||
| 1220 | } |
||
| 1221 | |||
| 1222 | $w = $weight * 2 - 1; |
||
| 1223 | $a = $first_a - $second_a; |
||
| 1224 | |||
| 1225 | $w1 = (($w * $a == -1 ? $w : ($w + $a)/(1 + $w * $a)) + 1) / 2.0; |
||
| 1226 | $w2 = 1.0 - $w1; |
||
| 1227 | |||
| 1228 | $new = array('color', |
||
| 1229 | $w1 * $first[1] + $w2 * $second[1], |
||
| 1230 | $w1 * $first[2] + $w2 * $second[2], |
||
| 1231 | $w1 * $first[3] + $w2 * $second[3], |
||
| 1232 | ); |
||
| 1233 | |||
| 1234 | if ($first_a != 1.0 || $second_a != 1.0) { |
||
| 1235 | $new[] = $first_a * $weight + $second_a * ($weight - 1); |
||
| 1236 | } |
||
| 1237 | |||
| 1238 | return $this->fixColor($new); |
||
| 1239 | } |
||
| 1240 | |||
| 1241 | protected function lib_contrast($args) { |
||
| 1242 | $darkColor = array('color', 0, 0, 0); |
||
| 1243 | $lightColor = array('color', 255, 255, 255); |
||
| 1244 | $threshold = 0.43; |
||
| 1245 | |||
| 1246 | if ( $args[0] == 'list' ) { |
||
| 1247 | $inputColor = ( isset($args[2][0]) ) ? $this->assertColor($args[2][0]) : $lightColor; |
||
| 1248 | $darkColor = ( isset($args[2][1]) ) ? $this->assertColor($args[2][1]) : $darkColor; |
||
| 1249 | $lightColor = ( isset($args[2][2]) ) ? $this->assertColor($args[2][2]) : $lightColor; |
||
| 1250 | $threshold = ( isset($args[2][3]) ) ? $this->assertNumber($args[2][3]) : $threshold; |
||
| 1251 | } |
||
| 1252 | else { |
||
| 1253 | $inputColor = $this->assertColor($args); |
||
| 1254 | } |
||
| 1255 | |||
| 1256 | $inputColor = $this->coerceColor($inputColor); |
||
| 1257 | $darkColor = $this->coerceColor($darkColor); |
||
| 1258 | $lightColor = $this->coerceColor($lightColor); |
||
| 1259 | |||
| 1260 | //Figure out which is actually light and dark! |
||
| 1261 | if ( $this->lib_luma($darkColor) > $this->lib_luma($lightColor) ) { |
||
| 1262 | $t = $lightColor; |
||
| 1263 | $lightColor = $darkColor; |
||
| 1264 | $darkColor = $t; |
||
| 1265 | } |
||
| 1266 | |||
| 1267 | $inputColor_alpha = $this->lib_alpha($inputColor); |
||
| 1268 | if ( ( $this->lib_luma($inputColor) * $inputColor_alpha) < $threshold) { |
||
| 1269 | return $lightColor; |
||
| 1270 | } |
||
| 1271 | return $darkColor; |
||
| 1272 | } |
||
| 1273 | |||
| 1274 | protected function lib_luma($color) { |
||
| 1278 | |||
| 1279 | |||
| 1280 | public function assertColor($value, $error = "expected color value") { |
||
| 1281 | $color = $this->coerceColor($value); |
||
| 1282 | if (is_null($color)) $this->throwError($error); |
||
| 1283 | return $color; |
||
| 1284 | } |
||
| 1285 | |||
| 1286 | public function assertNumber($value, $error = "expecting number") { |
||
| 1290 | |||
| 1291 | public function assertArgs($value, $expectedArgs, $name="") { |
||
| 1292 | if ($expectedArgs == 1) { |
||
| 1293 | return $value; |
||
| 1294 | } else { |
||
| 1295 | if ($value[0] !== "list" || $value[1] != ",") $this->throwError("expecting list"); |
||
| 1296 | $values = $value[2]; |
||
| 1297 | $numValues = count($values); |
||
| 1298 | if ($expectedArgs != $numValues) { |
||
| 1299 | if ($name) { |
||
| 1300 | $name = $name . ": "; |
||
| 1301 | } |
||
| 1302 | |||
| 1303 | $this->throwError("${name}expecting $expectedArgs arguments, got $numValues"); |
||
| 1304 | } |
||
| 1305 | |||
| 1306 | return $values; |
||
| 1307 | } |
||
| 1308 | } |
||
| 1309 | |||
| 1310 | protected function toHSL($color) { |
||
| 1311 | if ($color[0] == 'hsl') return $color; |
||
| 1312 | |||
| 1313 | $r = $color[1] / 255; |
||
| 1314 | $g = $color[2] / 255; |
||
| 1315 | $b = $color[3] / 255; |
||
| 1316 | |||
| 1317 | $min = min($r, $g, $b); |
||
| 1318 | $max = max($r, $g, $b); |
||
| 1319 | |||
| 1320 | $L = ($min + $max) / 2; |
||
| 1321 | if ($min == $max) { |
||
| 1322 | $S = $H = 0; |
||
| 1323 | } else { |
||
| 1324 | if ($L < 0.5) |
||
| 1325 | $S = ($max - $min)/($max + $min); |
||
| 1326 | else |
||
| 1327 | $S = ($max - $min)/(2.0 - $max - $min); |
||
| 1328 | |||
| 1329 | if ($r == $max) $H = ($g - $b)/($max - $min); |
||
| 1330 | elseif ($g == $max) $H = 2.0 + ($b - $r)/($max - $min); |
||
| 1331 | elseif ($b == $max) $H = 4.0 + ($r - $g)/($max - $min); |
||
| 1332 | |||
| 1333 | } |
||
| 1334 | |||
| 1335 | $out = array('hsl', |
||
| 1336 | ($H < 0 ? $H + 6 : $H)*60, |
||
| 1337 | $S*100, |
||
| 1338 | $L*100, |
||
| 1339 | ); |
||
| 1340 | |||
| 1341 | if (count($color) > 4) $out[] = $color[4]; // copy alpha |
||
| 1342 | return $out; |
||
| 1343 | } |
||
| 1344 | |||
| 1345 | protected function toRGB_helper($comp, $temp1, $temp2) { |
||
| 1346 | if ($comp < 0) $comp += 1.0; |
||
| 1347 | elseif ($comp > 1) $comp -= 1.0; |
||
| 1348 | |||
| 1349 | View Code Duplication | if (6 * $comp < 1) return $temp1 + ($temp2 - $temp1) * 6 * $comp; |
|
| 1350 | if (2 * $comp < 1) return $temp2; |
||
| 1351 | View Code Duplication | if (3 * $comp < 2) return $temp1 + ($temp2 - $temp1)*((2/3) - $comp) * 6; |
|
| 1352 | |||
| 1353 | return $temp1; |
||
| 1354 | } |
||
| 1355 | |||
| 1356 | /** |
||
| 1357 | * Converts a hsl array into a color value in rgb. |
||
| 1358 | * Expects H to be in range of 0 to 360, S and L in 0 to 100 |
||
| 1359 | */ |
||
| 1360 | protected function toRGB($color) { |
||
| 1361 | if ($color[0] == 'color') return $color; |
||
| 1362 | |||
| 1363 | $H = $color[1] / 360; |
||
| 1364 | $S = $color[2] / 100; |
||
| 1365 | $L = $color[3] / 100; |
||
| 1366 | |||
| 1367 | if ($S == 0) { |
||
| 1368 | $r = $g = $b = $L; |
||
| 1369 | } else { |
||
| 1370 | $temp2 = $L < 0.5 ? |
||
| 1371 | $L*(1.0 + $S) : |
||
| 1372 | $L + $S - $L * $S; |
||
| 1373 | |||
| 1374 | $temp1 = 2.0 * $L - $temp2; |
||
| 1375 | |||
| 1376 | $r = $this->toRGB_helper($H + 1/3, $temp1, $temp2); |
||
| 1377 | $g = $this->toRGB_helper($H, $temp1, $temp2); |
||
| 1378 | $b = $this->toRGB_helper($H - 1/3, $temp1, $temp2); |
||
| 1379 | } |
||
| 1380 | |||
| 1381 | // $out = array('color', round($r*255), round($g*255), round($b*255)); |
||
| 1382 | $out = array('color', $r*255, $g*255, $b*255); |
||
| 1383 | if (count($color) > 4) $out[] = $color[4]; // copy alpha |
||
| 1384 | return $out; |
||
| 1385 | } |
||
| 1386 | |||
| 1387 | protected function clamp($v, $max = 1, $min = 0) { |
||
| 1390 | |||
| 1391 | /** |
||
| 1392 | * Convert the rgb, rgba, hsl color literals of function type |
||
| 1393 | * as returned by the parser into values of color type. |
||
| 1394 | */ |
||
| 1395 | protected function funcToColor($func) { |
||
| 1396 | $fname = $func[1]; |
||
| 1397 | if ($func[2][0] != 'list') return false; // need a list of arguments |
||
| 1398 | $rawComponents = $func[2][2]; |
||
| 1399 | |||
| 1400 | if ($fname == 'hsl' || $fname == 'hsla') { |
||
| 1401 | $hsl = array('hsl'); |
||
| 1402 | $i = 0; |
||
| 1403 | foreach ($rawComponents as $c) { |
||
| 1404 | $val = $this->reduce($c); |
||
| 1405 | $val = isset($val[1]) ? floatval($val[1]) : 0; |
||
| 1406 | |||
| 1407 | if ($i == 0) $clamp = 360; |
||
| 1408 | elseif ($i < 3) $clamp = 100; |
||
| 1409 | else $clamp = 1; |
||
| 1410 | |||
| 1411 | $hsl[] = $this->clamp($val, $clamp); |
||
| 1412 | $i++; |
||
| 1413 | } |
||
| 1414 | |||
| 1415 | while (count($hsl) < 4) $hsl[] = 0; |
||
| 1416 | return $this->toRGB($hsl); |
||
| 1417 | |||
| 1418 | } elseif ($fname == 'rgb' || $fname == 'rgba') { |
||
| 1419 | $components = array(); |
||
| 1420 | $i = 1; |
||
| 1421 | foreach ($rawComponents as $c) { |
||
| 1422 | $c = $this->reduce($c); |
||
| 1423 | if ($i < 4) { |
||
| 1424 | View Code Duplication | if ($c[0] == "number" && $c[2] == "%") { |
|
| 1425 | $components[] = 255 * ($c[1] / 100); |
||
| 1426 | } else { |
||
| 1427 | $components[] = floatval($c[1]); |
||
| 1428 | } |
||
| 1429 | View Code Duplication | } elseif ($i == 4) { |
|
| 1430 | if ($c[0] == "number" && $c[2] == "%") { |
||
| 1431 | $components[] = 1.0 * ($c[1] / 100); |
||
| 1432 | } else { |
||
| 1433 | $components[] = floatval($c[1]); |
||
| 1434 | } |
||
| 1435 | } else break; |
||
| 1436 | |||
| 1437 | $i++; |
||
| 1438 | } |
||
| 1439 | while (count($components) < 3) $components[] = 0; |
||
| 1440 | array_unshift($components, 'color'); |
||
| 1441 | return $this->fixColor($components); |
||
| 1442 | } |
||
| 1443 | |||
| 1444 | return false; |
||
| 1445 | } |
||
| 1446 | |||
| 1447 | protected function reduce($value, $forExpression = false) { |
||
| 1448 | switch ($value[0]) { |
||
| 1449 | case "interpolate": |
||
| 1450 | $reduced = $this->reduce($value[1]); |
||
| 1451 | $var = $this->compileValue($reduced); |
||
| 1452 | $res = $this->reduce(array("variable", $this->vPrefix . $var)); |
||
| 1453 | |||
| 1454 | if ($res[0] == "raw_color") { |
||
| 1455 | $res = $this->coerceColor($res); |
||
| 1456 | } |
||
| 1457 | |||
| 1458 | if (empty($value[2])) $res = $this->lib_e($res); |
||
| 1459 | |||
| 1460 | return $res; |
||
| 1461 | case "variable": |
||
| 1462 | $key = $value[1]; |
||
| 1463 | if (is_array($key)) { |
||
| 1464 | $key = $this->reduce($key); |
||
| 1465 | $key = $this->vPrefix . $this->compileValue($this->lib_e($key)); |
||
| 1466 | } |
||
| 1467 | |||
| 1468 | $seen =& $this->env->seenNames; |
||
| 1469 | |||
| 1470 | if (!empty($seen[$key])) { |
||
| 1471 | $this->throwError("infinite loop detected: $key"); |
||
| 1472 | } |
||
| 1473 | |||
| 1474 | $seen[$key] = true; |
||
| 1475 | $out = $this->reduce($this->get($key)); |
||
| 1476 | $seen[$key] = false; |
||
| 1477 | return $out; |
||
| 1478 | case "list": |
||
| 1479 | foreach ($value[2] as &$item) { |
||
| 1480 | $item = $this->reduce($item, $forExpression); |
||
| 1481 | } |
||
| 1482 | return $value; |
||
| 1483 | case "expression": |
||
| 1484 | return $this->evaluate($value); |
||
| 1485 | case "string": |
||
| 1486 | foreach ($value[2] as &$part) { |
||
| 1487 | if (is_array($part)) { |
||
| 1488 | $strip = $part[0] == "variable"; |
||
| 1489 | $part = $this->reduce($part); |
||
| 1490 | if ($strip) $part = $this->lib_e($part); |
||
| 1491 | } |
||
| 1492 | } |
||
| 1493 | return $value; |
||
| 1494 | case "escape": |
||
| 1495 | list(,$inner) = $value; |
||
| 1496 | return $this->lib_e($this->reduce($inner)); |
||
| 1497 | case "function": |
||
| 1498 | $color = $this->funcToColor($value); |
||
| 1499 | if ($color) return $color; |
||
| 1500 | |||
| 1501 | list(, $name, $args) = $value; |
||
| 1502 | if ($name == "%") $name = "_sprintf"; |
||
| 1503 | |||
| 1504 | $f = isset($this->libFunctions[$name]) ? |
||
| 1505 | $this->libFunctions[$name] : array($this, 'lib_'.str_replace('-', '_', $name)); |
||
| 1506 | |||
| 1507 | if (is_callable($f)) { |
||
| 1508 | if ($args[0] == 'list') |
||
| 1509 | $args = self::compressList($args[2], $args[1]); |
||
| 1510 | |||
| 1511 | $ret = call_user_func($f, $this->reduce($args, true), $this); |
||
| 1512 | |||
| 1513 | if (is_null($ret)) { |
||
| 1514 | return array("string", "", array( |
||
| 1515 | $name, "(", $args, ")" |
||
| 1516 | )); |
||
| 1517 | } |
||
| 1518 | |||
| 1519 | // convert to a typed value if the result is a php primitive |
||
| 1520 | if (is_numeric($ret)) $ret = array('number', $ret, ""); |
||
| 1521 | elseif (!is_array($ret)) $ret = array('keyword', $ret); |
||
| 1522 | |||
| 1523 | return $ret; |
||
| 1524 | } |
||
| 1525 | |||
| 1526 | // plain function, reduce args |
||
| 1527 | $value[2] = $this->reduce($value[2]); |
||
| 1528 | return $value; |
||
| 1529 | case "unary": |
||
| 1530 | list(, $op, $exp) = $value; |
||
| 1531 | $exp = $this->reduce($exp); |
||
| 1532 | |||
| 1533 | View Code Duplication | if ($exp[0] == "number") { |
|
| 1534 | switch ($op) { |
||
| 1535 | case "+": |
||
| 1536 | return $exp; |
||
| 1537 | case "-": |
||
| 1538 | $exp[1] *= -1; |
||
| 1539 | return $exp; |
||
| 1540 | } |
||
| 1541 | } |
||
| 1542 | return array("string", "", array($op, $exp)); |
||
| 1543 | } |
||
| 1544 | |||
| 1545 | if ($forExpression) { |
||
| 1546 | switch ($value[0]) { |
||
| 1547 | case "keyword": |
||
| 1548 | if ($color = $this->coerceColor($value)) { |
||
| 1549 | return $color; |
||
| 1550 | } |
||
| 1551 | break; |
||
| 1552 | case "raw_color": |
||
| 1553 | return $this->coerceColor($value); |
||
| 1554 | } |
||
| 1555 | } |
||
| 1556 | |||
| 1557 | return $value; |
||
| 1558 | } |
||
| 1559 | |||
| 1560 | |||
| 1561 | // coerce a value for use in color operation |
||
| 1562 | protected function coerceColor($value) { |
||
| 1563 | switch($value[0]) { |
||
| 1564 | case 'color': return $value; |
||
| 1565 | case 'raw_color': |
||
| 1566 | $c = array("color", 0, 0, 0); |
||
| 1567 | $colorStr = substr($value[1], 1); |
||
| 1568 | $num = hexdec($colorStr); |
||
| 1569 | $width = strlen($colorStr) == 3 ? 16 : 256; |
||
| 1570 | |||
| 1571 | for ($i = 3; $i > 0; $i--) { // 3 2 1 |
||
| 1572 | $t = $num % $width; |
||
| 1573 | $num /= $width; |
||
| 1574 | |||
| 1575 | $c[$i] = $t * (256/$width) + $t * floor(16/$width); |
||
| 1576 | } |
||
| 1577 | |||
| 1578 | return $c; |
||
| 1579 | case 'keyword': |
||
| 1580 | $name = $value[1]; |
||
| 1581 | if (isset(self::$cssColors[$name])) { |
||
| 1582 | $rgba = explode(',', self::$cssColors[$name]); |
||
| 1583 | |||
| 1584 | if(isset($rgba[3])) |
||
| 1585 | return array('color', $rgba[0], $rgba[1], $rgba[2], $rgba[3]); |
||
| 1586 | |||
| 1587 | return array('color', $rgba[0], $rgba[1], $rgba[2]); |
||
| 1588 | } |
||
| 1589 | return null; |
||
| 1590 | } |
||
| 1591 | } |
||
| 1592 | |||
| 1593 | // make something string like into a string |
||
| 1594 | View Code Duplication | protected function coerceString($value) { |
|
| 1595 | switch ($value[0]) { |
||
| 1596 | case "string": |
||
| 1597 | return $value; |
||
| 1598 | case "keyword": |
||
| 1599 | return array("string", "", array($value[1])); |
||
| 1600 | } |
||
| 1601 | return null; |
||
| 1602 | } |
||
| 1603 | |||
| 1604 | // turn list of length 1 into value type |
||
| 1605 | protected function flattenList($value) { |
||
| 1606 | View Code Duplication | if ($value[0] == "list" && count($value[2]) == 1) { |
|
| 1607 | return $this->flattenList($value[2][0]); |
||
| 1608 | } |
||
| 1609 | return $value; |
||
| 1610 | } |
||
| 1611 | |||
| 1612 | public function toBool($a) { |
||
| 1616 | |||
| 1617 | // evaluate an expression |
||
| 1618 | protected function evaluate($exp) { |
||
| 1619 | list(, $op, $left, $right, $whiteBefore, $whiteAfter) = $exp; |
||
| 1620 | |||
| 1621 | $left = $this->reduce($left, true); |
||
| 1622 | $right = $this->reduce($right, true); |
||
| 1623 | |||
| 1624 | if ($leftColor = $this->coerceColor($left)) { |
||
| 1625 | $left = $leftColor; |
||
| 1626 | } |
||
| 1627 | |||
| 1628 | if ($rightColor = $this->coerceColor($right)) { |
||
| 1629 | $right = $rightColor; |
||
| 1630 | } |
||
| 1631 | |||
| 1632 | $ltype = $left[0]; |
||
| 1633 | $rtype = $right[0]; |
||
| 1634 | |||
| 1635 | // operators that work on all types |
||
| 1636 | if ($op == "and") { |
||
| 1637 | return $this->toBool($left == self::$TRUE && $right == self::$TRUE); |
||
| 1638 | } |
||
| 1639 | |||
| 1640 | if ($op == "=") { |
||
| 1641 | return $this->toBool($this->eq($left, $right) ); |
||
| 1642 | } |
||
| 1643 | |||
| 1644 | if ($op == "+" && !is_null($str = $this->stringConcatenate($left, $right))) { |
||
| 1645 | return $str; |
||
| 1646 | } |
||
| 1647 | |||
| 1648 | // type based operators |
||
| 1649 | $fname = "op_${ltype}_${rtype}"; |
||
| 1650 | if (is_callable(array($this, $fname))) { |
||
| 1651 | $out = $this->$fname($op, $left, $right); |
||
| 1652 | if (!is_null($out)) return $out; |
||
| 1653 | } |
||
| 1654 | |||
| 1655 | // make the expression look it did before being parsed |
||
| 1656 | $paddedOp = $op; |
||
| 1657 | if ($whiteBefore) $paddedOp = " " . $paddedOp; |
||
| 1658 | if ($whiteAfter) $paddedOp .= " "; |
||
| 1659 | |||
| 1660 | return array("string", "", array($left, $paddedOp, $right)); |
||
| 1661 | } |
||
| 1662 | |||
| 1663 | protected function stringConcatenate($left, $right) { |
||
| 1677 | |||
| 1678 | |||
| 1679 | // make sure a color's components don't go out of bounds |
||
| 1680 | View Code Duplication | protected function fixColor($c) { |
|
| 1688 | |||
| 1689 | protected function op_number_color($op, $lft, $rgt) { |
||
| 1690 | if ($op == '+' || $op == '*') { |
||
| 1691 | return $this->op_color_number($op, $rgt, $lft); |
||
| 1692 | } |
||
| 1693 | } |
||
| 1694 | |||
| 1695 | protected function op_color_number($op, $lft, $rgt) { |
||
| 1696 | if ($rgt[0] == '%') $rgt[1] /= 100; |
||
| 1697 | |||
| 1698 | return $this->op_color_color($op, $lft, |
||
| 1699 | array_fill(1, count($lft) - 1, $rgt[1])); |
||
| 1700 | } |
||
| 1701 | |||
| 1702 | protected function op_color_color($op, $left, $right) { |
||
| 1703 | $out = array('color'); |
||
| 1704 | $max = count($left) > count($right) ? count($left) : count($right); |
||
| 1705 | foreach (range(1, $max - 1) as $i) { |
||
| 1706 | $lval = isset($left[$i]) ? $left[$i] : 0; |
||
| 1707 | $rval = isset($right[$i]) ? $right[$i] : 0; |
||
| 1708 | switch ($op) { |
||
| 1709 | case '+': |
||
| 1710 | $out[] = $lval + $rval; |
||
| 1711 | break; |
||
| 1712 | case '-': |
||
| 1713 | $out[] = $lval - $rval; |
||
| 1714 | break; |
||
| 1715 | case '*': |
||
| 1716 | $out[] = $lval * $rval; |
||
| 1717 | break; |
||
| 1718 | case '%': |
||
| 1719 | $out[] = $lval % $rval; |
||
| 1720 | break; |
||
| 1721 | View Code Duplication | case '/': |
|
| 1722 | if ($rval == 0) $this->throwError("evaluate error: can't divide by zero"); |
||
| 1723 | $out[] = $lval / $rval; |
||
| 1724 | break; |
||
| 1725 | default: |
||
| 1726 | $this->throwError('evaluate error: color op number failed on op '.$op); |
||
| 1727 | } |
||
| 1728 | } |
||
| 1729 | return $this->fixColor($out); |
||
| 1730 | } |
||
| 1731 | |||
| 1732 | View Code Duplication | function lib_red($color){ |
|
| 1733 | $color = $this->coerceColor($color); |
||
| 1734 | if (is_null($color)) { |
||
| 1735 | $this->throwError('color expected for red()'); |
||
| 1736 | } |
||
| 1737 | |||
| 1738 | return $color[1]; |
||
| 1739 | } |
||
| 1740 | |||
| 1741 | View Code Duplication | function lib_green($color){ |
|
| 1742 | $color = $this->coerceColor($color); |
||
| 1743 | if (is_null($color)) { |
||
| 1744 | $this->throwError('color expected for green()'); |
||
| 1745 | } |
||
| 1746 | |||
| 1747 | return $color[2]; |
||
| 1748 | } |
||
| 1749 | |||
| 1750 | View Code Duplication | function lib_blue($color){ |
|
| 1751 | $color = $this->coerceColor($color); |
||
| 1752 | if (is_null($color)) { |
||
| 1753 | $this->throwError('color expected for blue()'); |
||
| 1754 | } |
||
| 1755 | |||
| 1756 | return $color[3]; |
||
| 1757 | } |
||
| 1758 | |||
| 1759 | |||
| 1760 | // operator on two numbers |
||
| 1761 | protected function op_number_number($op, $left, $right) { |
||
| 1762 | $unit = empty($left[2]) ? $right[2] : $left[2]; |
||
| 1763 | |||
| 1764 | $value = 0; |
||
| 1765 | switch ($op) { |
||
| 1766 | case '+': |
||
| 1767 | $value = $left[1] + $right[1]; |
||
| 1768 | break; |
||
| 1769 | case '*': |
||
| 1770 | $value = $left[1] * $right[1]; |
||
| 1771 | break; |
||
| 1772 | case '-': |
||
| 1773 | $value = $left[1] - $right[1]; |
||
| 1774 | break; |
||
| 1775 | case '%': |
||
| 1776 | $value = $left[1] % $right[1]; |
||
| 1777 | break; |
||
| 1778 | case '/': |
||
| 1779 | if ($right[1] == 0) $this->throwError('parse error: divide by zero'); |
||
| 1780 | $value = $left[1] / $right[1]; |
||
| 1781 | break; |
||
| 1782 | case '<': |
||
| 1783 | return $this->toBool($left[1] < $right[1]); |
||
| 1784 | case '>': |
||
| 1785 | return $this->toBool($left[1] > $right[1]); |
||
| 1786 | case '>=': |
||
| 1787 | return $this->toBool($left[1] >= $right[1]); |
||
| 1788 | case '=<': |
||
| 1789 | return $this->toBool($left[1] <= $right[1]); |
||
| 1790 | default: |
||
| 1791 | $this->throwError('parse error: unknown number operator: '.$op); |
||
| 1792 | } |
||
| 1793 | |||
| 1794 | return array("number", $value, $unit); |
||
| 1795 | } |
||
| 1796 | |||
| 1797 | |||
| 1798 | /* environment functions */ |
||
| 1799 | |||
| 1800 | protected function makeOutputBlock($type, $selectors = null) { |
||
| 1801 | $b = new stdclass; |
||
| 1802 | $b->lines = array(); |
||
| 1803 | $b->children = array(); |
||
| 1804 | $b->selectors = $selectors; |
||
| 1805 | $b->type = $type; |
||
| 1806 | $b->parent = $this->scope; |
||
| 1807 | return $b; |
||
| 1808 | } |
||
| 1809 | |||
| 1810 | // the state of execution |
||
| 1811 | protected function pushEnv($block = null) { |
||
| 1812 | $e = new stdclass; |
||
| 1813 | $e->parent = $this->env; |
||
| 1814 | $e->store = array(); |
||
| 1815 | $e->block = $block; |
||
| 1816 | |||
| 1817 | $this->env = $e; |
||
| 1818 | return $e; |
||
| 1819 | } |
||
| 1820 | |||
| 1821 | // pop something off the stack |
||
| 1822 | protected function popEnv() { |
||
| 1823 | $old = $this->env; |
||
| 1824 | $this->env = $this->env->parent; |
||
| 1825 | return $old; |
||
| 1826 | } |
||
| 1827 | |||
| 1828 | // set something in the current env |
||
| 1829 | protected function set($name, $value) { |
||
| 1832 | |||
| 1833 | |||
| 1834 | // get the highest occurrence entry for a name |
||
| 1835 | protected function get($name) { |
||
| 1836 | $current = $this->env; |
||
| 1837 | |||
| 1838 | $isArguments = $name == $this->vPrefix . 'arguments'; |
||
| 1839 | while ($current) { |
||
| 1840 | if ($isArguments && isset($current->arguments)) { |
||
| 1841 | return array('list', ' ', $current->arguments); |
||
| 1842 | } |
||
| 1843 | |||
| 1844 | View Code Duplication | if (isset($current->store[$name])) |
|
| 1845 | return $current->store[$name]; |
||
| 1846 | else { |
||
| 1847 | $current = isset($current->storeParent) ? |
||
| 1848 | $current->storeParent : $current->parent; |
||
| 1849 | } |
||
| 1850 | } |
||
| 1851 | |||
| 1852 | $this->throwError("variable $name is undefined"); |
||
| 1853 | } |
||
| 1854 | |||
| 1855 | // inject array of unparsed strings into environment as variables |
||
| 1856 | protected function injectVariables($args) { |
||
| 1870 | |||
| 1871 | /** |
||
| 1872 | * Initialize any static state, can initialize parser for a file |
||
| 1873 | * $opts isn't used yet |
||
| 1874 | */ |
||
| 1875 | public function __construct($fname = null) { |
||
| 1876 | if ($fname !== null) { |
||
| 1877 | // used for deprecated parse method |
||
| 1878 | $this->_parseFile = $fname; |
||
| 1879 | } |
||
| 1880 | } |
||
| 1881 | |||
| 1882 | public function compile($string, $name = null) { |
||
| 1883 | $locale = setlocale(LC_NUMERIC, 0); |
||
| 1884 | setlocale(LC_NUMERIC, "C"); |
||
| 1885 | |||
| 1886 | $this->parser = $this->makeParser($name); |
||
| 1887 | $root = $this->parser->parse($string); |
||
| 1888 | |||
| 1889 | $this->env = null; |
||
| 1890 | $this->scope = null; |
||
| 1891 | |||
| 1892 | $this->formatter = $this->newFormatter(); |
||
| 1893 | |||
| 1894 | if (!empty($this->registeredVars)) { |
||
| 1895 | $this->injectVariables($this->registeredVars); |
||
| 1896 | } |
||
| 1897 | |||
| 1898 | $this->sourceParser = $this->parser; // used for error messages |
||
| 1899 | $this->compileBlock($root); |
||
| 1900 | |||
| 1901 | ob_start(); |
||
| 1902 | $this->formatter->block($this->scope); |
||
| 1903 | $out = ob_get_clean(); |
||
| 1904 | setlocale(LC_NUMERIC, $locale); |
||
| 1905 | return $out; |
||
| 1906 | } |
||
| 1907 | |||
| 1908 | public function compileFile($fname, $outFname = null) { |
||
| 1909 | if (!is_readable($fname)) { |
||
| 1910 | throw new Exception('load error: failed to find '.$fname); |
||
| 1911 | } |
||
| 1912 | |||
| 1913 | $pi = pathinfo($fname); |
||
| 1914 | |||
| 1915 | $oldImport = $this->importDir; |
||
| 1916 | |||
| 1917 | $this->importDir = (array)$this->importDir; |
||
| 1918 | $this->importDir[] = $pi['dirname'].'/'; |
||
| 1919 | |||
| 1920 | $this->addParsedFile($fname); |
||
| 1921 | |||
| 1922 | $out = $this->compile(file_get_contents($fname), $fname); |
||
| 1923 | |||
| 1924 | $this->importDir = $oldImport; |
||
| 1925 | |||
| 1926 | if ($outFname !== null) { |
||
| 1927 | return file_put_contents($outFname, $out); |
||
| 1928 | } |
||
| 1929 | |||
| 1930 | return $out; |
||
| 1931 | } |
||
| 1932 | |||
| 1933 | // compile only if changed input has changed or output doesn't exist |
||
| 1934 | public function checkedCompile($in, $out) { |
||
| 1935 | if (!is_file($out) || filemtime($in) > filemtime($out)) { |
||
| 1936 | $this->compileFile($in, $out); |
||
| 1937 | return true; |
||
| 1938 | } |
||
| 1939 | return false; |
||
| 1940 | } |
||
| 1941 | |||
| 1942 | /** |
||
| 1943 | * Execute lessphp on a .less file or a lessphp cache structure |
||
| 1944 | * |
||
| 1945 | * The lessphp cache structure contains information about a specific |
||
| 1946 | * less file having been parsed. It can be used as a hint for future |
||
| 1947 | * calls to determine whether or not a rebuild is required. |
||
| 1948 | * |
||
| 1949 | * The cache structure contains two important keys that may be used |
||
| 1950 | * externally: |
||
| 1951 | * |
||
| 1952 | * compiled: The final compiled CSS |
||
| 1953 | * updated: The time (in seconds) the CSS was last compiled |
||
| 1954 | * |
||
| 1955 | * The cache structure is a plain-ol' PHP associative array and can |
||
| 1956 | * be serialized and unserialized without a hitch. |
||
| 1957 | * |
||
| 1958 | * @param mixed $in Input |
||
| 1959 | * @param bool $force Force rebuild? |
||
| 1960 | * @return array lessphp cache structure |
||
| 1961 | */ |
||
| 1962 | public function cachedCompile($in, $force = false) { |
||
| 1963 | // assume no root |
||
| 1964 | $root = null; |
||
| 1965 | |||
| 1966 | if (is_string($in)) { |
||
| 1967 | $root = $in; |
||
| 1968 | } elseif (is_array($in) and isset($in['root'])) { |
||
| 1969 | if ($force or ! isset($in['files'])) { |
||
| 1970 | // If we are forcing a recompile or if for some reason the |
||
| 1971 | // structure does not contain any file information we should |
||
| 1972 | // specify the root to trigger a rebuild. |
||
| 1973 | $root = $in['root']; |
||
| 1974 | } elseif (isset($in['files']) and is_array($in['files'])) { |
||
| 1975 | foreach ($in['files'] as $fname => $ftime ) { |
||
| 1976 | if (!file_exists($fname) or filemtime($fname) > $ftime) { |
||
| 1977 | // One of the files we knew about previously has changed |
||
| 1978 | // so we should look at our incoming root again. |
||
| 1979 | $root = $in['root']; |
||
| 1980 | break; |
||
| 1981 | } |
||
| 1982 | } |
||
| 1983 | } |
||
| 1984 | } else { |
||
| 1985 | // TODO: Throw an exception? We got neither a string nor something |
||
| 1986 | // that looks like a compatible lessphp cache structure. |
||
| 1987 | return null; |
||
| 1988 | } |
||
| 1989 | |||
| 1990 | if ($root !== null) { |
||
| 1991 | // If we have a root value which means we should rebuild. |
||
| 1992 | $out = array(); |
||
| 1993 | $out['root'] = $root; |
||
| 1994 | $out['compiled'] = $this->compileFile($root); |
||
| 1995 | $out['files'] = $this->allParsedFiles(); |
||
| 1996 | $out['updated'] = time(); |
||
| 1997 | return $out; |
||
| 1998 | } else { |
||
| 1999 | // No changes, pass back the structure |
||
| 2000 | // we were given initially. |
||
| 2001 | return $in; |
||
| 2002 | } |
||
| 2003 | |||
| 2004 | } |
||
| 2005 | |||
| 2006 | // parse and compile buffer |
||
| 2007 | // This is deprecated |
||
| 2008 | public function parse($str = null, $initialVariables = null) { |
||
| 2009 | if (is_array($str)) { |
||
| 2010 | $initialVariables = $str; |
||
| 2011 | $str = null; |
||
| 2012 | } |
||
| 2013 | |||
| 2014 | $oldVars = $this->registeredVars; |
||
| 2015 | if ($initialVariables !== null) { |
||
| 2016 | $this->setVariables($initialVariables); |
||
| 2017 | } |
||
| 2018 | |||
| 2019 | if ($str == null) { |
||
| 2020 | if (empty($this->_parseFile)) { |
||
| 2021 | throw new exception("nothing to parse"); |
||
| 2022 | } |
||
| 2023 | |||
| 2024 | $out = $this->compileFile($this->_parseFile); |
||
| 2025 | } else { |
||
| 2026 | $out = $this->compile($str); |
||
| 2027 | } |
||
| 2028 | |||
| 2029 | $this->registeredVars = $oldVars; |
||
| 2030 | return $out; |
||
| 2031 | } |
||
| 2032 | |||
| 2033 | protected function makeParser($name) { |
||
| 2034 | $parser = new lessc_parser($this, $name); |
||
| 2035 | $parser->writeComments = $this->preserveComments; |
||
| 2036 | |||
| 2037 | return $parser; |
||
| 2038 | } |
||
| 2039 | |||
| 2040 | public function setFormatter($name) { |
||
| 2043 | |||
| 2044 | protected function newFormatter() { |
||
| 2045 | $className = "lessc_formatter_lessjs"; |
||
| 2046 | if (!empty($this->formatterName)) { |
||
| 2047 | if (!is_string($this->formatterName)) |
||
| 2048 | return $this->formatterName; |
||
| 2049 | $className = "lessc_formatter_$this->formatterName"; |
||
| 2050 | } |
||
| 2051 | |||
| 2052 | return new $className; |
||
| 2053 | } |
||
| 2054 | |||
| 2055 | public function setPreserveComments($preserve) { |
||
| 2058 | |||
| 2059 | public function registerFunction($name, $func) { |
||
| 2062 | |||
| 2063 | public function unregisterFunction($name) { |
||
| 2066 | |||
| 2067 | public function setVariables($variables) { |
||
| 2070 | |||
| 2071 | public function unsetVariable($name) { |
||
| 2074 | |||
| 2075 | public function setImportDir($dirs) { |
||
| 2078 | |||
| 2079 | public function addImportDir($dir) { |
||
| 2083 | |||
| 2084 | public function allParsedFiles() { |
||
| 2087 | |||
| 2088 | public function addParsedFile($file) { |
||
| 2091 | |||
| 2092 | /** |
||
| 2093 | * Uses the current value of $this->count to show line and line number |
||
| 2094 | */ |
||
| 2095 | public function throwError($msg = null) { |
||
| 2096 | if ($this->sourceLoc >= 0) { |
||
| 2097 | $this->sourceParser->throwError($msg, $this->sourceLoc); |
||
| 2098 | } |
||
| 2099 | throw new exception($msg); |
||
| 2100 | } |
||
| 2101 | |||
| 2102 | // compile file $in to file $out if $in is newer than $out |
||
| 2103 | // returns true when it compiles, false otherwise |
||
| 2104 | public static function ccompile($in, $out, $less = null) { |
||
| 2105 | if ($less === null) { |
||
| 2106 | $less = new self; |
||
| 2107 | } |
||
| 2108 | return $less->checkedCompile($in, $out); |
||
| 2109 | } |
||
| 2110 | |||
| 2111 | public static function cexecute($in, $force = false, $less = null) { |
||
| 2112 | if ($less === null) { |
||
| 2113 | $less = new self; |
||
| 2114 | } |
||
| 2115 | return $less->cachedCompile($in, $force); |
||
| 2116 | } |
||
| 2117 | |||
| 2118 | static protected $cssColors = array( |
||
| 2119 | 'aliceblue' => '240,248,255', |
||
| 2120 | 'antiquewhite' => '250,235,215', |
||
| 2121 | 'aqua' => '0,255,255', |
||
| 2122 | 'aquamarine' => '127,255,212', |
||
| 2123 | 'azure' => '240,255,255', |
||
| 2124 | 'beige' => '245,245,220', |
||
| 2125 | 'bisque' => '255,228,196', |
||
| 2126 | 'black' => '0,0,0', |
||
| 2127 | 'blanchedalmond' => '255,235,205', |
||
| 2128 | 'blue' => '0,0,255', |
||
| 2129 | 'blueviolet' => '138,43,226', |
||
| 2130 | 'brown' => '165,42,42', |
||
| 2131 | 'burlywood' => '222,184,135', |
||
| 2132 | 'cadetblue' => '95,158,160', |
||
| 2133 | 'chartreuse' => '127,255,0', |
||
| 2134 | 'chocolate' => '210,105,30', |
||
| 2135 | 'coral' => '255,127,80', |
||
| 2136 | 'cornflowerblue' => '100,149,237', |
||
| 2137 | 'cornsilk' => '255,248,220', |
||
| 2138 | 'crimson' => '220,20,60', |
||
| 2139 | 'cyan' => '0,255,255', |
||
| 2140 | 'darkblue' => '0,0,139', |
||
| 2141 | 'darkcyan' => '0,139,139', |
||
| 2142 | 'darkgoldenrod' => '184,134,11', |
||
| 2143 | 'darkgray' => '169,169,169', |
||
| 2144 | 'darkgreen' => '0,100,0', |
||
| 2145 | 'darkgrey' => '169,169,169', |
||
| 2146 | 'darkkhaki' => '189,183,107', |
||
| 2147 | 'darkmagenta' => '139,0,139', |
||
| 2148 | 'darkolivegreen' => '85,107,47', |
||
| 2149 | 'darkorange' => '255,140,0', |
||
| 2150 | 'darkorchid' => '153,50,204', |
||
| 2151 | 'darkred' => '139,0,0', |
||
| 2152 | 'darksalmon' => '233,150,122', |
||
| 2153 | 'darkseagreen' => '143,188,143', |
||
| 2154 | 'darkslateblue' => '72,61,139', |
||
| 2155 | 'darkslategray' => '47,79,79', |
||
| 2156 | 'darkslategrey' => '47,79,79', |
||
| 2157 | 'darkturquoise' => '0,206,209', |
||
| 2158 | 'darkviolet' => '148,0,211', |
||
| 2159 | 'deeppink' => '255,20,147', |
||
| 2160 | 'deepskyblue' => '0,191,255', |
||
| 2161 | 'dimgray' => '105,105,105', |
||
| 2162 | 'dimgrey' => '105,105,105', |
||
| 2163 | 'dodgerblue' => '30,144,255', |
||
| 2164 | 'firebrick' => '178,34,34', |
||
| 2165 | 'floralwhite' => '255,250,240', |
||
| 2166 | 'forestgreen' => '34,139,34', |
||
| 2167 | 'fuchsia' => '255,0,255', |
||
| 2168 | 'gainsboro' => '220,220,220', |
||
| 2169 | 'ghostwhite' => '248,248,255', |
||
| 2170 | 'gold' => '255,215,0', |
||
| 2171 | 'goldenrod' => '218,165,32', |
||
| 2172 | 'gray' => '128,128,128', |
||
| 2173 | 'green' => '0,128,0', |
||
| 2174 | 'greenyellow' => '173,255,47', |
||
| 2175 | 'grey' => '128,128,128', |
||
| 2176 | 'honeydew' => '240,255,240', |
||
| 2177 | 'hotpink' => '255,105,180', |
||
| 2178 | 'indianred' => '205,92,92', |
||
| 2179 | 'indigo' => '75,0,130', |
||
| 2180 | 'ivory' => '255,255,240', |
||
| 2181 | 'khaki' => '240,230,140', |
||
| 2182 | 'lavender' => '230,230,250', |
||
| 2183 | 'lavenderblush' => '255,240,245', |
||
| 2184 | 'lawngreen' => '124,252,0', |
||
| 2185 | 'lemonchiffon' => '255,250,205', |
||
| 2186 | 'lightblue' => '173,216,230', |
||
| 2187 | 'lightcoral' => '240,128,128', |
||
| 2188 | 'lightcyan' => '224,255,255', |
||
| 2189 | 'lightgoldenrodyellow' => '250,250,210', |
||
| 2190 | 'lightgray' => '211,211,211', |
||
| 2191 | 'lightgreen' => '144,238,144', |
||
| 2192 | 'lightgrey' => '211,211,211', |
||
| 2193 | 'lightpink' => '255,182,193', |
||
| 2194 | 'lightsalmon' => '255,160,122', |
||
| 2195 | 'lightseagreen' => '32,178,170', |
||
| 2196 | 'lightskyblue' => '135,206,250', |
||
| 2197 | 'lightslategray' => '119,136,153', |
||
| 2198 | 'lightslategrey' => '119,136,153', |
||
| 2199 | 'lightsteelblue' => '176,196,222', |
||
| 2200 | 'lightyellow' => '255,255,224', |
||
| 2201 | 'lime' => '0,255,0', |
||
| 2202 | 'limegreen' => '50,205,50', |
||
| 2203 | 'linen' => '250,240,230', |
||
| 2204 | 'magenta' => '255,0,255', |
||
| 2205 | 'maroon' => '128,0,0', |
||
| 2206 | 'mediumaquamarine' => '102,205,170', |
||
| 2207 | 'mediumblue' => '0,0,205', |
||
| 2208 | 'mediumorchid' => '186,85,211', |
||
| 2209 | 'mediumpurple' => '147,112,219', |
||
| 2210 | 'mediumseagreen' => '60,179,113', |
||
| 2211 | 'mediumslateblue' => '123,104,238', |
||
| 2212 | 'mediumspringgreen' => '0,250,154', |
||
| 2213 | 'mediumturquoise' => '72,209,204', |
||
| 2214 | 'mediumvioletred' => '199,21,133', |
||
| 2215 | 'midnightblue' => '25,25,112', |
||
| 2216 | 'mintcream' => '245,255,250', |
||
| 2217 | 'mistyrose' => '255,228,225', |
||
| 2218 | 'moccasin' => '255,228,181', |
||
| 2219 | 'navajowhite' => '255,222,173', |
||
| 2220 | 'navy' => '0,0,128', |
||
| 2221 | 'oldlace' => '253,245,230', |
||
| 2222 | 'olive' => '128,128,0', |
||
| 2223 | 'olivedrab' => '107,142,35', |
||
| 2224 | 'orange' => '255,165,0', |
||
| 2225 | 'orangered' => '255,69,0', |
||
| 2226 | 'orchid' => '218,112,214', |
||
| 2227 | 'palegoldenrod' => '238,232,170', |
||
| 2228 | 'palegreen' => '152,251,152', |
||
| 2229 | 'paleturquoise' => '175,238,238', |
||
| 2230 | 'palevioletred' => '219,112,147', |
||
| 2231 | 'papayawhip' => '255,239,213', |
||
| 2232 | 'peachpuff' => '255,218,185', |
||
| 2233 | 'peru' => '205,133,63', |
||
| 2234 | 'pink' => '255,192,203', |
||
| 2235 | 'plum' => '221,160,221', |
||
| 2236 | 'powderblue' => '176,224,230', |
||
| 2237 | 'purple' => '128,0,128', |
||
| 2238 | 'red' => '255,0,0', |
||
| 2239 | 'rosybrown' => '188,143,143', |
||
| 2240 | 'royalblue' => '65,105,225', |
||
| 2241 | 'saddlebrown' => '139,69,19', |
||
| 2242 | 'salmon' => '250,128,114', |
||
| 2243 | 'sandybrown' => '244,164,96', |
||
| 2244 | 'seagreen' => '46,139,87', |
||
| 2245 | 'seashell' => '255,245,238', |
||
| 2246 | 'sienna' => '160,82,45', |
||
| 2247 | 'silver' => '192,192,192', |
||
| 2248 | 'skyblue' => '135,206,235', |
||
| 2249 | 'slateblue' => '106,90,205', |
||
| 2250 | 'slategray' => '112,128,144', |
||
| 2251 | 'slategrey' => '112,128,144', |
||
| 2252 | 'snow' => '255,250,250', |
||
| 2253 | 'springgreen' => '0,255,127', |
||
| 2254 | 'steelblue' => '70,130,180', |
||
| 2255 | 'tan' => '210,180,140', |
||
| 2256 | 'teal' => '0,128,128', |
||
| 2257 | 'thistle' => '216,191,216', |
||
| 2258 | 'tomato' => '255,99,71', |
||
| 2259 | 'transparent' => '0,0,0,0', |
||
| 2260 | 'turquoise' => '64,224,208', |
||
| 2261 | 'violet' => '238,130,238', |
||
| 2262 | 'wheat' => '245,222,179', |
||
| 2263 | 'white' => '255,255,255', |
||
| 2264 | 'whitesmoke' => '245,245,245', |
||
| 2265 | 'yellow' => '255,255,0', |
||
| 2266 | 'yellowgreen' => '154,205,50' |
||
| 2267 | ); |
||
| 2268 | } |
||
| 2269 | |||
| 3768 |