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 scssc 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 scssc, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
45 | class scssc { |
||
46 | static public $VERSION = "v0.0.9"; |
||
47 | |||
48 | static protected $operatorNames = array( |
||
49 | '+' => "add", |
||
50 | '-' => "sub", |
||
51 | '*' => "mul", |
||
52 | '/' => "div", |
||
53 | '%' => "mod", |
||
54 | |||
55 | '==' => "eq", |
||
56 | '!=' => "neq", |
||
57 | '<' => "lt", |
||
58 | '>' => "gt", |
||
59 | |||
60 | '<=' => "lte", |
||
61 | '>=' => "gte", |
||
62 | ); |
||
63 | |||
64 | static protected $namespaces = array( |
||
65 | "special" => "%", |
||
66 | "mixin" => "@", |
||
67 | "function" => "^", |
||
68 | ); |
||
69 | |||
70 | static protected $unitTable = array( |
||
71 | "in" => array( |
||
72 | "in" => 1, |
||
73 | "pt" => 72, |
||
74 | "pc" => 6, |
||
75 | "cm" => 2.54, |
||
76 | "mm" => 25.4, |
||
77 | "px" => 96, |
||
78 | ) |
||
79 | ); |
||
80 | |||
81 | static public $true = array("keyword", "true"); |
||
82 | static public $false = array("keyword", "false"); |
||
83 | static public $null = array("null"); |
||
84 | |||
85 | static public $defaultValue = array("keyword", ""); |
||
86 | static public $selfSelector = array("self"); |
||
87 | |||
88 | protected $importPaths = array(""); |
||
89 | protected $importCache = array(); |
||
90 | |||
91 | protected $userFunctions = array(); |
||
92 | |||
93 | protected $numberPrecision = 5; |
||
94 | |||
95 | protected $formatter = "scss_formatter_nested"; |
||
96 | |||
97 | public function compile($code, $name=null) { |
||
98 | $this->indentLevel = -1; |
||
|
|||
99 | $this->commentsSeen = array(); |
||
100 | $this->extends = array(); |
||
101 | $this->extendsMap = array(); |
||
102 | |||
103 | $locale = setlocale(LC_NUMERIC, 0); |
||
104 | setlocale(LC_NUMERIC, "C"); |
||
105 | |||
106 | $this->parsedFiles = array(); |
||
107 | $this->parser = new scss_parser($name); |
||
108 | $tree = $this->parser->parse($code); |
||
109 | |||
110 | $this->formatter = new $this->formatter(); |
||
111 | |||
112 | $this->env = null; |
||
113 | $this->scope = null; |
||
114 | |||
115 | $this->compileRoot($tree); |
||
116 | |||
117 | $out = $this->formatter->format($this->scope); |
||
118 | |||
119 | setlocale(LC_NUMERIC, $locale); |
||
120 | return $out; |
||
121 | } |
||
122 | |||
123 | protected function isSelfExtend($target, $origin) { |
||
124 | foreach ($origin as $sel) { |
||
125 | if (in_array($target, $sel)) { |
||
126 | return true; |
||
127 | } |
||
128 | } |
||
129 | |||
130 | return false; |
||
131 | } |
||
132 | |||
133 | protected function pushExtends($target, $origin) { |
||
149 | |||
150 | protected function makeOutputBlock($type, $selectors = null) { |
||
151 | $out = new stdClass; |
||
152 | $out->type = $type; |
||
153 | $out->lines = array(); |
||
154 | $out->children = array(); |
||
155 | $out->parent = $this->scope; |
||
156 | $out->selectors = $selectors; |
||
157 | $out->depth = $this->env->depth; |
||
158 | |||
159 | return $out; |
||
160 | } |
||
161 | |||
162 | protected function matchExtendsSingle($single, &$outOrigin) { |
||
163 | $counts = array(); |
||
164 | foreach ($single as $part) { |
||
165 | if (!is_string($part)) return false; // hmm |
||
166 | |||
167 | if (isset($this->extendsMap[$part])) { |
||
168 | foreach ($this->extendsMap[$part] as $idx) { |
||
169 | $counts[$idx] = |
||
170 | isset($counts[$idx]) ? $counts[$idx] + 1 : 1; |
||
171 | } |
||
172 | } |
||
173 | } |
||
174 | |||
175 | $outOrigin = array(); |
||
176 | $found = false; |
||
177 | |||
178 | foreach ($counts as $idx => $count) { |
||
179 | list($target, $origin) = $this->extends[$idx]; |
||
180 | |||
181 | // check count |
||
182 | if ($count != count($target)) continue; |
||
183 | |||
184 | // check if target is subset of single |
||
185 | if (array_diff(array_intersect($single, $target), $target)) continue; |
||
186 | |||
187 | $rem = array_diff($single, $target); |
||
188 | |||
189 | foreach ($origin as $j => $new) { |
||
190 | // prevent infinite loop when target extends itself |
||
191 | foreach ($new as $new_selector) { |
||
192 | if (!array_diff($single, $new_selector)) { |
||
193 | continue 2; |
||
194 | } |
||
195 | } |
||
196 | |||
197 | $origin[$j][count($origin[$j]) - 1] = $this->combineSelectorSingle(end($new), $rem); |
||
198 | } |
||
199 | |||
200 | $outOrigin = array_merge($outOrigin, $origin); |
||
201 | |||
202 | $found = true; |
||
203 | } |
||
204 | |||
205 | return $found; |
||
206 | } |
||
207 | |||
208 | protected function combineSelectorSingle($base, $other) { |
||
209 | $tag = null; |
||
210 | $out = array(); |
||
211 | |||
212 | foreach (array($base, $other) as $single) { |
||
213 | foreach ($single as $part) { |
||
214 | if (preg_match('/^[^\[.#:]/', $part)) { |
||
215 | $tag = $part; |
||
216 | } else { |
||
217 | $out[] = $part; |
||
218 | } |
||
219 | } |
||
220 | } |
||
221 | |||
222 | if ($tag) { |
||
223 | array_unshift($out, $tag); |
||
224 | } |
||
225 | |||
226 | return $out; |
||
227 | } |
||
228 | |||
229 | protected function matchExtends($selector, &$out, $from = 0, $initial=true) { |
||
230 | foreach ($selector as $i => $part) { |
||
231 | if ($i < $from) continue; |
||
232 | |||
233 | if ($this->matchExtendsSingle($part, $origin)) { |
||
234 | $before = array_slice($selector, 0, $i); |
||
235 | $after = array_slice($selector, $i + 1); |
||
236 | |||
237 | foreach ($origin as $new) { |
||
238 | $k = 0; |
||
239 | |||
240 | // remove shared parts |
||
241 | if ($initial) { |
||
242 | foreach ($before as $k => $val) { |
||
243 | if (!isset($new[$k]) || $val != $new[$k]) { |
||
244 | break; |
||
245 | } |
||
246 | } |
||
247 | } |
||
248 | |||
249 | $result = array_merge( |
||
250 | $before, |
||
251 | $k > 0 ? array_slice($new, $k) : $new, |
||
252 | $after); |
||
253 | |||
254 | |||
255 | if ($result == $selector) continue; |
||
256 | $out[] = $result; |
||
257 | |||
258 | // recursively check for more matches |
||
259 | $this->matchExtends($result, $out, $i, false); |
||
260 | |||
261 | // selector sequence merging |
||
262 | if (!empty($before) && count($new) > 1) { |
||
263 | $result2 = array_merge( |
||
264 | array_slice($new, 0, -1), |
||
265 | $k > 0 ? array_slice($before, $k) : $before, |
||
266 | array_slice($new, -1), |
||
267 | $after); |
||
268 | |||
269 | $out[] = $result2; |
||
270 | } |
||
271 | } |
||
272 | } |
||
273 | } |
||
274 | } |
||
275 | |||
276 | protected function flattenSelectors($block, $parentKey = null) { |
||
277 | if ($block->selectors) { |
||
278 | $selectors = array(); |
||
279 | foreach ($block->selectors as $s) { |
||
280 | $selectors[] = $s; |
||
281 | if (!is_array($s)) continue; |
||
282 | // check extends |
||
283 | if (!empty($this->extendsMap)) { |
||
284 | $this->matchExtends($s, $selectors); |
||
285 | } |
||
286 | } |
||
287 | |||
288 | $block->selectors = array(); |
||
289 | $placeholderSelector = false; |
||
290 | foreach ($selectors as $selector) { |
||
291 | if ($this->hasSelectorPlaceholder($selector)) { |
||
292 | $placeholderSelector = true; |
||
293 | continue; |
||
294 | } |
||
295 | $block->selectors[] = $this->compileSelector($selector); |
||
296 | } |
||
297 | |||
298 | if ($placeholderSelector && 0 == count($block->selectors) && null !== $parentKey) { |
||
299 | unset($block->parent->children[$parentKey]); |
||
300 | return; |
||
301 | } |
||
302 | } |
||
303 | |||
304 | foreach ($block->children as $key => $child) { |
||
305 | $this->flattenSelectors($child, $key); |
||
306 | } |
||
307 | } |
||
308 | |||
309 | protected function compileRoot($rootBlock) { |
||
310 | $this->pushEnv($rootBlock); |
||
311 | $this->scope = $this->makeOutputBlock("root"); |
||
312 | |||
313 | $this->compileChildren($rootBlock->children, $this->scope); |
||
314 | $this->flattenSelectors($this->scope); |
||
315 | |||
316 | $this->popEnv(); |
||
317 | } |
||
318 | |||
319 | protected function compileMedia($media) { |
||
320 | $this->pushEnv($media); |
||
321 | $parentScope = $this->mediaParent($this->scope); |
||
322 | |||
323 | $this->scope = $this->makeOutputBlock("media", array( |
||
324 | $this->compileMediaQuery($this->multiplyMedia($this->env))) |
||
325 | ); |
||
326 | |||
327 | $parentScope->children[] = $this->scope; |
||
328 | |||
329 | // top level properties in a media cause it to be wrapped |
||
330 | $needsWrap = false; |
||
331 | foreach ($media->children as $child) { |
||
332 | $type = $child[0]; |
||
333 | if ($type !== 'block' && $type !== 'media' && $type !== 'directive') { |
||
334 | $needsWrap = true; |
||
335 | break; |
||
336 | } |
||
337 | } |
||
338 | |||
339 | if ($needsWrap) { |
||
340 | $wrapped = (object)array( |
||
341 | "selectors" => array(), |
||
342 | "children" => $media->children |
||
343 | ); |
||
344 | $media->children = array(array("block", $wrapped)); |
||
345 | } |
||
346 | |||
347 | $this->compileChildren($media->children, $this->scope); |
||
348 | |||
349 | $this->scope = $this->scope->parent; |
||
350 | $this->popEnv(); |
||
351 | } |
||
352 | |||
353 | View Code Duplication | protected function mediaParent($scope) { |
|
363 | |||
364 | // TODO refactor compileNestedBlock and compileMedia into same thing |
||
365 | View Code Duplication | protected function compileNestedBlock($block, $selectors) { |
|
366 | $this->pushEnv($block); |
||
367 | |||
368 | $this->scope = $this->makeOutputBlock($block->type, $selectors); |
||
369 | $this->scope->parent->children[] = $this->scope; |
||
370 | $this->compileChildren($block->children, $this->scope); |
||
371 | |||
372 | $this->scope = $this->scope->parent; |
||
373 | $this->popEnv(); |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * Recursively compiles a block. |
||
378 | * |
||
379 | * A block is analogous to a CSS block in most cases. A single SCSS document |
||
380 | * is encapsulated in a block when parsed, but it does not have parent tags |
||
381 | * so all of its children appear on the root level when compiled. |
||
382 | * |
||
383 | * Blocks are made up of selectors and children. |
||
384 | * |
||
385 | * The children of a block are just all the blocks that are defined within. |
||
386 | * |
||
387 | * Compiling the block involves pushing a fresh environment on the stack, |
||
388 | * and iterating through the props, compiling each one. |
||
389 | * |
||
390 | * @see scss::compileChild() |
||
391 | * |
||
392 | * @param \StdClass $block |
||
393 | */ |
||
394 | protected function compileBlock($block) { |
||
395 | $env = $this->pushEnv($block); |
||
396 | |||
397 | $env->selectors = |
||
398 | array_map(array($this, "evalSelector"), $block->selectors); |
||
399 | |||
400 | $out = $this->makeOutputBlock(null, $this->multiplySelectors($env)); |
||
401 | $this->scope->children[] = $out; |
||
402 | $this->compileChildren($block->children, $out); |
||
403 | |||
404 | $this->popEnv(); |
||
405 | } |
||
406 | |||
407 | // joins together .classes and #ids |
||
408 | protected function flattenSelectorSingle($single) { |
||
409 | $joined = array(); |
||
410 | foreach ($single as $part) { |
||
411 | if (empty($joined) || |
||
412 | !is_string($part) || |
||
413 | preg_match('/[\[.:#%]/', $part)) |
||
414 | { |
||
415 | $joined[] = $part; |
||
416 | continue; |
||
417 | } |
||
418 | |||
419 | if (is_array(end($joined))) { |
||
420 | $joined[] = $part; |
||
421 | } else { |
||
422 | $joined[count($joined) - 1] .= $part; |
||
423 | } |
||
424 | } |
||
425 | |||
426 | return $joined; |
||
427 | } |
||
428 | |||
429 | // replaces all the interpolates |
||
430 | protected function evalSelector($selector) { |
||
433 | |||
434 | protected function evalSelectorPart($piece) { |
||
435 | View Code Duplication | foreach ($piece as &$p) { |
|
436 | if (!is_array($p)) continue; |
||
437 | |||
438 | switch ($p[0]) { |
||
439 | case "interpolate": |
||
440 | $p = $this->compileValue($p); |
||
441 | break; |
||
442 | case "string": |
||
443 | $p = $this->compileValue($p); |
||
444 | break; |
||
445 | } |
||
446 | } |
||
447 | |||
448 | return $this->flattenSelectorSingle($piece); |
||
449 | } |
||
450 | |||
451 | // compiles to string |
||
452 | // self(&) should have been replaced by now |
||
453 | protected function compileSelector($selector) { |
||
454 | if (!is_array($selector)) return $selector; // media and the like |
||
455 | |||
456 | return implode(" ", array_map( |
||
457 | array($this, "compileSelectorPart"), $selector)); |
||
458 | } |
||
459 | |||
460 | protected function compileSelectorPart($piece) { |
||
476 | |||
477 | protected function hasSelectorPlaceholder($selector) |
||
478 | { |
||
479 | if (!is_array($selector)) return false; |
||
480 | |||
481 | foreach ($selector as $parts) { |
||
482 | foreach ($parts as $part) { |
||
483 | if ('%' == $part[0]) { |
||
484 | return true; |
||
485 | } |
||
486 | } |
||
487 | } |
||
488 | |||
489 | return false; |
||
490 | } |
||
491 | |||
492 | protected function compileChildren($stms, $out) { |
||
493 | foreach ($stms as $stm) { |
||
494 | $ret = $this->compileChild($stm, $out); |
||
495 | if (!is_null($ret)) return $ret; |
||
496 | } |
||
497 | } |
||
498 | |||
499 | protected function compileMediaQuery($queryList) { |
||
500 | $out = "@media"; |
||
501 | $first = true; |
||
502 | foreach ($queryList as $query){ |
||
503 | $parts = array(); |
||
504 | foreach ($query as $q) { |
||
505 | switch ($q[0]) { |
||
506 | case "mediaType": |
||
507 | $parts[] = implode(" ", array_map(array($this, "compileValue"), array_slice($q, 1))); |
||
508 | break; |
||
509 | case "mediaExp": |
||
510 | if (isset($q[2])) { |
||
511 | $parts[] = "(". $this->compileValue($q[1]) . $this->formatter->assignSeparator . $this->compileValue($q[2]) . ")"; |
||
512 | } else { |
||
513 | $parts[] = "(" . $this->compileValue($q[1]) . ")"; |
||
514 | } |
||
515 | break; |
||
516 | } |
||
517 | } |
||
518 | if (!empty($parts)) { |
||
519 | if ($first) { |
||
520 | $first = false; |
||
521 | $out .= " "; |
||
522 | } else { |
||
523 | $out .= $this->formatter->tagSeparator; |
||
524 | } |
||
525 | $out .= implode(" and ", $parts); |
||
526 | } |
||
527 | } |
||
528 | return $out; |
||
529 | } |
||
530 | |||
531 | // returns true if the value was something that could be imported |
||
532 | protected function compileImport($rawPath, $out) { |
||
533 | if ($rawPath[0] == "string") { |
||
534 | $path = $this->compileStringContent($rawPath); |
||
535 | if ($path = $this->findImport($path)) { |
||
536 | $this->importFile($path, $out); |
||
537 | return true; |
||
538 | } |
||
539 | return false; |
||
540 | } |
||
541 | if ($rawPath[0] == "list") { |
||
542 | // handle a list of strings |
||
543 | if (count($rawPath[2]) == 0) return false; |
||
544 | foreach ($rawPath[2] as $path) { |
||
545 | if ($path[0] != "string") return false; |
||
546 | } |
||
547 | |||
548 | foreach ($rawPath[2] as $path) { |
||
549 | $this->compileImport($path, $out); |
||
550 | } |
||
551 | |||
552 | return true; |
||
553 | } |
||
554 | |||
555 | return false; |
||
556 | } |
||
557 | |||
558 | // return a value to halt execution |
||
559 | protected function compileChild($child, $out) { |
||
560 | $this->sourcePos = isset($child[-1]) ? $child[-1] : -1; |
||
561 | $this->sourceParser = isset($child[-2]) ? $child[-2] : $this->parser; |
||
562 | |||
563 | switch ($child[0]) { |
||
564 | View Code Duplication | case "import": |
|
565 | list(,$rawPath) = $child; |
||
566 | $rawPath = $this->reduce($rawPath); |
||
567 | if (!$this->compileImport($rawPath, $out)) { |
||
568 | $out->lines[] = "@import " . $this->compileValue($rawPath) . ";"; |
||
569 | } |
||
570 | break; |
||
571 | View Code Duplication | case "directive": |
|
572 | list(, $directive) = $child; |
||
573 | $s = "@" . $directive->name; |
||
574 | if (!empty($directive->value)) { |
||
575 | $s .= " " . $this->compileValue($directive->value); |
||
576 | } |
||
577 | $this->compileNestedBlock($directive, array($s)); |
||
578 | break; |
||
579 | case "media": |
||
580 | $this->compileMedia($child[1]); |
||
581 | break; |
||
582 | case "block": |
||
583 | $this->compileBlock($child[1]); |
||
584 | break; |
||
585 | case "charset": |
||
586 | $out->lines[] = "@charset ".$this->compileValue($child[1]).";"; |
||
587 | break; |
||
588 | case "assign": |
||
589 | list(,$name, $value) = $child; |
||
590 | if ($name[0] == "var") { |
||
591 | $isDefault = !empty($child[3]); |
||
592 | |||
593 | if ($isDefault) { |
||
594 | $existingValue = $this->get($name[1], true); |
||
595 | $shouldSet = $existingValue === true || $existingValue == self::$null; |
||
596 | } |
||
597 | |||
598 | if (!$isDefault || $shouldSet) { |
||
599 | $this->set($name[1], $this->reduce($value)); |
||
600 | } |
||
601 | break; |
||
602 | } |
||
603 | |||
604 | // if the value reduces to null from something else then |
||
605 | // the property should be discarded |
||
606 | if ($value[0] != "null") { |
||
607 | $value = $this->reduce($value); |
||
608 | if ($value[0] == "null") { |
||
609 | break; |
||
610 | } |
||
611 | } |
||
612 | |||
613 | $compiledValue = $this->compileValue($value); |
||
614 | $out->lines[] = $this->formatter->property( |
||
615 | $this->compileValue($name), |
||
616 | $compiledValue); |
||
617 | break; |
||
618 | case "comment": |
||
619 | $out->lines[] = $child[1]; |
||
620 | break; |
||
621 | case "mixin": |
||
622 | case "function": |
||
623 | list(,$block) = $child; |
||
624 | $this->set(self::$namespaces[$block->type] . $block->name, $block); |
||
625 | break; |
||
626 | case "extend": |
||
627 | list(, $selectors) = $child; |
||
628 | foreach ($selectors as $sel) { |
||
629 | // only use the first one |
||
630 | $sel = current($this->evalSelector($sel)); |
||
631 | $this->pushExtends($sel, $out->selectors); |
||
632 | } |
||
633 | break; |
||
634 | case "if": |
||
635 | list(, $if) = $child; |
||
636 | if ($this->isTruthy($this->reduce($if->cond, true))) { |
||
637 | return $this->compileChildren($if->children, $out); |
||
638 | } else { |
||
639 | foreach ($if->cases as $case) { |
||
640 | if ($case->type == "else" || |
||
641 | $case->type == "elseif" && $this->isTruthy($this->reduce($case->cond))) |
||
642 | { |
||
643 | return $this->compileChildren($case->children, $out); |
||
644 | } |
||
645 | } |
||
646 | } |
||
647 | break; |
||
648 | case "return": |
||
649 | return $this->reduce($child[1], true); |
||
650 | case "each": |
||
651 | list(,$each) = $child; |
||
652 | $list = $this->coerceList($this->reduce($each->list)); |
||
653 | foreach ($list[2] as $item) { |
||
654 | $this->pushEnv(); |
||
655 | $this->set($each->var, $item); |
||
656 | // TODO: allow return from here |
||
657 | $this->compileChildren($each->children, $out); |
||
658 | $this->popEnv(); |
||
659 | } |
||
660 | break; |
||
661 | case "while": |
||
662 | list(,$while) = $child; |
||
663 | while ($this->isTruthy($this->reduce($while->cond, true))) { |
||
664 | $ret = $this->compileChildren($while->children, $out); |
||
665 | if ($ret) return $ret; |
||
666 | } |
||
667 | break; |
||
668 | case "for": |
||
669 | list(,$for) = $child; |
||
670 | $start = $this->reduce($for->start, true); |
||
671 | $start = $start[1]; |
||
672 | $end = $this->reduce($for->end, true); |
||
673 | $end = $end[1]; |
||
674 | $d = $start < $end ? 1 : -1; |
||
675 | |||
676 | while (true) { |
||
677 | if ((!$for->until && $start - $d == $end) || |
||
678 | ($for->until && $start == $end)) |
||
679 | { |
||
680 | break; |
||
681 | } |
||
682 | |||
683 | $this->set($for->var, array("number", $start, "")); |
||
684 | $start += $d; |
||
685 | |||
686 | $ret = $this->compileChildren($for->children, $out); |
||
687 | if ($ret) return $ret; |
||
688 | } |
||
689 | |||
690 | break; |
||
691 | case "nestedprop": |
||
692 | list(,$prop) = $child; |
||
693 | $prefixed = array(); |
||
694 | $prefix = $this->compileValue($prop->prefix) . "-"; |
||
695 | foreach ($prop->children as $child) { |
||
696 | if ($child[0] == "assign") { |
||
697 | array_unshift($child[1][2], $prefix); |
||
698 | } |
||
699 | if ($child[0] == "nestedprop") { |
||
700 | array_unshift($child[1]->prefix[2], $prefix); |
||
701 | } |
||
702 | $prefixed[] = $child; |
||
703 | } |
||
704 | $this->compileChildren($prefixed, $out); |
||
705 | break; |
||
706 | case "include": // including a mixin |
||
707 | list(,$name, $argValues, $content) = $child; |
||
708 | $mixin = $this->get(self::$namespaces["mixin"] . $name, false); |
||
709 | if (!$mixin) { |
||
710 | $this->throwError("Undefined mixin $name"); |
||
711 | } |
||
712 | |||
713 | $callingScope = $this->env; |
||
714 | |||
715 | // push scope, apply args |
||
716 | $this->pushEnv(); |
||
717 | if ($this->env->depth > 0) { |
||
718 | $this->env->depth--; |
||
719 | } |
||
720 | |||
721 | if (!is_null($content)) { |
||
722 | $content->scope = $callingScope; |
||
723 | $this->setRaw(self::$namespaces["special"] . "content", $content); |
||
724 | } |
||
725 | |||
726 | if (!is_null($mixin->args)) { |
||
727 | $this->applyArguments($mixin->args, $argValues); |
||
728 | } |
||
729 | |||
730 | foreach ($mixin->children as $child) { |
||
731 | $this->compileChild($child, $out); |
||
732 | } |
||
733 | |||
734 | $this->popEnv(); |
||
735 | |||
736 | break; |
||
737 | case "mixin_content": |
||
738 | $content = $this->get(self::$namespaces["special"] . "content"); |
||
739 | if (is_null($content)) { |
||
740 | $this->throwError("Expected @content inside of mixin"); |
||
741 | } |
||
742 | |||
743 | $strongTypes = array('include', 'block', 'for', 'while'); |
||
744 | foreach ($content->children as $child) { |
||
745 | $this->storeEnv = (in_array($child[0], $strongTypes)) |
||
746 | ? null |
||
747 | : $content->scope; |
||
748 | |||
749 | $this->compileChild($child, $out); |
||
750 | } |
||
751 | |||
752 | unset($this->storeEnv); |
||
753 | break; |
||
754 | case "debug": |
||
755 | list(,$value, $pos) = $child; |
||
756 | $line = $this->parser->getLineNo($pos); |
||
757 | $value = $this->compileValue($this->reduce($value, true)); |
||
758 | fwrite(STDERR, "Line $line DEBUG: $value\n"); |
||
759 | break; |
||
760 | default: |
||
761 | $this->throwError("unknown child type: $child[0]"); |
||
762 | } |
||
763 | } |
||
764 | |||
765 | protected function expToString($exp) { |
||
766 | list(, $op, $left, $right, $inParens, $whiteLeft, $whiteRight) = $exp; |
||
767 | $content = array($this->reduce($left)); |
||
768 | if ($whiteLeft) $content[] = " "; |
||
769 | $content[] = $op; |
||
770 | if ($whiteRight) $content[] = " "; |
||
771 | $content[] = $this->reduce($right); |
||
772 | return array("string", "", $content); |
||
773 | } |
||
774 | |||
775 | protected function isTruthy($value) { |
||
778 | |||
779 | // should $value cause its operand to eval |
||
780 | protected function shouldEval($value) { |
||
781 | switch ($value[0]) { |
||
782 | case "exp": |
||
783 | if ($value[1] == "/") { |
||
784 | return $this->shouldEval($value[2], $value[3]); |
||
785 | } |
||
786 | case "var": |
||
787 | case "fncall": |
||
788 | return true; |
||
789 | } |
||
790 | return false; |
||
791 | } |
||
792 | |||
793 | protected function reduce($value, $inExp = false) { |
||
969 | |||
970 | public function normalizeValue($value) { |
||
971 | $value = $this->coerceForExpression($this->reduce($value)); |
||
972 | list($type) = $value; |
||
973 | |||
974 | switch ($type) { |
||
975 | case "list": |
||
976 | $value = $this->extractInterpolation($value); |
||
977 | if ($value[0] != "list") { |
||
978 | return array("keyword", $this->compileValue($value)); |
||
979 | } |
||
980 | foreach ($value[2] as $key => $item) { |
||
981 | $value[2][$key] = $this->normalizeValue($item); |
||
982 | } |
||
983 | return $value; |
||
984 | case "number": |
||
985 | return $this->normalizeNumber($value); |
||
986 | default: |
||
987 | return $value; |
||
988 | } |
||
989 | } |
||
990 | |||
991 | // just does physical lengths for now |
||
992 | protected function normalizeNumber($number) { |
||
993 | list(, $value, $unit) = $number; |
||
994 | if (isset(self::$unitTable["in"][$unit])) { |
||
995 | $conv = self::$unitTable["in"][$unit]; |
||
996 | return array("number", $value / $conv, "in"); |
||
997 | } |
||
998 | return $number; |
||
999 | } |
||
1000 | |||
1001 | // $number should be normalized |
||
1002 | protected function coerceUnit($number, $unit) { |
||
1003 | list(, $value, $baseUnit) = $number; |
||
1004 | if (isset(self::$unitTable[$baseUnit][$unit])) { |
||
1005 | $value = $value * self::$unitTable[$baseUnit][$unit]; |
||
1006 | } |
||
1007 | |||
1008 | return array("number", $value, $unit); |
||
1009 | } |
||
1010 | |||
1011 | protected function op_add_number_number($left, $right) { |
||
1014 | |||
1015 | protected function op_mul_number_number($left, $right) { |
||
1018 | |||
1019 | protected function op_sub_number_number($left, $right) { |
||
1022 | |||
1023 | protected function op_div_number_number($left, $right) { |
||
1026 | |||
1027 | protected function op_mod_number_number($left, $right) { |
||
1030 | |||
1031 | // adding strings |
||
1032 | protected function op_add($left, $right) { |
||
1033 | View Code Duplication | if ($strLeft = $this->coerceString($left)) { |
|
1034 | if ($right[0] == "string") { |
||
1035 | $right[1] = ""; |
||
1036 | } |
||
1037 | $strLeft[2][] = $right; |
||
1038 | return $strLeft; |
||
1039 | } |
||
1040 | |||
1041 | View Code Duplication | if ($strRight = $this->coerceString($right)) { |
|
1042 | if ($left[0] == "string") { |
||
1043 | $left[1] = ""; |
||
1044 | } |
||
1045 | array_unshift($strRight[2], $left); |
||
1046 | return $strRight; |
||
1047 | } |
||
1048 | } |
||
1049 | |||
1050 | protected function op_and($left, $right, $shouldEval) { |
||
1051 | if (!$shouldEval) return; |
||
1052 | if ($left != self::$false) return $right; |
||
1053 | return $left; |
||
1054 | } |
||
1055 | |||
1056 | protected function op_or($left, $right, $shouldEval) { |
||
1057 | if (!$shouldEval) return; |
||
1058 | if ($left != self::$false) return $left; |
||
1059 | return $right; |
||
1060 | } |
||
1061 | |||
1062 | protected function op_color_color($op, $left, $right) { |
||
1063 | $out = array('color'); |
||
1064 | foreach (range(1, 3) as $i) { |
||
1065 | $lval = isset($left[$i]) ? $left[$i] : 0; |
||
1066 | $rval = isset($right[$i]) ? $right[$i] : 0; |
||
1067 | switch ($op) { |
||
1068 | case '+': |
||
1069 | $out[] = $lval + $rval; |
||
1070 | break; |
||
1071 | case '-': |
||
1072 | $out[] = $lval - $rval; |
||
1073 | break; |
||
1074 | case '*': |
||
1075 | $out[] = $lval * $rval; |
||
1076 | break; |
||
1077 | case '%': |
||
1078 | $out[] = $lval % $rval; |
||
1079 | break; |
||
1080 | View Code Duplication | case '/': |
|
1081 | if ($rval == 0) { |
||
1082 | $this->throwError("color: Can't divide by zero"); |
||
1083 | } |
||
1084 | $out[] = $lval / $rval; |
||
1085 | break; |
||
1086 | case "==": |
||
1087 | return $this->op_eq($left, $right); |
||
1088 | case "!=": |
||
1089 | return $this->op_neq($left, $right); |
||
1090 | default: |
||
1091 | $this->throwError("color: unknown op $op"); |
||
1092 | } |
||
1093 | } |
||
1094 | |||
1095 | if (isset($left[4])) $out[4] = $left[4]; |
||
1096 | elseif (isset($right[4])) $out[4] = $right[4]; |
||
1097 | |||
1098 | return $this->fixColor($out); |
||
1099 | } |
||
1100 | |||
1101 | protected function op_color_number($op, $left, $right) { |
||
1102 | $value = $right[1]; |
||
1103 | return $this->op_color_color($op, $left, |
||
1104 | array("color", $value, $value, $value)); |
||
1105 | } |
||
1106 | |||
1107 | protected function op_number_color($op, $left, $right) { |
||
1108 | $value = $left[1]; |
||
1109 | return $this->op_color_color($op, |
||
1110 | array("color", $value, $value, $value), $right); |
||
1111 | } |
||
1112 | |||
1113 | protected function op_eq($left, $right) { |
||
1114 | if (($lStr = $this->coerceString($left)) && ($rStr = $this->coerceString($right))) { |
||
1115 | $lStr[1] = ""; |
||
1116 | $rStr[1] = ""; |
||
1117 | return $this->toBool($this->compileValue($lStr) == $this->compileValue($rStr)); |
||
1118 | } |
||
1119 | |||
1120 | return $this->toBool($left == $right); |
||
1121 | } |
||
1122 | |||
1123 | protected function op_neq($left, $right) { |
||
1126 | |||
1127 | protected function op_gte_number_number($left, $right) { |
||
1130 | |||
1131 | protected function op_gt_number_number($left, $right) { |
||
1134 | |||
1135 | protected function op_lte_number_number($left, $right) { |
||
1138 | |||
1139 | protected function op_lt_number_number($left, $right) { |
||
1142 | |||
1143 | public function toBool($thing) { |
||
1146 | |||
1147 | /** |
||
1148 | * Compiles a primitive value into a CSS property value. |
||
1149 | * |
||
1150 | * Values in scssphp are typed by being wrapped in arrays, their format is |
||
1151 | * typically: |
||
1152 | * |
||
1153 | * array(type, contents [, additional_contents]*) |
||
1154 | * |
||
1155 | * The input is expected to be reduced. This function will not work on |
||
1156 | * things like expressions and variables. |
||
1157 | * |
||
1158 | * @param array $value |
||
1159 | */ |
||
1160 | protected function compileValue($value) { |
||
1161 | $value = $this->reduce($value); |
||
1162 | |||
1163 | list($type) = $value; |
||
1164 | switch ($type) { |
||
1165 | case "keyword": |
||
1166 | return $value[1]; |
||
1167 | case "color": |
||
1168 | // [1] - red component (either number for a %) |
||
1169 | // [2] - green component |
||
1170 | // [3] - blue component |
||
1171 | // [4] - optional alpha component |
||
1172 | list(, $r, $g, $b) = $value; |
||
1173 | |||
1174 | $r = round($r); |
||
1175 | $g = round($g); |
||
1176 | $b = round($b); |
||
1177 | |||
1178 | View Code Duplication | if (count($value) == 5 && $value[4] != 1) { // rgba |
|
1179 | return 'rgba('.$r.', '.$g.', '.$b.', '.$value[4].')'; |
||
1180 | } |
||
1181 | |||
1182 | $h = sprintf("#%02x%02x%02x", $r, $g, $b); |
||
1183 | |||
1184 | // Converting hex color to short notation (e.g. #003399 to #039) |
||
1185 | View Code Duplication | if ($h[1] === $h[2] && $h[3] === $h[4] && $h[5] === $h[6]) { |
|
1186 | $h = '#' . $h[1] . $h[3] . $h[5]; |
||
1187 | } |
||
1188 | |||
1189 | return $h; |
||
1190 | case "number": |
||
1191 | return round($value[1], $this->numberPrecision) . $value[2]; |
||
1192 | case "string": |
||
1193 | return $value[1] . $this->compileStringContent($value) . $value[1]; |
||
1194 | case "function": |
||
1195 | $args = !empty($value[2]) ? $this->compileValue($value[2]) : ""; |
||
1196 | return "$value[1]($args)"; |
||
1197 | case "list": |
||
1198 | $value = $this->extractInterpolation($value); |
||
1199 | if ($value[0] != "list") return $this->compileValue($value); |
||
1200 | |||
1201 | list(, $delim, $items) = $value; |
||
1202 | |||
1203 | $filtered = array(); |
||
1204 | foreach ($items as $item) { |
||
1205 | if ($item[0] == "null") continue; |
||
1206 | $filtered[] = $this->compileValue($item); |
||
1207 | } |
||
1208 | |||
1209 | return implode("$delim ", $filtered); |
||
1210 | case "interpolated": # node created by extractInterpolation |
||
1211 | list(, $interpolate, $left, $right) = $value; |
||
1212 | list(,, $whiteLeft, $whiteRight) = $interpolate; |
||
1213 | |||
1214 | $left = count($left[2]) > 0 ? |
||
1215 | $this->compileValue($left).$whiteLeft : ""; |
||
1216 | |||
1217 | $right = count($right[2]) > 0 ? |
||
1218 | $whiteRight.$this->compileValue($right) : ""; |
||
1219 | |||
1220 | return $left.$this->compileValue($interpolate).$right; |
||
1221 | |||
1222 | case "interpolate": # raw parse node |
||
1223 | list(, $exp) = $value; |
||
1224 | |||
1225 | // strip quotes if it's a string |
||
1226 | $reduced = $this->reduce($exp); |
||
1227 | switch ($reduced[0]) { |
||
1228 | case "string": |
||
1229 | $reduced = array("keyword", |
||
1230 | $this->compileStringContent($reduced)); |
||
1231 | break; |
||
1232 | case "null": |
||
1233 | $reduced = array("keyword", ""); |
||
1234 | } |
||
1235 | |||
1236 | return $this->compileValue($reduced); |
||
1237 | case "null": |
||
1238 | return "null"; |
||
1239 | default: |
||
1240 | $this->throwError("unknown value type: $type"); |
||
1241 | } |
||
1242 | } |
||
1243 | |||
1244 | protected function compileStringContent($string) { |
||
1245 | $parts = array(); |
||
1246 | foreach ($string[2] as $part) { |
||
1247 | if (is_array($part)) { |
||
1248 | $parts[] = $this->compileValue($part); |
||
1249 | } else { |
||
1250 | $parts[] = $part; |
||
1251 | } |
||
1252 | } |
||
1253 | |||
1254 | return implode($parts); |
||
1255 | } |
||
1256 | |||
1257 | // doesn't need to be recursive, compileValue will handle that |
||
1258 | protected function extractInterpolation($list) { |
||
1259 | $items = $list[2]; |
||
1260 | foreach ($items as $i => $item) { |
||
1261 | if ($item[0] == "interpolate") { |
||
1262 | $before = array("list", $list[1], array_slice($items, 0, $i)); |
||
1263 | $after = array("list", $list[1], array_slice($items, $i + 1)); |
||
1264 | return array("interpolated", $item, $before, $after); |
||
1265 | } |
||
1266 | } |
||
1267 | return $list; |
||
1268 | } |
||
1269 | |||
1270 | // find the final set of selectors |
||
1271 | protected function multiplySelectors($env) { |
||
1272 | $envs = array(); |
||
1273 | while (null !== $env) { |
||
1274 | if (!empty($env->selectors)) { |
||
1275 | $envs[] = $env; |
||
1276 | } |
||
1277 | $env = $env->parent; |
||
1278 | }; |
||
1279 | |||
1280 | $selectors = array(); |
||
1281 | $parentSelectors = array(array()); |
||
1282 | while ($env = array_pop($envs)) { |
||
1283 | $selectors = array(); |
||
1284 | foreach ($env->selectors as $selector) { |
||
1285 | foreach ($parentSelectors as $parent) { |
||
1286 | $selectors[] = $this->joinSelectors($parent, $selector); |
||
1287 | } |
||
1288 | } |
||
1289 | $parentSelectors = $selectors; |
||
1290 | } |
||
1291 | |||
1292 | return $selectors; |
||
1293 | } |
||
1294 | |||
1295 | // looks for & to replace, or append parent before child |
||
1296 | protected function joinSelectors($parent, $child) { |
||
1297 | $setSelf = false; |
||
1298 | $out = array(); |
||
1299 | foreach ($child as $part) { |
||
1300 | $newPart = array(); |
||
1301 | foreach ($part as $p) { |
||
1302 | if ($p == self::$selfSelector) { |
||
1303 | $setSelf = true; |
||
1304 | foreach ($parent as $i => $parentPart) { |
||
1305 | if ($i > 0) { |
||
1306 | $out[] = $newPart; |
||
1307 | $newPart = array(); |
||
1308 | } |
||
1309 | |||
1310 | foreach ($parentPart as $pp) { |
||
1311 | $newPart[] = $pp; |
||
1312 | } |
||
1313 | } |
||
1314 | } else { |
||
1315 | $newPart[] = $p; |
||
1316 | } |
||
1317 | } |
||
1318 | |||
1319 | $out[] = $newPart; |
||
1320 | } |
||
1321 | |||
1322 | return $setSelf ? $out : array_merge($parent, $child); |
||
1323 | } |
||
1324 | |||
1325 | View Code Duplication | protected function multiplyMedia($env, $childQueries = null) { |
|
1326 | if (is_null($env) || |
||
1327 | !empty($env->block->type) && $env->block->type != "media") |
||
1328 | { |
||
1329 | return $childQueries; |
||
1330 | } |
||
1331 | |||
1332 | // plain old block, skip |
||
1333 | if (empty($env->block->type)) { |
||
1334 | return $this->multiplyMedia($env->parent, $childQueries); |
||
1335 | } |
||
1336 | |||
1337 | $parentQueries = $env->block->queryList; |
||
1338 | if ($childQueries == null) { |
||
1339 | $childQueries = $parentQueries; |
||
1340 | } else { |
||
1341 | $originalQueries = $childQueries; |
||
1342 | $childQueries = array(); |
||
1343 | |||
1344 | foreach ($parentQueries as $parentQuery){ |
||
1345 | foreach ($originalQueries as $childQuery) { |
||
1346 | $childQueries []= array_merge($parentQuery, $childQuery); |
||
1347 | } |
||
1348 | } |
||
1349 | } |
||
1350 | |||
1351 | return $this->multiplyMedia($env->parent, $childQueries); |
||
1352 | } |
||
1353 | |||
1354 | // convert something to list |
||
1355 | protected function coerceList($item, $delim = ",") { |
||
1362 | |||
1363 | protected function applyArguments($argDef, $argValues) { |
||
1364 | $hasVariable = false; |
||
1365 | $args = array(); |
||
1366 | foreach ($argDef as $i => $arg) { |
||
1367 | list($name, $default, $isVariable) = $argDef[$i]; |
||
1368 | $args[$name] = array($i, $name, $default, $isVariable); |
||
1369 | $hasVariable |= $isVariable; |
||
1370 | } |
||
1371 | |||
1372 | $keywordArgs = array(); |
||
1373 | $deferredKeywordArgs = array(); |
||
1374 | $remaining = array(); |
||
1375 | // assign the keyword args |
||
1376 | foreach ((array) $argValues as $arg) { |
||
1377 | if (!empty($arg[0])) { |
||
1378 | if (!isset($args[$arg[0][1]])) { |
||
1379 | if ($hasVariable) { |
||
1380 | $deferredKeywordArgs[$arg[0][1]] = $arg[1]; |
||
1381 | } else { |
||
1382 | $this->throwError("Mixin or function doesn't have an argument named $%s.", $arg[0][1]); |
||
1383 | } |
||
1384 | } elseif ($args[$arg[0][1]][0] < count($remaining)) { |
||
1385 | $this->throwError("The argument $%s was passed both by position and by name.", $arg[0][1]); |
||
1386 | } else { |
||
1387 | $keywordArgs[$arg[0][1]] = $arg[1]; |
||
1388 | } |
||
1389 | } elseif (count($keywordArgs)) { |
||
1390 | $this->throwError('Positional arguments must come before keyword arguments.'); |
||
1391 | } elseif ($arg[2] == true) { |
||
1392 | $val = $this->reduce($arg[1], true); |
||
1393 | if ($val[0] == "list") { |
||
1394 | foreach ($val[2] as $name => $item) { |
||
1395 | if (!is_numeric($name)) { |
||
1396 | $keywordArgs[$name] = $item; |
||
1397 | } else { |
||
1398 | $remaining[] = $item; |
||
1399 | } |
||
1400 | } |
||
1401 | } else { |
||
1402 | $remaining[] = $val; |
||
1403 | } |
||
1404 | } else { |
||
1405 | $remaining[] = $arg[1]; |
||
1406 | } |
||
1407 | } |
||
1408 | |||
1409 | foreach ($args as $arg) { |
||
1410 | list($i, $name, $default, $isVariable) = $arg; |
||
1411 | if ($isVariable) { |
||
1412 | $val = array("list", ",", array()); |
||
1413 | for ($count = count($remaining); $i < $count; $i++) { |
||
1414 | $val[2][] = $remaining[$i]; |
||
1415 | } |
||
1416 | foreach ($deferredKeywordArgs as $itemName => $item) { |
||
1417 | $val[2][$itemName] = $item; |
||
1418 | } |
||
1419 | } elseif (isset($remaining[$i])) { |
||
1420 | $val = $remaining[$i]; |
||
1421 | } elseif (isset($keywordArgs[$name])) { |
||
1422 | $val = $keywordArgs[$name]; |
||
1423 | } elseif (!empty($default)) { |
||
1424 | $val = $default; |
||
1425 | } else { |
||
1426 | $this->throwError("Missing argument $name"); |
||
1427 | } |
||
1428 | |||
1429 | $this->set($name, $this->reduce($val, true), true); |
||
1430 | } |
||
1431 | } |
||
1432 | |||
1433 | protected function pushEnv($block=null) { |
||
1434 | $env = new stdClass; |
||
1435 | $env->parent = $this->env; |
||
1436 | $env->store = array(); |
||
1437 | $env->block = $block; |
||
1438 | $env->depth = isset($this->env->depth) ? $this->env->depth + 1 : 0; |
||
1439 | |||
1440 | $this->env = $env; |
||
1441 | return $env; |
||
1442 | } |
||
1443 | |||
1444 | protected function normalizeName($name) { |
||
1447 | |||
1448 | protected function getStoreEnv() { |
||
1451 | |||
1452 | protected function set($name, $value, $shadow=false) { |
||
1453 | $name = $this->normalizeName($name); |
||
1454 | |||
1455 | if ($shadow) { |
||
1456 | $this->setRaw($name, $value); |
||
1457 | } else { |
||
1458 | $this->setExisting($name, $value); |
||
1459 | } |
||
1460 | } |
||
1461 | |||
1462 | protected function setExisting($name, $value, $env = null) { |
||
1471 | |||
1472 | protected function setRaw($name, $value) { |
||
1476 | |||
1477 | public function get($name, $defaultValue = null, $env = null) { |
||
1478 | $name = $this->normalizeName($name); |
||
1479 | |||
1480 | if (is_null($env)) $env = $this->getStoreEnv(); |
||
1481 | if (is_null($defaultValue)) $defaultValue = self::$defaultValue; |
||
1482 | |||
1483 | View Code Duplication | if (isset($env->store[$name])) { |
|
1484 | return $env->store[$name]; |
||
1485 | } elseif (isset($env->parent)) { |
||
1486 | return $this->get($name, $defaultValue, $env->parent); |
||
1487 | } |
||
1488 | |||
1489 | return $defaultValue; // found nothing |
||
1490 | } |
||
1491 | |||
1492 | protected function popEnv() { |
||
1493 | $env = $this->env; |
||
1494 | $this->env = $this->env->parent; |
||
1495 | return $env; |
||
1496 | } |
||
1497 | |||
1498 | public function getParsedFiles() { |
||
1501 | |||
1502 | public function addImportPath($path) { |
||
1505 | |||
1506 | public function setImportPaths($path) { |
||
1509 | |||
1510 | public function setNumberPrecision($numberPrecision) { |
||
1513 | |||
1514 | public function setFormatter($formatterName) { |
||
1517 | |||
1518 | public function registerFunction($name, $func) { |
||
1521 | |||
1522 | public function unregisterFunction($name) { |
||
1525 | |||
1526 | protected function importFile($path, $out) { |
||
1527 | // see if tree is cached |
||
1528 | $realPath = realpath($path); |
||
1529 | if (isset($this->importCache[$realPath])) { |
||
1530 | $tree = $this->importCache[$realPath]; |
||
1531 | } else { |
||
1532 | $code = file_get_contents($path); |
||
1533 | $parser = new scss_parser($path, false); |
||
1534 | $tree = $parser->parse($code); |
||
1535 | $this->parsedFiles[] = $path; |
||
1536 | |||
1537 | $this->importCache[$realPath] = $tree; |
||
1538 | } |
||
1539 | |||
1540 | $pi = pathinfo($path); |
||
1541 | array_unshift($this->importPaths, $pi['dirname']); |
||
1542 | $this->compileChildren($tree->children, $out); |
||
1543 | array_shift($this->importPaths); |
||
1544 | } |
||
1545 | |||
1546 | // results the file path for an import url if it exists |
||
1547 | public function findImport($url) { |
||
1548 | $urls = array(); |
||
1549 | |||
1550 | // for "normal" scss imports (ignore vanilla css and external requests) |
||
1551 | if (!preg_match('/\.css|^http:\/\/$/', $url)) { |
||
1552 | // try both normal and the _partial filename |
||
1553 | $urls = array($url, preg_replace('/[^\/]+$/', '_\0', $url)); |
||
1554 | } |
||
1555 | |||
1556 | foreach ($this->importPaths as $dir) { |
||
1557 | if (is_string($dir)) { |
||
1558 | // check urls for normal import paths |
||
1559 | foreach ($urls as $full) { |
||
1560 | $full = $dir . |
||
1561 | (!empty($dir) && substr($dir, -1) != '/' ? '/' : '') . |
||
1562 | $full; |
||
1563 | |||
1564 | if ($this->fileExists($file = $full.'.scss') || |
||
1565 | $this->fileExists($file = $full)) |
||
1566 | { |
||
1567 | return $file; |
||
1568 | } |
||
1569 | } |
||
1570 | } else { |
||
1571 | // check custom callback for import path |
||
1572 | $file = call_user_func($dir,$url,$this); |
||
1573 | if ($file !== null) { |
||
1574 | return $file; |
||
1575 | } |
||
1576 | } |
||
1577 | } |
||
1578 | |||
1579 | return null; |
||
1580 | } |
||
1581 | |||
1582 | protected function fileExists($name) { |
||
1585 | |||
1586 | protected function callBuiltin($name, $args, &$returnValue) { |
||
1587 | // try a lib function |
||
1588 | $name = $this->normalizeName($name); |
||
1589 | $libName = "lib_".$name; |
||
1590 | $f = array($this, $libName); |
||
1591 | $prototype = isset(self::$$libName) ? self::$$libName : null; |
||
1592 | |||
1593 | if (is_callable($f)) { |
||
1594 | $sorted = $this->sortArgs($prototype, $args); |
||
1595 | foreach ($sorted as &$val) { |
||
1596 | $val = $this->reduce($val, true); |
||
1597 | } |
||
1598 | $returnValue = call_user_func($f, $sorted, $this); |
||
1599 | } elseif (isset($this->userFunctions[$name])) { |
||
1600 | // see if we can find a user function |
||
1601 | $fn = $this->userFunctions[$name]; |
||
1602 | |||
1603 | foreach ($args as &$val) { |
||
1604 | $val = $this->reduce($val[1], true); |
||
1605 | } |
||
1606 | |||
1607 | $returnValue = call_user_func($fn, $args, $this); |
||
1608 | } |
||
1609 | |||
1610 | if (isset($returnValue)) { |
||
1611 | // coerce a php value into a scss one |
||
1612 | if (is_numeric($returnValue)) { |
||
1613 | $returnValue = array('number', $returnValue, ""); |
||
1614 | } elseif (is_bool($returnValue)) { |
||
1615 | $returnValue = $returnValue ? self::$true : self::$false; |
||
1616 | } elseif (!is_array($returnValue)) { |
||
1617 | $returnValue = array('keyword', $returnValue); |
||
1618 | } |
||
1619 | |||
1620 | return true; |
||
1621 | } |
||
1622 | |||
1623 | return false; |
||
1624 | } |
||
1625 | |||
1626 | // sorts any keyword arguments |
||
1627 | // TODO: merge with apply arguments |
||
1628 | protected function sortArgs($prototype, $args) { |
||
1629 | $keyArgs = array(); |
||
1630 | $posArgs = array(); |
||
1631 | |||
1632 | foreach ($args as $arg) { |
||
1633 | list($key, $value) = $arg; |
||
1634 | $key = $key[1]; |
||
1635 | if (empty($key)) { |
||
1636 | $posArgs[] = $value; |
||
1637 | } else { |
||
1638 | $keyArgs[$key] = $value; |
||
1639 | } |
||
1640 | } |
||
1641 | |||
1642 | if (is_null($prototype)) return $posArgs; |
||
1643 | |||
1644 | $finalArgs = array(); |
||
1645 | foreach ($prototype as $i => $names) { |
||
1646 | if (isset($posArgs[$i])) { |
||
1647 | $finalArgs[] = $posArgs[$i]; |
||
1648 | continue; |
||
1649 | } |
||
1650 | |||
1651 | $set = false; |
||
1652 | foreach ((array)$names as $name) { |
||
1653 | if (isset($keyArgs[$name])) { |
||
1654 | $finalArgs[] = $keyArgs[$name]; |
||
1655 | $set = true; |
||
1656 | break; |
||
1657 | } |
||
1658 | } |
||
1659 | |||
1660 | if (!$set) { |
||
1661 | $finalArgs[] = null; |
||
1662 | } |
||
1663 | } |
||
1664 | |||
1665 | return $finalArgs; |
||
1666 | } |
||
1667 | |||
1668 | protected function coerceForExpression($value) { |
||
1669 | if ($color = $this->coerceColor($value)) { |
||
1670 | return $color; |
||
1671 | } |
||
1672 | |||
1673 | return $value; |
||
1674 | } |
||
1675 | |||
1676 | protected function coerceColor($value) { |
||
1677 | switch ($value[0]) { |
||
1678 | case "color": return $value; |
||
1679 | case "keyword": |
||
1680 | $name = $value[1]; |
||
1681 | if (isset(self::$cssColors[$name])) { |
||
1682 | @list($r, $g, $b, $a) = explode(',', self::$cssColors[$name]); |
||
1683 | return isset($a) |
||
1684 | ? array('color', (int) $r, (int) $g, (int) $b, (int) $a) |
||
1685 | : array('color', (int) $r, (int) $g, (int) $b); |
||
1686 | } |
||
1687 | return null; |
||
1688 | } |
||
1689 | |||
1690 | return null; |
||
1691 | } |
||
1692 | |||
1693 | View Code Duplication | protected function coerceString($value) { |
|
1694 | switch ($value[0]) { |
||
1695 | case "string": |
||
1696 | return $value; |
||
1697 | case "keyword": |
||
1698 | return array("string", "", array($value[1])); |
||
1699 | } |
||
1700 | return null; |
||
1701 | } |
||
1702 | |||
1703 | public function assertList($value) { |
||
1704 | if ($value[0] != "list") |
||
1705 | $this->throwError("expecting list"); |
||
1706 | return $value; |
||
1707 | } |
||
1708 | |||
1709 | public function assertColor($value) { |
||
1713 | |||
1714 | public function assertNumber($value) { |
||
1715 | if ($value[0] != "number") |
||
1716 | $this->throwError("expecting number"); |
||
1717 | return $value[1]; |
||
1718 | } |
||
1719 | |||
1720 | protected function coercePercent($value) { |
||
1721 | if ($value[0] == "number") { |
||
1722 | if ($value[2] == "%") { |
||
1723 | return $value[1] / 100; |
||
1724 | } |
||
1725 | return $value[1]; |
||
1726 | } |
||
1727 | return 0; |
||
1728 | } |
||
1729 | |||
1730 | // make sure a color's components don't go out of bounds |
||
1731 | View Code Duplication | protected function fixColor($c) { |
|
1739 | |||
1740 | public function toHSL($red, $green, $blue) { |
||
1741 | $r = $red / 255; |
||
1742 | $g = $green / 255; |
||
1743 | $b = $blue / 255; |
||
1744 | |||
1745 | $min = min($r, $g, $b); |
||
1746 | $max = max($r, $g, $b); |
||
1747 | $d = $max - $min; |
||
1748 | $l = ($min + $max) / 2; |
||
1749 | |||
1750 | if ($min == $max) { |
||
1751 | $s = $h = 0; |
||
1752 | } else { |
||
1753 | if ($l < 0.5) |
||
1754 | $s = $d / (2 * $l); |
||
1755 | else |
||
1756 | $s = $d / (2 - 2 * $l); |
||
1757 | |||
1758 | if ($r == $max) |
||
1759 | $h = 60 * ($g - $b) / $d; |
||
1760 | elseif ($g == $max) |
||
1761 | $h = 60 * ($b - $r) / $d + 120; |
||
1762 | elseif ($b == $max) |
||
1763 | $h = 60 * ($r - $g) / $d + 240; |
||
1764 | } |
||
1765 | |||
1766 | return array('hsl', fmod($h, 360), $s * 100, $l * 100); |
||
1767 | } |
||
1768 | |||
1769 | public function hueToRGB($m1, $m2, $h) { |
||
1770 | if ($h < 0) |
||
1771 | $h += 1; |
||
1772 | elseif ($h > 1) |
||
1773 | $h -= 1; |
||
1774 | |||
1775 | View Code Duplication | if ($h * 6 < 1) |
|
1776 | return $m1 + ($m2 - $m1) * $h * 6; |
||
1777 | |||
1778 | if ($h * 2 < 1) |
||
1779 | return $m2; |
||
1780 | |||
1781 | View Code Duplication | if ($h * 3 < 2) |
|
1782 | return $m1 + ($m2 - $m1) * (2/3 - $h) * 6; |
||
1783 | |||
1784 | return $m1; |
||
1785 | } |
||
1786 | |||
1787 | // H from 0 to 360, S and L from 0 to 100 |
||
1788 | public function toRGB($hue, $saturation, $lightness) { |
||
1789 | if ($hue < 0) { |
||
1790 | $hue += 360; |
||
1791 | } |
||
1792 | |||
1793 | $h = $hue / 360; |
||
1794 | $s = min(100, max(0, $saturation)) / 100; |
||
1795 | $l = min(100, max(0, $lightness)) / 100; |
||
1796 | |||
1797 | $m2 = $l <= 0.5 ? $l * ($s + 1) : $l + $s - $l * $s; |
||
1798 | $m1 = $l * 2 - $m2; |
||
1799 | |||
1800 | $r = $this->hueToRGB($m1, $m2, $h + 1/3) * 255; |
||
1801 | $g = $this->hueToRGB($m1, $m2, $h) * 255; |
||
1802 | $b = $this->hueToRGB($m1, $m2, $h - 1/3) * 255; |
||
1803 | |||
1804 | $out = array('color', $r, $g, $b); |
||
1805 | return $out; |
||
1806 | } |
||
1807 | |||
1808 | // Built in functions |
||
1809 | |||
1810 | protected static $lib_if = array("condition", "if-true", "if-false"); |
||
1811 | protected function lib_if($args) { |
||
1812 | list($cond,$t, $f) = $args; |
||
1813 | if ($cond == self::$false) return $f; |
||
1814 | return $t; |
||
1815 | } |
||
1816 | |||
1817 | protected static $lib_index = array("list", "value"); |
||
1818 | protected function lib_index($args) { |
||
1819 | list($list, $value) = $args; |
||
1820 | $list = $this->assertList($list); |
||
1821 | |||
1822 | $values = array(); |
||
1823 | foreach ($list[2] as $item) { |
||
1824 | $values[] = $this->normalizeValue($item); |
||
1825 | } |
||
1826 | $key = array_search($this->normalizeValue($value), $values); |
||
1827 | |||
1828 | return false === $key ? false : $key + 1; |
||
1829 | } |
||
1830 | |||
1831 | protected static $lib_rgb = array("red", "green", "blue"); |
||
1832 | protected function lib_rgb($args) { |
||
1836 | |||
1837 | protected static $lib_rgba = array( |
||
1838 | array("red", "color"), |
||
1839 | "green", "blue", "alpha"); |
||
1840 | protected function lib_rgba($args) { |
||
1841 | if ($color = $this->coerceColor($args[0])) { |
||
1842 | $num = is_null($args[1]) ? $args[3] : $args[1]; |
||
1843 | $alpha = $this->assertNumber($num); |
||
1844 | $color[4] = $alpha; |
||
1845 | return $color; |
||
1846 | } |
||
1847 | |||
1848 | list($r,$g,$b, $a) = $args; |
||
1849 | return array("color", $r[1], $g[1], $b[1], $a[1]); |
||
1850 | } |
||
1851 | |||
1852 | // helper function for adjust_color, change_color, and scale_color |
||
1853 | protected function alter_color($args, $fn) { |
||
1854 | $color = $this->assertColor($args[0]); |
||
1855 | |||
1856 | foreach (array(1,2,3,7) as $i) { |
||
1857 | if (!is_null($args[$i])) { |
||
1858 | $val = $this->assertNumber($args[$i]); |
||
1859 | $ii = $i == 7 ? 4 : $i; // alpha |
||
1860 | $color[$ii] = |
||
1861 | $this->$fn(isset($color[$ii]) ? $color[$ii] : 0, $val, $i); |
||
1862 | } |
||
1863 | } |
||
1864 | |||
1865 | if (!is_null($args[4]) || !is_null($args[5]) || !is_null($args[6])) { |
||
1866 | $hsl = $this->toHSL($color[1], $color[2], $color[3]); |
||
1867 | foreach (array(4,5,6) as $i) { |
||
1868 | if (!is_null($args[$i])) { |
||
1869 | $val = $this->assertNumber($args[$i]); |
||
1870 | $hsl[$i - 3] = $this->$fn($hsl[$i - 3], $val, $i); |
||
1871 | } |
||
1872 | } |
||
1873 | |||
1874 | $rgb = $this->toRGB($hsl[1], $hsl[2], $hsl[3]); |
||
1875 | if (isset($color[4])) $rgb[4] = $color[4]; |
||
1876 | $color = $rgb; |
||
1877 | } |
||
1878 | |||
1879 | return $color; |
||
1880 | } |
||
1881 | |||
1882 | protected static $lib_adjust_color = array( |
||
1883 | "color", "red", "green", "blue", |
||
1884 | "hue", "saturation", "lightness", "alpha" |
||
1885 | ); |
||
1886 | protected function adjust_color_helper($base, $alter, $i) { |
||
1892 | |||
1893 | protected static $lib_change_color = array( |
||
1894 | "color", "red", "green", "blue", |
||
1895 | "hue", "saturation", "lightness", "alpha" |
||
1896 | ); |
||
1897 | protected function change_color_helper($base, $alter, $i) { |
||
1903 | |||
1904 | protected static $lib_scale_color = array( |
||
1905 | "color", "red", "green", "blue", |
||
1906 | "hue", "saturation", "lightness", "alpha" |
||
1907 | ); |
||
1908 | protected function scale_color_helper($base, $scale, $i) { |
||
1909 | // 1,2,3 - rgb |
||
1910 | // 4, 5, 6 - hsl |
||
1911 | // 7 - a |
||
1912 | switch ($i) { |
||
1913 | case 1: |
||
1914 | case 2: |
||
1915 | case 3: |
||
1916 | $max = 255; break; |
||
1917 | case 4: |
||
1918 | $max = 360; break; |
||
1919 | case 7: |
||
1920 | $max = 1; break; |
||
1921 | default: |
||
1922 | $max = 100; |
||
1923 | } |
||
1924 | |||
1925 | $scale = $scale / 100; |
||
1926 | if ($scale < 0) { |
||
1927 | return $base * $scale + $base; |
||
1928 | } else { |
||
1929 | return ($max - $base) * $scale + $base; |
||
1930 | } |
||
1931 | } |
||
1932 | protected function lib_scale_color($args) { |
||
1935 | |||
1936 | protected static $lib_ie_hex_str = array("color"); |
||
1937 | protected function lib_ie_hex_str($args) { |
||
1938 | $color = $this->coerceColor($args[0]); |
||
1939 | $color[4] = isset($color[4]) ? round(255*$color[4]) : 255; |
||
1940 | |||
1941 | return sprintf('#%02X%02X%02X%02X', $color[4], $color[1], $color[2], $color[3]); |
||
1942 | } |
||
1943 | |||
1944 | protected static $lib_red = array("color"); |
||
1945 | protected function lib_red($args) { |
||
1949 | |||
1950 | protected static $lib_green = array("color"); |
||
1951 | protected function lib_green($args) { |
||
1955 | |||
1956 | protected static $lib_blue = array("color"); |
||
1957 | protected function lib_blue($args) { |
||
1961 | |||
1962 | protected static $lib_alpha = array("color"); |
||
1963 | protected function lib_alpha($args) { |
||
1964 | if ($color = $this->coerceColor($args[0])) { |
||
1965 | return isset($color[4]) ? $color[4] : 1; |
||
1966 | } |
||
1967 | |||
1968 | // this might be the IE function, so return value unchanged |
||
1969 | return null; |
||
1970 | } |
||
1971 | |||
1972 | protected static $lib_opacity = array("color"); |
||
1973 | protected function lib_opacity($args) { |
||
1974 | $value = $args[0]; |
||
1975 | if ($value[0] === 'number') return null; |
||
1976 | return $this->lib_alpha($args); |
||
1977 | } |
||
1978 | |||
1979 | // mix two colors |
||
1980 | protected static $lib_mix = array("color-1", "color-2", "weight"); |
||
1981 | protected function lib_mix($args) { |
||
1982 | list($first, $second, $weight) = $args; |
||
1983 | $first = $this->assertColor($first); |
||
1984 | $second = $this->assertColor($second); |
||
1985 | |||
1986 | if (is_null($weight)) { |
||
1987 | $weight = 0.5; |
||
1988 | } else { |
||
1989 | $weight = $this->coercePercent($weight); |
||
1990 | } |
||
1991 | |||
1992 | $firstAlpha = isset($first[4]) ? $first[4] : 1; |
||
1993 | $secondAlpha = isset($second[4]) ? $second[4] : 1; |
||
1994 | |||
1995 | $w = $weight * 2 - 1; |
||
1996 | $a = $firstAlpha - $secondAlpha; |
||
1997 | |||
1998 | $w1 = (($w * $a == -1 ? $w : ($w + $a)/(1 + $w * $a)) + 1) / 2.0; |
||
1999 | $w2 = 1.0 - $w1; |
||
2000 | |||
2001 | $new = array('color', |
||
2002 | $w1 * $first[1] + $w2 * $second[1], |
||
2003 | $w1 * $first[2] + $w2 * $second[2], |
||
2004 | $w1 * $first[3] + $w2 * $second[3], |
||
2005 | ); |
||
2006 | |||
2007 | if ($firstAlpha != 1.0 || $secondAlpha != 1.0) { |
||
2008 | $new[] = $firstAlpha * $weight + $secondAlpha * ($weight - 1); |
||
2009 | } |
||
2010 | |||
2011 | return $this->fixColor($new); |
||
2012 | } |
||
2013 | |||
2014 | protected static $lib_hsl = array("hue", "saturation", "lightness"); |
||
2015 | protected function lib_hsl($args) { |
||
2019 | |||
2020 | protected static $lib_hsla = array("hue", "saturation", |
||
2021 | "lightness", "alpha"); |
||
2022 | protected function lib_hsla($args) { |
||
2023 | list($h, $s, $l, $a) = $args; |
||
2024 | $color = $this->toRGB($h[1], $s[1], $l[1]); |
||
2025 | $color[4] = $a[1]; |
||
2026 | return $color; |
||
2027 | } |
||
2028 | |||
2029 | protected static $lib_hue = array("color"); |
||
2030 | protected function lib_hue($args) { |
||
2031 | $color = $this->assertColor($args[0]); |
||
2032 | $hsl = $this->toHSL($color[1], $color[2], $color[3]); |
||
2033 | return array("number", $hsl[1], "deg"); |
||
2034 | } |
||
2035 | |||
2036 | protected static $lib_saturation = array("color"); |
||
2037 | protected function lib_saturation($args) { |
||
2038 | $color = $this->assertColor($args[0]); |
||
2039 | $hsl = $this->toHSL($color[1], $color[2], $color[3]); |
||
2040 | return array("number", $hsl[2], "%"); |
||
2041 | } |
||
2042 | |||
2043 | protected static $lib_lightness = array("color"); |
||
2044 | protected function lib_lightness($args) { |
||
2045 | $color = $this->assertColor($args[0]); |
||
2046 | $hsl = $this->toHSL($color[1], $color[2], $color[3]); |
||
2047 | return array("number", $hsl[3], "%"); |
||
2048 | } |
||
2049 | |||
2050 | protected function adjustHsl($color, $idx, $amount) { |
||
2051 | $hsl = $this->toHSL($color[1], $color[2], $color[3]); |
||
2052 | $hsl[$idx] += $amount; |
||
2053 | $out = $this->toRGB($hsl[1], $hsl[2], $hsl[3]); |
||
2054 | if (isset($color[4])) $out[4] = $color[4]; |
||
2055 | return $out; |
||
2056 | } |
||
2057 | |||
2058 | protected static $lib_adjust_hue = array("color", "degrees"); |
||
2059 | protected function lib_adjust_hue($args) { |
||
2060 | $color = $this->assertColor($args[0]); |
||
2061 | $degrees = $this->assertNumber($args[1]); |
||
2062 | return $this->adjustHsl($color, 1, $degrees); |
||
2063 | } |
||
2064 | |||
2065 | protected static $lib_lighten = array("color", "amount"); |
||
2066 | protected function lib_lighten($args) { |
||
2067 | $color = $this->assertColor($args[0]); |
||
2068 | $amount = 100*$this->coercePercent($args[1]); |
||
2069 | return $this->adjustHsl($color, 3, $amount); |
||
2070 | } |
||
2071 | |||
2072 | protected static $lib_darken = array("color", "amount"); |
||
2073 | protected function lib_darken($args) { |
||
2074 | $color = $this->assertColor($args[0]); |
||
2075 | $amount = 100*$this->coercePercent($args[1]); |
||
2076 | return $this->adjustHsl($color, 3, -$amount); |
||
2077 | } |
||
2078 | |||
2079 | protected static $lib_saturate = array("color", "amount"); |
||
2080 | protected function lib_saturate($args) { |
||
2081 | $value = $args[0]; |
||
2082 | if ($value[0] === 'number') return null; |
||
2083 | $color = $this->assertColor($value); |
||
2084 | $amount = 100*$this->coercePercent($args[1]); |
||
2085 | return $this->adjustHsl($color, 2, $amount); |
||
2086 | } |
||
2087 | |||
2088 | protected static $lib_desaturate = array("color", "amount"); |
||
2089 | protected function lib_desaturate($args) { |
||
2090 | $color = $this->assertColor($args[0]); |
||
2091 | $amount = 100*$this->coercePercent($args[1]); |
||
2092 | return $this->adjustHsl($color, 2, -$amount); |
||
2093 | } |
||
2094 | |||
2095 | protected static $lib_grayscale = array("color"); |
||
2096 | protected function lib_grayscale($args) { |
||
2097 | $value = $args[0]; |
||
2098 | if ($value[0] === 'number') return null; |
||
2099 | return $this->adjustHsl($this->assertColor($value), 2, -100); |
||
2100 | } |
||
2101 | |||
2102 | protected static $lib_complement = array("color"); |
||
2103 | protected function lib_complement($args) { |
||
2106 | |||
2107 | protected static $lib_invert = array("color"); |
||
2108 | protected function lib_invert($args) { |
||
2109 | $value = $args[0]; |
||
2110 | if ($value[0] === 'number') return null; |
||
2111 | $color = $this->assertColor($value); |
||
2112 | $color[1] = 255 - $color[1]; |
||
2113 | $color[2] = 255 - $color[2]; |
||
2114 | $color[3] = 255 - $color[3]; |
||
2115 | return $color; |
||
2116 | } |
||
2117 | |||
2118 | // increases opacity by amount |
||
2119 | protected static $lib_opacify = array("color", "amount"); |
||
2120 | View Code Duplication | protected function lib_opacify($args) { |
|
2121 | $color = $this->assertColor($args[0]); |
||
2122 | $amount = $this->coercePercent($args[1]); |
||
2123 | |||
2124 | $color[4] = (isset($color[4]) ? $color[4] : 1) + $amount; |
||
2125 | $color[4] = min(1, max(0, $color[4])); |
||
2126 | return $color; |
||
2127 | } |
||
2128 | |||
2129 | protected static $lib_fade_in = array("color", "amount"); |
||
2130 | protected function lib_fade_in($args) { |
||
2133 | |||
2134 | // decreases opacity by amount |
||
2135 | protected static $lib_transparentize = array("color", "amount"); |
||
2136 | View Code Duplication | protected function lib_transparentize($args) { |
|
2137 | $color = $this->assertColor($args[0]); |
||
2138 | $amount = $this->coercePercent($args[1]); |
||
2139 | |||
2140 | $color[4] = (isset($color[4]) ? $color[4] : 1) - $amount; |
||
2141 | $color[4] = min(1, max(0, $color[4])); |
||
2142 | return $color; |
||
2143 | } |
||
2144 | |||
2145 | protected static $lib_fade_out = array("color", "amount"); |
||
2146 | protected function lib_fade_out($args) { |
||
2149 | |||
2150 | protected static $lib_unquote = array("string"); |
||
2151 | protected function lib_unquote($args) { |
||
2152 | $str = $args[0]; |
||
2153 | if ($str[0] == "string") $str[1] = ""; |
||
2154 | return $str; |
||
2155 | } |
||
2156 | |||
2157 | protected static $lib_quote = array("string"); |
||
2158 | protected function lib_quote($args) { |
||
2159 | $value = $args[0]; |
||
2160 | if ($value[0] == "string" && !empty($value[1])) |
||
2161 | return $value; |
||
2162 | return array("string", '"', array($value)); |
||
2163 | } |
||
2164 | |||
2165 | protected static $lib_percentage = array("value"); |
||
2166 | protected function lib_percentage($args) { |
||
2167 | return array("number", |
||
2168 | $this->coercePercent($args[0]) * 100, |
||
2169 | "%"); |
||
2170 | } |
||
2171 | |||
2172 | protected static $lib_round = array("value"); |
||
2173 | protected function lib_round($args) { |
||
2174 | $num = $args[0]; |
||
2175 | $num[1] = round($num[1]); |
||
2176 | return $num; |
||
2177 | } |
||
2178 | |||
2179 | protected static $lib_floor = array("value"); |
||
2180 | protected function lib_floor($args) { |
||
2181 | $num = $args[0]; |
||
2182 | $num[1] = floor($num[1]); |
||
2183 | return $num; |
||
2184 | } |
||
2185 | |||
2186 | protected static $lib_ceil = array("value"); |
||
2187 | protected function lib_ceil($args) { |
||
2188 | $num = $args[0]; |
||
2189 | $num[1] = ceil($num[1]); |
||
2190 | return $num; |
||
2191 | } |
||
2192 | |||
2193 | protected static $lib_abs = array("value"); |
||
2194 | protected function lib_abs($args) { |
||
2195 | $num = $args[0]; |
||
2196 | $num[1] = abs($num[1]); |
||
2197 | return $num; |
||
2198 | } |
||
2199 | |||
2200 | View Code Duplication | protected function lib_min($args) { |
|
2211 | |||
2212 | View Code Duplication | protected function lib_max($args) { |
|
2223 | |||
2224 | protected function getNormalizedNumbers($args) { |
||
2225 | $unit = null; |
||
2226 | $originalUnit = null; |
||
2227 | $numbers = array(); |
||
2228 | foreach ($args as $key => $item) { |
||
2229 | if ('number' != $item[0]) { |
||
2230 | $this->throwError("%s is not a number", $item[0]); |
||
2231 | } |
||
2232 | $number = $this->normalizeNumber($item); |
||
2233 | |||
2234 | if (null === $unit) { |
||
2235 | $unit = $number[2]; |
||
2236 | $originalUnit = $item[2]; |
||
2237 | } elseif ($unit !== $number[2]) { |
||
2238 | $this->throwError('Incompatible units: "%s" and "%s".', $originalUnit, $item[2]); |
||
2239 | } |
||
2240 | |||
2241 | $numbers[$key] = $number; |
||
2242 | } |
||
2243 | |||
2244 | return $numbers; |
||
2245 | } |
||
2246 | |||
2247 | protected static $lib_length = array("list"); |
||
2248 | protected function lib_length($args) { |
||
2252 | |||
2253 | protected static $lib_nth = array("list", "n"); |
||
2254 | protected function lib_nth($args) { |
||
2255 | $list = $this->coerceList($args[0]); |
||
2256 | $n = $this->assertNumber($args[1]) - 1; |
||
2257 | return isset($list[2][$n]) ? $list[2][$n] : self::$defaultValue; |
||
2258 | } |
||
2259 | |||
2260 | protected function listSeparatorForJoin($list1, $sep) { |
||
2271 | |||
2272 | protected static $lib_join = array("list1", "list2", "separator"); |
||
2273 | protected function lib_join($args) { |
||
2274 | list($list1, $list2, $sep) = $args; |
||
2275 | $list1 = $this->coerceList($list1, " "); |
||
2276 | $list2 = $this->coerceList($list2, " "); |
||
2277 | $sep = $this->listSeparatorForJoin($list1, $sep); |
||
2278 | return array("list", $sep, array_merge($list1[2], $list2[2])); |
||
2279 | } |
||
2280 | |||
2281 | protected static $lib_append = array("list", "val", "separator"); |
||
2282 | protected function lib_append($args) { |
||
2283 | list($list1, $value, $sep) = $args; |
||
2284 | $list1 = $this->coerceList($list1, " "); |
||
2285 | $sep = $this->listSeparatorForJoin($list1, $sep); |
||
2286 | return array("list", $sep, array_merge($list1[2], array($value))); |
||
2287 | } |
||
2288 | |||
2289 | protected function lib_zip($args) { |
||
2290 | foreach ($args as $arg) { |
||
2291 | $this->assertList($arg); |
||
2292 | } |
||
2293 | |||
2294 | $lists = array(); |
||
2295 | $firstList = array_shift($args); |
||
2296 | foreach ($firstList[2] as $key => $item) { |
||
2297 | $list = array("list", "", array($item)); |
||
2298 | foreach ($args as $arg) { |
||
2299 | if (isset($arg[2][$key])) { |
||
2300 | $list[2][] = $arg[2][$key]; |
||
2301 | } else { |
||
2302 | break 2; |
||
2303 | } |
||
2304 | } |
||
2305 | $lists[] = $list; |
||
2306 | } |
||
2307 | |||
2308 | return array("list", ",", $lists); |
||
2309 | } |
||
2310 | |||
2311 | protected static $lib_type_of = array("value"); |
||
2312 | protected function lib_type_of($args) { |
||
2313 | $value = $args[0]; |
||
2314 | switch ($value[0]) { |
||
2315 | case "keyword": |
||
2316 | if ($value == self::$true || $value == self::$false) { |
||
2317 | return "bool"; |
||
2318 | } |
||
2319 | |||
2320 | if ($this->coerceColor($value)) { |
||
2321 | return "color"; |
||
2322 | } |
||
2323 | |||
2324 | return "string"; |
||
2325 | default: |
||
2326 | return $value[0]; |
||
2327 | } |
||
2328 | } |
||
2329 | |||
2330 | protected static $lib_unit = array("number"); |
||
2331 | protected function lib_unit($args) { |
||
2332 | $num = $args[0]; |
||
2333 | if ($num[0] == "number") { |
||
2334 | return array("string", '"', array($num[2])); |
||
2335 | } |
||
2336 | return ""; |
||
2337 | } |
||
2338 | |||
2339 | protected static $lib_unitless = array("number"); |
||
2340 | protected function lib_unitless($args) { |
||
2344 | |||
2345 | protected static $lib_comparable = array("number-1", "number-2"); |
||
2346 | protected function lib_comparable($args) { |
||
2347 | list($number1, $number2) = $args; |
||
2348 | if (!isset($number1[0]) || $number1[0] != "number" || !isset($number2[0]) || $number2[0] != "number") { |
||
2349 | $this->throwError('Invalid argument(s) for "comparable"'); |
||
2350 | } |
||
2351 | |||
2352 | $number1 = $this->normalizeNumber($number1); |
||
2353 | $number2 = $this->normalizeNumber($number2); |
||
2354 | |||
2355 | return $number1[2] == $number2[2] || $number1[2] == "" || $number2[2] == ""; |
||
2356 | } |
||
2357 | |||
2358 | /** |
||
2359 | * Workaround IE7's content counter bug. |
||
2360 | * |
||
2361 | * @param array $args |
||
2362 | */ |
||
2363 | protected function lib_counter($args) { |
||
2367 | |||
2368 | public function throwError($msg = null) { |
||
2379 | |||
2380 | /** |
||
2381 | * CSS Colors |
||
2382 | * |
||
2383 | * @see http://www.w3.org/TR/css3-color |
||
2384 | */ |
||
2385 | static protected $cssColors = array( |
||
2386 | 'aliceblue' => '240,248,255', |
||
2387 | 'antiquewhite' => '250,235,215', |
||
2388 | 'aqua' => '0,255,255', |
||
2389 | 'aquamarine' => '127,255,212', |
||
2390 | 'azure' => '240,255,255', |
||
2391 | 'beige' => '245,245,220', |
||
2392 | 'bisque' => '255,228,196', |
||
2393 | 'black' => '0,0,0', |
||
2394 | 'blanchedalmond' => '255,235,205', |
||
2395 | 'blue' => '0,0,255', |
||
2396 | 'blueviolet' => '138,43,226', |
||
2397 | 'brown' => '165,42,42', |
||
2398 | 'burlywood' => '222,184,135', |
||
2399 | 'cadetblue' => '95,158,160', |
||
2400 | 'chartreuse' => '127,255,0', |
||
2401 | 'chocolate' => '210,105,30', |
||
2402 | 'coral' => '255,127,80', |
||
2403 | 'cornflowerblue' => '100,149,237', |
||
2404 | 'cornsilk' => '255,248,220', |
||
2405 | 'crimson' => '220,20,60', |
||
2406 | 'cyan' => '0,255,255', |
||
2407 | 'darkblue' => '0,0,139', |
||
2408 | 'darkcyan' => '0,139,139', |
||
2409 | 'darkgoldenrod' => '184,134,11', |
||
2410 | 'darkgray' => '169,169,169', |
||
2411 | 'darkgreen' => '0,100,0', |
||
2412 | 'darkgrey' => '169,169,169', |
||
2413 | 'darkkhaki' => '189,183,107', |
||
2414 | 'darkmagenta' => '139,0,139', |
||
2415 | 'darkolivegreen' => '85,107,47', |
||
2416 | 'darkorange' => '255,140,0', |
||
2417 | 'darkorchid' => '153,50,204', |
||
2418 | 'darkred' => '139,0,0', |
||
2419 | 'darksalmon' => '233,150,122', |
||
2420 | 'darkseagreen' => '143,188,143', |
||
2421 | 'darkslateblue' => '72,61,139', |
||
2422 | 'darkslategray' => '47,79,79', |
||
2423 | 'darkslategrey' => '47,79,79', |
||
2424 | 'darkturquoise' => '0,206,209', |
||
2425 | 'darkviolet' => '148,0,211', |
||
2426 | 'deeppink' => '255,20,147', |
||
2427 | 'deepskyblue' => '0,191,255', |
||
2428 | 'dimgray' => '105,105,105', |
||
2429 | 'dimgrey' => '105,105,105', |
||
2430 | 'dodgerblue' => '30,144,255', |
||
2431 | 'firebrick' => '178,34,34', |
||
2432 | 'floralwhite' => '255,250,240', |
||
2433 | 'forestgreen' => '34,139,34', |
||
2434 | 'fuchsia' => '255,0,255', |
||
2435 | 'gainsboro' => '220,220,220', |
||
2436 | 'ghostwhite' => '248,248,255', |
||
2437 | 'gold' => '255,215,0', |
||
2438 | 'goldenrod' => '218,165,32', |
||
2439 | 'gray' => '128,128,128', |
||
2440 | 'green' => '0,128,0', |
||
2441 | 'greenyellow' => '173,255,47', |
||
2442 | 'grey' => '128,128,128', |
||
2443 | 'honeydew' => '240,255,240', |
||
2444 | 'hotpink' => '255,105,180', |
||
2445 | 'indianred' => '205,92,92', |
||
2446 | 'indigo' => '75,0,130', |
||
2447 | 'ivory' => '255,255,240', |
||
2448 | 'khaki' => '240,230,140', |
||
2449 | 'lavender' => '230,230,250', |
||
2450 | 'lavenderblush' => '255,240,245', |
||
2451 | 'lawngreen' => '124,252,0', |
||
2452 | 'lemonchiffon' => '255,250,205', |
||
2453 | 'lightblue' => '173,216,230', |
||
2454 | 'lightcoral' => '240,128,128', |
||
2455 | 'lightcyan' => '224,255,255', |
||
2456 | 'lightgoldenrodyellow' => '250,250,210', |
||
2457 | 'lightgray' => '211,211,211', |
||
2458 | 'lightgreen' => '144,238,144', |
||
2459 | 'lightgrey' => '211,211,211', |
||
2460 | 'lightpink' => '255,182,193', |
||
2461 | 'lightsalmon' => '255,160,122', |
||
2462 | 'lightseagreen' => '32,178,170', |
||
2463 | 'lightskyblue' => '135,206,250', |
||
2464 | 'lightslategray' => '119,136,153', |
||
2465 | 'lightslategrey' => '119,136,153', |
||
2466 | 'lightsteelblue' => '176,196,222', |
||
2467 | 'lightyellow' => '255,255,224', |
||
2468 | 'lime' => '0,255,0', |
||
2469 | 'limegreen' => '50,205,50', |
||
2470 | 'linen' => '250,240,230', |
||
2471 | 'magenta' => '255,0,255', |
||
2472 | 'maroon' => '128,0,0', |
||
2473 | 'mediumaquamarine' => '102,205,170', |
||
2474 | 'mediumblue' => '0,0,205', |
||
2475 | 'mediumorchid' => '186,85,211', |
||
2476 | 'mediumpurple' => '147,112,219', |
||
2477 | 'mediumseagreen' => '60,179,113', |
||
2478 | 'mediumslateblue' => '123,104,238', |
||
2479 | 'mediumspringgreen' => '0,250,154', |
||
2480 | 'mediumturquoise' => '72,209,204', |
||
2481 | 'mediumvioletred' => '199,21,133', |
||
2482 | 'midnightblue' => '25,25,112', |
||
2483 | 'mintcream' => '245,255,250', |
||
2484 | 'mistyrose' => '255,228,225', |
||
2485 | 'moccasin' => '255,228,181', |
||
2486 | 'navajowhite' => '255,222,173', |
||
2487 | 'navy' => '0,0,128', |
||
2488 | 'oldlace' => '253,245,230', |
||
2489 | 'olive' => '128,128,0', |
||
2490 | 'olivedrab' => '107,142,35', |
||
2491 | 'orange' => '255,165,0', |
||
2492 | 'orangered' => '255,69,0', |
||
2493 | 'orchid' => '218,112,214', |
||
2494 | 'palegoldenrod' => '238,232,170', |
||
2495 | 'palegreen' => '152,251,152', |
||
2496 | 'paleturquoise' => '175,238,238', |
||
2497 | 'palevioletred' => '219,112,147', |
||
2498 | 'papayawhip' => '255,239,213', |
||
2499 | 'peachpuff' => '255,218,185', |
||
2500 | 'peru' => '205,133,63', |
||
2501 | 'pink' => '255,192,203', |
||
2502 | 'plum' => '221,160,221', |
||
2503 | 'powderblue' => '176,224,230', |
||
2504 | 'purple' => '128,0,128', |
||
2505 | 'red' => '255,0,0', |
||
2506 | 'rosybrown' => '188,143,143', |
||
2507 | 'royalblue' => '65,105,225', |
||
2508 | 'saddlebrown' => '139,69,19', |
||
2509 | 'salmon' => '250,128,114', |
||
2510 | 'sandybrown' => '244,164,96', |
||
2511 | 'seagreen' => '46,139,87', |
||
2512 | 'seashell' => '255,245,238', |
||
2513 | 'sienna' => '160,82,45', |
||
2514 | 'silver' => '192,192,192', |
||
2515 | 'skyblue' => '135,206,235', |
||
2516 | 'slateblue' => '106,90,205', |
||
2517 | 'slategray' => '112,128,144', |
||
2518 | 'slategrey' => '112,128,144', |
||
2519 | 'snow' => '255,250,250', |
||
2520 | 'springgreen' => '0,255,127', |
||
2521 | 'steelblue' => '70,130,180', |
||
2522 | 'tan' => '210,180,140', |
||
2523 | 'teal' => '0,128,128', |
||
2524 | 'thistle' => '216,191,216', |
||
2525 | 'tomato' => '255,99,71', |
||
2526 | 'transparent' => '0,0,0,0', |
||
2527 | 'turquoise' => '64,224,208', |
||
2528 | 'violet' => '238,130,238', |
||
2529 | 'wheat' => '245,222,179', |
||
2530 | 'white' => '255,255,255', |
||
2531 | 'whitesmoke' => '245,245,245', |
||
2532 | 'yellow' => '255,255,0', |
||
2533 | 'yellowgreen' => '154,205,50' |
||
2534 | ); |
||
2535 | } |
||
2536 | |||
4384 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: