scssc

SCSS compiler.

Defined (1)

The class is defined in the following location(s).

/modules/custom-css/custom-css/preprocessors/scss.inc.php  
  1. class scssc { 
  2. static public $VERSION = "v0.0.9"; 
  3.  
  4. static protected $operatorNames = array( 
  5. '+' => "add",  
  6. '-' => "sub",  
  7. '*' => "mul",  
  8. '/' => "div",  
  9. '%' => "mod",  
  10.  
  11. '==' => "eq",  
  12. '!=' => "neq",  
  13. '<' => "lt",  
  14. '>' => "gt",  
  15.  
  16. '<=' => "lte",  
  17. '>=' => "gte",  
  18. ); 
  19.  
  20. static protected $namespaces = array( 
  21. "special" => "%",  
  22. "mixin" => "@",  
  23. "function" => "^",  
  24. ); 
  25.  
  26. static protected $unitTable = array( 
  27. "in" => array( 
  28. "in" => 1,  
  29. "pt" => 72,  
  30. "pc" => 6,  
  31. "cm" => 2.54,  
  32. "mm" => 25.4,  
  33. "px" => 96,  
  34. ); 
  35.  
  36. static public $true = array("keyword", "true"); 
  37. static public $false = array("keyword", "false"); 
  38. static public $null = array("null"); 
  39.  
  40. static public $defaultValue = array("keyword", ""); 
  41. static public $selfSelector = array("self"); 
  42.  
  43. protected $importPaths = array(""); 
  44. protected $importCache = array(); 
  45.  
  46. protected $userFunctions = array(); 
  47.  
  48. protected $numberPrecision = 5; 
  49.  
  50. protected $formatter = "scss_formatter_nested"; 
  51.  
  52. public function compile($code, $name=null) { 
  53. $this->indentLevel = -1; 
  54. $this->commentsSeen = array(); 
  55. $this->extends = array(); 
  56. $this->extendsMap = array(); 
  57.  
  58. $locale = setlocale(LC_NUMERIC, 0); 
  59. setlocale(LC_NUMERIC, "C"); 
  60.  
  61. $this->parsedFiles = array(); 
  62. $this->parser = new scss_parser($name); 
  63. $tree = $this->parser->parse($code); 
  64.  
  65. $this->formatter = new $this->formatter(); 
  66.  
  67. $this->env = null; 
  68. $this->scope = null; 
  69.  
  70. $this->compileRoot($tree); 
  71.  
  72. $out = $this->formatter->format($this->scope); 
  73.  
  74. setlocale(LC_NUMERIC, $locale); 
  75. return $out; 
  76.  
  77. protected function isSelfExtend($target, $origin) { 
  78. foreach ($origin as $sel) { 
  79. if (in_array($target, $sel)) { 
  80. return true; 
  81.  
  82. return false; 
  83.  
  84. protected function pushExtends($target, $origin) { 
  85. if ($this->isSelfExtend($target, $origin)) { 
  86. return; 
  87.  
  88. $i = count($this->extends); 
  89. $this->extends[] = array($target, $origin); 
  90.  
  91. foreach ($target as $part) { 
  92. if (isset($this->extendsMap[$part])) { 
  93. $this->extendsMap[$part][] = $i; 
  94. } else { 
  95. $this->extendsMap[$part] = array($i); 
  96.  
  97. protected function makeOutputBlock($type, $selectors = null) { 
  98. $out = new stdClass; 
  99. $out->type = $type; 
  100. $out->lines = array(); 
  101. $out->children = array(); 
  102. $out->parent = $this->scope; 
  103. $out->selectors = $selectors; 
  104. $out->depth = $this->env->depth; 
  105.  
  106. return $out; 
  107.  
  108. protected function matchExtendsSingle($single, &$outOrigin) { 
  109. $counts = array(); 
  110. foreach ($single as $part) { 
  111. if (!is_string($part)) return false; // hmm 
  112.  
  113. if (isset($this->extendsMap[$part])) { 
  114. foreach ($this->extendsMap[$part] as $idx) { 
  115. $counts[$idx] = 
  116. isset($counts[$idx]) ? $counts[$idx] + 1 : 1; 
  117.  
  118. $outOrigin = array(); 
  119. $found = false; 
  120.  
  121. foreach ($counts as $idx => $count) { 
  122. list($target, $origin) = $this->extends[$idx]; 
  123.  
  124. // check count 
  125. if ($count != count($target)) continue; 
  126.  
  127. // check if target is subset of single 
  128. if (array_diff(array_intersect($single, $target), $target)) continue; 
  129.  
  130. $rem = array_diff($single, $target); 
  131.  
  132. foreach ($origin as $j => $new) { 
  133. // prevent infinite loop when target extends itself 
  134. foreach ($new as $new_selector) { 
  135. if (!array_diff($single, $new_selector)) { 
  136. continue 2; 
  137.  
  138. $origin[$j][count($origin[$j]) - 1] = $this->combineSelectorSingle(end($new), $rem); 
  139.  
  140. $outOrigin = array_merge($outOrigin, $origin); 
  141.  
  142. $found = true; 
  143.  
  144. return $found; 
  145.  
  146. protected function combineSelectorSingle($base, $other) { 
  147. $tag = null; 
  148. $out = array(); 
  149.  
  150. foreach (array($base, $other) as $single) { 
  151. foreach ($single as $part) { 
  152. if (preg_match('/^[^\[.#:]/', $part)) { 
  153. $tag = $part; 
  154. } else { 
  155. $out[] = $part; 
  156.  
  157. if ($tag) { 
  158. array_unshift($out, $tag); 
  159.  
  160. return $out; 
  161.  
  162. protected function matchExtends($selector, &$out, $from = 0, $initial=true) { 
  163. foreach ($selector as $i => $part) { 
  164. if ($i < $from) continue; 
  165.  
  166. if ($this->matchExtendsSingle($part, $origin)) { 
  167. $before = array_slice($selector, 0, $i); 
  168. $after = array_slice($selector, $i + 1); 
  169.  
  170. foreach ($origin as $new) { 
  171. $k = 0; 
  172.  
  173. // remove shared parts 
  174. if ($initial) { 
  175. foreach ($before as $k => $val) { 
  176. if (!isset($new[$k]) || $val != $new[$k]) { 
  177. break; 
  178.  
  179. $result = array_merge( 
  180. $before,  
  181. $k > 0 ? array_slice($new, $k) : $new,  
  182. $after); 
  183.  
  184.  
  185. if ($result == $selector) continue; 
  186. $out[] = $result; 
  187.  
  188. // recursively check for more matches 
  189. $this->matchExtends($result, $out, $i, false); 
  190.  
  191. // selector sequence merging 
  192. if (!empty($before) && count($new) > 1) { 
  193. $result2 = array_merge( 
  194. array_slice($new, 0, -1),  
  195. $k > 0 ? array_slice($before, $k) : $before,  
  196. array_slice($new, -1),  
  197. $after); 
  198.  
  199. $out[] = $result2; 
  200.  
  201. protected function flattenSelectors($block, $parentKey = null) { 
  202. if ($block->selectors) { 
  203. $selectors = array(); 
  204. foreach ($block->selectors as $s) { 
  205. $selectors[] = $s; 
  206. if (!is_array($s)) continue; 
  207. // check extends 
  208. if (!empty($this->extendsMap)) { 
  209. $this->matchExtends($s, $selectors); 
  210.  
  211. $block->selectors = array(); 
  212. $placeholderSelector = false; 
  213. foreach ($selectors as $selector) { 
  214. if ($this->hasSelectorPlaceholder($selector)) { 
  215. $placeholderSelector = true; 
  216. continue; 
  217. $block->selectors[] = $this->compileSelector($selector); 
  218.  
  219. if ($placeholderSelector && 0 == count($block->selectors) && null !== $parentKey) { 
  220. unset($block->parent->children[$parentKey]); 
  221. return; 
  222.  
  223. foreach ($block->children as $key => $child) { 
  224. $this->flattenSelectors($child, $key); 
  225.  
  226. protected function compileRoot($rootBlock) { 
  227. $this->pushEnv($rootBlock); 
  228. $this->scope = $this->makeOutputBlock("root"); 
  229.  
  230. $this->compileChildren($rootBlock->children, $this->scope); 
  231. $this->flattenSelectors($this->scope); 
  232.  
  233. $this->popEnv(); 
  234.  
  235. protected function compileMedia($media) { 
  236. $this->pushEnv($media); 
  237. $parentScope = $this->mediaParent($this->scope); 
  238.  
  239. $this->scope = $this->makeOutputBlock("media", array( 
  240. $this->compileMediaQuery($this->multiplyMedia($this->env))) 
  241. ); 
  242.  
  243. $parentScope->children[] = $this->scope; 
  244.  
  245. // top level properties in a media cause it to be wrapped 
  246. $needsWrap = false; 
  247. foreach ($media->children as $child) { 
  248. $type = $child[0]; 
  249. if ($type !== 'block' && $type !== 'media' && $type !== 'directive') { 
  250. $needsWrap = true; 
  251. break; 
  252.  
  253. if ($needsWrap) { 
  254. $wrapped = (object)array( 
  255. "selectors" => array(),  
  256. "children" => $media->children 
  257. ); 
  258. $media->children = array(array("block", $wrapped)); 
  259.  
  260. $this->compileChildren($media->children, $this->scope); 
  261.  
  262. $this->scope = $this->scope->parent; 
  263. $this->popEnv(); 
  264.  
  265. protected function mediaParent($scope) { 
  266. while (!empty($scope->parent)) { 
  267. if (!empty($scope->type) && $scope->type != "media") { 
  268. break; 
  269. $scope = $scope->parent; 
  270.  
  271. return $scope; 
  272.  
  273. // TODO refactor compileNestedBlock and compileMedia into same thing 
  274. protected function compileNestedBlock($block, $selectors) { 
  275. $this->pushEnv($block); 
  276.  
  277. $this->scope = $this->makeOutputBlock($block->type, $selectors); 
  278. $this->scope->parent->children[] = $this->scope; 
  279. $this->compileChildren($block->children, $this->scope); 
  280.  
  281. $this->scope = $this->scope->parent; 
  282. $this->popEnv(); 
  283.  
  284. /** 
  285. * Recursively compiles a block. 
  286. * A block is analogous to a CSS block in most cases. A single SCSS document 
  287. * is encapsulated in a block when parsed, but it does not have parent tags 
  288. * so all of its children appear on the root level when compiled. 
  289. * Blocks are made up of selectors and children. 
  290. * The children of a block are just all the blocks that are defined within. 
  291. * Compiling the block involves pushing a fresh environment on the stack,  
  292. * and iterating through the props, compiling each one. 
  293. * @see scss::compileChild() 
  294. * @param \StdClass $block 
  295. */ 
  296. protected function compileBlock($block) { 
  297. $env = $this->pushEnv($block); 
  298.  
  299. $env->selectors = 
  300. array_map(array($this, "evalSelector"), $block->selectors); 
  301.  
  302. $out = $this->makeOutputBlock(null, $this->multiplySelectors($env)); 
  303. $this->scope->children[] = $out; 
  304. $this->compileChildren($block->children, $out); 
  305.  
  306. $this->popEnv(); 
  307.  
  308. // joins together .classes and #ids 
  309. protected function flattenSelectorSingle($single) { 
  310. $joined = array(); 
  311. foreach ($single as $part) { 
  312. if (empty($joined) || 
  313. !is_string($part) || 
  314. preg_match('/[\[.:#%]/', $part)) 
  315. $joined[] = $part; 
  316. continue; 
  317.  
  318. if (is_array(end($joined))) { 
  319. $joined[] = $part; 
  320. } else { 
  321. $joined[count($joined) - 1] .= $part; 
  322.  
  323. return $joined; 
  324.  
  325. // replaces all the interpolates 
  326. protected function evalSelector($selector) { 
  327. return array_map(array($this, "evalSelectorPart"), $selector); 
  328.  
  329. protected function evalSelectorPart($piece) { 
  330. foreach ($piece as &$p) { 
  331. if (!is_array($p)) continue; 
  332.  
  333. switch ($p[0]) { 
  334. case "interpolate": 
  335. $p = $this->compileValue($p); 
  336. break; 
  337. case "string": 
  338. $p = $this->compileValue($p); 
  339. break; 
  340.  
  341. return $this->flattenSelectorSingle($piece); 
  342.  
  343. // compiles to string 
  344. // self(&) should have been replaced by now 
  345. protected function compileSelector($selector) { 
  346. if (!is_array($selector)) return $selector; // media and the like 
  347.  
  348. return implode(" ", array_map( 
  349. array($this, "compileSelectorPart"), $selector)); 
  350.  
  351. protected function compileSelectorPart($piece) { 
  352. foreach ($piece as &$p) { 
  353. if (!is_array($p)) continue; 
  354.  
  355. switch ($p[0]) { 
  356. case "self": 
  357. $p = "&"; 
  358. break; 
  359. default: 
  360. $p = $this->compileValue($p); 
  361. break; 
  362.  
  363. return implode($piece); 
  364.  
  365. protected function hasSelectorPlaceholder($selector) 
  366. if (!is_array($selector)) return false; 
  367.  
  368. foreach ($selector as $parts) { 
  369. foreach ($parts as $part) { 
  370. if ('%' == $part[0]) { 
  371. return true; 
  372.  
  373. return false; 
  374.  
  375. protected function compileChildren($stms, $out) { 
  376. foreach ($stms as $stm) { 
  377. $ret = $this->compileChild($stm, $out); 
  378. if (!is_null($ret)) return $ret; 
  379.  
  380. protected function compileMediaQuery($queryList) { 
  381. $out = "@media"; 
  382. $first = true; 
  383. foreach ($queryList as $query) { 
  384. $parts = array(); 
  385. foreach ($query as $q) { 
  386. switch ($q[0]) { 
  387. case "mediaType": 
  388. $parts[] = implode(" ", array_map(array($this, "compileValue"), array_slice($q, 1))); 
  389. break; 
  390. case "mediaExp": 
  391. if (isset($q[2])) { 
  392. $parts[] = "(". $this->compileValue($q[1]) . $this->formatter->assignSeparator . $this->compileValue($q[2]) . ")"; 
  393. } else { 
  394. $parts[] = "(" . $this->compileValue($q[1]) . ")"; 
  395. break; 
  396. if (!empty($parts)) { 
  397. if ($first) { 
  398. $first = false; 
  399. $out .= " "; 
  400. } else { 
  401. $out .= $this->formatter->tagSeparator; 
  402. $out .= implode(" and ", $parts); 
  403. return $out; 
  404.  
  405. // returns true if the value was something that could be imported 
  406. protected function compileImport($rawPath, $out) { 
  407. if ($rawPath[0] == "string") { 
  408. $path = $this->compileStringContent($rawPath); 
  409. if ($path = $this->findImport($path)) { 
  410. $this->importFile($path, $out); 
  411. return true; 
  412. return false; 
  413. if ($rawPath[0] == "list") { 
  414. // handle a list of strings 
  415. if (count($rawPath[2]) == 0) return false; 
  416. foreach ($rawPath[2] as $path) { 
  417. if ($path[0] != "string") return false; 
  418.  
  419. foreach ($rawPath[2] as $path) { 
  420. $this->compileImport($path, $out); 
  421.  
  422. return true; 
  423.  
  424. return false; 
  425.  
  426. // return a value to halt execution 
  427. protected function compileChild($child, $out) { 
  428. $this->sourcePos = isset($child[-1]) ? $child[-1] : -1; 
  429. $this->sourceParser = isset($child[-2]) ? $child[-2] : $this->parser; 
  430.  
  431. switch ($child[0]) { 
  432. case "import": 
  433. list(, $rawPath) = $child; 
  434. $rawPath = $this->reduce($rawPath); 
  435. if (!$this->compileImport($rawPath, $out)) { 
  436. $out->lines[] = "@import " . $this->compileValue($rawPath) . ";"; 
  437. break; 
  438. case "directive": 
  439. list(, $directive) = $child; 
  440. $s = "@" . $directive->name; 
  441. if (!empty($directive->value)) { 
  442. $s .= " " . $this->compileValue($directive->value); 
  443. $this->compileNestedBlock($directive, array($s)); 
  444. break; 
  445. case "media": 
  446. $this->compileMedia($child[1]); 
  447. break; 
  448. case "block": 
  449. $this->compileBlock($child[1]); 
  450. break; 
  451. case "charset": 
  452. $out->lines[] = "@charset ".$this->compileValue($child[1]).";"; 
  453. break; 
  454. case "assign": 
  455. list(, $name, $value) = $child; 
  456. if ($name[0] == "var") { 
  457. $isDefault = !empty($child[3]); 
  458.  
  459. if ($isDefault) { 
  460. $existingValue = $this->get($name[1], true); 
  461. $shouldSet = $existingValue === true || $existingValue == self::$null; 
  462.  
  463. if (!$isDefault || $shouldSet) { 
  464. $this->set($name[1], $this->reduce($value)); 
  465. break; 
  466.  
  467. // if the value reduces to null from something else then 
  468. // the property should be discarded 
  469. if ($value[0] != "null") { 
  470. $value = $this->reduce($value); 
  471. if ($value[0] == "null") { 
  472. break; 
  473.  
  474. $compiledValue = $this->compileValue($value); 
  475. $out->lines[] = $this->formatter->property( 
  476. $this->compileValue($name),  
  477. $compiledValue); 
  478. break; 
  479. case "comment": 
  480. $out->lines[] = $child[1]; 
  481. break; 
  482. case "mixin": 
  483. case "function": 
  484. list(, $block) = $child; 
  485. $this->set(self::$namespaces[$block->type] . $block->name, $block); 
  486. break; 
  487. case "extend": 
  488. list(, $selectors) = $child; 
  489. foreach ($selectors as $sel) { 
  490. // only use the first one 
  491. $sel = current($this->evalSelector($sel)); 
  492. $this->pushExtends($sel, $out->selectors); 
  493. break; 
  494. case "if": 
  495. list(, $if) = $child; 
  496. if ($this->isTruthy($this->reduce($if->cond, true))) { 
  497. return $this->compileChildren($if->children, $out); 
  498. } else { 
  499. foreach ($if->cases as $case) { 
  500. if ($case->type == "else" || 
  501. $case->type == "elseif" && $this->isTruthy($this->reduce($case->cond))) 
  502. return $this->compileChildren($case->children, $out); 
  503. break; 
  504. case "return": 
  505. return $this->reduce($child[1], true); 
  506. case "each": 
  507. list(, $each) = $child; 
  508. $list = $this->coerceList($this->reduce($each->list)); 
  509. foreach ($list[2] as $item) { 
  510. $this->pushEnv(); 
  511. $this->set($each->var, $item); 
  512. // TODO: allow return from here 
  513. $this->compileChildren($each->children, $out); 
  514. $this->popEnv(); 
  515. break; 
  516. case "while": 
  517. list(, $while) = $child; 
  518. while ($this->isTruthy($this->reduce($while->cond, true))) { 
  519. $ret = $this->compileChildren($while->children, $out); 
  520. if ($ret) return $ret; 
  521. break; 
  522. case "for": 
  523. list(, $for) = $child; 
  524. $start = $this->reduce($for->start, true); 
  525. $start = $start[1]; 
  526. $end = $this->reduce($for->end, true); 
  527. $end = $end[1]; 
  528. $d = $start < $end ? 1 : -1; 
  529.  
  530. while (true) { 
  531. if ((!$for->until && $start - $d == $end) || 
  532. ($for->until && $start == $end)) 
  533. break; 
  534.  
  535. $this->set($for->var, array("number", $start, "")); 
  536. $start += $d; 
  537.  
  538. $ret = $this->compileChildren($for->children, $out); 
  539. if ($ret) return $ret; 
  540.  
  541. break; 
  542. case "nestedprop": 
  543. list(, $prop) = $child; 
  544. $prefixed = array(); 
  545. $prefix = $this->compileValue($prop->prefix) . "-"; 
  546. foreach ($prop->children as $child) { 
  547. if ($child[0] == "assign") { 
  548. array_unshift($child[1][2], $prefix); 
  549. if ($child[0] == "nestedprop") { 
  550. array_unshift($child[1]->prefix[2], $prefix); 
  551. $prefixed[] = $child; 
  552. $this->compileChildren($prefixed, $out); 
  553. break; 
  554. case "include": // including a mixin 
  555. list(, $name, $argValues, $content) = $child; 
  556. $mixin = $this->get(self::$namespaces["mixin"] . $name, false); 
  557. if (!$mixin) { 
  558. $this->throwError("Undefined mixin $name"); 
  559.  
  560. $callingScope = $this->env; 
  561.  
  562. // push scope, apply args 
  563. $this->pushEnv(); 
  564. if ($this->env->depth > 0) { 
  565. $this->env->depth--; 
  566.  
  567. if (!is_null($content)) { 
  568. $content->scope = $callingScope; 
  569. $this->setRaw(self::$namespaces["special"] . "content", $content); 
  570.  
  571. if (!is_null($mixin->args)) { 
  572. $this->applyArguments($mixin->args, $argValues); 
  573.  
  574. foreach ($mixin->children as $child) { 
  575. $this->compileChild($child, $out); 
  576.  
  577. $this->popEnv(); 
  578.  
  579. break; 
  580. case "mixin_content": 
  581. $content = $this->get(self::$namespaces["special"] . "content"); 
  582. if (is_null($content)) { 
  583. $this->throwError("Expected @content inside of mixin"); 
  584.  
  585. $strongTypes = array('include', 'block', 'for', 'while'); 
  586. foreach ($content->children as $child) { 
  587. $this->storeEnv = (in_array($child[0], $strongTypes)) 
  588. ? null 
  589. : $content->scope; 
  590.  
  591. $this->compileChild($child, $out); 
  592.  
  593. unset($this->storeEnv); 
  594. break; 
  595. case "debug": 
  596. list(, $value, $pos) = $child; 
  597. $line = $this->parser->getLineNo($pos); 
  598. $value = $this->compileValue($this->reduce($value, true)); 
  599. fwrite(STDERR, "Line $line DEBUG: $value\n"); 
  600. break; 
  601. default: 
  602. $this->throwError("unknown child type: $child[0]"); 
  603.  
  604. protected function expToString($exp) { 
  605. list(, $op, $left, $right, $inParens, $whiteLeft, $whiteRight) = $exp; 
  606. $content = array($this->reduce($left)); 
  607. if ($whiteLeft) $content[] = " "; 
  608. $content[] = $op; 
  609. if ($whiteRight) $content[] = " "; 
  610. $content[] = $this->reduce($right); 
  611. return array("string", "", $content); 
  612.  
  613. protected function isTruthy($value) { 
  614. return $value != self::$false && $value != self::$null; 
  615.  
  616. // should $value cause its operand to eval 
  617. protected function shouldEval($value) { 
  618. switch ($value[0]) { 
  619. case "exp": 
  620. if ($value[1] == "/") { 
  621. return $this->shouldEval($value[2], $value[3]); 
  622. case "var": 
  623. case "fncall": 
  624. return true; 
  625. return false; 
  626.  
  627. protected function reduce($value, $inExp = false) { 
  628. list($type) = $value; 
  629. switch ($type) { 
  630. case "exp": 
  631. list(, $op, $left, $right, $inParens) = $value; 
  632. $opName = isset(self::$operatorNames[$op]) ? self::$operatorNames[$op] : $op; 
  633.  
  634. $inExp = $inExp || $this->shouldEval($left) || $this->shouldEval($right); 
  635.  
  636. $left = $this->reduce($left, true); 
  637. $right = $this->reduce($right, true); 
  638.  
  639. // only do division in special cases 
  640. if ($opName == "div" && !$inParens && !$inExp) { 
  641. if ($left[0] != "color" && $right[0] != "color") { 
  642. return $this->expToString($value); 
  643.  
  644. $left = $this->coerceForExpression($left); 
  645. $right = $this->coerceForExpression($right); 
  646.  
  647. $ltype = $left[0]; 
  648. $rtype = $right[0]; 
  649.  
  650. // this tries: 
  651. // 1. op_[op name]_[left type]_[right type] 
  652. // 2. op_[left type]_[right type] (passing the op as first arg 
  653. // 3. op_[op name] 
  654. $fn = "op_${opName}_${ltype}_${rtype}"; 
  655. if (is_callable(array($this, $fn)) || 
  656. (($fn = "op_${ltype}_${rtype}") && 
  657. is_callable(array($this, $fn)) && 
  658. $passOp = true) || 
  659. (($fn = "op_${opName}") && 
  660. is_callable(array($this, $fn)) && 
  661. $genOp = true)) 
  662. $unitChange = false; 
  663. if (!isset($genOp) && 
  664. $left[0] == "number" && $right[0] == "number") 
  665. if ($opName == "mod" && $right[2] != "") { 
  666. $this->throwError("Cannot modulo by a number with units: $right[1]$right[2]."); 
  667.  
  668. $unitChange = true; 
  669. $emptyUnit = $left[2] == "" || $right[2] == ""; 
  670. $targetUnit = "" != $left[2] ? $left[2] : $right[2]; 
  671.  
  672. if ($opName != "mul") { 
  673. $left[2] = "" != $left[2] ? $left[2] : $targetUnit; 
  674. $right[2] = "" != $right[2] ? $right[2] : $targetUnit; 
  675.  
  676. if ($opName != "mod") { 
  677. $left = $this->normalizeNumber($left); 
  678. $right = $this->normalizeNumber($right); 
  679.  
  680. if ($opName == "div" && !$emptyUnit && $left[2] == $right[2]) { 
  681. $targetUnit = ""; 
  682.  
  683. if ($opName == "mul") { 
  684. $left[2] = "" != $left[2] ? $left[2] : $right[2]; 
  685. $right[2] = "" != $right[2] ? $right[2] : $left[2]; 
  686. } elseif ($opName == "div" && $left[2] == $right[2]) { 
  687. $left[2] = ""; 
  688. $right[2] = ""; 
  689.  
  690. $shouldEval = $inParens || $inExp; 
  691. if (isset($passOp)) { 
  692. $out = $this->$fn($op, $left, $right, $shouldEval); 
  693. } else { 
  694. $out = $this->$fn($left, $right, $shouldEval); 
  695.  
  696. if (!is_null($out)) { 
  697. if ($unitChange && $out[0] == "number") { 
  698. $out = $this->coerceUnit($out, $targetUnit); 
  699. return $out; 
  700.  
  701. return $this->expToString($value); 
  702. case "unary": 
  703. list(, $op, $exp, $inParens) = $value; 
  704. $inExp = $inExp || $this->shouldEval($exp); 
  705.  
  706. $exp = $this->reduce($exp); 
  707. if ($exp[0] == "number") { 
  708. switch ($op) { 
  709. case "+": 
  710. return $exp; 
  711. case "-": 
  712. $exp[1] *= -1; 
  713. return $exp; 
  714.  
  715. if ($op == "not") { 
  716. if ($inExp || $inParens) { 
  717. if ($exp == self::$false) { 
  718. return self::$true; 
  719. } else { 
  720. return self::$false; 
  721. } else { 
  722. $op = $op . " "; 
  723.  
  724. return array("string", "", array($op, $exp)); 
  725. case "var": 
  726. list(, $name) = $value; 
  727. return $this->reduce($this->get($name)); 
  728. case "list": 
  729. foreach ($value[2] as &$item) { 
  730. $item = $this->reduce($item); 
  731. return $value; 
  732. case "string": 
  733. foreach ($value[2] as &$item) { 
  734. if (is_array($item)) { 
  735. $item = $this->reduce($item); 
  736. return $value; 
  737. case "interpolate": 
  738. $value[1] = $this->reduce($value[1]); 
  739. return $value; 
  740. case "fncall": 
  741. list(, $name, $argValues) = $value; 
  742.  
  743. // user defined function? 
  744. $func = $this->get(self::$namespaces["function"] . $name, false); 
  745. if ($func) { 
  746. $this->pushEnv(); 
  747.  
  748. // set the args 
  749. if (isset($func->args)) { 
  750. $this->applyArguments($func->args, $argValues); 
  751.  
  752. // throw away lines and children 
  753. $tmp = (object)array( 
  754. "lines" => array(),  
  755. "children" => array() 
  756. ); 
  757. $ret = $this->compileChildren($func->children, $tmp); 
  758. $this->popEnv(); 
  759.  
  760. return is_null($ret) ? self::$defaultValue : $ret; 
  761.  
  762. // built in function 
  763. if ($this->callBuiltin($name, $argValues, $returnValue)) { 
  764. return $returnValue; 
  765.  
  766. // need to flatten the arguments into a list 
  767. $listArgs = array(); 
  768. foreach ((array)$argValues as $arg) { 
  769. if (empty($arg[0])) { 
  770. $listArgs[] = $this->reduce($arg[1]); 
  771. return array("function", $name, array("list", ", ", $listArgs)); 
  772. default: 
  773. return $value; 
  774.  
  775. public function normalizeValue($value) { 
  776. $value = $this->coerceForExpression($this->reduce($value)); 
  777. list($type) = $value; 
  778.  
  779. switch ($type) { 
  780. case "list": 
  781. $value = $this->extractInterpolation($value); 
  782. if ($value[0] != "list") { 
  783. return array("keyword", $this->compileValue($value)); 
  784. foreach ($value[2] as $key => $item) { 
  785. $value[2][$key] = $this->normalizeValue($item); 
  786. return $value; 
  787. case "number": 
  788. return $this->normalizeNumber($value); 
  789. default: 
  790. return $value; 
  791.  
  792. // just does physical lengths for now 
  793. protected function normalizeNumber($number) { 
  794. list(, $value, $unit) = $number; 
  795. if (isset(self::$unitTable["in"][$unit])) { 
  796. $conv = self::$unitTable["in"][$unit]; 
  797. return array("number", $value / $conv, "in"); 
  798. return $number; 
  799.  
  800. // $number should be normalized 
  801. protected function coerceUnit($number, $unit) { 
  802. list(, $value, $baseUnit) = $number; 
  803. if (isset(self::$unitTable[$baseUnit][$unit])) { 
  804. $value = $value * self::$unitTable[$baseUnit][$unit]; 
  805.  
  806. return array("number", $value, $unit); 
  807.  
  808. protected function op_add_number_number($left, $right) { 
  809. return array("number", $left[1] + $right[1], $left[2]); 
  810.  
  811. protected function op_mul_number_number($left, $right) { 
  812. return array("number", $left[1] * $right[1], $left[2]); 
  813.  
  814. protected function op_sub_number_number($left, $right) { 
  815. return array("number", $left[1] - $right[1], $left[2]); 
  816.  
  817. protected function op_div_number_number($left, $right) { 
  818. return array("number", $left[1] / $right[1], $left[2]); 
  819.  
  820. protected function op_mod_number_number($left, $right) { 
  821. return array("number", $left[1] % $right[1], $left[2]); 
  822.  
  823. // adding strings 
  824. protected function op_add($left, $right) { 
  825. if ($strLeft = $this->coerceString($left)) { 
  826. if ($right[0] == "string") { 
  827. $right[1] = ""; 
  828. $strLeft[2][] = $right; 
  829. return $strLeft; 
  830.  
  831. if ($strRight = $this->coerceString($right)) { 
  832. if ($left[0] == "string") { 
  833. $left[1] = ""; 
  834. array_unshift($strRight[2], $left); 
  835. return $strRight; 
  836.  
  837. protected function op_and($left, $right, $shouldEval) { 
  838. if (!$shouldEval) return; 
  839. if ($left != self::$false) return $right; 
  840. return $left; 
  841.  
  842. protected function op_or($left, $right, $shouldEval) { 
  843. if (!$shouldEval) return; 
  844. if ($left != self::$false) return $left; 
  845. return $right; 
  846.  
  847. protected function op_color_color($op, $left, $right) { 
  848. $out = array('color'); 
  849. foreach (range(1, 3) as $i) { 
  850. $lval = isset($left[$i]) ? $left[$i] : 0; 
  851. $rval = isset($right[$i]) ? $right[$i] : 0; 
  852. switch ($op) { 
  853. case '+': 
  854. $out[] = $lval + $rval; 
  855. break; 
  856. case '-': 
  857. $out[] = $lval - $rval; 
  858. break; 
  859. case '*': 
  860. $out[] = $lval * $rval; 
  861. break; 
  862. case '%': 
  863. $out[] = $lval % $rval; 
  864. break; 
  865. case '/': 
  866. if ($rval == 0) { 
  867. $this->throwError("color: Can't divide by zero"); 
  868. $out[] = $lval / $rval; 
  869. break; 
  870. case "==": 
  871. return $this->op_eq($left, $right); 
  872. case "!=": 
  873. return $this->op_neq($left, $right); 
  874. default: 
  875. $this->throwError("color: unknown op $op"); 
  876.  
  877. if (isset($left[4])) $out[4] = $left[4]; 
  878. elseif (isset($right[4])) $out[4] = $right[4]; 
  879.  
  880. return $this->fixColor($out); 
  881.  
  882. protected function op_color_number($op, $left, $right) { 
  883. $value = $right[1]; 
  884. return $this->op_color_color($op, $left,  
  885. array("color", $value, $value, $value)); 
  886.  
  887. protected function op_number_color($op, $left, $right) { 
  888. $value = $left[1]; 
  889. return $this->op_color_color($op,  
  890. array("color", $value, $value, $value), $right); 
  891.  
  892. protected function op_eq($left, $right) { 
  893. if (($lStr = $this->coerceString($left)) && ($rStr = $this->coerceString($right))) { 
  894. $lStr[1] = ""; 
  895. $rStr[1] = ""; 
  896. return $this->toBool($this->compileValue($lStr) == $this->compileValue($rStr)); 
  897.  
  898. return $this->toBool($left == $right); 
  899.  
  900. protected function op_neq($left, $right) { 
  901. return $this->toBool($left != $right); 
  902.  
  903. protected function op_gte_number_number($left, $right) { 
  904. return $this->toBool($left[1] >= $right[1]); 
  905.  
  906. protected function op_gt_number_number($left, $right) { 
  907. return $this->toBool($left[1] > $right[1]); 
  908.  
  909. protected function op_lte_number_number($left, $right) { 
  910. return $this->toBool($left[1] <= $right[1]); 
  911.  
  912. protected function op_lt_number_number($left, $right) { 
  913. return $this->toBool($left[1] < $right[1]); 
  914.  
  915. public function toBool($thing) { 
  916. return $thing ? self::$true : self::$false; 
  917.  
  918. /** 
  919. * Compiles a primitive value into a CSS property value. 
  920. * Values in scssphp are typed by being wrapped in arrays, their format is 
  921. * typically: 
  922. * array(type, contents [, additional_contents]*) 
  923. * The input is expected to be reduced. This function will not work on 
  924. * things like expressions and variables. 
  925. * @param array $value 
  926. */ 
  927. protected function compileValue($value) { 
  928. $value = $this->reduce($value); 
  929.  
  930. list($type) = $value; 
  931. switch ($type) { 
  932. case "keyword": 
  933. return $value[1]; 
  934. case "color": 
  935. // [1] - red component (either number for a %) 
  936. // [2] - green component 
  937. // [3] - blue component 
  938. // [4] - optional alpha component 
  939. list(, $r, $g, $b) = $value; 
  940.  
  941. $r = round($r); 
  942. $g = round($g); 
  943. $b = round($b); 
  944.  
  945. if (count($value) == 5 && $value[4] != 1) { // rgba 
  946. return 'rgba('.$r.', '.$g.', '.$b.', '.$value[4].')'; 
  947.  
  948. $h = sprintf("#%02x%02x%02x", $r, $g, $b); 
  949.  
  950. // Converting hex color to short notation (e.g. #003399 to #039) 
  951. if ($h[1] === $h[2] && $h[3] === $h[4] && $h[5] === $h[6]) { 
  952. $h = '#' . $h[1] . $h[3] . $h[5]; 
  953.  
  954. return $h; 
  955. case "number": 
  956. return round($value[1], $this->numberPrecision) . $value[2]; 
  957. case "string": 
  958. return $value[1] . $this->compileStringContent($value) . $value[1]; 
  959. case "function": 
  960. $args = !empty($value[2]) ? $this->compileValue($value[2]) : ""; 
  961. return "$value[1]($args)"; 
  962. case "list": 
  963. $value = $this->extractInterpolation($value); 
  964. if ($value[0] != "list") return $this->compileValue($value); 
  965.  
  966. list(, $delim, $items) = $value; 
  967.  
  968. $filtered = array(); 
  969. foreach ($items as $item) { 
  970. if ($item[0] == "null") continue; 
  971. $filtered[] = $this->compileValue($item); 
  972.  
  973. return implode("$delim ", $filtered); 
  974. case "interpolated": # node created by extractInterpolation 
  975. list(, $interpolate, $left, $right) = $value; 
  976. list(, , $whiteLeft, $whiteRight) = $interpolate; 
  977.  
  978. $left = count($left[2]) > 0 ? 
  979. $this->compileValue($left).$whiteLeft : ""; 
  980.  
  981. $right = count($right[2]) > 0 ? 
  982. $whiteRight.$this->compileValue($right) : ""; 
  983.  
  984. return $left.$this->compileValue($interpolate).$right; 
  985.  
  986. case "interpolate": # raw parse node 
  987. list(, $exp) = $value; 
  988.  
  989. // strip quotes if it's a string 
  990. $reduced = $this->reduce($exp); 
  991. switch ($reduced[0]) { 
  992. case "string": 
  993. $reduced = array("keyword",  
  994. $this->compileStringContent($reduced)); 
  995. break; 
  996. case "null": 
  997. $reduced = array("keyword", ""); 
  998.  
  999. return $this->compileValue($reduced); 
  1000. case "null": 
  1001. return "null"; 
  1002. default: 
  1003. $this->throwError("unknown value type: $type"); 
  1004.  
  1005. protected function compileStringContent($string) { 
  1006. $parts = array(); 
  1007. foreach ($string[2] as $part) { 
  1008. if (is_array($part)) { 
  1009. $parts[] = $this->compileValue($part); 
  1010. } else { 
  1011. $parts[] = $part; 
  1012.  
  1013. return implode($parts); 
  1014.  
  1015. // doesn't need to be recursive, compileValue will handle that 
  1016. protected function extractInterpolation($list) { 
  1017. $items = $list[2]; 
  1018. foreach ($items as $i => $item) { 
  1019. if ($item[0] == "interpolate") { 
  1020. $before = array("list", $list[1], array_slice($items, 0, $i)); 
  1021. $after = array("list", $list[1], array_slice($items, $i + 1)); 
  1022. return array("interpolated", $item, $before, $after); 
  1023. return $list; 
  1024.  
  1025. // find the final set of selectors 
  1026. protected function multiplySelectors($env) { 
  1027. $envs = array(); 
  1028. while (null !== $env) { 
  1029. if (!empty($env->selectors)) { 
  1030. $envs[] = $env; 
  1031. $env = $env->parent; 
  1032. }; 
  1033.  
  1034. $selectors = array(); 
  1035. $parentSelectors = array(array()); 
  1036. while ($env = array_pop($envs)) { 
  1037. $selectors = array(); 
  1038. foreach ($env->selectors as $selector) { 
  1039. foreach ($parentSelectors as $parent) { 
  1040. $selectors[] = $this->joinSelectors($parent, $selector); 
  1041. $parentSelectors = $selectors; 
  1042.  
  1043. return $selectors; 
  1044.  
  1045. // looks for & to replace, or append parent before child 
  1046. protected function joinSelectors($parent, $child) { 
  1047. $setSelf = false; 
  1048. $out = array(); 
  1049. foreach ($child as $part) { 
  1050. $newPart = array(); 
  1051. foreach ($part as $p) { 
  1052. if ($p == self::$selfSelector) { 
  1053. $setSelf = true; 
  1054. foreach ($parent as $i => $parentPart) { 
  1055. if ($i > 0) { 
  1056. $out[] = $newPart; 
  1057. $newPart = array(); 
  1058.  
  1059. foreach ($parentPart as $pp) { 
  1060. $newPart[] = $pp; 
  1061. } else { 
  1062. $newPart[] = $p; 
  1063.  
  1064. $out[] = $newPart; 
  1065.  
  1066. return $setSelf ? $out : array_merge($parent, $child); 
  1067.  
  1068. protected function multiplyMedia($env, $childQueries = null) { 
  1069. if (is_null($env) || 
  1070. !empty($env->block->type) && $env->block->type != "media") 
  1071. return $childQueries; 
  1072.  
  1073. // plain old block, skip 
  1074. if (empty($env->block->type)) { 
  1075. return $this->multiplyMedia($env->parent, $childQueries); 
  1076.  
  1077. $parentQueries = $env->block->queryList; 
  1078. if ($childQueries == null) { 
  1079. $childQueries = $parentQueries; 
  1080. } else { 
  1081. $originalQueries = $childQueries; 
  1082. $childQueries = array(); 
  1083.  
  1084. foreach ($parentQueries as $parentQuery) { 
  1085. foreach ($originalQueries as $childQuery) { 
  1086. $childQueries []= array_merge($parentQuery, $childQuery); 
  1087.  
  1088. return $this->multiplyMedia($env->parent, $childQueries); 
  1089.  
  1090. // convert something to list 
  1091. protected function coerceList($item, $delim = ", ") { 
  1092. if (!is_null($item) && $item[0] == "list") { 
  1093. return $item; 
  1094.  
  1095. return array("list", $delim, is_null($item) ? array(): array($item)); 
  1096.  
  1097. protected function applyArguments($argDef, $argValues) { 
  1098. $hasVariable = false; 
  1099. $args = array(); 
  1100. foreach ($argDef as $i => $arg) { 
  1101. list($name, $default, $isVariable) = $argDef[$i]; 
  1102. $args[$name] = array($i, $name, $default, $isVariable); 
  1103. $hasVariable |= $isVariable; 
  1104.  
  1105. $keywordArgs = array(); 
  1106. $deferredKeywordArgs = array(); 
  1107. $remaining = array(); 
  1108. // assign the keyword args 
  1109. foreach ((array) $argValues as $arg) { 
  1110. if (!empty($arg[0])) { 
  1111. if (!isset($args[$arg[0][1]])) { 
  1112. if ($hasVariable) { 
  1113. $deferredKeywordArgs[$arg[0][1]] = $arg[1]; 
  1114. } else { 
  1115. $this->throwError("Mixin or function doesn't have an argument named $%s.", $arg[0][1]); 
  1116. } elseif ($args[$arg[0][1]][0] < count($remaining)) { 
  1117. $this->throwError("The argument $%s was passed both by position and by name.", $arg[0][1]); 
  1118. } else { 
  1119. $keywordArgs[$arg[0][1]] = $arg[1]; 
  1120. } elseif (count($keywordArgs)) { 
  1121. $this->throwError('Positional arguments must come before keyword arguments.'); 
  1122. } elseif ($arg[2] == true) { 
  1123. $val = $this->reduce($arg[1], true); 
  1124. if ($val[0] == "list") { 
  1125. foreach ($val[2] as $name => $item) { 
  1126. if (!is_numeric($name)) { 
  1127. $keywordArgs[$name] = $item; 
  1128. } else { 
  1129. $remaining[] = $item; 
  1130. } else { 
  1131. $remaining[] = $val; 
  1132. } else { 
  1133. $remaining[] = $arg[1]; 
  1134.  
  1135. foreach ($args as $arg) { 
  1136. list($i, $name, $default, $isVariable) = $arg; 
  1137. if ($isVariable) { 
  1138. $val = array("list", ", ", array()); 
  1139. for ($count = count($remaining); $i < $count; $i++) { 
  1140. $val[2][] = $remaining[$i]; 
  1141. foreach ($deferredKeywordArgs as $itemName => $item) { 
  1142. $val[2][$itemName] = $item; 
  1143. } elseif (isset($remaining[$i])) { 
  1144. $val = $remaining[$i]; 
  1145. } elseif (isset($keywordArgs[$name])) { 
  1146. $val = $keywordArgs[$name]; 
  1147. } elseif (!empty($default)) { 
  1148. $val = $default; 
  1149. } else { 
  1150. $this->throwError("Missing argument $name"); 
  1151.  
  1152. $this->set($name, $this->reduce($val, true), true); 
  1153.  
  1154. protected function pushEnv($block=null) { 
  1155. $env = new stdClass; 
  1156. $env->parent = $this->env; 
  1157. $env->store = array(); 
  1158. $env->block = $block; 
  1159. $env->depth = isset($this->env->depth) ? $this->env->depth + 1 : 0; 
  1160.  
  1161. $this->env = $env; 
  1162. return $env; 
  1163.  
  1164. protected function normalizeName($name) { 
  1165. return str_replace("-", "_", $name); 
  1166.  
  1167. protected function getStoreEnv() { 
  1168. return isset($this->storeEnv) ? $this->storeEnv : $this->env; 
  1169.  
  1170. protected function set($name, $value, $shadow=false) { 
  1171. $name = $this->normalizeName($name); 
  1172.  
  1173. if ($shadow) { 
  1174. $this->setRaw($name, $value); 
  1175. } else { 
  1176. $this->setExisting($name, $value); 
  1177.  
  1178. protected function setExisting($name, $value, $env = null) { 
  1179. if (is_null($env)) $env = $this->getStoreEnv(); 
  1180.  
  1181. if (isset($env->store[$name]) || is_null($env->parent)) { 
  1182. $env->store[$name] = $value; 
  1183. } else { 
  1184. $this->setExisting($name, $value, $env->parent); 
  1185.  
  1186. protected function setRaw($name, $value) { 
  1187. $env = $this->getStoreEnv(); 
  1188. $env->store[$name] = $value; 
  1189.  
  1190. public function get($name, $defaultValue = null, $env = null) { 
  1191. $name = $this->normalizeName($name); 
  1192.  
  1193. if (is_null($env)) $env = $this->getStoreEnv(); 
  1194. if (is_null($defaultValue)) $defaultValue = self::$defaultValue; 
  1195.  
  1196. if (isset($env->store[$name])) { 
  1197. return $env->store[$name]; 
  1198. } elseif (isset($env->parent)) { 
  1199. return $this->get($name, $defaultValue, $env->parent); 
  1200.  
  1201. return $defaultValue; // found nothing 
  1202.  
  1203. protected function popEnv() { 
  1204. $env = $this->env; 
  1205. $this->env = $this->env->parent; 
  1206. return $env; 
  1207.  
  1208. public function getParsedFiles() { 
  1209. return $this->parsedFiles; 
  1210.  
  1211. public function addImportPath($path) { 
  1212. $this->importPaths[] = $path; 
  1213.  
  1214. public function setImportPaths($path) { 
  1215. $this->importPaths = (array)$path; 
  1216.  
  1217. public function setNumberPrecision($numberPrecision) { 
  1218. $this->numberPrecision = $numberPrecision; 
  1219.  
  1220. public function setFormatter($formatterName) { 
  1221. $this->formatter = $formatterName; 
  1222.  
  1223. public function registerFunction($name, $func) { 
  1224. $this->userFunctions[$this->normalizeName($name)] = $func; 
  1225.  
  1226. public function unregisterFunction($name) { 
  1227. unset($this->userFunctions[$this->normalizeName($name)]); 
  1228.  
  1229. protected function importFile($path, $out) { 
  1230. // see if tree is cached 
  1231. $realPath = realpath($path); 
  1232. if (isset($this->importCache[$realPath])) { 
  1233. $tree = $this->importCache[$realPath]; 
  1234. } else { 
  1235. $code = file_get_contents($path); 
  1236. $parser = new scss_parser($path, false); 
  1237. $tree = $parser->parse($code); 
  1238. $this->parsedFiles[] = $path; 
  1239.  
  1240. $this->importCache[$realPath] = $tree; 
  1241.  
  1242. $pi = pathinfo($path); 
  1243. array_unshift($this->importPaths, $pi['dirname']); 
  1244. $this->compileChildren($tree->children, $out); 
  1245. array_shift($this->importPaths); 
  1246.  
  1247. // results the file path for an import url if it exists 
  1248. public function findImport($url) { 
  1249. $urls = array(); 
  1250.  
  1251. // for "normal" scss imports (ignore vanilla css and external requests) 
  1252. if (!preg_match('/\.css|^http:\/\/$/', $url)) { 
  1253. // try both normal and the _partial filename 
  1254. $urls = array($url, preg_replace('/[^\/]+$/', '_\0', $url)); 
  1255.  
  1256. foreach ($this->importPaths as $dir) { 
  1257. if (is_string($dir)) { 
  1258. // check urls for normal import paths 
  1259. foreach ($urls as $full) { 
  1260. $full = $dir . 
  1261. (!empty($dir) && substr($dir, -1) != '/' ? '/' : '') . 
  1262. $full; 
  1263.  
  1264. if ($this->fileExists($file = $full.'.scss') || 
  1265. $this->fileExists($file = $full)) 
  1266. return $file; 
  1267. } else { 
  1268. // check custom callback for import path 
  1269. $file = call_user_func($dir, $url, $this); 
  1270. if ($file !== null) { 
  1271. return $file; 
  1272.  
  1273. return null; 
  1274.  
  1275. protected function fileExists($name) { 
  1276. return is_file($name); 
  1277.  
  1278. protected function callBuiltin($name, $args, &$returnValue) { 
  1279. // try a lib function 
  1280. $name = $this->normalizeName($name); 
  1281. $libName = "lib_".$name; 
  1282. $f = array($this, $libName); 
  1283. $prototype = isset(self::$$libName) ? self::$$libName : null; 
  1284.  
  1285. if (is_callable($f)) { 
  1286. $sorted = $this->sortArgs($prototype, $args); 
  1287. foreach ($sorted as &$val) { 
  1288. $val = $this->reduce($val, true); 
  1289. $returnValue = call_user_func($f, $sorted, $this); 
  1290. } elseif (isset($this->userFunctions[$name])) { 
  1291. // see if we can find a user function 
  1292. $fn = $this->userFunctions[$name]; 
  1293.  
  1294. foreach ($args as &$val) { 
  1295. $val = $this->reduce($val[1], true); 
  1296.  
  1297. $returnValue = call_user_func($fn, $args, $this); 
  1298.  
  1299. if (isset($returnValue)) { 
  1300. // coerce a php value into a scss one 
  1301. if (is_numeric($returnValue)) { 
  1302. $returnValue = array('number', $returnValue, ""); 
  1303. } elseif (is_bool($returnValue)) { 
  1304. $returnValue = $returnValue ? self::$true : self::$false; 
  1305. } elseif (!is_array($returnValue)) { 
  1306. $returnValue = array('keyword', $returnValue); 
  1307.  
  1308. return true; 
  1309.  
  1310. return false; 
  1311.  
  1312. // sorts any keyword arguments 
  1313. // TODO: merge with apply arguments 
  1314. protected function sortArgs($prototype, $args) { 
  1315. $keyArgs = array(); 
  1316. $posArgs = array(); 
  1317.  
  1318. foreach ($args as $arg) { 
  1319. list($key, $value) = $arg; 
  1320. $key = $key[1]; 
  1321. if (empty($key)) { 
  1322. $posArgs[] = $value; 
  1323. } else { 
  1324. $keyArgs[$key] = $value; 
  1325.  
  1326. if (is_null($prototype)) return $posArgs; 
  1327.  
  1328. $finalArgs = array(); 
  1329. foreach ($prototype as $i => $names) { 
  1330. if (isset($posArgs[$i])) { 
  1331. $finalArgs[] = $posArgs[$i]; 
  1332. continue; 
  1333.  
  1334. $set = false; 
  1335. foreach ((array)$names as $name) { 
  1336. if (isset($keyArgs[$name])) { 
  1337. $finalArgs[] = $keyArgs[$name]; 
  1338. $set = true; 
  1339. break; 
  1340.  
  1341. if (!$set) { 
  1342. $finalArgs[] = null; 
  1343.  
  1344. return $finalArgs; 
  1345.  
  1346. protected function coerceForExpression($value) { 
  1347. if ($color = $this->coerceColor($value)) { 
  1348. return $color; 
  1349.  
  1350. return $value; 
  1351.  
  1352. protected function coerceColor($value) { 
  1353. switch ($value[0]) { 
  1354. case "color": return $value; 
  1355. case "keyword": 
  1356. $name = $value[1]; 
  1357. if (isset(self::$cssColors[$name])) { 
  1358. @list($r, $g, $b, $a) = explode(', ', self::$cssColors[$name]); 
  1359. return isset($a) 
  1360. ? array('color', (int) $r, (int) $g, (int) $b, (int) $a) 
  1361. : array('color', (int) $r, (int) $g, (int) $b); 
  1362. return null; 
  1363.  
  1364. return null; 
  1365.  
  1366. protected function coerceString($value) { 
  1367. switch ($value[0]) { 
  1368. case "string": 
  1369. return $value; 
  1370. case "keyword": 
  1371. return array("string", "", array($value[1])); 
  1372. return null; 
  1373.  
  1374. public function assertList($value) { 
  1375. if ($value[0] != "list") 
  1376. $this->throwError("expecting list"); 
  1377. return $value; 
  1378.  
  1379. public function assertColor($value) { 
  1380. if ($color = $this->coerceColor($value)) return $color; 
  1381. $this->throwError("expecting color"); 
  1382.  
  1383. public function assertNumber($value) { 
  1384. if ($value[0] != "number") 
  1385. $this->throwError("expecting number"); 
  1386. return $value[1]; 
  1387.  
  1388. protected function coercePercent($value) { 
  1389. if ($value[0] == "number") { 
  1390. if ($value[2] == "%") { 
  1391. return $value[1] / 100; 
  1392. return $value[1]; 
  1393. return 0; 
  1394.  
  1395. // make sure a color's components don't go out of bounds 
  1396. protected function fixColor($c) { 
  1397. foreach (range(1, 3) as $i) { 
  1398. if ($c[$i] < 0) $c[$i] = 0; 
  1399. if ($c[$i] > 255) $c[$i] = 255; 
  1400.  
  1401. return $c; 
  1402.  
  1403. public function toHSL($red, $green, $blue) { 
  1404. $r = $red / 255; 
  1405. $g = $green / 255; 
  1406. $b = $blue / 255; 
  1407.  
  1408. $min = min($r, $g, $b); 
  1409. $max = max($r, $g, $b); 
  1410. $d = $max - $min; 
  1411. $l = ($min + $max) / 2; 
  1412.  
  1413. if ($min == $max) { 
  1414. $s = $h = 0; 
  1415. } else { 
  1416. if ($l < 0.5) 
  1417. $s = $d / (2 * $l); 
  1418. else 
  1419. $s = $d / (2 - 2 * $l); 
  1420.  
  1421. if ($r == $max) 
  1422. $h = 60 * ($g - $b) / $d; 
  1423. elseif ($g == $max) 
  1424. $h = 60 * ($b - $r) / $d + 120; 
  1425. elseif ($b == $max) 
  1426. $h = 60 * ($r - $g) / $d + 240; 
  1427.  
  1428. return array('hsl', fmod($h, 360), $s * 100, $l * 100); 
  1429.  
  1430. public function hueToRGB($m1, $m2, $h) { 
  1431. if ($h < 0) 
  1432. $h += 1; 
  1433. elseif ($h > 1) 
  1434. $h -= 1; 
  1435.  
  1436. if ($h * 6 < 1) 
  1437. return $m1 + ($m2 - $m1) * $h * 6; 
  1438.  
  1439. if ($h * 2 < 1) 
  1440. return $m2; 
  1441.  
  1442. if ($h * 3 < 2) 
  1443. return $m1 + ($m2 - $m1) * (2/3 - $h) * 6; 
  1444.  
  1445. return $m1; 
  1446.  
  1447. // H from 0 to 360, S and L from 0 to 100 
  1448. public function toRGB($hue, $saturation, $lightness) { 
  1449. if ($hue < 0) { 
  1450. $hue += 360; 
  1451.  
  1452. $h = $hue / 360; 
  1453. $s = min(100, max(0, $saturation)) / 100; 
  1454. $l = min(100, max(0, $lightness)) / 100; 
  1455.  
  1456. $m2 = $l <= 0.5 ? $l * ($s + 1) : $l + $s - $l * $s; 
  1457. $m1 = $l * 2 - $m2; 
  1458.  
  1459. $r = $this->hueToRGB($m1, $m2, $h + 1/3) * 255; 
  1460. $g = $this->hueToRGB($m1, $m2, $h) * 255; 
  1461. $b = $this->hueToRGB($m1, $m2, $h - 1/3) * 255; 
  1462.  
  1463. $out = array('color', $r, $g, $b); 
  1464. return $out; 
  1465.  
  1466. // Built in functions 
  1467.  
  1468. protected static $lib_if = array("condition", "if-true", "if-false"); 
  1469. protected function lib_if($args) { 
  1470. list($cond, $t, $f) = $args; 
  1471. if ($cond == self::$false) return $f; 
  1472. return $t; 
  1473.  
  1474. protected static $lib_index = array("list", "value"); 
  1475. protected function lib_index($args) { 
  1476. list($list, $value) = $args; 
  1477. $list = $this->assertList($list); 
  1478.  
  1479. $values = array(); 
  1480. foreach ($list[2] as $item) { 
  1481. $values[] = $this->normalizeValue($item); 
  1482. $key = array_search($this->normalizeValue($value), $values); 
  1483.  
  1484. return false === $key ? false : $key + 1; 
  1485.  
  1486. protected static $lib_rgb = array("red", "green", "blue"); 
  1487. protected function lib_rgb($args) { 
  1488. list($r, $g, $b) = $args; 
  1489. return array("color", $r[1], $g[1], $b[1]); 
  1490.  
  1491. protected static $lib_rgba = array( 
  1492. array("red", "color"),  
  1493. "green", "blue", "alpha"); 
  1494. protected function lib_rgba($args) { 
  1495. if ($color = $this->coerceColor($args[0])) { 
  1496. $num = is_null($args[1]) ? $args[3] : $args[1]; 
  1497. $alpha = $this->assertNumber($num); 
  1498. $color[4] = $alpha; 
  1499. return $color; 
  1500.  
  1501. list($r, $g, $b, $a) = $args; 
  1502. return array("color", $r[1], $g[1], $b[1], $a[1]); 
  1503.  
  1504. // helper function for adjust_color, change_color, and scale_color 
  1505. protected function alter_color($args, $fn) { 
  1506. $color = $this->assertColor($args[0]); 
  1507.  
  1508. foreach (array(1, 2, 3, 7) as $i) { 
  1509. if (!is_null($args[$i])) { 
  1510. $val = $this->assertNumber($args[$i]); 
  1511. $ii = $i == 7 ? 4 : $i; // alpha 
  1512. $color[$ii] = 
  1513. $this->$fn(isset($color[$ii]) ? $color[$ii] : 0, $val, $i); 
  1514.  
  1515. if (!is_null($args[4]) || !is_null($args[5]) || !is_null($args[6])) { 
  1516. $hsl = $this->toHSL($color[1], $color[2], $color[3]); 
  1517. foreach (array(4, 5, 6) as $i) { 
  1518. if (!is_null($args[$i])) { 
  1519. $val = $this->assertNumber($args[$i]); 
  1520. $hsl[$i - 3] = $this->$fn($hsl[$i - 3], $val, $i); 
  1521.  
  1522. $rgb = $this->toRGB($hsl[1], $hsl[2], $hsl[3]); 
  1523. if (isset($color[4])) $rgb[4] = $color[4]; 
  1524. $color = $rgb; 
  1525.  
  1526. return $color; 
  1527.  
  1528. protected static $lib_adjust_color = array( 
  1529. "color", "red", "green", "blue",  
  1530. "hue", "saturation", "lightness", "alpha" 
  1531. ); 
  1532. protected function adjust_color_helper($base, $alter, $i) { 
  1533. return $base += $alter; 
  1534. protected function lib_adjust_color($args) { 
  1535. return $this->alter_color($args, "adjust_color_helper"); 
  1536.  
  1537. protected static $lib_change_color = array( 
  1538. "color", "red", "green", "blue",  
  1539. "hue", "saturation", "lightness", "alpha" 
  1540. ); 
  1541. protected function change_color_helper($base, $alter, $i) { 
  1542. return $alter; 
  1543. protected function lib_change_color($args) { 
  1544. return $this->alter_color($args, "change_color_helper"); 
  1545.  
  1546. protected static $lib_scale_color = array( 
  1547. "color", "red", "green", "blue",  
  1548. "hue", "saturation", "lightness", "alpha" 
  1549. ); 
  1550. protected function scale_color_helper($base, $scale, $i) { 
  1551. // 1, 2, 3 - rgb 
  1552. // 4, 5, 6 - hsl 
  1553. // 7 - a 
  1554. switch ($i) { 
  1555. case 1: 
  1556. case 2: 
  1557. case 3: 
  1558. $max = 255; break; 
  1559. case 4: 
  1560. $max = 360; break; 
  1561. case 7: 
  1562. $max = 1; break; 
  1563. default: 
  1564. $max = 100; 
  1565.  
  1566. $scale = $scale / 100; 
  1567. if ($scale < 0) { 
  1568. return $base * $scale + $base; 
  1569. } else { 
  1570. return ($max - $base) * $scale + $base; 
  1571. protected function lib_scale_color($args) { 
  1572. return $this->alter_color($args, "scale_color_helper"); 
  1573.  
  1574. protected static $lib_ie_hex_str = array("color"); 
  1575. protected function lib_ie_hex_str($args) { 
  1576. $color = $this->coerceColor($args[0]); 
  1577. $color[4] = isset($color[4]) ? round(255*$color[4]) : 255; 
  1578.  
  1579. return sprintf('#%02X%02X%02X%02X', $color[4], $color[1], $color[2], $color[3]); 
  1580.  
  1581. protected static $lib_red = array("color"); 
  1582. protected function lib_red($args) { 
  1583. $color = $this->coerceColor($args[0]); 
  1584. return $color[1]; 
  1585.  
  1586. protected static $lib_green = array("color"); 
  1587. protected function lib_green($args) { 
  1588. $color = $this->coerceColor($args[0]); 
  1589. return $color[2]; 
  1590.  
  1591. protected static $lib_blue = array("color"); 
  1592. protected function lib_blue($args) { 
  1593. $color = $this->coerceColor($args[0]); 
  1594. return $color[3]; 
  1595.  
  1596. protected static $lib_alpha = array("color"); 
  1597. protected function lib_alpha($args) { 
  1598. if ($color = $this->coerceColor($args[0])) { 
  1599. return isset($color[4]) ? $color[4] : 1; 
  1600.  
  1601. // this might be the IE function, so return value unchanged 
  1602. return null; 
  1603.  
  1604. protected static $lib_opacity = array("color"); 
  1605. protected function lib_opacity($args) { 
  1606. $value = $args[0]; 
  1607. if ($value[0] === 'number') return null; 
  1608. return $this->lib_alpha($args); 
  1609.  
  1610. // mix two colors 
  1611. protected static $lib_mix = array("color-1", "color-2", "weight"); 
  1612. protected function lib_mix($args) { 
  1613. list($first, $second, $weight) = $args; 
  1614. $first = $this->assertColor($first); 
  1615. $second = $this->assertColor($second); 
  1616.  
  1617. if (is_null($weight)) { 
  1618. $weight = 0.5; 
  1619. } else { 
  1620. $weight = $this->coercePercent($weight); 
  1621.  
  1622. $firstAlpha = isset($first[4]) ? $first[4] : 1; 
  1623. $secondAlpha = isset($second[4]) ? $second[4] : 1; 
  1624.  
  1625. $w = $weight * 2 - 1; 
  1626. $a = $firstAlpha - $secondAlpha; 
  1627.  
  1628. $w1 = (($w * $a == -1 ? $w : ($w + $a)/(1 + $w * $a)) + 1) / 2.0; 
  1629. $w2 = 1.0 - $w1; 
  1630.  
  1631. $new = array('color',  
  1632. $w1 * $first[1] + $w2 * $second[1],  
  1633. $w1 * $first[2] + $w2 * $second[2],  
  1634. $w1 * $first[3] + $w2 * $second[3],  
  1635. ); 
  1636.  
  1637. if ($firstAlpha != 1.0 || $secondAlpha != 1.0) { 
  1638. $new[] = $firstAlpha * $weight + $secondAlpha * ($weight - 1); 
  1639.  
  1640. return $this->fixColor($new); 
  1641.  
  1642. protected static $lib_hsl = array("hue", "saturation", "lightness"); 
  1643. protected function lib_hsl($args) { 
  1644. list($h, $s, $l) = $args; 
  1645. return $this->toRGB($h[1], $s[1], $l[1]); 
  1646.  
  1647. protected static $lib_hsla = array("hue", "saturation",  
  1648. "lightness", "alpha"); 
  1649. protected function lib_hsla($args) { 
  1650. list($h, $s, $l, $a) = $args; 
  1651. $color = $this->toRGB($h[1], $s[1], $l[1]); 
  1652. $color[4] = $a[1]; 
  1653. return $color; 
  1654.  
  1655. protected static $lib_hue = array("color"); 
  1656. protected function lib_hue($args) { 
  1657. $color = $this->assertColor($args[0]); 
  1658. $hsl = $this->toHSL($color[1], $color[2], $color[3]); 
  1659. return array("number", $hsl[1], "deg"); 
  1660.  
  1661. protected static $lib_saturation = array("color"); 
  1662. protected function lib_saturation($args) { 
  1663. $color = $this->assertColor($args[0]); 
  1664. $hsl = $this->toHSL($color[1], $color[2], $color[3]); 
  1665. return array("number", $hsl[2], "%"); 
  1666.  
  1667. protected static $lib_lightness = array("color"); 
  1668. protected function lib_lightness($args) { 
  1669. $color = $this->assertColor($args[0]); 
  1670. $hsl = $this->toHSL($color[1], $color[2], $color[3]); 
  1671. return array("number", $hsl[3], "%"); 
  1672.  
  1673. protected function adjustHsl($color, $idx, $amount) { 
  1674. $hsl = $this->toHSL($color[1], $color[2], $color[3]); 
  1675. $hsl[$idx] += $amount; 
  1676. $out = $this->toRGB($hsl[1], $hsl[2], $hsl[3]); 
  1677. if (isset($color[4])) $out[4] = $color[4]; 
  1678. return $out; 
  1679.  
  1680. protected static $lib_adjust_hue = array("color", "degrees"); 
  1681. protected function lib_adjust_hue($args) { 
  1682. $color = $this->assertColor($args[0]); 
  1683. $degrees = $this->assertNumber($args[1]); 
  1684. return $this->adjustHsl($color, 1, $degrees); 
  1685.  
  1686. protected static $lib_lighten = array("color", "amount"); 
  1687. protected function lib_lighten($args) { 
  1688. $color = $this->assertColor($args[0]); 
  1689. $amount = 100*$this->coercePercent($args[1]); 
  1690. return $this->adjustHsl($color, 3, $amount); 
  1691.  
  1692. protected static $lib_darken = array("color", "amount"); 
  1693. protected function lib_darken($args) { 
  1694. $color = $this->assertColor($args[0]); 
  1695. $amount = 100*$this->coercePercent($args[1]); 
  1696. return $this->adjustHsl($color, 3, -$amount); 
  1697.  
  1698. protected static $lib_saturate = array("color", "amount"); 
  1699. protected function lib_saturate($args) { 
  1700. $value = $args[0]; 
  1701. if ($value[0] === 'number') return null; 
  1702. $color = $this->assertColor($value); 
  1703. $amount = 100*$this->coercePercent($args[1]); 
  1704. return $this->adjustHsl($color, 2, $amount); 
  1705.  
  1706. protected static $lib_desaturate = array("color", "amount"); 
  1707. protected function lib_desaturate($args) { 
  1708. $color = $this->assertColor($args[0]); 
  1709. $amount = 100*$this->coercePercent($args[1]); 
  1710. return $this->adjustHsl($color, 2, -$amount); 
  1711.  
  1712. protected static $lib_grayscale = array("color"); 
  1713. protected function lib_grayscale($args) { 
  1714. $value = $args[0]; 
  1715. if ($value[0] === 'number') return null; 
  1716. return $this->adjustHsl($this->assertColor($value), 2, -100); 
  1717.  
  1718. protected static $lib_complement = array("color"); 
  1719. protected function lib_complement($args) { 
  1720. return $this->adjustHsl($this->assertColor($args[0]), 1, 180); 
  1721.  
  1722. protected static $lib_invert = array("color"); 
  1723. protected function lib_invert($args) { 
  1724. $value = $args[0]; 
  1725. if ($value[0] === 'number') return null; 
  1726. $color = $this->assertColor($value); 
  1727. $color[1] = 255 - $color[1]; 
  1728. $color[2] = 255 - $color[2]; 
  1729. $color[3] = 255 - $color[3]; 
  1730. return $color; 
  1731.  
  1732. // increases opacity by amount 
  1733. protected static $lib_opacify = array("color", "amount"); 
  1734. protected function lib_opacify($args) { 
  1735. $color = $this->assertColor($args[0]); 
  1736. $amount = $this->coercePercent($args[1]); 
  1737.  
  1738. $color[4] = (isset($color[4]) ? $color[4] : 1) + $amount; 
  1739. $color[4] = min(1, max(0, $color[4])); 
  1740. return $color; 
  1741.  
  1742. protected static $lib_fade_in = array("color", "amount"); 
  1743. protected function lib_fade_in($args) { 
  1744. return $this->lib_opacify($args); 
  1745.  
  1746. // decreases opacity by amount 
  1747. protected static $lib_transparentize = array("color", "amount"); 
  1748. protected function lib_transparentize($args) { 
  1749. $color = $this->assertColor($args[0]); 
  1750. $amount = $this->coercePercent($args[1]); 
  1751.  
  1752. $color[4] = (isset($color[4]) ? $color[4] : 1) - $amount; 
  1753. $color[4] = min(1, max(0, $color[4])); 
  1754. return $color; 
  1755.  
  1756. protected static $lib_fade_out = array("color", "amount"); 
  1757. protected function lib_fade_out($args) { 
  1758. return $this->lib_transparentize($args); 
  1759.  
  1760. protected static $lib_unquote = array("string"); 
  1761. protected function lib_unquote($args) { 
  1762. $str = $args[0]; 
  1763. if ($str[0] == "string") $str[1] = ""; 
  1764. return $str; 
  1765.  
  1766. protected static $lib_quote = array("string"); 
  1767. protected function lib_quote($args) { 
  1768. $value = $args[0]; 
  1769. if ($value[0] == "string" && !empty($value[1])) 
  1770. return $value; 
  1771. return array("string", '"', array($value)); 
  1772.  
  1773. protected static $lib_percentage = array("value"); 
  1774. protected function lib_percentage($args) { 
  1775. return array("number",  
  1776. $this->coercePercent($args[0]) * 100,  
  1777. "%"); 
  1778.  
  1779. protected static $lib_round = array("value"); 
  1780. protected function lib_round($args) { 
  1781. $num = $args[0]; 
  1782. $num[1] = round($num[1]); 
  1783. return $num; 
  1784.  
  1785. protected static $lib_floor = array("value"); 
  1786. protected function lib_floor($args) { 
  1787. $num = $args[0]; 
  1788. $num[1] = floor($num[1]); 
  1789. return $num; 
  1790.  
  1791. protected static $lib_ceil = array("value"); 
  1792. protected function lib_ceil($args) { 
  1793. $num = $args[0]; 
  1794. $num[1] = ceil($num[1]); 
  1795. return $num; 
  1796.  
  1797. protected static $lib_abs = array("value"); 
  1798. protected function lib_abs($args) { 
  1799. $num = $args[0]; 
  1800. $num[1] = abs($num[1]); 
  1801. return $num; 
  1802.  
  1803. protected function lib_min($args) { 
  1804. $numbers = $this->getNormalizedNumbers($args); 
  1805. $min = null; 
  1806. foreach ($numbers as $key => $number) { 
  1807. if (null === $min || $number[1] <= $min[1]) { 
  1808. $min = array($key, $number[1]); 
  1809.  
  1810. return $args[$min[0]]; 
  1811.  
  1812. protected function lib_max($args) { 
  1813. $numbers = $this->getNormalizedNumbers($args); 
  1814. $max = null; 
  1815. foreach ($numbers as $key => $number) { 
  1816. if (null === $max || $number[1] >= $max[1]) { 
  1817. $max = array($key, $number[1]); 
  1818.  
  1819. return $args[$max[0]]; 
  1820.  
  1821. protected function getNormalizedNumbers($args) { 
  1822. $unit = null; 
  1823. $originalUnit = null; 
  1824. $numbers = array(); 
  1825. foreach ($args as $key => $item) { 
  1826. if ('number' != $item[0]) { 
  1827. $this->throwError("%s is not a number", $item[0]); 
  1828. $number = $this->normalizeNumber($item); 
  1829.  
  1830. if (null === $unit) { 
  1831. $unit = $number[2]; 
  1832. $originalUnit = $item[2]; 
  1833. } elseif ($unit !== $number[2]) { 
  1834. $this->throwError('Incompatible units: "%s" and "%s".', $originalUnit, $item[2]); 
  1835.  
  1836. $numbers[$key] = $number; 
  1837.  
  1838. return $numbers; 
  1839.  
  1840. protected static $lib_length = array("list"); 
  1841. protected function lib_length($args) { 
  1842. $list = $this->coerceList($args[0]); 
  1843. return count($list[2]); 
  1844.  
  1845. protected static $lib_nth = array("list", "n"); 
  1846. protected function lib_nth($args) { 
  1847. $list = $this->coerceList($args[0]); 
  1848. $n = $this->assertNumber($args[1]) - 1; 
  1849. return isset($list[2][$n]) ? $list[2][$n] : self::$defaultValue; 
  1850.  
  1851. protected function listSeparatorForJoin($list1, $sep) { 
  1852. if (is_null($sep)) return $list1[1]; 
  1853. switch ($this->compileValue($sep)) { 
  1854. case "comma": 
  1855. return ", "; 
  1856. case "space": 
  1857. return ""; 
  1858. default: 
  1859. return $list1[1]; 
  1860.  
  1861. protected static $lib_join = array("list1", "list2", "separator"); 
  1862. protected function lib_join($args) { 
  1863. list($list1, $list2, $sep) = $args; 
  1864. $list1 = $this->coerceList($list1, " "); 
  1865. $list2 = $this->coerceList($list2, " "); 
  1866. $sep = $this->listSeparatorForJoin($list1, $sep); 
  1867. return array("list", $sep, array_merge($list1[2], $list2[2])); 
  1868.  
  1869. protected static $lib_append = array("list", "val", "separator"); 
  1870. protected function lib_append($args) { 
  1871. list($list1, $value, $sep) = $args; 
  1872. $list1 = $this->coerceList($list1, " "); 
  1873. $sep = $this->listSeparatorForJoin($list1, $sep); 
  1874. return array("list", $sep, array_merge($list1[2], array($value))); 
  1875.  
  1876. protected function lib_zip($args) { 
  1877. foreach ($args as $arg) { 
  1878. $this->assertList($arg); 
  1879.  
  1880. $lists = array(); 
  1881. $firstList = array_shift($args); 
  1882. foreach ($firstList[2] as $key => $item) { 
  1883. $list = array("list", "", array($item)); 
  1884. foreach ($args as $arg) { 
  1885. if (isset($arg[2][$key])) { 
  1886. $list[2][] = $arg[2][$key]; 
  1887. } else { 
  1888. break 2; 
  1889. $lists[] = $list; 
  1890.  
  1891. return array("list", ", ", $lists); 
  1892.  
  1893. protected static $lib_type_of = array("value"); 
  1894. protected function lib_type_of($args) { 
  1895. $value = $args[0]; 
  1896. switch ($value[0]) { 
  1897. case "keyword": 
  1898. if ($value == self::$true || $value == self::$false) { 
  1899. return "bool"; 
  1900.  
  1901. if ($this->coerceColor($value)) { 
  1902. return "color"; 
  1903.  
  1904. return "string"; 
  1905. default: 
  1906. return $value[0]; 
  1907.  
  1908. protected static $lib_unit = array("number"); 
  1909. protected function lib_unit($args) { 
  1910. $num = $args[0]; 
  1911. if ($num[0] == "number") { 
  1912. return array("string", '"', array($num[2])); 
  1913. return ""; 
  1914.  
  1915. protected static $lib_unitless = array("number"); 
  1916. protected function lib_unitless($args) { 
  1917. $value = $args[0]; 
  1918. return $value[0] == "number" && empty($value[2]); 
  1919.  
  1920. protected static $lib_comparable = array("number-1", "number-2"); 
  1921. protected function lib_comparable($args) { 
  1922. list($number1, $number2) = $args; 
  1923. if (!isset($number1[0]) || $number1[0] != "number" || !isset($number2[0]) || $number2[0] != "number") { 
  1924. $this->throwError('Invalid argument(s) for "comparable"'); 
  1925.  
  1926. $number1 = $this->normalizeNumber($number1); 
  1927. $number2 = $this->normalizeNumber($number2); 
  1928.  
  1929. return $number1[2] == $number2[2] || $number1[2] == "" || $number2[2] == ""; 
  1930.  
  1931. /** 
  1932. * Workaround IE7's content counter bug. 
  1933. * @param array $args 
  1934. */ 
  1935. protected function lib_counter($args) { 
  1936. $list = array_map(array($this, 'compileValue'), $args); 
  1937. return array('string', '', array('counter(' . implode(', ', $list) . ')')); 
  1938.  
  1939. public function throwError($msg = null) { 
  1940. if (func_num_args() > 1) { 
  1941. $msg = call_user_func_array("sprintf", func_get_args()); 
  1942.  
  1943. if ($this->sourcePos >= 0 && isset($this->sourceParser)) { 
  1944. $this->sourceParser->throwParseError($msg, $this->sourcePos); 
  1945.  
  1946. throw new Exception($msg); 
  1947.  
  1948. /** 
  1949. * CSS Colors 
  1950. * @see http://www.w3.org/TR/css3-color 
  1951. */ 
  1952. static protected $cssColors = array( 
  1953. 'aliceblue' => '240, 248, 255',  
  1954. 'antiquewhite' => '250, 235, 215',  
  1955. 'aqua' => '0, 255, 255',  
  1956. 'aquamarine' => '127, 255, 212',  
  1957. 'azure' => '240, 255, 255',  
  1958. 'beige' => '245, 245, 220',  
  1959. 'bisque' => '255, 228, 196',  
  1960. 'black' => '0, 0, 0',  
  1961. 'blanchedalmond' => '255, 235, 205',  
  1962. 'blue' => '0, 0, 255',  
  1963. 'blueviolet' => '138, 43, 226',  
  1964. 'brown' => '165, 42, 42',  
  1965. 'burlywood' => '222, 184, 135',  
  1966. 'cadetblue' => '95, 158, 160',  
  1967. 'chartreuse' => '127, 255, 0',  
  1968. 'chocolate' => '210, 105, 30',  
  1969. 'coral' => '255, 127, 80',  
  1970. 'cornflowerblue' => '100, 149, 237',  
  1971. 'cornsilk' => '255, 248, 220',  
  1972. 'crimson' => '220, 20, 60',  
  1973. 'cyan' => '0, 255, 255',  
  1974. 'darkblue' => '0, 0, 139',  
  1975. 'darkcyan' => '0, 139, 139',  
  1976. 'darkgoldenrod' => '184, 134, 11',  
  1977. 'darkgray' => '169, 169, 169',  
  1978. 'darkgreen' => '0, 100, 0',  
  1979. 'darkgrey' => '169, 169, 169',  
  1980. 'darkkhaki' => '189, 183, 107',  
  1981. 'darkmagenta' => '139, 0, 139',  
  1982. 'darkolivegreen' => '85, 107, 47',  
  1983. 'darkorange' => '255, 140, 0',  
  1984. 'darkorchid' => '153, 50, 204',  
  1985. 'darkred' => '139, 0, 0',  
  1986. 'darksalmon' => '233, 150, 122',  
  1987. 'darkseagreen' => '143, 188, 143',  
  1988. 'darkslateblue' => '72, 61, 139',  
  1989. 'darkslategray' => '47, 79, 79',  
  1990. 'darkslategrey' => '47, 79, 79',  
  1991. 'darkturquoise' => '0, 206, 209',  
  1992. 'darkviolet' => '148, 0, 211',  
  1993. 'deeppink' => '255, 20, 147',  
  1994. 'deepskyblue' => '0, 191, 255',  
  1995. 'dimgray' => '105, 105, 105',  
  1996. 'dimgrey' => '105, 105, 105',  
  1997. 'dodgerblue' => '30, 144, 255',  
  1998. 'firebrick' => '178, 34, 34',  
  1999. 'floralwhite' => '255, 250, 240',  
  2000. 'forestgreen' => '34, 139, 34',  
  2001. 'fuchsia' => '255, 0, 255',  
  2002. 'gainsboro' => '220, 220, 220',  
  2003. 'ghostwhite' => '248, 248, 255',  
  2004. 'gold' => '255, 215, 0',  
  2005. 'goldenrod' => '218, 165, 32',  
  2006. 'gray' => '128, 128, 128',  
  2007. 'green' => '0, 128, 0',  
  2008. 'greenyellow' => '173, 255, 47',  
  2009. 'grey' => '128, 128, 128',  
  2010. 'honeydew' => '240, 255, 240',  
  2011. 'hotpink' => '255, 105, 180',  
  2012. 'indianred' => '205, 92, 92',  
  2013. 'indigo' => '75, 0, 130',  
  2014. 'ivory' => '255, 255, 240',  
  2015. 'khaki' => '240, 230, 140',  
  2016. 'lavender' => '230, 230, 250',  
  2017. 'lavenderblush' => '255, 240, 245',  
  2018. 'lawngreen' => '124, 252, 0',  
  2019. 'lemonchiffon' => '255, 250, 205',  
  2020. 'lightblue' => '173, 216, 230',  
  2021. 'lightcoral' => '240, 128, 128',  
  2022. 'lightcyan' => '224, 255, 255',  
  2023. 'lightgoldenrodyellow' => '250, 250, 210',  
  2024. 'lightgray' => '211, 211, 211',  
  2025. 'lightgreen' => '144, 238, 144',  
  2026. 'lightgrey' => '211, 211, 211',  
  2027. 'lightpink' => '255, 182, 193',  
  2028. 'lightsalmon' => '255, 160, 122',  
  2029. 'lightseagreen' => '32, 178, 170',  
  2030. 'lightskyblue' => '135, 206, 250',  
  2031. 'lightslategray' => '119, 136, 153',  
  2032. 'lightslategrey' => '119, 136, 153',  
  2033. 'lightsteelblue' => '176, 196, 222',  
  2034. 'lightyellow' => '255, 255, 224',  
  2035. 'lime' => '0, 255, 0',  
  2036. 'limegreen' => '50, 205, 50',  
  2037. 'linen' => '250, 240, 230',  
  2038. 'magenta' => '255, 0, 255',  
  2039. 'maroon' => '128, 0, 0',  
  2040. 'mediumaquamarine' => '102, 205, 170',  
  2041. 'mediumblue' => '0, 0, 205',  
  2042. 'mediumorchid' => '186, 85, 211',  
  2043. 'mediumpurple' => '147, 112, 219',  
  2044. 'mediumseagreen' => '60, 179, 113',  
  2045. 'mediumslateblue' => '123, 104, 238',  
  2046. 'mediumspringgreen' => '0, 250, 154',  
  2047. 'mediumturquoise' => '72, 209, 204',  
  2048. 'mediumvioletred' => '199, 21, 133',  
  2049. 'midnightblue' => '25, 25, 112',  
  2050. 'mintcream' => '245, 255, 250',  
  2051. 'mistyrose' => '255, 228, 225',  
  2052. 'moccasin' => '255, 228, 181',  
  2053. 'navajowhite' => '255, 222, 173',  
  2054. 'navy' => '0, 0, 128',  
  2055. 'oldlace' => '253, 245, 230',  
  2056. 'olive' => '128, 128, 0',  
  2057. 'olivedrab' => '107, 142, 35',  
  2058. 'orange' => '255, 165, 0',  
  2059. 'orangered' => '255, 69, 0',  
  2060. 'orchid' => '218, 112, 214',  
  2061. 'palegoldenrod' => '238, 232, 170',  
  2062. 'palegreen' => '152, 251, 152',  
  2063. 'paleturquoise' => '175, 238, 238',  
  2064. 'palevioletred' => '219, 112, 147',  
  2065. 'papayawhip' => '255, 239, 213',  
  2066. 'peachpuff' => '255, 218, 185',  
  2067. 'peru' => '205, 133, 63',  
  2068. 'pink' => '255, 192, 203',  
  2069. 'plum' => '221, 160, 221',  
  2070. 'powderblue' => '176, 224, 230',  
  2071. 'purple' => '128, 0, 128',  
  2072. 'red' => '255, 0, 0',  
  2073. 'rosybrown' => '188, 143, 143',  
  2074. 'royalblue' => '65, 105, 225',  
  2075. 'saddlebrown' => '139, 69, 19',  
  2076. 'salmon' => '250, 128, 114',  
  2077. 'sandybrown' => '244, 164, 96',  
  2078. 'seagreen' => '46, 139, 87',  
  2079. 'seashell' => '255, 245, 238',  
  2080. 'sienna' => '160, 82, 45',  
  2081. 'silver' => '192, 192, 192',  
  2082. 'skyblue' => '135, 206, 235',  
  2083. 'slateblue' => '106, 90, 205',  
  2084. 'slategray' => '112, 128, 144',  
  2085. 'slategrey' => '112, 128, 144',  
  2086. 'snow' => '255, 250, 250',  
  2087. 'springgreen' => '0, 255, 127',  
  2088. 'steelblue' => '70, 130, 180',  
  2089. 'tan' => '210, 180, 140',  
  2090. 'teal' => '0, 128, 128',  
  2091. 'thistle' => '216, 191, 216',  
  2092. 'tomato' => '255, 99, 71',  
  2093. 'transparent' => '0, 0, 0, 0',  
  2094. 'turquoise' => '64, 224, 208',  
  2095. 'violet' => '238, 130, 238',  
  2096. 'wheat' => '245, 222, 179',  
  2097. 'white' => '255, 255, 255',  
  2098. 'whitesmoke' => '245, 245, 245',  
  2099. 'yellow' => '255, 255, 0',  
  2100. 'yellowgreen' => '154, 205, 50' 
  2101. );