PageRenderTime 64ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/SCSS/scss.inc.php

https://bitbucket.org/Pathou/autres
PHP | 3577 lines | 2934 code | 533 blank | 110 comment | 580 complexity | 32e7110be07e394c81ce91d4e0739937 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. class scssc {
  3. static public $VERSION = "v0.0.3";
  4. static protected $operatorNames = array(
  5. '+' => "add",
  6. '-' => "sub",
  7. '*' => "mul",
  8. '/' => "div",
  9. '%' => "mod",
  10. '==' => "eq",
  11. '!=' => "neq",
  12. '<' => "lt",
  13. '>' => "gt",
  14. '<=' => "lte",
  15. '>=' => "gte",
  16. );
  17. static protected $namespaces = array(
  18. "mixin" => "@",
  19. "function" => "^",
  20. );
  21. static protected $numberPrecision = 3;
  22. static protected $unitTable = array(
  23. "in" => array(
  24. "in" => 1,
  25. "pt" => 72,
  26. "pc" => 6,
  27. "cm" => 2.54,
  28. "mm" => 25.4,
  29. )
  30. );
  31. static public $true = array("keyword", "true");
  32. static public $false = array("keyword", "false");
  33. static public $defaultValue = array("keyword", "");
  34. static public $selfSelector = array("self");
  35. protected $importPaths = array("");
  36. protected $importCache = array();
  37. protected $userFunctions = array();
  38. protected $formatter = "scss_formatter_nested";
  39. function compile($code, $name=null) {
  40. $this->indentLevel = -1;
  41. $this->commentsSeen = array();
  42. $this->extends = array();
  43. $this->extendsMap = array();
  44. $locale = setlocale(LC_NUMERIC, 0);
  45. setlocale(LC_NUMERIC, "C");
  46. $this->parsedFiles = array();
  47. $this->parser = new scss_parser($name);
  48. $tree = $this->parser->parse($code);
  49. $this->formatter = new $this->formatter();
  50. $this->env = null;
  51. $this->scope = null;
  52. $this->compileRoot($tree);
  53. $this->flattenSelectors($this->scope);
  54. ob_start();
  55. $this->formatter->block($this->scope);
  56. $out = ob_get_clean();
  57. setlocale(LC_NUMERIC, $locale);
  58. return $out;
  59. }
  60. protected function pushExtends($target, $origin) {
  61. $i = count($this->extends);
  62. $this->extends[] = array($target, $origin);
  63. foreach ($target as $part) {
  64. if (isset($this->extendsMap[$part])) {
  65. $this->extendsMap[$part][] = $i;
  66. } else {
  67. $this->extendsMap[$part] = array($i);
  68. }
  69. }
  70. }
  71. protected function makeOutputBlock($type, $selectors = null) {
  72. $out = new stdclass;
  73. $out->type = $type;
  74. $out->lines = array();
  75. $out->children = array();
  76. $out->parent = $this->scope;
  77. $out->selectors = $selectors;
  78. $out->depth = $this->env->depth;
  79. return $out;
  80. }
  81. protected function matchExtendsSingle($single, &$out_origin, &$out_rem) {
  82. $counts = array();
  83. foreach ($single as $part) {
  84. if (!is_string($part)) return false; // hmm
  85. if (isset($this->extendsMap[$part])) {
  86. foreach ($this->extendsMap[$part] as $idx) {
  87. $counts[$idx] =
  88. isset($counts[$idx]) ? $counts[$idx] + 1 : 1;
  89. }
  90. }
  91. }
  92. foreach ($counts as $idx => $count) {
  93. list($target, $origin) = $this->extends[$idx];
  94. // check count
  95. if ($count != count($target)) continue;
  96. // check if target is subset of single
  97. if (array_diff(array_intersect($single, $target), $target)) continue;
  98. $out_origin = $origin;
  99. $out_rem = array_diff($single, $target);
  100. return true;
  101. }
  102. return false;
  103. }
  104. protected function combineSelectorSingle($base, $other) {
  105. $tag = null;
  106. $out = array();
  107. foreach (array($base, $other) as $single) {
  108. foreach ($single as $part) {
  109. if (preg_match('/^[^.#:]/', $part)) {
  110. $tag = $part;
  111. } else {
  112. $out[] = $part;
  113. }
  114. }
  115. }
  116. if ($tag) {
  117. array_unshift($out, $tag);
  118. }
  119. return $out;
  120. }
  121. protected function matchExtends($selector, &$out, $from = 0, $initial=true) {
  122. foreach ($selector as $i => $part) {
  123. if ($i < $from) continue;
  124. if ($this->matchExtendsSingle($part, $origin, $rem)) {
  125. $before = array_slice($selector, 0, $i);
  126. $after = array_slice($selector, $i + 1);
  127. foreach ($origin as $new) {
  128. $new[count($new) - 1] =
  129. $this->combineSelectorSingle(end($new), $rem);
  130. $k = 0;
  131. // remove shared parts
  132. if ($initial) {
  133. foreach ($before as $k => $val) {
  134. if (!isset($new[$k]) || $val != $new[$k]) {
  135. break;
  136. }
  137. }
  138. }
  139. $result = array_merge(
  140. $before,
  141. $k > 0 ? array_slice($new, $k) : $new,
  142. $after);
  143. if ($result == $selector) continue;
  144. $out[] = $result;
  145. // recursively check for more matches
  146. $this->matchExtends($result, $out, $i, false);
  147. // selector sequence merging
  148. if (!empty($before) && count($new) > 1) {
  149. $result2 = array_merge(
  150. array_slice($new, 0, -1),
  151. $k > 0 ? array_slice($before, $k) : $before,
  152. array_slice($new, -1),
  153. $after);
  154. $out[] = $result2;
  155. }
  156. }
  157. }
  158. }
  159. }
  160. protected function flattenSelectors($block) {
  161. if ($block->selectors) {
  162. $selectors = array();
  163. foreach ($block->selectors as $s) {
  164. $selectors[] = $s;
  165. if (!is_array($s)) continue;
  166. // check extends
  167. if (!empty($this->extendsMap)) {
  168. $this->matchExtends($s, $selectors);
  169. }
  170. }
  171. $selectors = array_map(array($this, "compileSelector"), $selectors);
  172. $block->selectors = $selectors;
  173. }
  174. foreach ($block->children as $child) {
  175. $this->flattenSelectors($child);
  176. }
  177. }
  178. protected function compileRoot($rootBlock) {
  179. $this->pushEnv($rootBlock);
  180. $this->scope = $this->makeOutputBlock("root");
  181. $this->compileChildren($rootBlock->children, $this->scope);
  182. $this->popEnv();
  183. }
  184. protected function compileMedia($media) {
  185. $this->pushEnv($media);
  186. $parentScope = $this->mediaParent($this->scope);
  187. $this->scope = $this->makeOutputBlock("media", array(
  188. $this->compileMediaQuery($this->multiplyMedia($this->env)))
  189. );
  190. $parentScope->children[] = $this->scope;
  191. $this->compileChildren($media->children, $this->scope);
  192. $this->scope = $this->scope->parent;
  193. $this->popEnv();
  194. }
  195. protected function mediaParent($scope) {
  196. while (!empty($scope->parent)) {
  197. if (!empty($scope->type) && $scope->type != "media") {
  198. break;
  199. }
  200. $scope = $scope->parent;
  201. }
  202. return $scope;
  203. }
  204. // TODO refactor compileNestedBlock and compileMedia into same thing
  205. protected function compileNestedBlock($block, $selectors) {
  206. $this->pushEnv($block);
  207. $this->scope = $this->makeOutputBlock($block->type, $selectors);
  208. $this->scope->parent->children[] = $this->scope;
  209. $this->compileChildren($block->children, $this->scope);
  210. $this->scope = $this->scope->parent;
  211. $this->popEnv();
  212. }
  213. protected function compileBlock($block) {
  214. $env = $this->pushEnv($block);
  215. $env->selectors =
  216. array_map(array($this, "evalSelector"), $block->selectors);
  217. $out = $this->makeOutputBlock(null, $this->multiplySelectors($env));
  218. $this->scope->children[] = $out;
  219. $this->compileChildren($block->children, $out);
  220. $this->popEnv();
  221. }
  222. // joins together .classes and #ids
  223. protected function flattenSelectorSingle($single) {
  224. $joined = array();
  225. foreach ($single as $part) {
  226. if (empty($joined) ||
  227. !is_string($part) ||
  228. preg_match('/[.:#]/', $part))
  229. {
  230. $joined[] = $part;
  231. continue;
  232. }
  233. if (is_array(end($joined))) {
  234. $joined[] = $part;
  235. } else {
  236. $joined[count($joined) - 1] .= $part;
  237. }
  238. }
  239. return $joined;
  240. }
  241. // replaces all the interpolates
  242. protected function evalSelector($selector) {
  243. return array_map(array($this, "evalSelectorPart"), $selector);
  244. }
  245. protected function evalSelectorPart($piece) {
  246. foreach ($piece as &$p) {
  247. if (!is_array($p)) continue;
  248. switch ($p[0]) {
  249. case "interpolate":
  250. $p = $this->compileValue($p);
  251. break;
  252. }
  253. }
  254. return $this->flattenSelectorSingle($piece);
  255. }
  256. // compiles to string
  257. // self(&) should have been replaced by now
  258. protected function compileSelector($selector) {
  259. if (!is_array($selector)) return $selector; // media and the like
  260. return implode(" ", array_map(
  261. array($this, "compileSelectorPart"), $selector));
  262. }
  263. protected function compileSelectorPart($piece) {
  264. foreach ($piece as &$p) {
  265. if (!is_array($p)) continue;
  266. switch ($p[0]) {
  267. case "self":
  268. $p = "&";
  269. break;
  270. default:
  271. $p = $this->compileValue($p);
  272. break;
  273. }
  274. }
  275. return implode($piece);
  276. }
  277. protected function compileChildren($stms, $out) {
  278. foreach ($stms as $stm) {
  279. $ret = $this->compileChild($stm, $out);
  280. if (!is_null($ret)) return $ret;
  281. }
  282. }
  283. protected function compileMediaQuery($query) {
  284. $parts = array();
  285. foreach ($query as $q) {
  286. switch ($q[0]) {
  287. case "mediaType":
  288. $parts[] = implode(" ", array_slice($q, 1));
  289. break;
  290. case "mediaExp":
  291. if (isset($q[2])) {
  292. $parts[] = "($q[1]: " . $this->compileValue($q[2]) . ")";
  293. } else {
  294. $parts[] = "($q[1])";
  295. }
  296. break;
  297. }
  298. }
  299. $out = "@media";
  300. if (!empty($parts)) {
  301. $out = $out . " " . implode(" and ", $parts);
  302. }
  303. return $out;
  304. }
  305. // returns true if the value was something that could be imported
  306. protected function compileImport($rawPath, $out) {
  307. if ($rawPath[0] == "string") {
  308. $path = $this->compileStringContent($rawPath);
  309. if ($path = $this->findImport($path)) {
  310. $this->importFile($path, $out);
  311. return true;
  312. }
  313. return false;
  314. } if ($rawPath[0] == "list") {
  315. // handle a list of strings
  316. if (count($rawPath[2]) == 0) return false;
  317. foreach ($rawPath[2] as $path) {
  318. if ($path[0] != "string") return false;
  319. }
  320. foreach ($rawPath[2] as $path) {
  321. $this->compileImport($path, $out);
  322. }
  323. return true;
  324. }
  325. return false;
  326. }
  327. // return a value to halt execution
  328. protected function compileChild($child, $out) {
  329. switch ($child[0]) {
  330. case "import":
  331. list(,$rawPath) = $child;
  332. $rawPath = $this->reduce($rawPath);
  333. if (!$this->compileImport($rawPath, $out)) {
  334. $out->lines[] = "@import " . $this->compileValue($rawPath) . ";";
  335. }
  336. break;
  337. case "directive":
  338. list(, $directive) = $child;
  339. $s = "@" . $directive->name;
  340. if (!empty($directive->value)) {
  341. $s .= " " . $this->compileValue($directive->value);
  342. }
  343. $this->compileNestedBlock($directive, array($s));
  344. break;
  345. case "media":
  346. $this->compileMedia($child[1]);
  347. break;
  348. case "block":
  349. $this->compileBlock($child[1]);
  350. break;
  351. case "charset":
  352. $out->lines[] = "@charset ".$this->compileValue($child[1]).";";
  353. break;
  354. case "assign":
  355. list(,$name, $value) = $child;
  356. if ($name[0] == "var") {
  357. $isDefault = !empty($child[3]);
  358. if (!$isDefault || $this->get($name[1], true) === true) {
  359. $this->set($name[1], $this->reduce($value));
  360. }
  361. break;
  362. }
  363. $out->lines[] = $this->formatter->property(
  364. $this->compileValue($child[1]),
  365. $this->compileValue($child[2]));
  366. break;
  367. case "comment":
  368. $out->lines[] = $child[1];
  369. break;
  370. case "mixin":
  371. case "function":
  372. list(,$block) = $child;
  373. $this->set(self::$namespaces[$block->type] . $block->name, $block);
  374. break;
  375. case "extend":
  376. list(, $selectors) = $child;
  377. foreach ($selectors as $sel) {
  378. // only use the first one
  379. $sel = current($this->evalSelector($sel));
  380. $this->pushExtends($sel, $out->selectors);
  381. }
  382. break;
  383. case "if":
  384. list(, $if) = $child;
  385. if ($this->reduce($if->cond, true) != self::$false) {
  386. return $this->compileChildren($if->children, $out);
  387. } else {
  388. foreach ($if->cases as $case) {
  389. if ($case->type == "else" ||
  390. $case->type == "elseif" && ($this->reduce($case->cond) != self::$false))
  391. {
  392. return $this->compileChildren($case->children, $out);
  393. }
  394. }
  395. }
  396. break;
  397. case "return":
  398. return $this->reduce($child[1], true);
  399. case "each":
  400. list(,$each) = $child;
  401. $list = $this->reduce($this->coerceList($each->list));
  402. foreach ($list[2] as $item) {
  403. $this->pushEnv();
  404. $this->set($each->var, $item);
  405. // TODO: allow return from here
  406. $this->compileChildren($each->children, $out);
  407. $this->popEnv();
  408. }
  409. break;
  410. case "while":
  411. list(,$while) = $child;
  412. while ($this->reduce($while->cond, true) != self::$false) {
  413. $ret = $this->compileChildren($while->children, $out);
  414. if ($ret) return $ret;
  415. }
  416. break;
  417. case "for":
  418. list(,$for) = $child;
  419. $start = $this->reduce($for->start, true);
  420. $start = $start[1];
  421. $end = $this->reduce($for->end, true);
  422. $end = $end[1];
  423. $d = $start < $end ? 1 : -1;
  424. while (true) {
  425. if ((!$for->until && $start - $d == $end) ||
  426. ($for->until && $start == $end))
  427. {
  428. break;
  429. }
  430. $this->set($for->var, array("number", $start, ""));
  431. $start += $d;
  432. $ret = $this->compileChildren($for->children, $out);
  433. if ($ret) return $ret;
  434. }
  435. break;
  436. case "nestedprop":
  437. list(,$prop) = $child;
  438. $prefixed = array();
  439. $prefix = $this->compileValue($prop->prefix) . "-";
  440. foreach ($prop->children as $child) {
  441. if ($child[0] == "assign") {
  442. array_unshift($child[1][2], $prefix);
  443. }
  444. if ($child[0] == "nestedprop") {
  445. array_unshift($child[1]->prefix[2], $prefix);
  446. }
  447. $prefixed[] = $child;
  448. }
  449. $this->compileChildren($prefixed, $out);
  450. break;
  451. case "include": // including a mixin
  452. list(,$name, $argValues) = $child;
  453. $mixin = $this->get(self::$namespaces["mixin"] . $name, false);
  454. if (!$mixin) break; // throw error?
  455. // push scope, apply args
  456. $this->pushEnv();
  457. if (!is_null($mixin->args)) {
  458. $this->applyArguments($mixin->args, $argValues);
  459. }
  460. foreach ($mixin->children as $child) {
  461. $this->compileChild($child, $out);
  462. }
  463. $this->popEnv();
  464. break;
  465. case "debug":
  466. list(,$value, $pos) = $child;
  467. $line = $this->parser->getLineNo($pos);
  468. $value = $this->compileValue($this->reduce($value, true));
  469. fwrite(STDERR, "Line $line DEBUG: $value\n");
  470. break;
  471. default:
  472. throw new exception("unknown child type: $child[0]");
  473. }
  474. }
  475. protected function expToString($exp) {
  476. list(, $op, $left, $right, $inParens, $whiteLeft, $whiteRight) = $exp;
  477. $content = array($left);
  478. if ($whiteLeft) $content[] = " ";
  479. $content[] = $op;
  480. if ($whiteRight) $content[] = " ";
  481. $content[] = $right;
  482. return array("string", "", $content);
  483. }
  484. // should $value cause its operand to eval
  485. protected function shouldEval($value) {
  486. switch ($value[0]) {
  487. case "exp":
  488. if ($value[1] == "/") {
  489. return $this->shouldEval($value[2], $value[3]);
  490. }
  491. case "var":
  492. case "fncall":
  493. return true;
  494. }
  495. return false;
  496. }
  497. protected function reduce($value, $inExp = false) {
  498. list($type) = $value;
  499. switch ($type) {
  500. case "exp":
  501. list(, $op, $left, $right, $inParens) = $value;
  502. $opName = isset(self::$operatorNames[$op]) ? self::$operatorNames[$op] : $op;
  503. $inExp = $inExp || $this->shouldEval($left) || $this->shouldEval($right);
  504. $left = $this->reduce($left, true);
  505. $right = $this->reduce($right, true);
  506. // only do division in special cases
  507. if ($opName == "div" && !$inParens && !$inExp) {
  508. if ($left[0] != "color" && $right[0] != "color") {
  509. return $this->expToString($value);
  510. }
  511. }
  512. $left = $this->coerceForExpression($left);
  513. $right = $this->coerceForExpression($right);
  514. $ltype = $left[0];
  515. $rtype = $right[0];
  516. // this tries:
  517. // 1. op_[op name]_[left type]_[right type]
  518. // 2. op_[left type]_[right type] (passing the op as first arg
  519. // 3. op_[op name]
  520. $fn = "op_${opName}_${ltype}_${rtype}";
  521. if (is_callable(array($this, $fn)) ||
  522. (($fn = "op_${ltype}_${rtype}") &&
  523. is_callable(array($this, $fn)) &&
  524. $passOp = true) ||
  525. (($fn = "op_${opName}") &&
  526. is_callable(array($this, $fn)) &&
  527. $genOp = true))
  528. {
  529. $unitChange = false;
  530. if (!isset($genOp) &&
  531. $left[0] == "number" && $right[0] == "number")
  532. {
  533. $unitChange = true;
  534. if ($opName == "div" && $left[2] == $right[2]) {
  535. $targetUnit = "";
  536. } else {
  537. $targetUnit = $left[2];
  538. $left = $this->normalizeNumber($left);
  539. $right = $this->normalizeNumber($right);
  540. }
  541. }
  542. $shouldEval = $inParens || $inExp;
  543. if (isset($passOp)) {
  544. $out = $this->$fn($op, $left, $right, $shouldEval);
  545. } else {
  546. $out = $this->$fn($left, $right, $shouldEval);
  547. }
  548. if (!is_null($out)) {
  549. if ($unitChange && $out[0] == "number") {
  550. $out = $this->coerceUnit($out, $targetUnit);
  551. }
  552. return $out;
  553. }
  554. }
  555. return $this->expToString($value);
  556. case "unary":
  557. list(, $op, $exp, $inParens) = $value;
  558. $inExp = $inExp || $this->shouldEval($exp);
  559. $exp = $this->reduce($exp);
  560. if ($exp[0] == "number") {
  561. switch ($op) {
  562. case "+":
  563. return $exp;
  564. case "-":
  565. $exp[1] *= -1;
  566. return $exp;
  567. }
  568. }
  569. if ($op == "not") {
  570. if ($inExp || $inParens) {
  571. if ($exp == self::$false) {
  572. return self::$true;
  573. } else {
  574. return self::$false;
  575. }
  576. } else {
  577. $op = $op . " ";
  578. }
  579. }
  580. return array("string", "", array($op, $exp));
  581. case "var":
  582. list(, $name) = $value;
  583. return $this->reduce($this->get($name));
  584. case "list":
  585. foreach ($value[2] as &$item) {
  586. $item = $this->reduce($item);
  587. }
  588. return $value;
  589. case "string":
  590. foreach ($value[2] as &$item) {
  591. if (is_array($item)) {
  592. $item = $this->reduce($item);
  593. }
  594. }
  595. return $value;
  596. case "interpolate":
  597. $value[1] = $this->reduce($value[1]);
  598. return $value;
  599. case "fncall":
  600. list(,$name, $argValues) = $value;
  601. // user defined function?
  602. $func = $this->get(self::$namespaces["function"] . $name, false);
  603. if ($func) {
  604. $this->pushEnv();
  605. // set the args
  606. if (isset($func->args)) {
  607. $this->applyArguments($func->args, $argValues);
  608. }
  609. // throw away lines and children
  610. $tmp = (object)array(
  611. "lines" => array(),
  612. "children" => array()
  613. );
  614. $ret = $this->compileChildren($func->children, $tmp);
  615. $this->popEnv();
  616. return is_null($ret) ? self::$defaultValue : $ret;
  617. }
  618. // built in function
  619. if ($this->callBuiltin($name, $argValues, $returnValue)) {
  620. return $returnValue;
  621. }
  622. // need to flatten the arguments into a list
  623. $listArgs = array();
  624. foreach ((array)$argValues as $arg) {
  625. if (empty($arg[0])) {
  626. $listArgs[] = $this->reduce($arg[1]);
  627. }
  628. }
  629. return array("function", $name, array("list", ",", $listArgs));
  630. default:
  631. return $value;
  632. }
  633. }
  634. // just does physical lengths for now
  635. protected function normalizeNumber($number) {
  636. list(, $value, $unit) = $number;
  637. if (isset(self::$unitTable["in"][$unit])) {
  638. $conv = self::$unitTable["in"][$unit];
  639. return array("number", $value / $conv, "in");
  640. }
  641. return $number;
  642. }
  643. // $number should be normalized
  644. protected function coerceUnit($number, $unit) {
  645. list(, $value, $baseUnit) = $number;
  646. if (isset(self::$unitTable[$baseUnit][$unit])) {
  647. $value = $value * self::$unitTable[$baseUnit][$unit];
  648. }
  649. return array("number", $value, $unit);
  650. }
  651. protected function op_add_number_number($left, $right) {
  652. return array("number", $left[1] + $right[1], $left[2]);
  653. }
  654. protected function op_mul_number_number($left, $right) {
  655. return array("number", $left[1] * $right[1], $left[2]);
  656. }
  657. protected function op_sub_number_number($left, $right) {
  658. return array("number", $left[1] - $right[1], $left[2]);
  659. }
  660. protected function op_div_number_number($left, $right) {
  661. return array("number", $left[1] / $right[1], $left[2]);
  662. }
  663. protected function op_mod_number_number($left, $right) {
  664. return array("number", $left[1] % $right[1], $left[2]);
  665. }
  666. // adding strings
  667. protected function op_add($left, $right) {
  668. if ($strLeft = $this->coerceString($left)) {
  669. if ($right[0] == "string") {
  670. $right[1] = "";
  671. }
  672. $strLeft[2][] = $right;
  673. return $strLeft;
  674. }
  675. if ($strRight = $this->coerceString($right)) {
  676. if ($left[0] == "string") {
  677. $left[1] = "";
  678. }
  679. array_unshift($strRight[2], $left);
  680. return $strRight;
  681. }
  682. }
  683. protected function op_and($left, $right, $shouldEval) {
  684. if (!$shouldEval) return;
  685. if ($left != self::$false) return $right;
  686. return $left;
  687. }
  688. protected function op_or($left, $right, $shouldEval) {
  689. if (!$shouldEval) return;
  690. if ($left != self::$false) return $left;
  691. return $right;
  692. }
  693. protected function op_color_color($op, $left, $right) {
  694. $out = array('color');
  695. foreach (range(1, 3) as $i) {
  696. $lval = isset($left[$i]) ? $left[$i] : 0;
  697. $rval = isset($right[$i]) ? $right[$i] : 0;
  698. switch ($op) {
  699. case '+':
  700. $out[] = $lval + $rval;
  701. break;
  702. case '-':
  703. $out[] = $lval - $rval;
  704. break;
  705. case '*':
  706. $out[] = $lval * $rval;
  707. break;
  708. case '%':
  709. $out[] = $lval % $rval;
  710. break;
  711. case '/':
  712. if ($rval == 0) {
  713. throw new exception("color: Can't divide by zero");
  714. }
  715. $out[] = $lval / $rval;
  716. break;
  717. default:
  718. throw new exception("color: unknow op $op");
  719. }
  720. }
  721. if (isset($left[4])) $out[4] = $left[4];
  722. elseif (isset($right[4])) $out[4] = $right[4];
  723. return $this->fixColor($out);
  724. }
  725. protected function op_color_number($op, $left, $right) {
  726. $value = $right[1];
  727. return $this->op_color_color($op, $left,
  728. array("color", $value, $value, $value));
  729. }
  730. protected function op_number_color($op, $left, $right) {
  731. $value = $left[1];
  732. return $this->op_color_color($op,
  733. array("color", $value, $value, $value), $right);
  734. }
  735. protected function op_eq($left, $right) {
  736. if (($lStr = $this->coerceString($left)) && ($rStr = $this->coerceString($right))) {
  737. $lStr[1] = "";
  738. $rStr[1] = "";
  739. return $this->toBool($this->compileValue($lStr) == $this->compileValue($rStr));
  740. }
  741. return $this->toBool($left == $right);
  742. }
  743. protected function op_neq($left, $right) {
  744. return $this->toBool($left != $right);
  745. }
  746. protected function op_gte_number_number($left, $right) {
  747. return $this->toBool($left[1] >= $right[1]);
  748. }
  749. protected function op_gt_number_number($left, $right) {
  750. return $this->toBool($left[1] > $right[1]);
  751. }
  752. protected function op_lte_number_number($left, $right) {
  753. return $this->toBool($left[1] <= $right[1]);
  754. }
  755. protected function op_lt_number_number($left, $right) {
  756. return $this->toBool($left[1] < $right[1]);
  757. }
  758. protected function toBool($thing) {
  759. return $thing ? self::$true : self::$false;
  760. }
  761. protected function compileValue($value) {
  762. $value = $this->reduce($value);
  763. list($type) = $value;
  764. switch ($type) {
  765. case "keyword":
  766. return $value[1];
  767. case "color":
  768. // [1] - red component (either number for a %)
  769. // [2] - green component
  770. // [3] - blue component
  771. // [4] - optional alpha component
  772. list(, $r, $g, $b) = $value;
  773. $r = round($r);
  774. $g = round($g);
  775. $b = round($b);
  776. if (count($value) == 5 && $value[4] != 1) { // rgba
  777. return 'rgba('.$r.', '.$g.', '.$b.', '.$value[4].')';
  778. }
  779. $h = sprintf("#%02x%02x%02x", $r, $g, $b);
  780. // Converting hex color to short notation (e.g. #003399 to #039)
  781. if ($h[1] === $h[2] && $h[3] === $h[4] && $h[5] === $h[6]) {
  782. $h = '#' . $h[1] . $h[3] . $h[5];
  783. }
  784. return $h;
  785. case "number":
  786. return round($value[1], self::$numberPrecision) . $value[2];
  787. case "string":
  788. return $value[1] . $this->compileStringContent($value) . $value[1];
  789. case "function":
  790. $args = !empty($value[2]) ? $this->compileValue($value[2]) : "";
  791. return "$value[1]($args)";
  792. case "list":
  793. $value = $this->extractInterpolation($value);
  794. if ($value[0] != "list") return $this->compileValue($value);
  795. list(, $delim, $items) = $value;
  796. foreach ($items as &$item) {
  797. $item = $this->compileValue($item);
  798. }
  799. return implode("$delim ", $items);
  800. case "interpolated": # node created by extractInterpolation
  801. list(, $interpolate, $left, $right) = $value;
  802. list(,, $whiteLeft, $whiteRight) = $interpolate;
  803. $left = count($left[2]) > 0 ?
  804. $this->compileValue($left).$whiteLeft : "";
  805. $right = count($right[2]) > 0 ?
  806. $whiteRight.$this->compileValue($right) : "";
  807. return $left.$this->compileValue($interpolate).$right;
  808. case "interpolate": # raw parse node
  809. list(, $exp) = $value;
  810. // strip quotes if it's a string
  811. $reduced = $this->reduce($exp);
  812. if ($reduced[0] == "string") {
  813. $reduced = array("keyword",
  814. $this->compileStringContent($reduced));
  815. }
  816. return $this->compileValue($reduced);
  817. default:
  818. throw new exception("unknown value type: $type");
  819. }
  820. }
  821. protected function compileStringContent($string) {
  822. $parts = array();
  823. foreach ($string[2] as $part) {
  824. if (is_array($part)) {
  825. $parts[] = $this->compileValue($part);
  826. } else {
  827. $parts[] = $part;
  828. }
  829. }
  830. return implode($parts);
  831. }
  832. // doesn't need to be recursive, compileValue will handle that
  833. protected function extractInterpolation($list) {
  834. $items = $list[2];
  835. foreach ($items as $i => $item) {
  836. if ($item[0] == "interpolate") {
  837. $before = array("list", $list[1], array_slice($items, 0, $i));
  838. $after = array("list", $list[1], array_slice($items, $i + 1));
  839. return array("interpolated", $item, $before, $after);
  840. }
  841. }
  842. return $list;
  843. }
  844. // find the final set of selectors
  845. protected function multiplySelectors($env, $childSelectors = null) {
  846. if (is_null($env)) {
  847. return $childSelectors;
  848. }
  849. // skip env, has no selectors
  850. if (empty($env->selectors)) {
  851. return $this->multiplySelectors($env->parent, $childSelectors);
  852. }
  853. if (is_null($childSelectors)) {
  854. $selectors = $env->selectors;
  855. } else {
  856. $selectors = array();
  857. foreach ($env->selectors as $parent) {
  858. foreach ($childSelectors as $child) {
  859. $selectors[] = $this->joinSelectors($parent, $child);
  860. }
  861. }
  862. }
  863. return $this->multiplySelectors($env->parent, $selectors);
  864. }
  865. // looks for & to replace, or append parent before child
  866. protected function joinSelectors($parent, $child) {
  867. $setSelf = false;
  868. $out = array();
  869. foreach ($child as $part) {
  870. $newPart = array();
  871. foreach ($part as $p) {
  872. if ($p == self::$selfSelector) {
  873. $setSelf = true;
  874. foreach ($parent as $i => $parentPart) {
  875. if ($i > 0) {
  876. $out[] = $newPart;
  877. $newPart = array();
  878. }
  879. foreach ($parentPart as $pp) {
  880. $newPart[] = $pp;
  881. }
  882. }
  883. } else {
  884. $newPart[] = $p;
  885. }
  886. }
  887. $out[] = $newPart;
  888. }
  889. return $setSelf ? $out : array_merge($parent, $child);
  890. }
  891. protected function multiplyMedia($env, $childMedia = null) {
  892. if (is_null($env) ||
  893. !empty($env->block->type) && $env->block->type != "media")
  894. {
  895. return $childMedia;
  896. }
  897. // plain old block, skip
  898. if (empty($env->block->type)) {
  899. return $this->multiplyMedia($env->parent, $childMedia);
  900. }
  901. $query = $env->block->query;
  902. if ($childMedia == null) {
  903. $childMedia = $query;
  904. } else {
  905. $childMedia = array_merge($query, $childMedia);
  906. }
  907. return $this->multiplyMedia($env->parent, $childMedia);
  908. }
  909. // convert something to list
  910. protected function coerceList($item, $delim = ",") {
  911. if (!is_null($item) && $item[0] == "list") {
  912. return $item;
  913. }
  914. return array("list", $delim, is_null($item) ? array(): array($item));
  915. }
  916. protected function applyArguments($argDef, $argValues) {
  917. $argValues = (array)$argValues;
  918. $keywordArgs = array();
  919. $remaining = array();
  920. // assign the keyword args
  921. foreach ($argValues as $arg) {
  922. if (!empty($arg[0])) {
  923. $keywordArgs[$arg[0][1]] = $arg[1];
  924. } else {
  925. $remaining[] = $arg[1];
  926. }
  927. }
  928. foreach ($argDef as $i => $arg) {
  929. list($name, $default) = $arg;
  930. if (isset($remaining[$i])) {
  931. $val = $remaining[$i];
  932. } elseif (isset($keywordArgs[$name])) {
  933. $val = $keywordArgs[$name];
  934. } elseif (!empty($default)) {
  935. $val = $default;
  936. } else {
  937. $val = self::$defaultValue;
  938. }
  939. $this->set($name, $this->reduce($val, true));
  940. }
  941. }
  942. protected function pushEnv($block=null) {
  943. $env = new stdclass;
  944. $env->parent = $this->env;
  945. $env->store = array();
  946. $env->block = $block;
  947. $env->depth = isset($this->env->depth) ? $this->env->depth + 1 : 0;
  948. $this->env = $env;
  949. return $env;
  950. }
  951. protected function normalizeName($name) {
  952. return str_replace("-", "_", $name);
  953. }
  954. protected function set($name, $value) {
  955. $name = $this->normalizeName($name);
  956. $this->setExisting($name, $value);
  957. }
  958. protected function setExisting($name, $value, $env = null) {
  959. if (is_null($env)) $env = $this->env;
  960. if (isset($env->store[$name])) {
  961. $env->store[$name] = $value;
  962. } elseif (!is_null($env->parent)) {
  963. $this->setExisting($name, $value, $env->parent);
  964. } else {
  965. $this->env->store[$name] = $value;
  966. }
  967. }
  968. protected function get($name, $defaultValue = null, $env = null) {
  969. $name = $this->normalizeName($name);
  970. if (is_null($env)) $env = $this->env;
  971. if (is_null($defaultValue)) $defaultValue = self::$defaultValue;
  972. if (isset($env->store[$name])) {
  973. return $env->store[$name];
  974. } elseif (!is_null($env->parent)) {
  975. return $this->get($name, $defaultValue, $env->parent);
  976. }
  977. return $defaultValue; // found nothing
  978. }
  979. protected function popEnv() {
  980. $env = $this->env;
  981. $this->env = $this->env->parent;
  982. return $env;
  983. }
  984. public function getParsedFiles() {
  985. return $this->parsedFiles;
  986. }
  987. public function addImportPath($path) {
  988. $this->importPaths[] = $path;
  989. }
  990. public function setImportPaths($path) {
  991. $this->importPaths = (array)$path;
  992. }
  993. public function setFormatter($formatterName) {
  994. $this->formatter = $formatterName;
  995. }
  996. public function registerFunction($name, $func) {
  997. $this->userFunctions[$this->normalizeName($name)] = $func;
  998. }
  999. public function unregisterFunction($name) {
  1000. unset($this->userFunctions[$this->normalizeName($name)]);
  1001. }
  1002. protected function importFile($path, $out) {
  1003. // see if tree is cached
  1004. $realPath = realpath($path);
  1005. if (isset($this->importCache[$realPath])) {
  1006. $tree = $this->importCache[$realPath];
  1007. } else {
  1008. $code = file_get_contents($path);
  1009. $parser = new scss_parser($path);
  1010. $tree = $parser->parse($code);
  1011. $this->parsedFiles[] = $path;
  1012. $this->importCache[$realPath] = $tree;
  1013. }
  1014. $pi = pathinfo($path);
  1015. array_unshift($this->importPaths, $pi['dirname']);
  1016. $this->compileChildren($tree->children, $out);
  1017. array_shift($this->importPaths);
  1018. }
  1019. // results the file path for an import url if it exists
  1020. protected function findImport($url) {
  1021. if (preg_match('/\.css|^http:\/\/$/', $url)) return null;
  1022. // try the _partial filename
  1023. $_url = preg_replace('/[^\/]+$/', '_\0', $url);
  1024. foreach ((array)$this->importPaths as $dir) {
  1025. foreach (array($url, $_url) as $full) {
  1026. $full = $dir .
  1027. (!empty($dir) && substr($dir, -1) != '/' ? '/' : '') .
  1028. $full;
  1029. if ($this->fileExists($file = $full.'.scss') ||
  1030. $this->fileExists($file = $full))
  1031. {
  1032. return $file;
  1033. }
  1034. }
  1035. }
  1036. return null;
  1037. }
  1038. protected function fileExists($name) {
  1039. return is_file($name);
  1040. }
  1041. protected function callBuiltin($name, $args, &$returnValue) {
  1042. // try a lib function
  1043. $name = $this->normalizeName($name);
  1044. $libName = "lib_".$name;
  1045. $f = array($this, $libName);
  1046. $prototype = isset(self::$$libName) ? self::$$libName : null;
  1047. if (is_callable($f)) {
  1048. $sorted = $this->sortArgs($prototype, $args);
  1049. foreach ($sorted as &$val) {
  1050. $val = $this->reduce($val, true);
  1051. }
  1052. $returnValue = call_user_func($f, $sorted, $this);
  1053. } else if (isset($this->userFunctions[$name])) {
  1054. // see if we can find a user function
  1055. $fn = $this->userFunctions[$name];
  1056. foreach ($args as &$val) {
  1057. $val = $this->reduce($val[1], true);
  1058. }
  1059. $returnValue = call_user_func($fn, $args, $this);
  1060. }
  1061. if (isset($returnValue)) {
  1062. // coerce a php value into a scss one
  1063. if (is_numeric($returnValue)) {
  1064. $returnValue = array('number', $returnValue, "");
  1065. } elseif (is_bool($returnValue)) {
  1066. $returnValue = $returnValue ? self::$true : self::$false;
  1067. } elseif (!is_array($returnValue)) {
  1068. $returnValue = array('keyword', $returnValue);
  1069. }
  1070. return true;
  1071. }
  1072. return false;
  1073. }
  1074. // sorts any keyword arguments
  1075. // TODO: merge with apply arguments
  1076. protected function sortArgs($prototype, $args) {
  1077. $keyArgs = array();
  1078. $posArgs = array();
  1079. foreach ($args as $arg) {
  1080. list($key, $value) = $arg;
  1081. $key = $key[1];
  1082. if (empty($key)) {
  1083. $posArgs[] = $value;
  1084. } else {
  1085. $keyArgs[$key] = $value;
  1086. }
  1087. }
  1088. if (is_null($prototype)) return $posArgs;
  1089. $finalArgs = array();
  1090. foreach ($prototype as $i => $names) {
  1091. if (isset($posArgs[$i])) {
  1092. $finalArgs[] = $posArgs[$i];
  1093. continue;
  1094. }
  1095. $set = false;
  1096. foreach ((array)$names as $name) {
  1097. if (isset($keyArgs[$name])) {
  1098. $finalArgs[] = $keyArgs[$name];
  1099. $set = true;
  1100. break;
  1101. }
  1102. }
  1103. if (!$set) {
  1104. $finalArgs[] = null;
  1105. }
  1106. }
  1107. return $finalArgs;
  1108. }
  1109. protected function coerceForExpression($value) {
  1110. if ($color = $this->coerceColor($value)) {
  1111. return $color;
  1112. }
  1113. return $value;
  1114. }
  1115. protected function coerceColor($value) {
  1116. switch ($value[0]) {
  1117. case "color": return $value;
  1118. case "keyword":
  1119. $name = $value[1];
  1120. if (isset(self::$cssColors[$name])) {
  1121. list($r, $g, $b) = explode(',', self::$cssColors[$name]);
  1122. return array('color', $r, $g, $b);
  1123. }
  1124. return null;
  1125. }
  1126. return null;
  1127. }
  1128. protected function coerceString($value) {
  1129. switch ($value[0]) {
  1130. case "string":
  1131. return $value;
  1132. case "keyword":
  1133. return array("string", "", array($value[1]));
  1134. }
  1135. return null;
  1136. }
  1137. protected function assertColor($value) {
  1138. if ($color = $this->coerceColor($value)) return $color;
  1139. throw new exception("expecting color");
  1140. }
  1141. protected function assertNumber($value) {
  1142. if ($value[0] != "number")
  1143. throw new exception("expecting number");
  1144. return $value[1];
  1145. }
  1146. protected function coercePercent($value) {
  1147. if ($value[0] == "number") {
  1148. if ($value[2] == "%") {
  1149. return $value[1] / 100;
  1150. }
  1151. return $value[1];
  1152. }
  1153. return 0;
  1154. }
  1155. // make sure a color's components don't go out of bounds
  1156. protected function fixColor($c) {
  1157. foreach (range(1, 3) as $i) {
  1158. if ($c[$i] < 0) $c[$i] = 0;
  1159. if ($c[$i] > 255) $c[$i] = 255;
  1160. }
  1161. return $c;
  1162. }
  1163. function toHSL($r, $g, $b) {
  1164. $r = $r / 255;
  1165. $g = $g / 255;
  1166. $b = $b / 255;
  1167. $min = min($r, $g, $b);
  1168. $max = max($r, $g, $b);
  1169. $L = ($min + $max) / 2;
  1170. if ($min == $max) {
  1171. $S = $H = 0;
  1172. } else {
  1173. if ($L < 0.5)
  1174. $S = ($max - $min)/($max + $min);
  1175. else
  1176. $S = ($max - $min)/(2.0 - $max - $min);
  1177. if ($r == $max) $H = ($g - $b)/($max - $min);
  1178. elseif ($g == $max) $H = 2.0 + ($b - $r)/($max - $min);
  1179. elseif ($b == $max) $H = 4.0 + ($r - $g)/($max - $min);
  1180. }
  1181. return array('hsl',
  1182. ($H < 0 ? $H + 6 : $H)*60,
  1183. $S*100,
  1184. $L*100,
  1185. );
  1186. }
  1187. function toRGB_helper($comp, $temp1, $temp2) {
  1188. if ($comp < 0) $comp += 1.0;
  1189. elseif ($comp > 1) $comp -= 1.0;
  1190. if (6 * $comp < 1) return $temp1 + ($temp2 - $temp1) * 6 * $comp;
  1191. if (2 * $comp < 1) return $temp2;
  1192. if (3 * $comp < 2) return $temp1 + ($temp2 - $temp1)*((2/3) - $comp) * 6;
  1193. return $temp1;
  1194. }
  1195. // H from 0 to 360, S and L from 0 to 100
  1196. function toRGB($H, $S, $L) {
  1197. $H = $H % 360;
  1198. if ($H < 0) $H += 360;
  1199. $S = min(100, max(0, $S));
  1200. $L = min(100, max(0, $L));
  1201. $H = $H / 360;
  1202. $S = $S / 100;
  1203. $L = $L / 100;
  1204. if ($S == 0) {
  1205. $r = $g = $b = $L;
  1206. } else {
  1207. $temp2 = $L < 0.5 ?
  1208. $L*(1.0 + $S) :
  1209. $L + $S - $L * $S;
  1210. $temp1 = 2.0 * $L - $temp2;
  1211. $r = $this->toRGB_helper($H + 1/3, $temp1, $temp2);
  1212. $g = $this->toRGB_helper($H, $temp1, $temp2);
  1213. $b = $this->toRGB_helper($H - 1/3, $temp1, $temp2);
  1214. }
  1215. $out = array('color', $r*255, $g*255, $b*255);
  1216. return $out;
  1217. }
  1218. // Built in functions
  1219. protected static $lib_if = array("condition", "if-true", "if-false");
  1220. protected function lib_if($args) {
  1221. list($cond,$t, $f) = $args;
  1222. if ($cond == self::$false) return $f;
  1223. return $t;
  1224. }
  1225. protected static $lib_rgb = array("red", "green", "blue");
  1226. protected function lib_rgb($args) {
  1227. list($r,$g,$b) = $args;
  1228. return array("color", $r[1], $g[1], $b[1]);
  1229. }
  1230. protected static $lib_rgba = array(
  1231. array("red", "color"),
  1232. "green", "blue", "alpha");
  1233. protected function lib_rgba($args) {
  1234. if ($color = $this->coerceColor($args[0])) {
  1235. $num = is_null($args[1]) ? $args[3] : $args[1];
  1236. $alpha = $this->assertNumber($num);
  1237. $color[4] = $alpha;
  1238. return $color;
  1239. }
  1240. list($r,$g,$b, $a) = $args;
  1241. return array("color", $r[1], $g[1], $b[1], $a[1]);
  1242. }
  1243. // helper function for adjust_color, change_color, and scale_color
  1244. protected function alter_color($args, $fn) {
  1245. $color = $this->assertColor($args[0]);
  1246. foreach (array(1,2,3,7) as $i) {
  1247. if (!is_null($args[$i])) {
  1248. $val = $this->assertNumber($args[$i]);
  1249. $ii = $i == 7 ? 4 : $i; // alpha
  1250. $color[$ii] =
  1251. $this->$fn(isset($color[$ii]) ? $color[$ii] : 0, $val, $i);
  1252. }
  1253. }
  1254. if (!is_null($args[4]) || !is_null($args[5]) || !is_null($args[6])) {
  1255. $hsl = $this->toHSL($color[1], $color[2], $color[3]);
  1256. foreach (array(4,5,6) as $i) {
  1257. if (!is_null($args[$i])) {
  1258. $val = $this->assertNumber($args[$i]);
  1259. $hsl[$i - 3] = $this->$fn($hsl[$i - 3], $val, $i);
  1260. }
  1261. }
  1262. $rgb = $this->toRGB($hsl[1], $hsl[2], $hsl[3]);
  1263. if (isset($color[4])) $rgb[4] = $color[4];
  1264. $color = $rgb;
  1265. }
  1266. return $color;
  1267. }
  1268. protected static $lib_adjust_color = array(
  1269. "color", "red", "green", "blue",
  1270. "hue", "saturation", "lightness", "alpha"
  1271. );
  1272. protected function adjust_color_helper($base, $alter, $i) {
  1273. return $base += $alter;
  1274. }
  1275. protected function lib_adjust_color($args) {
  1276. return $this->alter_color($args, "adjust_color_helper");
  1277. }
  1278. protected static $lib_change_color = array(
  1279. "color", "red", "green", "blue",
  1280. "hue", "saturation", "lightness", "alpha"
  1281. );
  1282. protected function change_color_helper($base, $alter, $i) {
  1283. return $alter;
  1284. }
  1285. protected function lib_change_color($args) {
  1286. return $this->alter_color($args, "change_color_helper");
  1287. }
  1288. protected static $lib_scale_color = array(
  1289. "color", "red", "green", "blue",
  1290. "hue", "saturation", "lightness", "alpha"
  1291. );
  1292. protected function scale_color_helper($base, $scale, $i) {
  1293. // 1,2,3 - rgb
  1294. // 4, 5, 6 - hsl
  1295. // 7 - a
  1296. switch ($i) {
  1297. case 1:
  1298. case 2:
  1299. case 3:
  1300. $max = 255; break;
  1301. case 4:
  1302. $max = 360; break;
  1303. case 7:
  1304. $max = 1; break;
  1305. default:
  1306. $max = 100;
  1307. }
  1308. $scale = $scale / 100;
  1309. if ($scale < 0) {
  1310. return $base * $scale + $base;
  1311. } else {
  1312. return ($max - $base) * $scale + $base;
  1313. }
  1314. }
  1315. protected function lib_scale_color($args) {
  1316. return $this->alter_color($args, "scale_color_helper");
  1317. }
  1318. protected static $lib_red = array("color");
  1319. protected function lib_red($args) {
  1320. list($color) = $args;
  1321. return $color[1];
  1322. }
  1323. protected static $lib_green = array("color");
  1324. protected function lib_green($args) {
  1325. list($color) = $args;
  1326. return $color[2];
  1327. }
  1328. protected static $lib_blue = array("color");
  1329. protected function lib_blue($args) {
  1330. list($color) = $args;
  1331. return $color[3];
  1332. }
  1333. protected static $lib_alpha = array("color");
  1334. protected function lib_alpha($args) {
  1335. if ($color = $this->coerceColor($args[0])) {
  1336. return isset($color[4]) ? $color[4] : 1;
  1337. }
  1338. // this might be the IE function, so return value unchanged
  1339. return array("function", "alpha", array("list", ",", $args));
  1340. }
  1341. protected static $lib_opacity = array("color");
  1342. protected function lib_opacity($args) {
  1343. return $this->lib_alpha($args);
  1344. }
  1345. // mix two colors
  1346. protected static $lib_mix = array("color-1", "color-2", "weight");
  1347. protected function lib_mix($args) {
  1348. list($first, $second, $weight) = $args;
  1349. $first = $this->assertColor($first);
  1350. $second = $this->assertColor($second);
  1351. if (is_null($weight)) {
  1352. $weight = 0.5;
  1353. } else {
  1354. $weight = $this->coercePercent($weight);
  1355. }
  1356. $first_a = isset($first[4]) ? $first[4] : 1;
  1357. $second_a = isset($second[4]) ? $second[4] : 1;
  1358. $w = $weight * 2 - 1;
  1359. $a = $first_a - $second_a;
  1360. $w1 = (($w * $a == -1 ? $w : ($w + $a)/(1 + $w * $a)) + 1) / 2.0;
  1361. $w2 = 1.0 - $w1;
  1362. $new = array('color',
  1363. $w1 * $first[1] + $w2 * $second[1],
  1364. $w1 * $first[2] + $w2 * $second[2],
  1365. $w1 * $first[3] + $w2 * $second[3],
  1366. );
  1367. if ($first_a != 1.0 || $second_a != 1.0) {
  1368. $new[] = $first_a * $weight + $second_a * ($weight - 1);
  1369. }
  1370. return $this->fixColor($new);
  1371. }
  1372. protected static $lib_hsl = array("hue", "saturation", "lightness");
  1373. protected function lib_hsl($args) {
  1374. list($h, $s, $l) = $args;
  1375. return $this->toRGB($h[1], $s[1], $l[1]);
  1376. }
  1377. protected static $lib_hsla = array("hue", "saturation",
  1378. "lightness", "alpha");
  1379. protected function lib_hsla($args) {
  1380. list($h, $s, $l, $a) = $args;
  1381. $color = $this->toRGB($h[1], $s[1], $l[1]);
  1382. $color[4] = $a[1];
  1383. return $color;
  1384. }
  1385. protected static $lib_hue = array("color");
  1386. protected function lib_hue($args) {
  1387. $color = $this->assertColor($args[0]);
  1388. $hsl = $this->toHSL($color[1], $color[2], $color[3]);
  1389. return array("number", $hsl[1], "deg");
  1390. }
  1391. protected static $lib_saturation = array("color");
  1392. protected function lib_saturation($args) {
  1393. $color = $this->assertColor($args[0]);
  1394. $hsl = $this->toHSL($color[1], $color[2], $color[3]);
  1395. return array("number", $hsl[2], "%");
  1396. }
  1397. protected static $lib_lightness = array("color");
  1398. protected function lib_lightness($args) {
  1399. $color = $this->assertColor($args[0]);
  1400. $hsl = $this->toHSL($color[1], $color[2], $color[3]);
  1401. return array("number", $hsl[3], "%");
  1402. }
  1403. protected function adjustHsl($color, $idx, $amount) {
  1404. $hsl = $this->toHSL($color[1], $color[2], $color[3]);
  1405. $hsl[$idx] += $amount;
  1406. $out = $this->toRGB($hsl[1], $hsl[2], $hsl[3]);
  1407. if (isset($color[4])) $out[4] = $color[4];
  1408. return $out;
  1409. }
  1410. protected static $lib_adjust_hue = array("color", "degrees");
  1411. protected function lib_adjust_hue($args) {
  1412. $color = $this->assertColor($args[0]);
  1413. $degrees = $this->assertNumber($args[1]);
  1414. return $this->adjustHsl($color, 1, $degrees);
  1415. }
  1416. protected static $lib_lighten = array("color", "amount");
  1417. protected function lib_lighten($args) {
  1418. $color = $this->assertColor($args[0]);
  1419. $amount = 100*$this->coercePercent($args[1]);
  1420. return $this->adjustHsl($color, 3, $amount);
  1421. }
  1422. protected static $lib_darken = array("color", "amount");
  1423. protected function lib_darken($args) {
  1424. $color = $this->assertColor($args[0]);
  1425. $amount = 100*$this->coercePercent($args[1]);
  1426. return $this->adjustHsl($color, 3, -$amount);
  1427. }
  1428. protected static $lib_saturate = array("color", "amount");
  1429. protected function lib_saturate($args) {
  1430. $color = $this->assertColor($args[0]);
  1431. $amount = 100*$this->coercePercent($args[1]);
  1432. return $this->adjustHsl($color, 2, $amount);
  1433. }
  1434. protected static $lib_desaturate = array("color", "amount");
  1435. protected function lib_desaturate($args) {
  1436. $color = $this->assertColor($args[0]);
  1437. $amount = 100*$this->coercePercent($args[1]);
  1438. return $this->adjustHsl($color, 2, -$amount);
  1439. }
  1440. protected static $lib_grayscale = array("color");
  1441. protected function lib_grayscale($args) {
  1442. return $this->adjustHsl($this->assertColor($args[0]), 2, -100);
  1443. }
  1444. protected static $lib_complement = array("color");
  1445. protected function lib_complement($args) {
  1446. return $this->adjustHsl($this->assertColor($args[0]), 1, 180);
  1447. }
  1448. protected static $lib_invert = array("color");
  1449. protected function lib_invert($args) {
  1450. $color = $this->assertColor($args[0]);
  1451. $color[1] = 255 - $color[1];
  1452. $color[2] = 255 - $color[2];
  1453. $color[3] = 255 - $color[3];
  1454. return $color;
  1455. }
  1456. // increases opacity by amount
  1457. protected static $lib_opacify = array("color", "amount");
  1458. protected function lib_opacify($args) {
  1459. $color = $this->assertColor($args[0]);
  1460. $amount = $this->coercePercent($args[1]);
  1461. $color[4] = (isset($color[4]) ? $color[4] : 1) + $amount;
  1462. $color[4] = min(1, max(0, $color[4]));
  1463. return $color;
  1464. }
  1465. protected static $lib_fade_in = array("color", "amount");
  1466. protected function lib_fade_in($args) {
  1467. return $this->lib_opacify($args);
  1468. }
  1469. // decreases opacity by amount
  1470. protected static $lib_transparentize = array("color", "amount");
  1471. protected function lib_transparentize($args) {
  1472. $color = $this->assertColor($args[0]);
  1473. $amount = $this->coercePercent($args[1]);
  1474. $color[4] = (isset($color[4]) ? $color[4] : 1) - $amount;
  1475. $color[4] = min(1, max(0, $color[4]));
  1476. return $color;
  1477. }
  1478. protected static $lib_fade_out = array("color", "amount");
  1479. protected function lib_fade_out($args) {
  1480. return $this->lib_transparentize($args);
  1481. }
  1482. protected static $lib_unquote = array("string");
  1483. protected function lib_unquote($args) {
  1484. $str = $args[0];
  1485. if ($str[0] == "string") $str[1] = "";
  1486. return $str;
  1487. }
  1488. protected static $lib_quote = array("string");
  1489. protected function lib_quote($args) {
  1490. $value = $args[0];
  1491. if ($value[0] == "string" && !empty($value[1]))
  1492. return $value;
  1493. return array("string", '"', array($value));
  1494. }
  1495. protected static $lib_percentage = array("value");
  1496. protected function lib_percentage($args) {
  1497. return array("number",
  1498. $this->coercePercent($args[0]) * 100,
  1499. "%");
  1500. }
  1501. protected static $lib_round = array("value");
  1502. protected function lib_round($args) {
  1503. $num = $args[0];
  1504. $num[1] = round($num[1]);
  1505. return $num;
  1506. }
  1507. protected static $lib_floor = array("value");
  1508. protected function lib_floor($args) {
  1509. $num = $args[0];
  1510. $num[1] = floor($num[1]);
  1511. return $num;
  1512. }
  1513. protected static $lib_ceil = array("value");
  1514. protected function lib_ceil($args) {
  1515. $num = $args[0];
  1516. $num[1] = ceil($num[1]);
  1517. return $num;
  1518. }
  1519. protected static $lib_length = array("list");
  1520. protected function lib_length($args) {
  1521. $list = $this->coerceList($args[0]);
  1522. return count($list[2]);
  1523. }
  1524. protected static $lib_nth = array("list", "n");
  1525. protected function lib_nth($args) {
  1526. $list = $this->coerceList($args[0]);
  1527. $n = $this->assertNumber($args[1]) - 1;
  1528. return isset($list[2][$n]) ? $list[2][$n] : self::$defaultValue;
  1529. }
  1530. protected function listSeparatorForJoin($list1, $sep) {
  1531. if (is_null($sep)) return $list1[1];
  1532. switch ($this->compileValue($sep)) {
  1533. case "comma":
  1534. return ",";
  1535. case "space":
  1536. return "";
  1537. default:
  1538. return $list1[1];
  1539. }
  1540. }
  1541. protected static $lib_join = array("list1", "list2", "separator");
  1542. protected function lib_join($args) {
  1543. list($list1, $list2, $sep) = $args;
  1544. $list1 = $this->coerceList($list1, " ");
  1545. $list2 = $this->coerceList($list2, " ");
  1546. $sep = $this->listSeparatorForJoin($list1, $sep);
  1547. return array("list", $sep, array_merge($list1[2], $list2[2]));
  1548. }
  1549. protected static $lib_append = array("list", "val", "separator");
  1550. protected function lib_append($args) {
  1551. list($list1, $value, $sep) = $args;
  1552. $list1 = $this->coerceList($list1, " ");
  1553. $sep = $this->listSeparatorForJoin($list1, $sep);
  1554. return array("list", $sep, array_merge($list1[2], array($value)));
  1555. }
  1556. protected static $lib_type_of = array("value");
  1557. protected function lib_type_of($args) {
  1558. $value = $args[0];
  1559. switch ($value[0]) {
  1560. case "keyword":
  1561. if ($value == self::$true || $value == self::$false) {
  1562. return "bool";
  1563. }
  1564. if ($this->coerceColor($value)) {
  1565. return "color";
  1566. }
  1567. return "string";
  1568. default:
  1569. return $value[0];
  1570. }
  1571. }
  1572. protected static $lib_unit = array("number");
  1573. protected function lib_unit($args) {
  1574. $num = $args[0];
  1575. if ($num[0] == "number") {
  1576. return array("string", '"', array($num[2]));
  1577. }
  1578. return "";
  1579. }
  1580. protected static $lib_unitless = array("number");
  1581. protected function lib_unitless($args) {
  1582. $value = $args[0];
  1583. return $value[0] == "number" && empty($value[2]);
  1584. }
  1585. protected static $lib_comparable = array("number-1", "number-2");
  1586. protected function lib_comparable($args) {
  1587. return true; // TODO: THIS
  1588. }
  1589. static protected $cssColors = array(
  1590. 'aliceblue' => '240,248,255',
  1591. 'antiquewhite' => '250,235,215',
  1592. 'aqua' => '0,255,255',
  1593. 'aquamarine' => '127,255,212',
  1594. 'azure' => '240,255,255',
  1595. 'beige' => '245,245,220',
  1596. 'bisque' => '255,228,196',
  1597. 'black' => '0,0,0',
  1598. 'blanchedalmond' => '255,235,205',
  1599. 'blue' => '0,0,255',
  1600. 'blueviolet' => '138,43,226',
  1601. 'brown' => '165,42,42',
  1602. 'burlywood' => '222,184,135',
  1603. 'cadetblue' => '95,158,160',
  1604. 'chartreuse' => '127,255,0',
  1605. 'chocolate' => '210,105,30',
  1606. 'coral' => '255,127,80',
  1607. 'cornflowerblue' => '100,149,237',
  1608. 'cornsilk' => '255,248,220',
  1609. 'crimson' => '220,20,60',
  1610. 'cyan' => '0,255,255',
  1611. 'darkblue' => '0,0,139',
  1612. 'darkcyan' => '0,139,139',
  1613. 'darkgoldenrod' => '184,134,11',
  1614. 'darkgray' => '169,169,169',
  1615. 'darkgreen' => '0,100,0',
  1616. 'darkgrey' => '169,169,169',
  1617. 'darkkhaki' => '189,183,107',
  1618. 'darkmagenta' => '139,0,139',
  1619. 'darkolivegreen' => '85,107,47',
  1620. 'darkorange' => '255,140,0',
  1621. 'darkorchid' => '153,50,204',
  1622. 'darkred' => '139,0,0',
  1623. 'darksalmon' => '233,150,122',
  1624. 'darkseagreen' => '143,188,143',
  1625. 'darkslateblue' => '72,61,139',
  1626. 'darkslategray' => '47,79,79',
  1627. 'darkslategrey' => '47,79,79',
  1628. 'darkturquoise' => '0,206,209',
  1629. 'darkviolet' => '148,0,211',
  1630. 'deeppink' => '255,20,147',
  1631. 'deepskyblue' => '0,191,255',
  1632. 'dimgray' => '105,105,105',
  1633. 'dimgrey' => '105,105,105',
  1634. 'dodgerblue' => '30,144,255',
  1635. 'firebrick' => '178,34,34',
  1636. 'floralwhite' => '255,250,240',
  1637. 'forestgreen' => '34,139,34',
  1638. 'fuchsia' => '255,0,255',
  1639. 'gainsboro' => '220,220,220',
  1640. 'ghostwhite' => '248,248,255',
  1641. 'gold' => '255,215,0',
  1642. 'goldenrod' => '218,165,32',
  1643. 'gray' => '128,128,128',
  1644. 'green' => '0,128,0',
  1645. 'greenyellow' => '173,255,47',
  1646. 'grey' => '128,128,128',
  1647. 'honeydew' => '240,255,240',
  1648. 'hotpink' => '255,105,180',
  1649. 'indianred' => '205,92,92',
  1650. 'indigo' => '75,0,130',
  1651. 'ivory' => '255,255,240',
  1652. 'khaki' => '240,230,140',
  1653. 'lavender' => '230,230,250',
  1654. 'lavenderblush' => '255,240,245',
  1655. 'lawngreen' => '124,252,0',
  1656. 'lemonchiffon' => '255,250,205',
  1657. 'lightblue' => '173,216,230',
  1658. 'lightcoral' => '240,128,128',
  1659. 'lightcyan' => '224,255,255',
  1660. 'lightgoldenrodyellow' => '250,250,210',
  1661. 'lightgray' => '211,211,211',
  1662. 'lightgreen' => '144,238,144',
  1663. 'lightgrey' => '211,211,211',
  1664. 'lightpink' => '255,182,193',
  1665. 'lightsalmon' => '255,160,122',
  1666. 'lightseagreen' => '32,178,170',
  1667. 'lightskyblue' => '135,206,250',
  1668. 'lightslategray' => '119,136,153',
  1669. 'lightslategrey' => '119,136,153',
  1670. 'lightsteelblue' => '176,196,222',
  1671. 'lightyellow' => '255,255,224',
  1672. 'lime' => '0,255,0',
  1673. 'limegreen' => '50,205,50',
  1674. 'linen' => '250,240,230',
  1675. 'magenta' => '255,0,255',
  1676. 'maroon' => '128,0,0',
  1677. 'mediumaquamarine' => '102,205,170',
  1678. 'mediumblue' => '0,0,205',
  1679. 'mediumorchid' => '186,85,211',
  1680. 'mediumpurple' => '147,112,219',
  1681. 'mediumseagreen' => '60,179,113',
  1682. 'mediumslateblue' => '123,104,238',
  1683. 'mediumspringgreen' => '0,250,154',
  1684. 'mediumturquoise' => '72,209,20…

Large files files are truncated, but you can click here to view the full file