scssc

SCSS compiler.

Defined (1)

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

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