@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | $this->db = $db; |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
| 52 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
| 53 | 53 | /** |
| 54 | 54 | * Return list of social contributions. |
| 55 | 55 | * Use mysoc->country_id or mysoc->country_code so they must be defined. |
@@ -65,7 +65,7 @@ discard block |
||
| 65 | 65 | */ |
| 66 | 66 | public function select_type_socialcontrib($selected = '', $htmlname = 'actioncode', $useempty = 0, $maxlen = 40, $help = 1, $morecss = 'minwidth300', $noerrorifempty = 0) |
| 67 | 67 | { |
| 68 | - // phpcs:enable |
|
| 68 | + // phpcs:enable |
|
| 69 | 69 | global $conf, $db, $langs, $user, $mysoc; |
| 70 | 70 | |
| 71 | 71 | if (empty($mysoc->country_id) && empty($mysoc->country_code)) { |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | |
| 71 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
| 71 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
| 72 | 72 | /** |
| 73 | 73 | * Load array this->attributes with list of fields per object that need an alternate translation. The object and field must be managed with |
| 74 | 74 | * the widgetForTranslation() method. |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | */ |
| 82 | 82 | public function fetch_name_extralanguages($elementtype, $forceload = false) |
| 83 | 83 | { |
| 84 | - // phpcs:enable |
|
| 84 | + // phpcs:enable |
|
| 85 | 85 | global $conf; |
| 86 | 86 | |
| 87 | 87 | if (empty($elementtype)) { |
@@ -526,7 +526,7 @@ discard block |
||
| 526 | 526 | |
| 527 | 527 | |
| 528 | 528 | |
| 529 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
| 529 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
| 530 | 530 | /** |
| 531 | 531 | * Triggered when opened tag is found |
| 532 | 532 | * |
@@ -537,7 +537,7 @@ discard block |
||
| 537 | 537 | */ |
| 538 | 538 | public function feed_start_element($p, $element, $attrs) |
| 539 | 539 | { |
| 540 | - // phpcs:enable |
|
| 540 | + // phpcs:enable |
|
| 541 | 541 | $el = $element = strtolower($element); |
| 542 | 542 | $attrs = array_change_key_case($attrs, CASE_LOWER); |
| 543 | 543 | |
@@ -615,7 +615,7 @@ discard block |
||
| 615 | 615 | } |
| 616 | 616 | |
| 617 | 617 | |
| 618 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
| 618 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
| 619 | 619 | /** |
| 620 | 620 | * Triggered when CDATA is found |
| 621 | 621 | * |
@@ -625,7 +625,7 @@ discard block |
||
| 625 | 625 | */ |
| 626 | 626 | public function feed_cdata($p, $text) |
| 627 | 627 | { |
| 628 | - // phpcs:enable |
|
| 628 | + // phpcs:enable |
|
| 629 | 629 | if ($this->_format == 'atom' and $this->incontent) { |
| 630 | 630 | $this->append_content($text); |
| 631 | 631 | } else { |
@@ -634,7 +634,7 @@ discard block |
||
| 634 | 634 | } |
| 635 | 635 | } |
| 636 | 636 | |
| 637 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
| 637 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
| 638 | 638 | /** |
| 639 | 639 | * Triggered when closed tag is found |
| 640 | 640 | * |
@@ -644,7 +644,7 @@ discard block |
||
| 644 | 644 | */ |
| 645 | 645 | public function feed_end_element($p, $el) |
| 646 | 646 | { |
| 647 | - // phpcs:enable |
|
| 647 | + // phpcs:enable |
|
| 648 | 648 | $el = strtolower($el); |
| 649 | 649 | |
| 650 | 650 | if ($el == 'item' or $el == 'entry') { |
@@ -693,7 +693,7 @@ discard block |
||
| 693 | 693 | return $str1; |
| 694 | 694 | } |
| 695 | 695 | |
| 696 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
| 696 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
| 697 | 697 | /** |
| 698 | 698 | * Enter description here ... |
| 699 | 699 | * |
@@ -702,7 +702,7 @@ discard block |
||
| 702 | 702 | */ |
| 703 | 703 | public function append_content($text) |
| 704 | 704 | { |
| 705 | - // phpcs:enable |
|
| 705 | + // phpcs:enable |
|
| 706 | 706 | if (!empty($this->initem)) { |
| 707 | 707 | $this->concat($this->current_item[$this->incontent], $text); |
| 708 | 708 | } elseif (!empty($this->inchannel)) { |
@@ -59,107 +59,107 @@ |
||
| 59 | 59 | |
| 60 | 60 | class lessc_formatter_classic |
| 61 | 61 | { |
| 62 | - public $indentChar = " "; |
|
| 63 | - |
|
| 64 | - public $break = "\n"; |
|
| 65 | - public $open = " {"; |
|
| 66 | - public $close = "}"; |
|
| 67 | - public $selectorSeparator = ", "; |
|
| 68 | - public $assignSeparator = ":"; |
|
| 69 | - |
|
| 70 | - public $openSingle = " { "; |
|
| 71 | - public $closeSingle = " }"; |
|
| 72 | - |
|
| 73 | - public $disableSingle = false; |
|
| 74 | - public $breakSelectors = false; |
|
| 75 | - |
|
| 76 | - public $compressColors = false; |
|
| 77 | - public $indentLevel; |
|
| 78 | - |
|
| 79 | - public function __construct() |
|
| 80 | - { |
|
| 81 | - $this->indentLevel = 0; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - public function indentStr($n = 0) |
|
| 85 | - { |
|
| 86 | - return str_repeat($this->indentChar, max($this->indentLevel + $n, 0)); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - public function property($name, $value) |
|
| 90 | - { |
|
| 91 | - return $name.$this->assignSeparator.$value.";"; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - protected function isEmpty($block) |
|
| 95 | - { |
|
| 96 | - if (empty($block->lines)) { |
|
| 97 | - foreach ($block->children as $child) { |
|
| 98 | - if (!$this->isEmpty($child)) { |
|
| 99 | - return false; |
|
| 100 | - } |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - return true; |
|
| 104 | - } |
|
| 105 | - return false; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - public function block($block) |
|
| 109 | - { |
|
| 110 | - if ($this->isEmpty($block)) { |
|
| 111 | - return; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - $inner = $pre = $this->indentStr(); |
|
| 115 | - |
|
| 116 | - $isSingle = !$this->disableSingle && |
|
| 117 | - is_null($block->type) && count($block->lines) == 1; |
|
| 118 | - |
|
| 119 | - if (!empty($block->selectors)) { |
|
| 120 | - $this->indentLevel++; |
|
| 121 | - |
|
| 122 | - if ($this->breakSelectors) { |
|
| 123 | - $selectorSeparator = $this->selectorSeparator.$this->break.$pre; |
|
| 124 | - } else { |
|
| 125 | - $selectorSeparator = $this->selectorSeparator; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - echo $pre. |
|
| 129 | - implode($selectorSeparator, $block->selectors); |
|
| 130 | - if ($isSingle) { |
|
| 131 | - echo $this->openSingle; |
|
| 132 | - $inner = ""; |
|
| 133 | - } else { |
|
| 134 | - echo $this->open.$this->break; |
|
| 135 | - $inner = $this->indentStr(); |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - if (!empty($block->lines)) { |
|
| 140 | - $glue = $this->break.$inner; |
|
| 141 | - echo $inner.implode($glue, $block->lines); |
|
| 142 | - if (!$isSingle && !empty($block->children)) { |
|
| 143 | - echo $this->break; |
|
| 144 | - } |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - foreach ($block->children as $child) { |
|
| 148 | - $this->block($child); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - if (!empty($block->selectors)) { |
|
| 152 | - if (!$isSingle && empty($block->children)) { |
|
| 153 | - echo $this->break; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - if ($isSingle) { |
|
| 157 | - echo $this->closeSingle.$this->break; |
|
| 158 | - } else { |
|
| 159 | - echo $pre.$this->close.$this->break; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - $this->indentLevel--; |
|
| 163 | - } |
|
| 164 | - } |
|
| 62 | + public $indentChar = " "; |
|
| 63 | + |
|
| 64 | + public $break = "\n"; |
|
| 65 | + public $open = " {"; |
|
| 66 | + public $close = "}"; |
|
| 67 | + public $selectorSeparator = ", "; |
|
| 68 | + public $assignSeparator = ":"; |
|
| 69 | + |
|
| 70 | + public $openSingle = " { "; |
|
| 71 | + public $closeSingle = " }"; |
|
| 72 | + |
|
| 73 | + public $disableSingle = false; |
|
| 74 | + public $breakSelectors = false; |
|
| 75 | + |
|
| 76 | + public $compressColors = false; |
|
| 77 | + public $indentLevel; |
|
| 78 | + |
|
| 79 | + public function __construct() |
|
| 80 | + { |
|
| 81 | + $this->indentLevel = 0; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + public function indentStr($n = 0) |
|
| 85 | + { |
|
| 86 | + return str_repeat($this->indentChar, max($this->indentLevel + $n, 0)); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + public function property($name, $value) |
|
| 90 | + { |
|
| 91 | + return $name.$this->assignSeparator.$value.";"; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + protected function isEmpty($block) |
|
| 95 | + { |
|
| 96 | + if (empty($block->lines)) { |
|
| 97 | + foreach ($block->children as $child) { |
|
| 98 | + if (!$this->isEmpty($child)) { |
|
| 99 | + return false; |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + return true; |
|
| 104 | + } |
|
| 105 | + return false; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + public function block($block) |
|
| 109 | + { |
|
| 110 | + if ($this->isEmpty($block)) { |
|
| 111 | + return; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + $inner = $pre = $this->indentStr(); |
|
| 115 | + |
|
| 116 | + $isSingle = !$this->disableSingle && |
|
| 117 | + is_null($block->type) && count($block->lines) == 1; |
|
| 118 | + |
|
| 119 | + if (!empty($block->selectors)) { |
|
| 120 | + $this->indentLevel++; |
|
| 121 | + |
|
| 122 | + if ($this->breakSelectors) { |
|
| 123 | + $selectorSeparator = $this->selectorSeparator.$this->break.$pre; |
|
| 124 | + } else { |
|
| 125 | + $selectorSeparator = $this->selectorSeparator; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + echo $pre. |
|
| 129 | + implode($selectorSeparator, $block->selectors); |
|
| 130 | + if ($isSingle) { |
|
| 131 | + echo $this->openSingle; |
|
| 132 | + $inner = ""; |
|
| 133 | + } else { |
|
| 134 | + echo $this->open.$this->break; |
|
| 135 | + $inner = $this->indentStr(); |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + if (!empty($block->lines)) { |
|
| 140 | + $glue = $this->break.$inner; |
|
| 141 | + echo $inner.implode($glue, $block->lines); |
|
| 142 | + if (!$isSingle && !empty($block->children)) { |
|
| 143 | + echo $this->break; |
|
| 144 | + } |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + foreach ($block->children as $child) { |
|
| 148 | + $this->block($child); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + if (!empty($block->selectors)) { |
|
| 152 | + if (!$isSingle && empty($block->children)) { |
|
| 153 | + echo $this->break; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + if ($isSingle) { |
|
| 157 | + echo $this->closeSingle.$this->break; |
|
| 158 | + } else { |
|
| 159 | + echo $pre.$this->close.$this->break; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + $this->indentLevel--; |
|
| 163 | + } |
|
| 164 | + } |
|
| 165 | 165 | } |
@@ -222,7 +222,7 @@ discard block |
||
| 222 | 222 | } |
| 223 | 223 | |
| 224 | 224 | |
| 225 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
| 225 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
| 226 | 226 | /** |
| 227 | 227 | * Write content of a SendSms request into a dump file (mode = all) |
| 228 | 228 | * Used for debugging. |
@@ -231,7 +231,7 @@ discard block |
||
| 231 | 231 | */ |
| 232 | 232 | public function dump_sms() |
| 233 | 233 | { |
| 234 | - // phpcs:enable |
|
| 234 | + // phpcs:enable |
|
| 235 | 235 | global $conf, $dolibarr_main_data_root; |
| 236 | 236 | |
| 237 | 237 | if (@is_writable($dolibarr_main_data_root)) { // Avoid fatal error on fopen with open_basedir |
@@ -252,7 +252,7 @@ discard block |
||
| 252 | 252 | } |
| 253 | 253 | } |
| 254 | 254 | |
| 255 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
| 255 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
| 256 | 256 | /** |
| 257 | 257 | * Write content of a SendSms result into a dump file (mode = all) |
| 258 | 258 | * Used for debugging. |
@@ -262,7 +262,7 @@ discard block |
||
| 262 | 262 | */ |
| 263 | 263 | public function dump_sms_result($result) |
| 264 | 264 | { |
| 265 | - // phpcs:enable |
|
| 265 | + // phpcs:enable |
|
| 266 | 266 | global $dolibarr_main_data_root; |
| 267 | 267 | |
| 268 | 268 | if (@is_writable($dolibarr_main_data_root)) { // Avoid fatal error on fopen with open_basedir |
@@ -59,2570 +59,2570 @@ |
||
| 59 | 59 | class Lessc |
| 60 | 60 | { |
| 61 | 61 | |
| 62 | - public static $VERSION = "v0.8.0"; |
|
| 63 | - |
|
| 64 | - public static $TRUE = array("keyword", "true"); |
|
| 65 | - public static $FALSE = array("keyword", "false"); |
|
| 66 | - |
|
| 67 | - protected $libFunctions = array(); |
|
| 68 | - protected $registeredVars = array(); |
|
| 69 | - protected $preserveComments = false; |
|
| 70 | - |
|
| 71 | - public $vPrefix = '@'; // prefix of abstract properties |
|
| 72 | - public $mPrefix = '$'; // prefix of abstract blocks |
|
| 73 | - public $parentSelector = '&'; |
|
| 74 | - |
|
| 75 | - public $importDisabled = false; |
|
| 76 | - public $importDir = ''; |
|
| 77 | - |
|
| 78 | - public $scope; |
|
| 79 | - public $formatter; |
|
| 80 | - public $formatterName; |
|
| 81 | - public $parser; |
|
| 82 | - public $_parseFile; |
|
| 83 | - public $env; |
|
| 84 | - public $count; |
|
| 85 | - |
|
| 86 | - protected $numberPrecision = null; |
|
| 87 | - |
|
| 88 | - protected $allParsedFiles = array(); |
|
| 89 | - |
|
| 90 | - // set to the parser that generated the current line when compiling |
|
| 91 | - // so we know how to create error messages |
|
| 92 | - protected $sourceParser = null; |
|
| 93 | - protected $sourceLoc = null; |
|
| 94 | - |
|
| 95 | - protected static $nextImportId = 0; // uniquely identify imports |
|
| 96 | - |
|
| 97 | - // attempts to find the path of an import url, returns null for css files |
|
| 98 | - protected function findImport($url) |
|
| 99 | - { |
|
| 100 | - foreach ((array) $this->importDir as $dir) { |
|
| 101 | - $full = $dir.(substr($dir, -1) != '/' ? '/' : '').$url; |
|
| 102 | - if ($this->fileExists($file = $full.'.less') || $this->fileExists($file = $full)) { |
|
| 103 | - return $file; |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - return null; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * fileExists |
|
| 112 | - * |
|
| 113 | - * @param string $name Filename |
|
| 114 | - * @return boolean |
|
| 115 | - */ |
|
| 116 | - protected function fileExists($name) |
|
| 117 | - { |
|
| 118 | - return is_file($name); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - public static function compressList($items, $delim) |
|
| 122 | - { |
|
| 123 | - if (!isset($items[1]) && isset($items[0])) { |
|
| 124 | - return $items[0]; |
|
| 125 | - } else { |
|
| 126 | - return array('list', $delim, $items); |
|
| 127 | - } |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - public static function preg_quote($what) |
|
| 131 | - { |
|
| 132 | - return preg_quote($what, '/'); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - protected function tryImport($importPath, $parentBlock, $out) |
|
| 136 | - { |
|
| 137 | - if ($importPath[0] == "function" && $importPath[1] == "url") { |
|
| 138 | - $importPath = $this->flattenList($importPath[2]); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - $str = $this->coerceString($importPath); |
|
| 142 | - if ($str === null) { |
|
| 143 | - return false; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - $url = $this->compileValue($this->lib_e($str)); |
|
| 147 | - |
|
| 148 | - // don't import if it ends in css |
|
| 149 | - if (substr_compare($url, '.css', -4, 4) === 0) { |
|
| 150 | - return false; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - $realPath = $this->findImport($url); |
|
| 154 | - |
|
| 155 | - if ($realPath === null) { |
|
| 156 | - return false; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - if ($this->importDisabled) { |
|
| 160 | - return array(false, "/* import disabled */"); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - if (isset($this->allParsedFiles[realpath($realPath)])) { |
|
| 164 | - return array(false, null); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - $this->addParsedFile($realPath); |
|
| 168 | - $parser = $this->makeParser($realPath); |
|
| 169 | - $root = $parser->parse(file_get_contents($realPath)); |
|
| 170 | - |
|
| 171 | - // set the parents of all the block props |
|
| 172 | - foreach ($root->props as $prop) { |
|
| 173 | - if ($prop[0] == "block") { |
|
| 174 | - $prop[1]->parent = $parentBlock; |
|
| 175 | - } |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - // copy mixins into scope, set their parents |
|
| 179 | - // bring blocks from import into current block |
|
| 180 | - // TODO: need to mark the source parser these came from this file |
|
| 181 | - foreach ($root->children as $childName => $child) { |
|
| 182 | - if (isset($parentBlock->children[$childName])) { |
|
| 183 | - $parentBlock->children[$childName] = array_merge( |
|
| 184 | - $parentBlock->children[$childName], |
|
| 185 | - $child |
|
| 186 | - ); |
|
| 187 | - } else { |
|
| 188 | - $parentBlock->children[$childName] = $child; |
|
| 189 | - } |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - $pi = pathinfo($realPath); |
|
| 193 | - $dir = $pi["dirname"]; |
|
| 194 | - |
|
| 195 | - list($top, $bottom) = $this->sortProps($root->props, true); |
|
| 196 | - $this->compileImportedProps($top, $parentBlock, $out, $parser, $dir); |
|
| 197 | - |
|
| 198 | - return array(true, $bottom, $parser, $dir); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - protected function compileImportedProps($props, $block, $out, $sourceParser, $importDir) |
|
| 202 | - { |
|
| 203 | - $oldSourceParser = $this->sourceParser; |
|
| 204 | - |
|
| 205 | - $oldImport = $this->importDir; |
|
| 206 | - |
|
| 207 | - // TODO: this is because the importDir api is stupid |
|
| 208 | - $this->importDir = (array) $this->importDir; |
|
| 209 | - array_unshift($this->importDir, $importDir); |
|
| 210 | - |
|
| 211 | - foreach ($props as $prop) { |
|
| 212 | - $this->compileProp($prop, $block, $out); |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - $this->importDir = $oldImport; |
|
| 216 | - $this->sourceParser = $oldSourceParser; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * Recursively compiles a block. |
|
| 221 | - * |
|
| 222 | - * A block is analogous to a CSS block in most cases. A single LESS document |
|
| 223 | - * is encapsulated in a block when parsed, but it does not have parent tags |
|
| 224 | - * so all of it's children appear on the root level when compiled. |
|
| 225 | - * |
|
| 226 | - * Blocks are made up of props and children. |
|
| 227 | - * |
|
| 228 | - * Props are property instructions, array tuples which describe an action |
|
| 229 | - * to be taken, eg. write a property, set a variable, mixin a block. |
|
| 230 | - * |
|
| 231 | - * The children of a block are just all the blocks that are defined within. |
|
| 232 | - * This is used to look up mixins when performing a mixin. |
|
| 233 | - * |
|
| 234 | - * Compiling the block involves pushing a fresh environment on the stack, |
|
| 235 | - * and iterating through the props, compiling each one. |
|
| 236 | - * |
|
| 237 | - * See Lessc::compileProp() |
|
| 238 | - * |
|
| 239 | - */ |
|
| 240 | - protected function compileBlock($block) |
|
| 241 | - { |
|
| 242 | - switch ($block->type) { |
|
| 243 | - case "root": |
|
| 244 | - $this->compileRoot($block); |
|
| 245 | - break; |
|
| 246 | - case null: |
|
| 247 | - $this->compileCSSBlock($block); |
|
| 248 | - break; |
|
| 249 | - case "media": |
|
| 250 | - $this->compileMedia($block); |
|
| 251 | - break; |
|
| 252 | - case "directive": |
|
| 253 | - $name = "@".$block->name; |
|
| 254 | - if (!empty($block->value)) { |
|
| 255 | - $name .= " ".$this->compileValue($this->reduce($block->value)); |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - $this->compileNestedBlock($block, array($name)); |
|
| 259 | - break; |
|
| 260 | - default: |
|
| 261 | - $this->throwError("unknown block type: $block->type\n"); |
|
| 262 | - } |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - protected function compileCSSBlock($block) |
|
| 266 | - { |
|
| 267 | - $env = $this->pushEnv(); |
|
| 268 | - |
|
| 269 | - $selectors = $this->compileSelectors($block->tags); |
|
| 270 | - $env->selectors = $this->multiplySelectors($selectors); |
|
| 271 | - $out = $this->makeOutputBlock(null, $env->selectors); |
|
| 272 | - |
|
| 273 | - $this->scope->children[] = $out; |
|
| 274 | - $this->compileProps($block, $out); |
|
| 275 | - |
|
| 276 | - $block->scope = $env; // mixins carry scope with them! |
|
| 277 | - $this->popEnv(); |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - protected function compileMedia($media) |
|
| 281 | - { |
|
| 282 | - $env = $this->pushEnv($media); |
|
| 283 | - $parentScope = $this->mediaParent($this->scope); |
|
| 284 | - |
|
| 285 | - $query = $this->compileMediaQuery($this->multiplyMedia($env)); |
|
| 286 | - |
|
| 287 | - $this->scope = $this->makeOutputBlock($media->type, array($query)); |
|
| 288 | - $parentScope->children[] = $this->scope; |
|
| 289 | - |
|
| 290 | - $this->compileProps($media, $this->scope); |
|
| 291 | - |
|
| 292 | - if (count($this->scope->lines) > 0) { |
|
| 293 | - $orphanSelelectors = $this->findClosestSelectors(); |
|
| 294 | - if (!is_null($orphanSelelectors)) { |
|
| 295 | - $orphan = $this->makeOutputBlock(null, $orphanSelelectors); |
|
| 296 | - $orphan->lines = $this->scope->lines; |
|
| 297 | - array_unshift($this->scope->children, $orphan); |
|
| 298 | - $this->scope->lines = array(); |
|
| 299 | - } |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - $this->scope = $this->scope->parent; |
|
| 303 | - $this->popEnv(); |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - protected function mediaParent($scope) |
|
| 307 | - { |
|
| 308 | - while (!empty($scope->parent)) { |
|
| 309 | - if (!empty($scope->type) && $scope->type != "media") { |
|
| 310 | - break; |
|
| 311 | - } |
|
| 312 | - $scope = $scope->parent; |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - return $scope; |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - protected function compileNestedBlock($block, $selectors) |
|
| 319 | - { |
|
| 320 | - $this->pushEnv($block); |
|
| 321 | - $this->scope = $this->makeOutputBlock($block->type, $selectors); |
|
| 322 | - $this->scope->parent->children[] = $this->scope; |
|
| 323 | - |
|
| 324 | - $this->compileProps($block, $this->scope); |
|
| 325 | - |
|
| 326 | - $this->scope = $this->scope->parent; |
|
| 327 | - $this->popEnv(); |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - protected function compileRoot($root) |
|
| 331 | - { |
|
| 332 | - $this->pushEnv(); |
|
| 333 | - $this->scope = $this->makeOutputBlock($root->type); |
|
| 334 | - $this->compileProps($root, $this->scope); |
|
| 335 | - $this->popEnv(); |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - protected function compileProps($block, $out) |
|
| 339 | - { |
|
| 340 | - foreach ($this->sortProps($block->props) as $prop) { |
|
| 341 | - $this->compileProp($prop, $block, $out); |
|
| 342 | - } |
|
| 343 | - $out->lines = $this->deduplicate($out->lines); |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - /** |
|
| 347 | - * Deduplicate lines in a block. Comments are not deduplicated. If a |
|
| 348 | - * duplicate rule is detected, the comments immediately preceding each |
|
| 349 | - * occurence are consolidated. |
|
| 350 | - */ |
|
| 351 | - protected function deduplicate($lines) |
|
| 352 | - { |
|
| 353 | - $unique = array(); |
|
| 354 | - $comments = array(); |
|
| 355 | - |
|
| 356 | - foreach ($lines as $line) { |
|
| 357 | - if (strpos($line, '/*') === 0) { |
|
| 358 | - $comments[] = $line; |
|
| 359 | - continue; |
|
| 360 | - } |
|
| 361 | - if (!in_array($line, $unique)) { |
|
| 362 | - $unique[] = $line; |
|
| 363 | - } |
|
| 364 | - array_splice($unique, array_search($line, $unique), 0, $comments); |
|
| 365 | - $comments = array(); |
|
| 366 | - } |
|
| 367 | - return array_merge($unique, $comments); |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - protected function sortProps($props, $split = false) |
|
| 371 | - { |
|
| 372 | - $vars = array(); |
|
| 373 | - $imports = array(); |
|
| 374 | - $other = array(); |
|
| 375 | - $stack = array(); |
|
| 376 | - |
|
| 377 | - foreach ($props as $prop) { |
|
| 378 | - switch ($prop[0]) { |
|
| 379 | - case "comment": |
|
| 380 | - $stack[] = $prop; |
|
| 381 | - break; |
|
| 382 | - case "assign": |
|
| 383 | - $stack[] = $prop; |
|
| 384 | - if (isset($prop[1][0]) && $prop[1][0] == $this->vPrefix) { |
|
| 385 | - $vars = array_merge($vars, $stack); |
|
| 386 | - } else { |
|
| 387 | - $other = array_merge($other, $stack); |
|
| 388 | - } |
|
| 389 | - $stack = array(); |
|
| 390 | - break; |
|
| 391 | - case "import": |
|
| 392 | - $id = self::$nextImportId++; |
|
| 393 | - $prop[] = $id; |
|
| 394 | - $stack[] = $prop; |
|
| 395 | - $imports = array_merge($imports, $stack); |
|
| 396 | - $other[] = array("import_mixin", $id); |
|
| 397 | - $stack = array(); |
|
| 398 | - break; |
|
| 399 | - default: |
|
| 400 | - $stack[] = $prop; |
|
| 401 | - $other = array_merge($other, $stack); |
|
| 402 | - $stack = array(); |
|
| 403 | - break; |
|
| 404 | - } |
|
| 405 | - } |
|
| 406 | - $other = array_merge($other, $stack); |
|
| 407 | - |
|
| 408 | - if ($split) { |
|
| 409 | - return array(array_merge($imports, $vars), $other); |
|
| 410 | - } else { |
|
| 411 | - return array_merge($imports, $vars, $other); |
|
| 412 | - } |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - protected function compileMediaQuery($queries) |
|
| 416 | - { |
|
| 417 | - $compiledQueries = array(); |
|
| 418 | - foreach ($queries as $query) { |
|
| 419 | - $parts = array(); |
|
| 420 | - foreach ($query as $q) { |
|
| 421 | - switch ($q[0]) { |
|
| 422 | - case "mediaType": |
|
| 423 | - $parts[] = implode(" ", array_slice($q, 1)); |
|
| 424 | - break; |
|
| 425 | - case "mediaExp": |
|
| 426 | - if (isset($q[2])) { |
|
| 427 | - $parts[] = "($q[1]: ". |
|
| 428 | - $this->compileValue($this->reduce($q[2])).")"; |
|
| 429 | - } else { |
|
| 430 | - $parts[] = "($q[1])"; |
|
| 431 | - } |
|
| 432 | - break; |
|
| 433 | - case "variable": |
|
| 434 | - $parts[] = $this->compileValue($this->reduce($q)); |
|
| 435 | - break; |
|
| 436 | - } |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - if (count($parts) > 0) { |
|
| 440 | - $compiledQueries[] = implode(" and ", $parts); |
|
| 441 | - } |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - $out = "@media"; |
|
| 445 | - if (!empty($parts)) { |
|
| 446 | - $out .= " ". |
|
| 447 | - implode($this->formatter->selectorSeparator, $compiledQueries); |
|
| 448 | - } |
|
| 449 | - return $out; |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - protected function multiplyMedia($env, $childQueries = null) |
|
| 453 | - { |
|
| 454 | - if (is_null($env) || |
|
| 455 | - !empty($env->block->type) && $env->block->type != "media" |
|
| 456 | - ) { |
|
| 457 | - return $childQueries; |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - // plain old block, skip |
|
| 461 | - if (empty($env->block->type)) { |
|
| 462 | - return $this->multiplyMedia($env->parent, $childQueries); |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - $out = array(); |
|
| 466 | - $queries = $env->block->queries; |
|
| 467 | - if (is_null($childQueries)) { |
|
| 468 | - $out = $queries; |
|
| 469 | - } else { |
|
| 470 | - foreach ($queries as $parent) { |
|
| 471 | - foreach ($childQueries as $child) { |
|
| 472 | - $out[] = array_merge($parent, $child); |
|
| 473 | - } |
|
| 474 | - } |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - return $this->multiplyMedia($env->parent, $out); |
|
| 478 | - } |
|
| 479 | - |
|
| 480 | - protected function expandParentSelectors(&$tag, $replace) |
|
| 481 | - { |
|
| 482 | - $parts = explode("$&$", $tag); |
|
| 483 | - $count = 0; |
|
| 484 | - foreach ($parts as &$part) { |
|
| 485 | - $c = 0; |
|
| 486 | - $part = str_replace($this->parentSelector, $replace, $part, $c); |
|
| 487 | - $count += $c; |
|
| 488 | - } |
|
| 489 | - $tag = implode($this->parentSelector, $parts); |
|
| 490 | - return $count; |
|
| 491 | - } |
|
| 492 | - |
|
| 493 | - protected function findClosestSelectors() |
|
| 494 | - { |
|
| 495 | - $env = $this->env; |
|
| 496 | - $selectors = null; |
|
| 497 | - while ($env !== null) { |
|
| 498 | - if (isset($env->selectors)) { |
|
| 499 | - $selectors = $env->selectors; |
|
| 500 | - break; |
|
| 501 | - } |
|
| 502 | - $env = $env->parent; |
|
| 503 | - } |
|
| 504 | - |
|
| 505 | - return $selectors; |
|
| 506 | - } |
|
| 507 | - |
|
| 508 | - |
|
| 509 | - // multiply $selectors against the nearest selectors in env |
|
| 510 | - protected function multiplySelectors($selectors) |
|
| 511 | - { |
|
| 512 | - // find parent selectors |
|
| 513 | - |
|
| 514 | - $parentSelectors = $this->findClosestSelectors(); |
|
| 515 | - if (is_null($parentSelectors)) { |
|
| 516 | - // kill parent reference in top level selector |
|
| 517 | - foreach ($selectors as &$s) { |
|
| 518 | - $this->expandParentSelectors($s, ""); |
|
| 519 | - } |
|
| 520 | - |
|
| 521 | - return $selectors; |
|
| 522 | - } |
|
| 523 | - |
|
| 524 | - $out = array(); |
|
| 525 | - foreach ($parentSelectors as $parent) { |
|
| 526 | - foreach ($selectors as $child) { |
|
| 527 | - $count = $this->expandParentSelectors($child, $parent); |
|
| 528 | - |
|
| 529 | - // don't prepend the parent tag if & was used |
|
| 530 | - if ($count > 0) { |
|
| 531 | - $out[] = trim($child); |
|
| 532 | - } else { |
|
| 533 | - $out[] = trim($parent.' '.$child); |
|
| 534 | - } |
|
| 535 | - } |
|
| 536 | - } |
|
| 537 | - |
|
| 538 | - return $out; |
|
| 539 | - } |
|
| 540 | - |
|
| 541 | - // reduces selector expressions |
|
| 542 | - protected function compileSelectors($selectors) |
|
| 543 | - { |
|
| 544 | - $out = array(); |
|
| 545 | - |
|
| 546 | - foreach ($selectors as $s) { |
|
| 547 | - if (is_array($s)) { |
|
| 548 | - list(, $value) = $s; |
|
| 549 | - $out[] = trim($this->compileValue($this->reduce($value))); |
|
| 550 | - } else { |
|
| 551 | - $out[] = $s; |
|
| 552 | - } |
|
| 553 | - } |
|
| 554 | - |
|
| 555 | - return $out; |
|
| 556 | - } |
|
| 557 | - |
|
| 558 | - protected function eq($left, $right) |
|
| 559 | - { |
|
| 560 | - return $left == $right; |
|
| 561 | - } |
|
| 562 | - |
|
| 563 | - protected function patternMatch($block, $orderedArgs, $keywordArgs) |
|
| 564 | - { |
|
| 565 | - // match the guards if it has them |
|
| 566 | - // any one of the groups must have all its guards pass for a match |
|
| 567 | - if (!empty($block->guards)) { |
|
| 568 | - $groupPassed = false; |
|
| 569 | - foreach ($block->guards as $guardGroup) { |
|
| 570 | - foreach ($guardGroup as $guard) { |
|
| 571 | - $this->pushEnv(); |
|
| 572 | - $this->zipSetArgs($block->args, $orderedArgs, $keywordArgs); |
|
| 573 | - |
|
| 574 | - $negate = false; |
|
| 575 | - if ($guard[0] == "negate") { |
|
| 576 | - $guard = $guard[1]; |
|
| 577 | - $negate = true; |
|
| 578 | - } |
|
| 579 | - |
|
| 580 | - $passed = $this->reduce($guard) == self::$TRUE; |
|
| 581 | - if ($negate) { |
|
| 582 | - $passed = !$passed; |
|
| 583 | - } |
|
| 584 | - |
|
| 585 | - $this->popEnv(); |
|
| 586 | - |
|
| 587 | - if ($passed) { |
|
| 588 | - $groupPassed = true; |
|
| 589 | - } else { |
|
| 590 | - $groupPassed = false; |
|
| 591 | - break; |
|
| 592 | - } |
|
| 593 | - } |
|
| 594 | - |
|
| 595 | - if ($groupPassed) { |
|
| 596 | - break; |
|
| 597 | - } |
|
| 598 | - } |
|
| 599 | - |
|
| 600 | - if (!$groupPassed) { |
|
| 601 | - return false; |
|
| 602 | - } |
|
| 603 | - } |
|
| 604 | - |
|
| 605 | - if (empty($block->args)) { |
|
| 606 | - return $block->isVararg || empty($orderedArgs) && empty($keywordArgs); |
|
| 607 | - } |
|
| 608 | - |
|
| 609 | - $remainingArgs = $block->args; |
|
| 610 | - if ($keywordArgs) { |
|
| 611 | - $remainingArgs = array(); |
|
| 612 | - foreach ($block->args as $arg) { |
|
| 613 | - if ($arg[0] == "arg" && isset($keywordArgs[$arg[1]])) { |
|
| 614 | - continue; |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - $remainingArgs[] = $arg; |
|
| 618 | - } |
|
| 619 | - } |
|
| 620 | - |
|
| 621 | - $i = -1; // no args |
|
| 622 | - // try to match by arity or by argument literal |
|
| 623 | - foreach ($remainingArgs as $i => $arg) { |
|
| 624 | - switch ($arg[0]) { |
|
| 625 | - case "lit": |
|
| 626 | - if (empty($orderedArgs[$i]) || !$this->eq($arg[1], $orderedArgs[$i])) { |
|
| 627 | - return false; |
|
| 628 | - } |
|
| 629 | - break; |
|
| 630 | - case "arg": |
|
| 631 | - // no arg and no default value |
|
| 632 | - if (!isset($orderedArgs[$i]) && !isset($arg[2])) { |
|
| 633 | - return false; |
|
| 634 | - } |
|
| 635 | - break; |
|
| 636 | - case "rest": |
|
| 637 | - $i--; // rest can be empty |
|
| 638 | - break 2; |
|
| 639 | - } |
|
| 640 | - } |
|
| 641 | - |
|
| 642 | - if ($block->isVararg) { |
|
| 643 | - return true; // not having enough is handled above |
|
| 644 | - } else { |
|
| 645 | - $numMatched = $i + 1; |
|
| 646 | - // greater than because default values always match |
|
| 647 | - return $numMatched >= count($orderedArgs); |
|
| 648 | - } |
|
| 649 | - } |
|
| 650 | - |
|
| 651 | - protected function patternMatchAll($blocks, $orderedArgs, $keywordArgs, $skip = array()) |
|
| 652 | - { |
|
| 653 | - $matches = null; |
|
| 654 | - foreach ($blocks as $block) { |
|
| 655 | - // skip seen blocks that don't have arguments |
|
| 656 | - if (isset($skip[$block->id]) && !isset($block->args)) { |
|
| 657 | - continue; |
|
| 658 | - } |
|
| 659 | - |
|
| 660 | - if ($this->patternMatch($block, $orderedArgs, $keywordArgs)) { |
|
| 661 | - $matches[] = $block; |
|
| 662 | - } |
|
| 663 | - } |
|
| 664 | - |
|
| 665 | - return $matches; |
|
| 666 | - } |
|
| 667 | - |
|
| 668 | - // attempt to find blocks matched by path and args |
|
| 669 | - protected function findBlocks($searchIn, $path, $orderedArgs, $keywordArgs, $seen = array()) |
|
| 670 | - { |
|
| 671 | - if ($searchIn == null) { |
|
| 672 | - return null; |
|
| 673 | - } |
|
| 674 | - if (isset($seen[$searchIn->id])) { |
|
| 675 | - return null; |
|
| 676 | - } |
|
| 677 | - $seen[$searchIn->id] = true; |
|
| 678 | - |
|
| 679 | - $name = $path[0]; |
|
| 680 | - |
|
| 681 | - if (isset($searchIn->children[$name])) { |
|
| 682 | - $blocks = $searchIn->children[$name]; |
|
| 683 | - if (count($path) == 1) { |
|
| 684 | - $matches = $this->patternMatchAll($blocks, $orderedArgs, $keywordArgs, $seen); |
|
| 685 | - if (!empty($matches)) { |
|
| 686 | - // This will return all blocks that match in the closest |
|
| 687 | - // scope that has any matching block, like lessjs |
|
| 688 | - return $matches; |
|
| 689 | - } |
|
| 690 | - } else { |
|
| 691 | - $matches = array(); |
|
| 692 | - foreach ($blocks as $subBlock) { |
|
| 693 | - $subMatches = $this->findBlocks( |
|
| 694 | - $subBlock, |
|
| 695 | - array_slice($path, 1), |
|
| 696 | - $orderedArgs, |
|
| 697 | - $keywordArgs, |
|
| 698 | - $seen |
|
| 699 | - ); |
|
| 700 | - |
|
| 701 | - if (!is_null($subMatches)) { |
|
| 702 | - foreach ($subMatches as $sm) { |
|
| 703 | - $matches[] = $sm; |
|
| 704 | - } |
|
| 705 | - } |
|
| 706 | - } |
|
| 707 | - |
|
| 708 | - return count($matches) > 0 ? $matches : null; |
|
| 709 | - } |
|
| 710 | - } |
|
| 711 | - if ($searchIn->parent === $searchIn) { |
|
| 712 | - return null; |
|
| 713 | - } |
|
| 714 | - return $this->findBlocks($searchIn->parent, $path, $orderedArgs, $keywordArgs, $seen); |
|
| 715 | - } |
|
| 716 | - |
|
| 717 | - // sets all argument names in $args to either the default value |
|
| 718 | - // or the one passed in through $values |
|
| 719 | - protected function zipSetArgs($args, $orderedValues, $keywordValues) |
|
| 720 | - { |
|
| 721 | - $assignedValues = array(); |
|
| 722 | - |
|
| 723 | - $i = 0; |
|
| 724 | - foreach ($args as $a) { |
|
| 725 | - if ($a[0] == "arg") { |
|
| 726 | - if (isset($keywordValues[$a[1]])) { |
|
| 727 | - // has keyword arg |
|
| 728 | - $value = $keywordValues[$a[1]]; |
|
| 729 | - } elseif (isset($orderedValues[$i])) { |
|
| 730 | - // has ordered arg |
|
| 731 | - $value = $orderedValues[$i]; |
|
| 732 | - $i++; |
|
| 733 | - } elseif (isset($a[2])) { |
|
| 734 | - // has default value |
|
| 735 | - $value = $a[2]; |
|
| 736 | - } else { |
|
| 737 | - $value = null; // :( |
|
| 738 | - $this->throwError("Failed to assign arg ".$a[1]); // This ends function by throwing an exception |
|
| 739 | - } |
|
| 740 | - |
|
| 741 | - $value = $this->reduce($value); |
|
| 742 | - $this->set($a[1], $value); |
|
| 743 | - $assignedValues[] = $value; |
|
| 744 | - } else { |
|
| 745 | - // a lit |
|
| 746 | - $i++; |
|
| 747 | - } |
|
| 748 | - } |
|
| 749 | - |
|
| 750 | - // check for a rest |
|
| 751 | - $last = end($args); |
|
| 752 | - if ($last && $last[0] == "rest") { |
|
| 753 | - $rest = array_slice($orderedValues, count($args) - 1); |
|
| 754 | - $this->set($last[1], $this->reduce(array("list", " ", $rest))); |
|
| 755 | - } |
|
| 756 | - |
|
| 757 | - // wow is this the only true use of PHP's + operator for arrays? |
|
| 758 | - $this->env->arguments = $assignedValues + $orderedValues; |
|
| 759 | - } |
|
| 760 | - |
|
| 761 | - // compile a prop and update $lines or $blocks appropriately |
|
| 762 | - protected function compileProp($prop, $block, $out) |
|
| 763 | - { |
|
| 764 | - // set error position context |
|
| 765 | - $this->sourceLoc = isset($prop[-1]) ? $prop[-1] : -1; |
|
| 766 | - |
|
| 767 | - switch ($prop[0]) { |
|
| 768 | - case 'assign': |
|
| 769 | - list(, $name, $value) = $prop; |
|
| 770 | - if ($name[0] == $this->vPrefix) { |
|
| 771 | - $this->set($name, $value); |
|
| 772 | - } else { |
|
| 773 | - $out->lines[] = $this->formatter->property( |
|
| 774 | - $name, |
|
| 775 | - $this->compileValue($this->reduce($value)) |
|
| 776 | - ); |
|
| 777 | - } |
|
| 778 | - break; |
|
| 779 | - case 'block': |
|
| 780 | - list(, $child) = $prop; |
|
| 781 | - $this->compileBlock($child); |
|
| 782 | - break; |
|
| 783 | - case 'mixin': |
|
| 784 | - list(, $path, $args, $suffix) = $prop; |
|
| 785 | - |
|
| 786 | - $orderedArgs = array(); |
|
| 787 | - $keywordArgs = array(); |
|
| 788 | - foreach ((array) $args as $arg) { |
|
| 789 | - $argval = null; |
|
| 790 | - switch ($arg[0]) { |
|
| 791 | - case "arg": |
|
| 792 | - if (!isset($arg[2])) { |
|
| 793 | - $orderedArgs[] = $this->reduce(array("variable", $arg[1])); |
|
| 794 | - } else { |
|
| 795 | - $keywordArgs[$arg[1]] = $this->reduce($arg[2]); |
|
| 796 | - } |
|
| 797 | - break; |
|
| 798 | - |
|
| 799 | - case "lit": |
|
| 800 | - $orderedArgs[] = $this->reduce($arg[1]); |
|
| 801 | - break; |
|
| 802 | - default: |
|
| 803 | - $this->throwError("Unknown arg type: ".$arg[0]); |
|
| 804 | - } |
|
| 805 | - } |
|
| 806 | - |
|
| 807 | - $mixins = $this->findBlocks($block, $path, $orderedArgs, $keywordArgs); |
|
| 808 | - |
|
| 809 | - if ($mixins === null) { |
|
| 810 | - $this->throwError("{$prop[1][0]} is undefined"); |
|
| 811 | - } |
|
| 812 | - |
|
| 813 | - foreach ($mixins as $mixin) { |
|
| 814 | - if ($mixin === $block && !$orderedArgs) { |
|
| 815 | - continue; |
|
| 816 | - } |
|
| 817 | - |
|
| 818 | - $haveScope = false; |
|
| 819 | - if (isset($mixin->parent->scope)) { |
|
| 820 | - $haveScope = true; |
|
| 821 | - $mixinParentEnv = $this->pushEnv(); |
|
| 822 | - $mixinParentEnv->storeParent = $mixin->parent->scope; |
|
| 823 | - } |
|
| 824 | - |
|
| 825 | - $haveArgs = false; |
|
| 826 | - if (isset($mixin->args)) { |
|
| 827 | - $haveArgs = true; |
|
| 828 | - $this->pushEnv(); |
|
| 829 | - $this->zipSetArgs($mixin->args, $orderedArgs, $keywordArgs); |
|
| 830 | - } |
|
| 831 | - |
|
| 832 | - $oldParent = $mixin->parent; |
|
| 833 | - if ($mixin != $block) { |
|
| 834 | - $mixin->parent = $block; |
|
| 835 | - } |
|
| 836 | - |
|
| 837 | - foreach ($this->sortProps($mixin->props) as $subProp) { |
|
| 838 | - if ($suffix !== null && |
|
| 839 | - $subProp[0] == "assign" && |
|
| 840 | - is_string($subProp[1]) && |
|
| 841 | - $subProp[1][0] != $this->vPrefix |
|
| 842 | - ) { |
|
| 843 | - $subProp[2] = array( |
|
| 844 | - 'list', ' ', |
|
| 845 | - array($subProp[2], array('keyword', $suffix)) |
|
| 846 | - ); |
|
| 847 | - } |
|
| 848 | - |
|
| 849 | - $this->compileProp($subProp, $mixin, $out); |
|
| 850 | - } |
|
| 851 | - |
|
| 852 | - $mixin->parent = $oldParent; |
|
| 853 | - |
|
| 854 | - if ($haveArgs) { |
|
| 855 | - $this->popEnv(); |
|
| 856 | - } |
|
| 857 | - if ($haveScope) { |
|
| 858 | - $this->popEnv(); |
|
| 859 | - } |
|
| 860 | - } |
|
| 861 | - |
|
| 862 | - break; |
|
| 863 | - case 'raw': |
|
| 864 | - $out->lines[] = $prop[1]; |
|
| 865 | - break; |
|
| 866 | - case "directive": |
|
| 867 | - list(, $name, $value) = $prop; |
|
| 868 | - $out->lines[] = "@$name ".$this->compileValue($this->reduce($value)).';'; |
|
| 869 | - break; |
|
| 870 | - case "comment": |
|
| 871 | - $out->lines[] = $prop[1]; |
|
| 872 | - break; |
|
| 873 | - case "import": |
|
| 874 | - list(, $importPath, $importId) = $prop; |
|
| 875 | - $importPath = $this->reduce($importPath); |
|
| 876 | - |
|
| 877 | - if (!isset($this->env->imports)) { |
|
| 878 | - $this->env->imports = array(); |
|
| 879 | - } |
|
| 880 | - |
|
| 881 | - $result = $this->tryImport($importPath, $block, $out); |
|
| 882 | - |
|
| 883 | - $this->env->imports[$importId] = $result === false ? |
|
| 884 | - array(false, "@import ".$this->compileValue($importPath).";") : $result; |
|
| 885 | - |
|
| 886 | - break; |
|
| 887 | - case "import_mixin": |
|
| 888 | - list(, $importId) = $prop; |
|
| 889 | - $import = $this->env->imports[$importId]; |
|
| 890 | - if ($import[0] === false) { |
|
| 891 | - if (isset($import[1])) { |
|
| 892 | - $out->lines[] = $import[1]; |
|
| 893 | - } |
|
| 894 | - } else { |
|
| 895 | - list(, $bottom, $parser, $importDir) = $import; |
|
| 896 | - $this->compileImportedProps($bottom, $block, $out, $parser, $importDir); |
|
| 897 | - } |
|
| 898 | - |
|
| 899 | - break; |
|
| 900 | - default: |
|
| 901 | - $this->throwError("unknown op: {$prop[0]}\n"); |
|
| 902 | - } |
|
| 903 | - } |
|
| 904 | - |
|
| 905 | - |
|
| 906 | - /** |
|
| 907 | - * Compiles a primitive value into a CSS property value. |
|
| 908 | - * |
|
| 909 | - * Values in lessphp are typed by being wrapped in arrays, their format is |
|
| 910 | - * typically: |
|
| 911 | - * |
|
| 912 | - * array(type, contents [, additional_contents]*) |
|
| 913 | - * |
|
| 914 | - * The input is expected to be reduced. This function will not work on |
|
| 915 | - * things like expressions and variables. |
|
| 916 | - */ |
|
| 917 | - public function compileValue($value) |
|
| 918 | - { |
|
| 919 | - switch ($value[0]) { |
|
| 920 | - case 'list': |
|
| 921 | - // [1] - delimiter |
|
| 922 | - // [2] - array of values |
|
| 923 | - return implode($value[1], array_map(array($this, 'compileValue'), $value[2])); |
|
| 924 | - case 'raw_color': |
|
| 925 | - if (!empty($this->formatter->compressColors)) { |
|
| 926 | - return $this->compileValue($this->coerceColor($value)); |
|
| 927 | - } |
|
| 928 | - return $value[1]; |
|
| 929 | - case 'keyword': |
|
| 930 | - // [1] - the keyword |
|
| 931 | - return $value[1]; |
|
| 932 | - case 'number': |
|
| 933 | - list(, $num, $unit) = $value; |
|
| 934 | - // [1] - the number |
|
| 935 | - // [2] - the unit |
|
| 936 | - if ($this->numberPrecision !== null) { |
|
| 937 | - $num = round($num, $this->numberPrecision); |
|
| 938 | - } |
|
| 939 | - return $num.$unit; |
|
| 940 | - case 'string': |
|
| 941 | - // [1] - contents of string (includes quotes) |
|
| 942 | - list(, $delim, $content) = $value; |
|
| 943 | - foreach ($content as &$part) { |
|
| 944 | - if (is_array($part)) { |
|
| 945 | - $part = $this->compileValue($part); |
|
| 946 | - } |
|
| 947 | - } |
|
| 948 | - return $delim.implode($content).$delim; |
|
| 949 | - case 'color': |
|
| 950 | - // [1] - red component (either number or a %) |
|
| 951 | - // [2] - green component |
|
| 952 | - // [3] - blue component |
|
| 953 | - // [4] - optional alpha component |
|
| 954 | - list(, $r, $g, $b) = $value; |
|
| 955 | - $r = round($r); |
|
| 956 | - $g = round($g); |
|
| 957 | - $b = round($b); |
|
| 958 | - |
|
| 959 | - if (count($value) == 5 && $value[4] != 1) { // rgba |
|
| 960 | - return 'rgba('.$r.','.$g.','.$b.','.$value[4].')'; |
|
| 961 | - } |
|
| 962 | - |
|
| 963 | - $h = sprintf("#%02x%02x%02x", $r, $g, $b); |
|
| 964 | - |
|
| 965 | - if (!empty($this->formatter->compressColors)) { |
|
| 966 | - // Converting hex color to short notation (e.g. #003399 to #039) |
|
| 967 | - if ($h[1] === $h[2] && $h[3] === $h[4] && $h[5] === $h[6]) { |
|
| 968 | - $h = '#'.$h[1].$h[3].$h[5]; |
|
| 969 | - } |
|
| 970 | - } |
|
| 971 | - |
|
| 972 | - return $h; |
|
| 973 | - |
|
| 974 | - case 'function': |
|
| 975 | - list(, $name, $args) = $value; |
|
| 976 | - return $name.'('.$this->compileValue($args).')'; |
|
| 977 | - default: // assumed to be unit |
|
| 978 | - $this->throwError("unknown value type: $value[0]"); |
|
| 979 | - } |
|
| 980 | - } |
|
| 981 | - |
|
| 982 | - protected function lib_pow($args) |
|
| 983 | - { |
|
| 984 | - list($base, $exp) = $this->assertArgs($args, 2, "pow"); |
|
| 985 | - return pow($this->assertNumber($base), $this->assertNumber($exp)); |
|
| 986 | - } |
|
| 987 | - |
|
| 988 | - protected function lib_pi() |
|
| 989 | - { |
|
| 990 | - return pi(); |
|
| 991 | - } |
|
| 992 | - |
|
| 993 | - protected function lib_mod($args) |
|
| 994 | - { |
|
| 995 | - list($a, $b) = $this->assertArgs($args, 2, "mod"); |
|
| 996 | - return $this->assertNumber($a) % $this->assertNumber($b); |
|
| 997 | - } |
|
| 998 | - |
|
| 999 | - protected function lib_tan($num) |
|
| 1000 | - { |
|
| 1001 | - return tan($this->assertNumber($num)); |
|
| 1002 | - } |
|
| 1003 | - |
|
| 1004 | - protected function lib_sin($num) |
|
| 1005 | - { |
|
| 1006 | - return sin($this->assertNumber($num)); |
|
| 1007 | - } |
|
| 1008 | - |
|
| 1009 | - protected function lib_cos($num) |
|
| 1010 | - { |
|
| 1011 | - return cos($this->assertNumber($num)); |
|
| 1012 | - } |
|
| 1013 | - |
|
| 1014 | - protected function lib_atan($num) |
|
| 1015 | - { |
|
| 1016 | - $num = atan($this->assertNumber($num)); |
|
| 1017 | - return array("number", $num, "rad"); |
|
| 1018 | - } |
|
| 1019 | - |
|
| 1020 | - protected function lib_asin($num) |
|
| 1021 | - { |
|
| 1022 | - $num = asin($this->assertNumber($num)); |
|
| 1023 | - return array("number", $num, "rad"); |
|
| 1024 | - } |
|
| 1025 | - |
|
| 1026 | - protected function lib_acos($num) |
|
| 1027 | - { |
|
| 1028 | - $num = acos($this->assertNumber($num)); |
|
| 1029 | - return array("number", $num, "rad"); |
|
| 1030 | - } |
|
| 1031 | - |
|
| 1032 | - protected function lib_sqrt($num) |
|
| 1033 | - { |
|
| 1034 | - return sqrt($this->assertNumber($num)); |
|
| 1035 | - } |
|
| 1036 | - |
|
| 1037 | - protected function lib_extract($value) |
|
| 1038 | - { |
|
| 1039 | - list($list, $idx) = $this->assertArgs($value, 2, "extract"); |
|
| 1040 | - $idx = $this->assertNumber($idx); |
|
| 1041 | - // 1 indexed |
|
| 1042 | - if ($list[0] == "list" && isset($list[2][$idx - 1])) { |
|
| 1043 | - return $list[2][$idx - 1]; |
|
| 1044 | - } |
|
| 1045 | - return ''; |
|
| 1046 | - } |
|
| 1047 | - |
|
| 1048 | - protected function lib_isnumber($value) |
|
| 1049 | - { |
|
| 1050 | - return $this->toBool($value[0] == "number"); |
|
| 1051 | - } |
|
| 1052 | - |
|
| 1053 | - protected function lib_isstring($value) |
|
| 1054 | - { |
|
| 1055 | - return $this->toBool($value[0] == "string"); |
|
| 1056 | - } |
|
| 1057 | - |
|
| 1058 | - protected function lib_iscolor($value) |
|
| 1059 | - { |
|
| 1060 | - return $this->toBool($this->coerceColor($value)); |
|
| 1061 | - } |
|
| 1062 | - |
|
| 1063 | - protected function lib_iskeyword($value) |
|
| 1064 | - { |
|
| 1065 | - return $this->toBool($value[0] == "keyword"); |
|
| 1066 | - } |
|
| 1067 | - |
|
| 1068 | - protected function lib_ispixel($value) |
|
| 1069 | - { |
|
| 1070 | - return $this->toBool($value[0] == "number" && $value[2] == "px"); |
|
| 1071 | - } |
|
| 1072 | - |
|
| 1073 | - protected function lib_ispercentage($value) |
|
| 1074 | - { |
|
| 1075 | - return $this->toBool($value[0] == "number" && $value[2] == "%"); |
|
| 1076 | - } |
|
| 1077 | - |
|
| 1078 | - protected function lib_isem($value) |
|
| 1079 | - { |
|
| 1080 | - return $this->toBool($value[0] == "number" && $value[2] == "em"); |
|
| 1081 | - } |
|
| 1082 | - |
|
| 1083 | - protected function lib_isrem($value) |
|
| 1084 | - { |
|
| 1085 | - return $this->toBool($value[0] == "number" && $value[2] == "rem"); |
|
| 1086 | - } |
|
| 1087 | - |
|
| 1088 | - protected function lib_rgbahex($color) |
|
| 1089 | - { |
|
| 1090 | - $color = $this->coerceColor($color); |
|
| 1091 | - if (is_null($color)) { |
|
| 1092 | - $this->throwError("color expected for rgbahex"); |
|
| 1093 | - } |
|
| 1094 | - |
|
| 1095 | - return sprintf( |
|
| 1096 | - "#%02x%02x%02x%02x", |
|
| 1097 | - isset($color[4]) ? $color[4] * 255 : 255, |
|
| 1098 | - $color[1], |
|
| 1099 | - $color[2], |
|
| 1100 | - $color[3] |
|
| 1101 | - ); |
|
| 1102 | - } |
|
| 1103 | - |
|
| 1104 | - protected function lib_argb($color) |
|
| 1105 | - { |
|
| 1106 | - return $this->lib_rgbahex($color); |
|
| 1107 | - } |
|
| 1108 | - |
|
| 1109 | - /** |
|
| 1110 | - * Given an url, decide whether to output a regular link or the base64-encoded contents of the file |
|
| 1111 | - * |
|
| 1112 | - * @param array $value either an argument list (two strings) or a single string |
|
| 1113 | - * @return string formatted url(), either as a link or base64-encoded |
|
| 1114 | - */ |
|
| 1115 | - protected function lib_data_uri($value) |
|
| 1116 | - { |
|
| 1117 | - $mime = ($value[0] === 'list') ? $value[2][0][2] : null; |
|
| 1118 | - $url = ($value[0] === 'list') ? $value[2][1][2][0] : $value[2][0]; |
|
| 1119 | - |
|
| 1120 | - $fullpath = $this->findImport($url); |
|
| 1121 | - |
|
| 1122 | - if ($fullpath && ($fsize = filesize($fullpath)) !== false) { |
|
| 1123 | - // IE8 can't handle data uris larger than 32KB |
|
| 1124 | - if ($fsize / 1024 < 32) { |
|
| 1125 | - if (is_null($mime)) { |
|
| 1126 | - if (class_exists('finfo')) { // php 5.3+ |
|
| 1127 | - $finfo = new finfo(FILEINFO_MIME); |
|
| 1128 | - $mime = explode('; ', $finfo->file($fullpath)); |
|
| 1129 | - $mime = $mime[0]; |
|
| 1130 | - } elseif (function_exists('mime_content_type')) { // PHP 5.2 |
|
| 1131 | - $mime = mime_content_type($fullpath); |
|
| 1132 | - } |
|
| 1133 | - } |
|
| 1134 | - |
|
| 1135 | - if (!is_null($mime)) { // fallback if the mime type is still unknown |
|
| 1136 | - $url = sprintf('data:%s;base64,%s', $mime, base64_encode(file_get_contents($fullpath))); |
|
| 1137 | - } |
|
| 1138 | - } |
|
| 1139 | - } |
|
| 1140 | - |
|
| 1141 | - return 'url("'.$url.'")'; |
|
| 1142 | - } |
|
| 1143 | - |
|
| 1144 | - // utility func to unquote a string |
|
| 1145 | - protected function lib_e($arg) |
|
| 1146 | - { |
|
| 1147 | - switch ($arg[0]) { |
|
| 1148 | - case "list": |
|
| 1149 | - $items = $arg[2]; |
|
| 1150 | - if (isset($items[0])) { |
|
| 1151 | - return $this->lib_e($items[0]); |
|
| 1152 | - } |
|
| 1153 | - $this->throwError("unrecognised input"); // This ends function by throwing an exception |
|
| 1154 | - // no break |
|
| 1155 | - case "string": |
|
| 1156 | - $arg[1] = ""; |
|
| 1157 | - return $arg; |
|
| 1158 | - case "keyword": |
|
| 1159 | - return $arg; |
|
| 1160 | - default: |
|
| 1161 | - return array("keyword", $this->compileValue($arg)); |
|
| 1162 | - } |
|
| 1163 | - } |
|
| 1164 | - |
|
| 1165 | - protected function lib__sprintf($args) |
|
| 1166 | - { |
|
| 1167 | - if ($args[0] != "list") { |
|
| 1168 | - return $args; |
|
| 1169 | - } |
|
| 1170 | - $values = $args[2]; |
|
| 1171 | - $string = array_shift($values); |
|
| 1172 | - $template = $this->compileValue($this->lib_e($string)); |
|
| 1173 | - |
|
| 1174 | - $i = 0; |
|
| 1175 | - $m = array(); |
|
| 1176 | - if (preg_match_all('/%[dsa]/', $template, $m)) { |
|
| 1177 | - foreach ($m[0] as $match) { |
|
| 1178 | - $val = isset($values[$i]) ? |
|
| 1179 | - $this->reduce($values[$i]) : array('keyword', ''); |
|
| 1180 | - |
|
| 1181 | - // lessjs compat, renders fully expanded color, not raw color |
|
| 1182 | - if ($color = $this->coerceColor($val)) { |
|
| 1183 | - $val = $color; |
|
| 1184 | - } |
|
| 1185 | - |
|
| 1186 | - $i++; |
|
| 1187 | - $rep = $this->compileValue($this->lib_e($val)); |
|
| 1188 | - $template = preg_replace( |
|
| 1189 | - '/'.self::preg_quote($match).'/', |
|
| 1190 | - $rep, |
|
| 1191 | - $template, |
|
| 1192 | - 1 |
|
| 1193 | - ); |
|
| 1194 | - } |
|
| 1195 | - } |
|
| 1196 | - |
|
| 1197 | - $d = $string[0] == "string" ? $string[1] : '"'; |
|
| 1198 | - return array("string", $d, array($template)); |
|
| 1199 | - } |
|
| 1200 | - |
|
| 1201 | - protected function lib_floor($arg) |
|
| 1202 | - { |
|
| 1203 | - $value = $this->assertNumber($arg); |
|
| 1204 | - return array("number", floor($value), $arg[2]); |
|
| 1205 | - } |
|
| 1206 | - |
|
| 1207 | - protected function lib_ceil($arg) |
|
| 1208 | - { |
|
| 1209 | - $value = $this->assertNumber($arg); |
|
| 1210 | - return array("number", ceil($value), $arg[2]); |
|
| 1211 | - } |
|
| 1212 | - |
|
| 1213 | - protected function lib_round($arg) |
|
| 1214 | - { |
|
| 1215 | - if ($arg[0] != "list") { |
|
| 1216 | - $value = $this->assertNumber($arg); |
|
| 1217 | - return array("number", round($value), $arg[2]); |
|
| 1218 | - } else { |
|
| 1219 | - $value = $this->assertNumber($arg[2][0]); |
|
| 1220 | - $precision = $this->assertNumber($arg[2][1]); |
|
| 1221 | - return array("number", round($value, $precision), $arg[2][0][2]); |
|
| 1222 | - } |
|
| 1223 | - } |
|
| 1224 | - |
|
| 1225 | - protected function lib_unit($arg) |
|
| 1226 | - { |
|
| 1227 | - if ($arg[0] == "list") { |
|
| 1228 | - list($number, $newUnit) = $arg[2]; |
|
| 1229 | - return array("number", $this->assertNumber($number), |
|
| 1230 | - $this->compileValue($this->lib_e($newUnit))); |
|
| 1231 | - } else { |
|
| 1232 | - return array("number", $this->assertNumber($arg), ""); |
|
| 1233 | - } |
|
| 1234 | - } |
|
| 1235 | - |
|
| 1236 | - /** |
|
| 1237 | - * Helper function to get arguments for color manipulation functions. |
|
| 1238 | - * takes a list that contains a color like thing and a percentage |
|
| 1239 | - */ |
|
| 1240 | - public function colorArgs($args) |
|
| 1241 | - { |
|
| 1242 | - if ($args[0] != 'list' || count($args[2]) < 2) { |
|
| 1243 | - return array(array('color', 0, 0, 0), 0); |
|
| 1244 | - } |
|
| 1245 | - list($color, $delta) = $args[2]; |
|
| 1246 | - $color = $this->assertColor($color); |
|
| 1247 | - $delta = (float) $delta[1]; |
|
| 1248 | - |
|
| 1249 | - return array($color, $delta); |
|
| 1250 | - } |
|
| 1251 | - |
|
| 1252 | - protected function lib_darken($args) |
|
| 1253 | - { |
|
| 1254 | - list($color, $delta) = $this->colorArgs($args); |
|
| 1255 | - |
|
| 1256 | - $hsl = $this->toHSL($color); |
|
| 1257 | - $hsl[3] = $this->clamp($hsl[3] - $delta, 100); |
|
| 1258 | - return $this->toRGB($hsl); |
|
| 1259 | - } |
|
| 1260 | - |
|
| 1261 | - protected function lib_lighten($args) |
|
| 1262 | - { |
|
| 1263 | - list($color, $delta) = $this->colorArgs($args); |
|
| 1264 | - |
|
| 1265 | - $hsl = $this->toHSL($color); |
|
| 1266 | - $hsl[3] = $this->clamp($hsl[3] + $delta, 100); |
|
| 1267 | - return $this->toRGB($hsl); |
|
| 1268 | - } |
|
| 1269 | - |
|
| 1270 | - protected function lib_saturate($args) |
|
| 1271 | - { |
|
| 1272 | - list($color, $delta) = $this->colorArgs($args); |
|
| 1273 | - |
|
| 1274 | - $hsl = $this->toHSL($color); |
|
| 1275 | - $hsl[2] = $this->clamp($hsl[2] + $delta, 100); |
|
| 1276 | - return $this->toRGB($hsl); |
|
| 1277 | - } |
|
| 1278 | - |
|
| 1279 | - protected function lib_desaturate($args) |
|
| 1280 | - { |
|
| 1281 | - list($color, $delta) = $this->colorArgs($args); |
|
| 1282 | - |
|
| 1283 | - $hsl = $this->toHSL($color); |
|
| 1284 | - $hsl[2] = $this->clamp($hsl[2] - $delta, 100); |
|
| 1285 | - return $this->toRGB($hsl); |
|
| 1286 | - } |
|
| 1287 | - |
|
| 1288 | - protected function lib_spin($args) |
|
| 1289 | - { |
|
| 1290 | - list($color, $delta) = $this->colorArgs($args); |
|
| 1291 | - |
|
| 1292 | - $hsl = $this->toHSL($color); |
|
| 1293 | - |
|
| 1294 | - $hsl[1] = $hsl[1] + $delta % 360; |
|
| 1295 | - if ($hsl[1] < 0) { |
|
| 1296 | - $hsl[1] += 360; |
|
| 1297 | - } |
|
| 1298 | - |
|
| 1299 | - return $this->toRGB($hsl); |
|
| 1300 | - } |
|
| 1301 | - |
|
| 1302 | - protected function lib_fadeout($args) |
|
| 1303 | - { |
|
| 1304 | - list($color, $delta) = $this->colorArgs($args); |
|
| 1305 | - $color[4] = $this->clamp((isset($color[4]) ? $color[4] : 1) - $delta / 100); |
|
| 1306 | - return $color; |
|
| 1307 | - } |
|
| 1308 | - |
|
| 1309 | - protected function lib_fadein($args) |
|
| 1310 | - { |
|
| 1311 | - list($color, $delta) = $this->colorArgs($args); |
|
| 1312 | - $color[4] = $this->clamp((isset($color[4]) ? $color[4] : 1) + $delta / 100); |
|
| 1313 | - return $color; |
|
| 1314 | - } |
|
| 1315 | - |
|
| 1316 | - protected function lib_hue($color) |
|
| 1317 | - { |
|
| 1318 | - $hsl = $this->toHSL($this->assertColor($color)); |
|
| 1319 | - return round($hsl[1]); |
|
| 1320 | - } |
|
| 1321 | - |
|
| 1322 | - protected function lib_saturation($color) |
|
| 1323 | - { |
|
| 1324 | - $hsl = $this->toHSL($this->assertColor($color)); |
|
| 1325 | - return round($hsl[2]); |
|
| 1326 | - } |
|
| 1327 | - |
|
| 1328 | - protected function lib_lightness($color) |
|
| 1329 | - { |
|
| 1330 | - $hsl = $this->toHSL($this->assertColor($color)); |
|
| 1331 | - return round($hsl[3]); |
|
| 1332 | - } |
|
| 1333 | - |
|
| 1334 | - // get the alpha of a color |
|
| 1335 | - // defaults to 1 for non-colors or colors without an alpha |
|
| 1336 | - protected function lib_alpha($value) |
|
| 1337 | - { |
|
| 1338 | - if (!is_null($color = $this->coerceColor($value))) { |
|
| 1339 | - return isset($color[4]) ? $color[4] : 1; |
|
| 1340 | - } |
|
| 1341 | - return ''; |
|
| 1342 | - } |
|
| 1343 | - |
|
| 1344 | - // set the alpha of the color |
|
| 1345 | - protected function lib_fade($args) |
|
| 1346 | - { |
|
| 1347 | - list($color, $alpha) = $this->colorArgs($args); |
|
| 1348 | - $color[4] = $this->clamp($alpha / 100.0); |
|
| 1349 | - return $color; |
|
| 1350 | - } |
|
| 1351 | - |
|
| 1352 | - protected function lib_percentage($arg) |
|
| 1353 | - { |
|
| 1354 | - $num = $this->assertNumber($arg); |
|
| 1355 | - return array("number", $num * 100, "%"); |
|
| 1356 | - } |
|
| 1357 | - |
|
| 1358 | - /** |
|
| 1359 | - * Mix color with white in variable proportion. |
|
| 1360 | - * |
|
| 1361 | - * It is the same as calling `mix(#ffffff, @color, @weight)`. |
|
| 1362 | - * |
|
| 1363 | - * tint(@color, [@weight: 50%]); |
|
| 1364 | - * |
|
| 1365 | - * http://lesscss.org/functions/#color-operations-tint |
|
| 1366 | - * |
|
| 1367 | - * @return array Color |
|
| 1368 | - */ |
|
| 1369 | - protected function lib_tint($args) |
|
| 1370 | - { |
|
| 1371 | - $white = ['color', 255, 255, 255]; |
|
| 1372 | - if ($args[0] == 'color') { |
|
| 1373 | - return $this->lib_mix(['list', ',', [$white, $args]]); |
|
| 1374 | - } elseif ($args[0] == "list" && count($args[2]) == 2) { |
|
| 1375 | - return $this->lib_mix([$args[0], $args[1], [$white, $args[2][0], $args[2][1]]]); |
|
| 1376 | - } else { |
|
| 1377 | - $this->throwError("tint expects (color, weight)"); |
|
| 1378 | - } |
|
| 1379 | - return array(); |
|
| 1380 | - } |
|
| 1381 | - |
|
| 1382 | - /** |
|
| 1383 | - * Mix color with black in variable proportion. |
|
| 1384 | - * |
|
| 1385 | - * It is the same as calling `mix(#000000, @color, @weight)` |
|
| 1386 | - * |
|
| 1387 | - * shade(@color, [@weight: 50%]); |
|
| 1388 | - * |
|
| 1389 | - * http://lesscss.org/functions/#color-operations-shade |
|
| 1390 | - * |
|
| 1391 | - * @return array Color |
|
| 1392 | - */ |
|
| 1393 | - protected function lib_shade($args) |
|
| 1394 | - { |
|
| 1395 | - $black = ['color', 0, 0, 0]; |
|
| 1396 | - if ($args[0] == 'color') { |
|
| 1397 | - return $this->lib_mix(['list', ',', [$black, $args]]); |
|
| 1398 | - } elseif ($args[0] == "list" && count($args[2]) == 2) { |
|
| 1399 | - return $this->lib_mix([$args[0], $args[1], [$black, $args[2][0], $args[2][1]]]); |
|
| 1400 | - } else { |
|
| 1401 | - $this->throwError("shade expects (color, weight)"); |
|
| 1402 | - } |
|
| 1403 | - return array(); |
|
| 1404 | - } |
|
| 1405 | - |
|
| 1406 | - /** |
|
| 1407 | - * lib_mix |
|
| 1408 | - * mixes two colors by weight |
|
| 1409 | - * mix(@color1, @color2, [@weight: 50%]); |
|
| 1410 | - * http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html#mix-instance_method |
|
| 1411 | - * |
|
| 1412 | - * @param array $args Args |
|
| 1413 | - * @return array |
|
| 1414 | - */ |
|
| 1415 | - protected function lib_mix($args) |
|
| 1416 | - { |
|
| 1417 | - if ($args[0] != "list" || count($args[2]) < 2) { |
|
| 1418 | - $this->throwError("mix expects (color1, color2, weight)"); |
|
| 1419 | - } |
|
| 1420 | - |
|
| 1421 | - list($first, $second) = $args[2]; |
|
| 1422 | - $first = $this->assertColor($first); |
|
| 1423 | - $second = $this->assertColor($second); |
|
| 1424 | - |
|
| 1425 | - $first_a = $this->lib_alpha($first); |
|
| 1426 | - $second_a = $this->lib_alpha($second); |
|
| 1427 | - |
|
| 1428 | - if (isset($args[2][2])) { |
|
| 1429 | - $weight = $args[2][2][1] / 100.0; |
|
| 1430 | - } else { |
|
| 1431 | - $weight = 0.5; |
|
| 1432 | - } |
|
| 1433 | - |
|
| 1434 | - $w = $weight * 2 - 1; |
|
| 1435 | - $a = $first_a - $second_a; |
|
| 1436 | - |
|
| 1437 | - $w1 = (($w * $a == -1 ? $w : ($w + $a) / (1 + $w * $a)) + 1) / 2.0; |
|
| 1438 | - $w2 = 1.0 - $w1; |
|
| 1439 | - |
|
| 1440 | - $new = array('color', |
|
| 1441 | - $w1 * $first[1] + $w2 * $second[1], |
|
| 1442 | - $w1 * $first[2] + $w2 * $second[2], |
|
| 1443 | - $w1 * $first[3] + $w2 * $second[3], |
|
| 1444 | - ); |
|
| 1445 | - |
|
| 1446 | - if ($first_a != 1.0 || $second_a != 1.0) { |
|
| 1447 | - $new[] = $first_a * $weight + $second_a * ($weight - 1); |
|
| 1448 | - } |
|
| 1449 | - |
|
| 1450 | - return $this->fixColor($new); |
|
| 1451 | - } |
|
| 1452 | - |
|
| 1453 | - /** |
|
| 1454 | - * lib_contrast |
|
| 1455 | - * |
|
| 1456 | - * @param array $args Args |
|
| 1457 | - * @return array |
|
| 1458 | - */ |
|
| 1459 | - protected function lib_contrast($args) |
|
| 1460 | - { |
|
| 1461 | - $darkColor = array('color', 0, 0, 0); |
|
| 1462 | - $lightColor = array('color', 255, 255, 255); |
|
| 1463 | - $threshold = 0.43; |
|
| 1464 | - |
|
| 1465 | - if ($args[0] == 'list') { |
|
| 1466 | - $inputColor = (isset($args[2][0])) ? $this->assertColor($args[2][0]) : $lightColor; |
|
| 1467 | - $darkColor = (isset($args[2][1])) ? $this->assertColor($args[2][1]) : $darkColor; |
|
| 1468 | - $lightColor = (isset($args[2][2])) ? $this->assertColor($args[2][2]) : $lightColor; |
|
| 1469 | - $threshold = (isset($args[2][3])) ? $this->assertNumber($args[2][3]) : $threshold; |
|
| 1470 | - } else { |
|
| 1471 | - $inputColor = $this->assertColor($args); |
|
| 1472 | - } |
|
| 1473 | - |
|
| 1474 | - $inputColor = $this->coerceColor($inputColor); |
|
| 1475 | - $darkColor = $this->coerceColor($darkColor); |
|
| 1476 | - $lightColor = $this->coerceColor($lightColor); |
|
| 1477 | - |
|
| 1478 | - //Figure out which is actually light and dark! |
|
| 1479 | - if ($this->toLuma($darkColor) > $this->toLuma($lightColor)) { |
|
| 1480 | - $t = $lightColor; |
|
| 1481 | - $lightColor = $darkColor; |
|
| 1482 | - $darkColor = $t; |
|
| 1483 | - } |
|
| 1484 | - |
|
| 1485 | - $inputColor_alpha = $this->lib_alpha($inputColor); |
|
| 1486 | - if (($this->toLuma($inputColor) * $inputColor_alpha) < $threshold) { |
|
| 1487 | - return $lightColor; |
|
| 1488 | - } |
|
| 1489 | - return $darkColor; |
|
| 1490 | - } |
|
| 1491 | - |
|
| 1492 | - private function toLuma($color) |
|
| 1493 | - { |
|
| 1494 | - list(, $r, $g, $b) = $this->coerceColor($color); |
|
| 1495 | - |
|
| 1496 | - $r = $r / 255; |
|
| 1497 | - $g = $g / 255; |
|
| 1498 | - $b = $b / 255; |
|
| 1499 | - |
|
| 1500 | - $r = ($r <= 0.03928) ? $r / 12.92 : pow((($r + 0.055) / 1.055), 2.4); |
|
| 1501 | - $g = ($g <= 0.03928) ? $g / 12.92 : pow((($g + 0.055) / 1.055), 2.4); |
|
| 1502 | - $b = ($b <= 0.03928) ? $b / 12.92 : pow((($b + 0.055) / 1.055), 2.4); |
|
| 1503 | - |
|
| 1504 | - return (0.2126 * $r) + (0.7152 * $g) + (0.0722 * $b); |
|
| 1505 | - } |
|
| 1506 | - |
|
| 1507 | - protected function lib_luma($color) |
|
| 1508 | - { |
|
| 1509 | - return array("number", round($this->toLuma($color) * 100, 8), "%"); |
|
| 1510 | - } |
|
| 1511 | - |
|
| 1512 | - |
|
| 1513 | - public function assertColor($value, $error = "expected color value") |
|
| 1514 | - { |
|
| 1515 | - $color = $this->coerceColor($value); |
|
| 1516 | - if (is_null($color)) { |
|
| 1517 | - $this->throwError($error); |
|
| 1518 | - } |
|
| 1519 | - return $color; |
|
| 1520 | - } |
|
| 1521 | - |
|
| 1522 | - public function assertNumber($value, $error = "expecting number") |
|
| 1523 | - { |
|
| 1524 | - if ($value[0] == "number") { |
|
| 1525 | - return $value[1]; |
|
| 1526 | - } |
|
| 1527 | - $this->throwError($error); |
|
| 1528 | - } |
|
| 1529 | - |
|
| 1530 | - public function assertArgs($value, $expectedArgs, $name = "") |
|
| 1531 | - { |
|
| 1532 | - if ($expectedArgs == 1) { |
|
| 1533 | - return $value; |
|
| 1534 | - } else { |
|
| 1535 | - if ($value[0] !== "list" || $value[1] != ",") { |
|
| 1536 | - $this->throwError("expecting list"); |
|
| 1537 | - } |
|
| 1538 | - $values = $value[2]; |
|
| 1539 | - $numValues = count($values); |
|
| 1540 | - if ($expectedArgs != $numValues) { |
|
| 1541 | - if ($name) { |
|
| 1542 | - $name = $name.": "; |
|
| 1543 | - } |
|
| 1544 | - |
|
| 1545 | - $this->throwError("{$name}expecting $expectedArgs arguments, got $numValues"); |
|
| 1546 | - } |
|
| 1547 | - |
|
| 1548 | - return $values; |
|
| 1549 | - } |
|
| 1550 | - } |
|
| 1551 | - |
|
| 1552 | - protected function toHSL($color) |
|
| 1553 | - { |
|
| 1554 | - if ($color[0] === 'hsl') { |
|
| 1555 | - return $color; |
|
| 1556 | - } |
|
| 1557 | - |
|
| 1558 | - $r = $color[1] / 255; |
|
| 1559 | - $g = $color[2] / 255; |
|
| 1560 | - $b = $color[3] / 255; |
|
| 1561 | - |
|
| 1562 | - $min = min($r, $g, $b); |
|
| 1563 | - $max = max($r, $g, $b); |
|
| 1564 | - |
|
| 1565 | - $L = ($min + $max) / 2; |
|
| 1566 | - if ($min == $max) { |
|
| 1567 | - $S = $H = 0; |
|
| 1568 | - } else { |
|
| 1569 | - if ($L < 0.5) { |
|
| 1570 | - $S = ($max - $min) / ($max + $min); |
|
| 1571 | - } else { |
|
| 1572 | - $S = ($max - $min) / (2.0 - $max - $min); |
|
| 1573 | - } |
|
| 1574 | - if ($r == $max) { |
|
| 1575 | - $H = ($g - $b) / ($max - $min); |
|
| 1576 | - } elseif ($g == $max) { |
|
| 1577 | - $H = 2.0 + ($b - $r) / ($max - $min); |
|
| 1578 | - } elseif ($b == $max) { |
|
| 1579 | - $H = 4.0 + ($r - $g) / ($max - $min); |
|
| 1580 | - } |
|
| 1581 | - } |
|
| 1582 | - |
|
| 1583 | - $out = array('hsl', |
|
| 1584 | - ($H < 0 ? $H + 6 : $H) * 60, |
|
| 1585 | - $S * 100, |
|
| 1586 | - $L * 100, |
|
| 1587 | - ); |
|
| 1588 | - |
|
| 1589 | - if (count($color) > 4) { |
|
| 1590 | - // copy alpha |
|
| 1591 | - $out[] = $color[4]; |
|
| 1592 | - } |
|
| 1593 | - return $out; |
|
| 1594 | - } |
|
| 1595 | - |
|
| 1596 | - protected function toRGB_helper($comp, $temp1, $temp2) |
|
| 1597 | - { |
|
| 1598 | - if ($comp < 0) { |
|
| 1599 | - $comp += 1.0; |
|
| 1600 | - } elseif ($comp > 1) { |
|
| 1601 | - $comp -= 1.0; |
|
| 1602 | - } |
|
| 1603 | - |
|
| 1604 | - if (6 * $comp < 1) { |
|
| 1605 | - return $temp1 + ($temp2 - $temp1) * 6 * $comp; |
|
| 1606 | - } |
|
| 1607 | - if (2 * $comp < 1) { |
|
| 1608 | - return $temp2; |
|
| 1609 | - } |
|
| 1610 | - if (3 * $comp < 2) { |
|
| 1611 | - return $temp1 + ($temp2 - $temp1) * ((2 / 3) - $comp) * 6; |
|
| 1612 | - } |
|
| 1613 | - |
|
| 1614 | - return $temp1; |
|
| 1615 | - } |
|
| 1616 | - |
|
| 1617 | - /** |
|
| 1618 | - * Converts a hsl array into a color value in rgb. |
|
| 1619 | - * Expects H to be in range of 0 to 360, S and L in 0 to 100 |
|
| 1620 | - */ |
|
| 1621 | - protected function toRGB($color) |
|
| 1622 | - { |
|
| 1623 | - if ($color[0] === 'color') { |
|
| 1624 | - return $color; |
|
| 1625 | - } |
|
| 1626 | - |
|
| 1627 | - $H = $color[1] / 360; |
|
| 1628 | - $S = $color[2] / 100; |
|
| 1629 | - $L = $color[3] / 100; |
|
| 1630 | - |
|
| 1631 | - if ($S == 0) { |
|
| 1632 | - $r = $g = $b = $L; |
|
| 1633 | - } else { |
|
| 1634 | - $temp2 = $L < 0.5 ? |
|
| 1635 | - $L * (1.0 + $S) : $L + $S - $L * $S; |
|
| 1636 | - |
|
| 1637 | - $temp1 = 2.0 * $L - $temp2; |
|
| 1638 | - |
|
| 1639 | - $r = $this->toRGB_helper($H + 1 / 3, $temp1, $temp2); |
|
| 1640 | - $g = $this->toRGB_helper($H, $temp1, $temp2); |
|
| 1641 | - $b = $this->toRGB_helper($H - 1 / 3, $temp1, $temp2); |
|
| 1642 | - } |
|
| 1643 | - |
|
| 1644 | - // $out = array('color', round($r*255), round($g*255), round($b*255)); |
|
| 1645 | - $out = array('color', $r * 255, $g * 255, $b * 255); |
|
| 1646 | - if (count($color) > 4) { |
|
| 1647 | - // copy alpha |
|
| 1648 | - $out[] = $color[4]; |
|
| 1649 | - } |
|
| 1650 | - return $out; |
|
| 1651 | - } |
|
| 1652 | - |
|
| 1653 | - protected function clamp($v, $max = 1, $min = 0) |
|
| 1654 | - { |
|
| 1655 | - return min($max, max($min, $v)); |
|
| 1656 | - } |
|
| 1657 | - |
|
| 1658 | - /** |
|
| 1659 | - * Convert the rgb, rgba, hsl color literals of function type |
|
| 1660 | - * as returned by the parser into values of color type. |
|
| 1661 | - */ |
|
| 1662 | - protected function funcToColor($func) |
|
| 1663 | - { |
|
| 1664 | - $fname = $func[1]; |
|
| 1665 | - if ($func[2][0] != 'list') { |
|
| 1666 | - // need a list of arguments |
|
| 1667 | - return false; |
|
| 1668 | - } |
|
| 1669 | - $rawComponents = $func[2][2]; |
|
| 1670 | - |
|
| 1671 | - if ($fname == 'hsl' || $fname == 'hsla') { |
|
| 1672 | - $hsl = array('hsl'); |
|
| 1673 | - $i = 0; |
|
| 1674 | - foreach ($rawComponents as $c) { |
|
| 1675 | - $val = $this->reduce($c); |
|
| 1676 | - $val = isset($val[1]) ? (float) $val[1] : 0; |
|
| 1677 | - |
|
| 1678 | - if ($i == 0) { |
|
| 1679 | - $clamp = 360; |
|
| 1680 | - } elseif ($i < 3) { |
|
| 1681 | - $clamp = 100; |
|
| 1682 | - } else { |
|
| 1683 | - $clamp = 1; |
|
| 1684 | - } |
|
| 1685 | - |
|
| 1686 | - $hsl[] = $this->clamp($val, $clamp); |
|
| 1687 | - $i++; |
|
| 1688 | - } |
|
| 1689 | - |
|
| 1690 | - while (count($hsl) < 4) { |
|
| 1691 | - $hsl[] = 0; |
|
| 1692 | - } |
|
| 1693 | - return $this->toRGB($hsl); |
|
| 1694 | - |
|
| 1695 | - } elseif ($fname == 'rgb' || $fname == 'rgba') { |
|
| 1696 | - $components = array(); |
|
| 1697 | - $i = 1; |
|
| 1698 | - foreach ($rawComponents as $c) { |
|
| 1699 | - $c = $this->reduce($c); |
|
| 1700 | - if ($i < 4) { |
|
| 1701 | - if ($c[0] == "number" && $c[2] == "%") { |
|
| 1702 | - $components[] = 255 * ($c[1] / 100); |
|
| 1703 | - } else { |
|
| 1704 | - $components[] = (float) $c[1]; |
|
| 1705 | - } |
|
| 1706 | - } elseif ($i == 4) { |
|
| 1707 | - if ($c[0] == "number" && $c[2] == "%") { |
|
| 1708 | - $components[] = 1.0 * ($c[1] / 100); |
|
| 1709 | - } else { |
|
| 1710 | - $components[] = (float) $c[1]; |
|
| 1711 | - } |
|
| 1712 | - } else { |
|
| 1713 | - break; |
|
| 1714 | - } |
|
| 1715 | - |
|
| 1716 | - $i++; |
|
| 1717 | - } |
|
| 1718 | - while (count($components) < 3) { |
|
| 1719 | - $components[] = 0; |
|
| 1720 | - } |
|
| 1721 | - array_unshift($components, 'color'); |
|
| 1722 | - return $this->fixColor($components); |
|
| 1723 | - } |
|
| 1724 | - |
|
| 1725 | - return false; |
|
| 1726 | - } |
|
| 1727 | - |
|
| 1728 | - protected function reduce($value, $forExpression = false) |
|
| 1729 | - { |
|
| 1730 | - switch ($value[0]) { |
|
| 1731 | - case "interpolate": |
|
| 1732 | - $reduced = $this->reduce($value[1]); |
|
| 1733 | - $var = $this->compileValue($reduced); |
|
| 1734 | - $res = $this->reduce(array("variable", $this->vPrefix.$var)); |
|
| 1735 | - |
|
| 1736 | - if ($res[0] == "raw_color") { |
|
| 1737 | - $res = $this->coerceColor($res); |
|
| 1738 | - } |
|
| 1739 | - |
|
| 1740 | - if (empty($value[2])) { |
|
| 1741 | - $res = $this->lib_e($res); |
|
| 1742 | - } |
|
| 1743 | - |
|
| 1744 | - return $res; |
|
| 1745 | - case "variable": |
|
| 1746 | - $key = $value[1]; |
|
| 1747 | - if (is_array($key)) { |
|
| 1748 | - $key = $this->reduce($key); |
|
| 1749 | - $key = $this->vPrefix.$this->compileValue($this->lib_e($key)); |
|
| 1750 | - } |
|
| 1751 | - |
|
| 1752 | - $seen = & $this->env->seenNames; |
|
| 1753 | - |
|
| 1754 | - if (!empty($seen[$key])) { |
|
| 1755 | - $this->throwError("infinite loop detected: $key"); |
|
| 1756 | - } |
|
| 1757 | - |
|
| 1758 | - $seen[$key] = true; |
|
| 1759 | - $out = $this->reduce($this->get($key)); |
|
| 1760 | - $seen[$key] = false; |
|
| 1761 | - return $out; |
|
| 1762 | - case "list": |
|
| 1763 | - foreach ($value[2] as &$item) { |
|
| 1764 | - $item = $this->reduce($item, $forExpression); |
|
| 1765 | - } |
|
| 1766 | - return $value; |
|
| 1767 | - case "expression": |
|
| 1768 | - return $this->evaluate($value); |
|
| 1769 | - case "string": |
|
| 1770 | - foreach ($value[2] as &$part) { |
|
| 1771 | - if (is_array($part)) { |
|
| 1772 | - $strip = $part[0] == "variable"; |
|
| 1773 | - $part = $this->reduce($part); |
|
| 1774 | - if ($strip) { |
|
| 1775 | - $part = $this->lib_e($part); |
|
| 1776 | - } |
|
| 1777 | - } |
|
| 1778 | - } |
|
| 1779 | - return $value; |
|
| 1780 | - case "escape": |
|
| 1781 | - list(, $inner) = $value; |
|
| 1782 | - return $this->lib_e($this->reduce($inner)); |
|
| 1783 | - case "function": |
|
| 1784 | - $color = $this->funcToColor($value); |
|
| 1785 | - if ($color) { |
|
| 1786 | - return $color; |
|
| 1787 | - } |
|
| 1788 | - |
|
| 1789 | - list(, $name, $args) = $value; |
|
| 1790 | - if ($name == "%") { |
|
| 1791 | - $name = "_sprintf"; |
|
| 1792 | - } |
|
| 1793 | - |
|
| 1794 | - $f = isset($this->libFunctions[$name]) ? |
|
| 1795 | - $this->libFunctions[$name] : array($this, 'lib_'.str_replace('-', '_', $name)); |
|
| 1796 | - |
|
| 1797 | - if (is_callable($f)) { |
|
| 1798 | - if ($args[0] == 'list') { |
|
| 1799 | - $args = self::compressList($args[2], $args[1]); |
|
| 1800 | - } |
|
| 1801 | - |
|
| 1802 | - $ret = call_user_func($f, $this->reduce($args, true), $this); |
|
| 1803 | - |
|
| 1804 | - if (is_null($ret)) { |
|
| 1805 | - return array("string", "", array( |
|
| 1806 | - $name, "(", $args, ")" |
|
| 1807 | - )); |
|
| 1808 | - } |
|
| 1809 | - |
|
| 1810 | - // convert to a typed value if the result is a php primitive |
|
| 1811 | - if (is_numeric($ret)) { |
|
| 1812 | - $ret = array('number', $ret, ""); |
|
| 1813 | - } elseif (!is_array($ret)) { |
|
| 1814 | - $ret = array('keyword', $ret); |
|
| 1815 | - } |
|
| 1816 | - |
|
| 1817 | - return $ret; |
|
| 1818 | - } |
|
| 1819 | - |
|
| 1820 | - // plain function, reduce args |
|
| 1821 | - $value[2] = $this->reduce($value[2]); |
|
| 1822 | - return $value; |
|
| 1823 | - case "unary": |
|
| 1824 | - list(, $op, $exp) = $value; |
|
| 1825 | - $exp = $this->reduce($exp); |
|
| 1826 | - |
|
| 1827 | - if ($exp[0] == "number") { |
|
| 1828 | - switch ($op) { |
|
| 1829 | - case "+": |
|
| 1830 | - return $exp; |
|
| 1831 | - case "-": |
|
| 1832 | - $exp[1] *= -1; |
|
| 1833 | - return $exp; |
|
| 1834 | - } |
|
| 1835 | - } |
|
| 1836 | - return array("string", "", array($op, $exp)); |
|
| 1837 | - } |
|
| 1838 | - |
|
| 1839 | - if ($forExpression) { |
|
| 1840 | - switch ($value[0]) { |
|
| 1841 | - case "keyword": |
|
| 1842 | - if ($color = $this->coerceColor($value)) { |
|
| 1843 | - return $color; |
|
| 1844 | - } |
|
| 1845 | - break; |
|
| 1846 | - case "raw_color": |
|
| 1847 | - return $this->coerceColor($value); |
|
| 1848 | - } |
|
| 1849 | - } |
|
| 1850 | - |
|
| 1851 | - return $value; |
|
| 1852 | - } |
|
| 1853 | - |
|
| 1854 | - |
|
| 1855 | - // coerce a value for use in color operation |
|
| 1856 | - protected function coerceColor($value) |
|
| 1857 | - { |
|
| 1858 | - switch ($value[0]) { |
|
| 1859 | - case 'color': |
|
| 1860 | - return $value; |
|
| 1861 | - case 'raw_color': |
|
| 1862 | - $c = array("color", 0, 0, 0); |
|
| 1863 | - $colorStr = substr($value[1], 1); |
|
| 1864 | - $num = hexdec($colorStr); |
|
| 1865 | - $width = strlen($colorStr) == 3 ? 16 : 256; |
|
| 1866 | - |
|
| 1867 | - for ($i = 3; $i > 0; $i--) { // 3 2 1 |
|
| 1868 | - $t = intval($num) % $width; |
|
| 1869 | - $num /= $width; |
|
| 1870 | - |
|
| 1871 | - $c[$i] = $t * (256 / $width) + $t * floor(16/$width); |
|
| 1872 | - } |
|
| 1873 | - |
|
| 1874 | - return $c; |
|
| 1875 | - case 'keyword': |
|
| 1876 | - $name = $value[1]; |
|
| 1877 | - if (isset(self::$cssColors[$name])) { |
|
| 1878 | - $rgba = explode(',', self::$cssColors[$name]); |
|
| 1879 | - |
|
| 1880 | - if (isset($rgba[3])) { |
|
| 1881 | - return array('color', $rgba[0], $rgba[1], $rgba[2], $rgba[3]); |
|
| 1882 | - } |
|
| 1883 | - return array('color', $rgba[0], $rgba[1], $rgba[2]); |
|
| 1884 | - } |
|
| 1885 | - return null; |
|
| 1886 | - } |
|
| 1887 | - return null; |
|
| 1888 | - } |
|
| 1889 | - |
|
| 1890 | - // make something string like into a string |
|
| 1891 | - protected function coerceString($value) |
|
| 1892 | - { |
|
| 1893 | - switch ($value[0]) { |
|
| 1894 | - case "string": |
|
| 1895 | - return $value; |
|
| 1896 | - case "keyword": |
|
| 1897 | - return array("string", "", array($value[1])); |
|
| 1898 | - } |
|
| 1899 | - return null; |
|
| 1900 | - } |
|
| 1901 | - |
|
| 1902 | - // turn list of length 1 into value type |
|
| 1903 | - protected function flattenList($value) |
|
| 1904 | - { |
|
| 1905 | - if ($value[0] == "list" && count($value[2]) == 1) { |
|
| 1906 | - return $this->flattenList($value[2][0]); |
|
| 1907 | - } |
|
| 1908 | - return $value; |
|
| 1909 | - } |
|
| 1910 | - |
|
| 1911 | - public function toBool($a) |
|
| 1912 | - { |
|
| 1913 | - return $a ? self::$TRUE : self::$FALSE; |
|
| 1914 | - } |
|
| 1915 | - |
|
| 1916 | - // evaluate an expression |
|
| 1917 | - protected function evaluate($exp) |
|
| 1918 | - { |
|
| 1919 | - list(, $op, $left, $right, $whiteBefore, $whiteAfter) = $exp; |
|
| 1920 | - |
|
| 1921 | - $left = $this->reduce($left, true); |
|
| 1922 | - $right = $this->reduce($right, true); |
|
| 1923 | - |
|
| 1924 | - if ($leftColor = $this->coerceColor($left)) { |
|
| 1925 | - $left = $leftColor; |
|
| 1926 | - } |
|
| 1927 | - |
|
| 1928 | - if ($rightColor = $this->coerceColor($right)) { |
|
| 1929 | - $right = $rightColor; |
|
| 1930 | - } |
|
| 1931 | - |
|
| 1932 | - $ltype = $left[0]; |
|
| 1933 | - $rtype = $right[0]; |
|
| 1934 | - |
|
| 1935 | - // operators that work on all types |
|
| 1936 | - if ($op == "and") { |
|
| 1937 | - return $this->toBool($left == self::$TRUE && $right == self::$TRUE); |
|
| 1938 | - } |
|
| 1939 | - |
|
| 1940 | - if ($op == "=") { |
|
| 1941 | - return $this->toBool($this->eq($left, $right)); |
|
| 1942 | - } |
|
| 1943 | - |
|
| 1944 | - if ($op == "+" && !is_null($str = $this->stringConcatenate($left, $right))) { |
|
| 1945 | - return $str; |
|
| 1946 | - } |
|
| 1947 | - |
|
| 1948 | - // type based operators |
|
| 1949 | - $fname = "op_{$ltype}_{$rtype}"; |
|
| 1950 | - if (is_callable(array($this, $fname))) { |
|
| 1951 | - $out = $this->$fname($op, $left, $right); |
|
| 1952 | - if (!is_null($out)) { |
|
| 1953 | - return $out; |
|
| 1954 | - } |
|
| 1955 | - } |
|
| 1956 | - |
|
| 1957 | - // make the expression look it did before being parsed |
|
| 1958 | - $paddedOp = $op; |
|
| 1959 | - if ($whiteBefore) { |
|
| 1960 | - $paddedOp = " ".$paddedOp; |
|
| 1961 | - } |
|
| 1962 | - if ($whiteAfter) { |
|
| 1963 | - $paddedOp .= " "; |
|
| 1964 | - } |
|
| 1965 | - |
|
| 1966 | - return array("string", "", array($left, $paddedOp, $right)); |
|
| 1967 | - } |
|
| 1968 | - |
|
| 1969 | - protected function stringConcatenate($left, $right) |
|
| 1970 | - { |
|
| 1971 | - if ($strLeft = $this->coerceString($left)) { |
|
| 1972 | - if ($right[0] == "string") { |
|
| 1973 | - $right[1] = ""; |
|
| 1974 | - } |
|
| 1975 | - $strLeft[2][] = $right; |
|
| 1976 | - return $strLeft; |
|
| 1977 | - } |
|
| 1978 | - |
|
| 1979 | - if ($strRight = $this->coerceString($right)) { |
|
| 1980 | - array_unshift($strRight[2], $left); |
|
| 1981 | - return $strRight; |
|
| 1982 | - } |
|
| 1983 | - return ''; |
|
| 1984 | - } |
|
| 1985 | - |
|
| 1986 | - |
|
| 1987 | - // make sure a color's components don't go out of bounds |
|
| 1988 | - protected function fixColor($c) |
|
| 1989 | - { |
|
| 1990 | - foreach (range(1, 3) as $i) { |
|
| 1991 | - if ($c[$i] < 0) { |
|
| 1992 | - $c[$i] = 0; |
|
| 1993 | - } |
|
| 1994 | - if ($c[$i] > 255) { |
|
| 1995 | - $c[$i] = 255; |
|
| 1996 | - } |
|
| 1997 | - } |
|
| 1998 | - |
|
| 1999 | - return $c; |
|
| 2000 | - } |
|
| 2001 | - |
|
| 2002 | - protected function op_number_color($op, $lft, $rgt) |
|
| 2003 | - { |
|
| 2004 | - if ($op == '+' || $op == '*') { |
|
| 2005 | - return $this->op_color_number($op, $rgt, $lft); |
|
| 2006 | - } |
|
| 2007 | - return array(); |
|
| 2008 | - } |
|
| 2009 | - |
|
| 2010 | - protected function op_color_number($op, $lft, $rgt) |
|
| 2011 | - { |
|
| 2012 | - if ($rgt[0] == '%') { |
|
| 2013 | - $rgt[1] /= 100; |
|
| 2014 | - } |
|
| 2015 | - |
|
| 2016 | - return $this->op_color_color( |
|
| 2017 | - $op, |
|
| 2018 | - $lft, |
|
| 2019 | - array_fill(1, count($lft) - 1, $rgt[1]) |
|
| 2020 | - ); |
|
| 2021 | - } |
|
| 2022 | - |
|
| 2023 | - protected function op_color_color($op, $left, $right) |
|
| 2024 | - { |
|
| 2025 | - $out = array('color'); |
|
| 2026 | - $max = count($left) > count($right) ? count($left) : count($right); |
|
| 2027 | - foreach (range(1, $max - 1) as $i) { |
|
| 2028 | - $lval = isset($left[$i]) ? $left[$i] : 0; |
|
| 2029 | - $rval = isset($right[$i]) ? $right[$i] : 0; |
|
| 2030 | - switch ($op) { |
|
| 2031 | - case '+': |
|
| 2032 | - $out[] = $lval + $rval; |
|
| 2033 | - break; |
|
| 2034 | - case '-': |
|
| 2035 | - $out[] = $lval - $rval; |
|
| 2036 | - break; |
|
| 2037 | - case '*': |
|
| 2038 | - $out[] = $lval * $rval; |
|
| 2039 | - break; |
|
| 2040 | - case '%': |
|
| 2041 | - $out[] = $lval % $rval; |
|
| 2042 | - break; |
|
| 2043 | - case '/': |
|
| 2044 | - if ($rval == 0) { |
|
| 2045 | - $this->throwError("evaluate error: can't divide by zero"); |
|
| 2046 | - } |
|
| 2047 | - $out[] = $lval / $rval; |
|
| 2048 | - break; |
|
| 2049 | - default: |
|
| 2050 | - $this->throwError('evaluate error: color op number failed on op '.$op); |
|
| 2051 | - } |
|
| 2052 | - } |
|
| 2053 | - return $this->fixColor($out); |
|
| 2054 | - } |
|
| 2055 | - |
|
| 2056 | - public function lib_red($color) |
|
| 2057 | - { |
|
| 2058 | - $color = $this->coerceColor($color); |
|
| 2059 | - if (is_null($color)) { |
|
| 2060 | - $this->throwError('color expected for red()'); |
|
| 2061 | - } |
|
| 2062 | - |
|
| 2063 | - return $color[1]; |
|
| 2064 | - } |
|
| 2065 | - |
|
| 2066 | - public function lib_green($color) |
|
| 2067 | - { |
|
| 2068 | - $color = $this->coerceColor($color); |
|
| 2069 | - if (is_null($color)) { |
|
| 2070 | - $this->throwError('color expected for green()'); |
|
| 2071 | - } |
|
| 2072 | - |
|
| 2073 | - return $color[2]; |
|
| 2074 | - } |
|
| 2075 | - |
|
| 2076 | - public function lib_blue($color) |
|
| 2077 | - { |
|
| 2078 | - $color = $this->coerceColor($color); |
|
| 2079 | - if (is_null($color)) { |
|
| 2080 | - $this->throwError('color expected for blue()'); |
|
| 2081 | - } |
|
| 2082 | - |
|
| 2083 | - return $color[3]; |
|
| 2084 | - } |
|
| 2085 | - |
|
| 2086 | - |
|
| 2087 | - // operator on two numbers |
|
| 2088 | - protected function op_number_number($op, $left, $right) |
|
| 2089 | - { |
|
| 2090 | - $unit = empty($left[2]) ? $right[2] : $left[2]; |
|
| 2091 | - |
|
| 2092 | - $value = 0; |
|
| 2093 | - switch ($op) { |
|
| 2094 | - case '+': |
|
| 2095 | - $value = $left[1] + $right[1]; |
|
| 2096 | - break; |
|
| 2097 | - case '*': |
|
| 2098 | - $value = $left[1] * $right[1]; |
|
| 2099 | - break; |
|
| 2100 | - case '-': |
|
| 2101 | - $value = $left[1] - $right[1]; |
|
| 2102 | - break; |
|
| 2103 | - case '%': |
|
| 2104 | - $value = $left[1] % $right[1]; |
|
| 2105 | - break; |
|
| 2106 | - case '/': |
|
| 2107 | - if ($right[1] == 0) { |
|
| 2108 | - $this->throwError('parse error: divide by zero'); |
|
| 2109 | - } |
|
| 2110 | - $value = $left[1] / $right[1]; |
|
| 2111 | - break; |
|
| 2112 | - case '<': |
|
| 2113 | - return $this->toBool($left[1] < $right[1]); |
|
| 2114 | - case '>': |
|
| 2115 | - return $this->toBool($left[1] > $right[1]); |
|
| 2116 | - case '>=': |
|
| 2117 | - return $this->toBool($left[1] >= $right[1]); |
|
| 2118 | - case '=<': |
|
| 2119 | - return $this->toBool($left[1] <= $right[1]); |
|
| 2120 | - default: |
|
| 2121 | - $this->throwError('parse error: unknown number operator: '.$op); |
|
| 2122 | - } |
|
| 2123 | - |
|
| 2124 | - return array("number", $value, $unit); |
|
| 2125 | - } |
|
| 2126 | - |
|
| 2127 | - |
|
| 2128 | - /* environment functions */ |
|
| 2129 | - |
|
| 2130 | - protected function makeOutputBlock($type, $selectors = null) |
|
| 2131 | - { |
|
| 2132 | - $b = new stdclass(); |
|
| 2133 | - $b->lines = array(); |
|
| 2134 | - $b->children = array(); |
|
| 2135 | - $b->selectors = $selectors; |
|
| 2136 | - $b->type = $type; |
|
| 2137 | - $b->parent = $this->scope; |
|
| 2138 | - return $b; |
|
| 2139 | - } |
|
| 2140 | - |
|
| 2141 | - // the state of execution |
|
| 2142 | - protected function pushEnv($block = null) |
|
| 2143 | - { |
|
| 2144 | - $e = new stdclass(); |
|
| 2145 | - $e->parent = $this->env; |
|
| 2146 | - $e->store = array(); |
|
| 2147 | - $e->block = $block; |
|
| 2148 | - |
|
| 2149 | - $this->env = $e; |
|
| 2150 | - return $e; |
|
| 2151 | - } |
|
| 2152 | - |
|
| 2153 | - // pop something off the stack |
|
| 2154 | - protected function popEnv() |
|
| 2155 | - { |
|
| 2156 | - $old = $this->env; |
|
| 2157 | - $this->env = $this->env->parent; |
|
| 2158 | - return $old; |
|
| 2159 | - } |
|
| 2160 | - |
|
| 2161 | - // set something in the current env |
|
| 2162 | - protected function set($name, $value) |
|
| 2163 | - { |
|
| 2164 | - $this->env->store[$name] = $value; |
|
| 2165 | - } |
|
| 2166 | - |
|
| 2167 | - |
|
| 2168 | - // get the highest occurrence entry for a name |
|
| 2169 | - protected function get($name) |
|
| 2170 | - { |
|
| 2171 | - $current = $this->env; |
|
| 2172 | - |
|
| 2173 | - $isArguments = $name == $this->vPrefix.'arguments'; |
|
| 2174 | - while ($current) { |
|
| 2175 | - if ($isArguments && isset($current->arguments)) { |
|
| 2176 | - return array('list', ' ', $current->arguments); |
|
| 2177 | - } |
|
| 2178 | - |
|
| 2179 | - if (isset($current->store[$name])) { |
|
| 2180 | - return $current->store[$name]; |
|
| 2181 | - } |
|
| 2182 | - |
|
| 2183 | - $current = isset($current->storeParent) ? |
|
| 2184 | - $current->storeParent : $current->parent; |
|
| 2185 | - } |
|
| 2186 | - |
|
| 2187 | - $this->throwError("variable $name is undefined"); |
|
| 2188 | - } |
|
| 2189 | - |
|
| 2190 | - // inject array of unparsed strings into environment as variables |
|
| 2191 | - protected function injectVariables($args) |
|
| 2192 | - { |
|
| 2193 | - $this->pushEnv(); |
|
| 2194 | - $parser = new lessc_parser($this, __METHOD__); |
|
| 2195 | - $value = null; |
|
| 2196 | - foreach ($args as $name => $strValue) { |
|
| 2197 | - if ($name[0] !== '@') { |
|
| 2198 | - $name = '@'.$name; |
|
| 2199 | - } |
|
| 2200 | - $parser->count = 0; |
|
| 2201 | - $parser->buffer = (string) $strValue; |
|
| 2202 | - if (!$parser->propertyValue($value)) { |
|
| 2203 | - throw new Exception("failed to parse passed in variable $name: $strValue"); |
|
| 2204 | - } |
|
| 2205 | - |
|
| 2206 | - $this->set($name, $value); |
|
| 2207 | - } |
|
| 2208 | - } |
|
| 2209 | - |
|
| 2210 | - /** |
|
| 2211 | - * Initialize any static state, can initialize parser for a file |
|
| 2212 | - * $opts isn't used yet |
|
| 2213 | - */ |
|
| 2214 | - public function __construct($fname = null) |
|
| 2215 | - { |
|
| 2216 | - if ($fname !== null) { |
|
| 2217 | - // used for deprecated parse method |
|
| 2218 | - $this->_parseFile = $fname; |
|
| 2219 | - } |
|
| 2220 | - } |
|
| 2221 | - |
|
| 2222 | - public function compile($string, $name = null) |
|
| 2223 | - { |
|
| 2224 | - $locale = setlocale(LC_NUMERIC, 0); |
|
| 2225 | - setlocale(LC_NUMERIC, "C"); |
|
| 2226 | - |
|
| 2227 | - $this->parser = $this->makeParser($name); |
|
| 2228 | - $root = $this->parser->parse($string); |
|
| 2229 | - |
|
| 2230 | - $this->env = null; |
|
| 2231 | - $this->scope = null; |
|
| 2232 | - |
|
| 2233 | - $this->formatter = $this->newFormatter(); |
|
| 2234 | - |
|
| 2235 | - if (!empty($this->registeredVars)) { |
|
| 2236 | - $this->injectVariables($this->registeredVars); |
|
| 2237 | - } |
|
| 2238 | - |
|
| 2239 | - $this->sourceParser = $this->parser; // used for error messages |
|
| 2240 | - $this->compileBlock($root); |
|
| 2241 | - |
|
| 2242 | - ob_start(); |
|
| 2243 | - $this->formatter->block($this->scope); |
|
| 2244 | - $out = ob_get_clean(); |
|
| 2245 | - setlocale(LC_NUMERIC, $locale); |
|
| 2246 | - return $out; |
|
| 2247 | - } |
|
| 2248 | - |
|
| 2249 | - public function compileFile($fname, $outFname = null) |
|
| 2250 | - { |
|
| 2251 | - if (!is_readable($fname)) { |
|
| 2252 | - throw new Exception('load error: failed to find '.$fname); |
|
| 2253 | - } |
|
| 2254 | - |
|
| 2255 | - $pi = pathinfo($fname); |
|
| 2256 | - |
|
| 2257 | - $oldImport = $this->importDir; |
|
| 2258 | - |
|
| 2259 | - $this->importDir = (array) $this->importDir; |
|
| 2260 | - $this->importDir[] = $pi['dirname'].'/'; |
|
| 2261 | - |
|
| 2262 | - $this->addParsedFile($fname); |
|
| 2263 | - |
|
| 2264 | - $out = $this->compile(file_get_contents($fname), $fname); |
|
| 2265 | - |
|
| 2266 | - $this->importDir = $oldImport; |
|
| 2267 | - |
|
| 2268 | - if ($outFname !== null) { |
|
| 2269 | - return file_put_contents($outFname, $out); |
|
| 2270 | - } |
|
| 2271 | - |
|
| 2272 | - return $out; |
|
| 2273 | - } |
|
| 2274 | - |
|
| 2275 | - // compile only if changed input has changed or output doesn't exist |
|
| 2276 | - public function checkedCompile($in, $out) |
|
| 2277 | - { |
|
| 2278 | - if (!is_file($out) || filemtime($in) > filemtime($out)) { |
|
| 2279 | - $this->compileFile($in, $out); |
|
| 2280 | - return true; |
|
| 2281 | - } |
|
| 2282 | - return false; |
|
| 2283 | - } |
|
| 2284 | - |
|
| 2285 | - /** |
|
| 2286 | - * Execute lessphp on a .less file or a lessphp cache structure |
|
| 2287 | - * |
|
| 2288 | - * The lessphp cache structure contains information about a specific |
|
| 2289 | - * less file having been parsed. It can be used as a hint for future |
|
| 2290 | - * calls to determine whether or not a rebuild is required. |
|
| 2291 | - * |
|
| 2292 | - * The cache structure contains two important keys that may be used |
|
| 2293 | - * externally: |
|
| 2294 | - * |
|
| 2295 | - * compiled: The final compiled CSS |
|
| 2296 | - * updated: The time (in seconds) the CSS was last compiled |
|
| 2297 | - * |
|
| 2298 | - * The cache structure is a plain-ol' PHP associative array and can |
|
| 2299 | - * be serialized and unserialized without a hitch. |
|
| 2300 | - * |
|
| 2301 | - * @param mixed $in Input |
|
| 2302 | - * @param bool $force Force rebuild? |
|
| 2303 | - * @return array|null lessphp cache structure |
|
| 2304 | - */ |
|
| 2305 | - public function cachedCompile($in, $force = false) |
|
| 2306 | - { |
|
| 2307 | - // assume no root |
|
| 2308 | - $root = null; |
|
| 2309 | - |
|
| 2310 | - if (is_string($in)) { |
|
| 2311 | - $root = $in; |
|
| 2312 | - } elseif (is_array($in) && isset($in['root'])) { |
|
| 2313 | - if ($force || !isset($in['files'])) { |
|
| 2314 | - // If we are forcing a recompile or if for some reason the |
|
| 2315 | - // structure does not contain any file information we should |
|
| 2316 | - // specify the root to trigger a rebuild. |
|
| 2317 | - $root = $in['root']; |
|
| 2318 | - } elseif (isset($in['files']) && is_array($in['files'])) { |
|
| 2319 | - foreach ($in['files'] as $fname => $ftime) { |
|
| 2320 | - if (!file_exists($fname) || filemtime($fname) > $ftime) { |
|
| 2321 | - // One of the files we knew about previously has changed |
|
| 2322 | - // so we should look at our incoming root again. |
|
| 2323 | - $root = $in['root']; |
|
| 2324 | - break; |
|
| 2325 | - } |
|
| 2326 | - } |
|
| 2327 | - } |
|
| 2328 | - } else { |
|
| 2329 | - // TODO: Throw an exception? We got neither a string nor something |
|
| 2330 | - // that looks like a compatible lessphp cache structure. |
|
| 2331 | - return null; |
|
| 2332 | - } |
|
| 2333 | - |
|
| 2334 | - if ($root !== null) { |
|
| 2335 | - // If we have a root value which means we should rebuild. |
|
| 2336 | - $out = array(); |
|
| 2337 | - $out['root'] = $root; |
|
| 2338 | - $out['compiled'] = $this->compileFile($root); |
|
| 2339 | - $out['files'] = $this->allParsedFiles(); |
|
| 2340 | - $out['updated'] = time(); |
|
| 2341 | - return $out; |
|
| 2342 | - } else { |
|
| 2343 | - // No changes, pass back the structure |
|
| 2344 | - // we were given initially. |
|
| 2345 | - return $in; |
|
| 2346 | - } |
|
| 2347 | - } |
|
| 2348 | - |
|
| 2349 | - // parse and compile buffer |
|
| 2350 | - // This is deprecated |
|
| 2351 | - public function parse($str = null, $initialVariables = null) |
|
| 2352 | - { |
|
| 2353 | - if (is_array($str)) { |
|
| 2354 | - $initialVariables = $str; |
|
| 2355 | - $str = null; |
|
| 2356 | - } |
|
| 2357 | - |
|
| 2358 | - $oldVars = $this->registeredVars; |
|
| 2359 | - if ($initialVariables !== null) { |
|
| 2360 | - $this->setVariables($initialVariables); |
|
| 2361 | - } |
|
| 2362 | - |
|
| 2363 | - if ($str == null) { |
|
| 2364 | - if (empty($this->_parseFile)) { |
|
| 2365 | - throw new exception("nothing to parse"); |
|
| 2366 | - } |
|
| 2367 | - |
|
| 2368 | - $out = $this->compileFile($this->_parseFile); |
|
| 2369 | - } else { |
|
| 2370 | - $out = $this->compile($str); |
|
| 2371 | - } |
|
| 2372 | - |
|
| 2373 | - $this->registeredVars = $oldVars; |
|
| 2374 | - return $out; |
|
| 2375 | - } |
|
| 2376 | - |
|
| 2377 | - protected function makeParser($name) |
|
| 2378 | - { |
|
| 2379 | - $parser = new lessc_parser($this, $name); |
|
| 2380 | - $parser->writeComments = $this->preserveComments; |
|
| 2381 | - |
|
| 2382 | - return $parser; |
|
| 2383 | - } |
|
| 2384 | - |
|
| 2385 | - public function setFormatter($name) |
|
| 2386 | - { |
|
| 2387 | - $this->formatterName = $name; |
|
| 2388 | - } |
|
| 2389 | - |
|
| 2390 | - protected function newFormatter() |
|
| 2391 | - { |
|
| 2392 | - $className = "lessc_formatter_lessjs"; |
|
| 2393 | - if (!empty($this->formatterName)) { |
|
| 2394 | - if (!is_string($this->formatterName)) { |
|
| 2395 | - return $this->formatterName; |
|
| 2396 | - } |
|
| 2397 | - $className = "lessc_formatter_$this->formatterName"; |
|
| 2398 | - } |
|
| 2399 | - |
|
| 2400 | - return new $className(); |
|
| 2401 | - } |
|
| 2402 | - |
|
| 2403 | - public function setPreserveComments($preserve) |
|
| 2404 | - { |
|
| 2405 | - $this->preserveComments = $preserve; |
|
| 2406 | - } |
|
| 2407 | - |
|
| 2408 | - public function registerFunction($name, $func) |
|
| 2409 | - { |
|
| 2410 | - $this->libFunctions[$name] = $func; |
|
| 2411 | - } |
|
| 2412 | - |
|
| 2413 | - public function unregisterFunction($name) |
|
| 2414 | - { |
|
| 2415 | - unset($this->libFunctions[$name]); |
|
| 2416 | - } |
|
| 2417 | - |
|
| 2418 | - public function setVariables($variables) |
|
| 2419 | - { |
|
| 2420 | - $this->registeredVars = array_merge($this->registeredVars, $variables); |
|
| 2421 | - } |
|
| 2422 | - |
|
| 2423 | - public function unsetVariable($name) |
|
| 2424 | - { |
|
| 2425 | - unset($this->registeredVars[$name]); |
|
| 2426 | - } |
|
| 2427 | - |
|
| 2428 | - public function setImportDir($dirs) |
|
| 2429 | - { |
|
| 2430 | - $this->importDir = (array) $dirs; |
|
| 2431 | - } |
|
| 2432 | - |
|
| 2433 | - public function addImportDir($dir) |
|
| 2434 | - { |
|
| 2435 | - $this->importDir = (array) $this->importDir; |
|
| 2436 | - $this->importDir[] = $dir; |
|
| 2437 | - } |
|
| 2438 | - |
|
| 2439 | - public function allParsedFiles() |
|
| 2440 | - { |
|
| 2441 | - return $this->allParsedFiles; |
|
| 2442 | - } |
|
| 2443 | - |
|
| 2444 | - public function addParsedFile($file) |
|
| 2445 | - { |
|
| 2446 | - $this->allParsedFiles[realpath($file)] = filemtime($file); |
|
| 2447 | - } |
|
| 2448 | - |
|
| 2449 | - /** |
|
| 2450 | - * Uses the current value of $this->count to show line and line number |
|
| 2451 | - */ |
|
| 2452 | - public function throwError($msg = null) |
|
| 2453 | - { |
|
| 2454 | - if ($this->sourceLoc >= 0) { |
|
| 2455 | - $this->sourceParser->throwError($msg, $this->sourceLoc); |
|
| 2456 | - } |
|
| 2457 | - throw new exception($msg); |
|
| 2458 | - } |
|
| 2459 | - |
|
| 2460 | - // compile file $in to file $out if $in is newer than $out |
|
| 2461 | - // returns true when it compiles, false otherwise |
|
| 2462 | - public static function ccompile($in, $out, $less = null) |
|
| 2463 | - { |
|
| 2464 | - if ($less === null) { |
|
| 2465 | - $less = new self(); |
|
| 2466 | - } |
|
| 2467 | - return $less->checkedCompile($in, $out); |
|
| 2468 | - } |
|
| 2469 | - |
|
| 2470 | - public static function cexecute($in, $force = false, $less = null) |
|
| 2471 | - { |
|
| 2472 | - if ($less === null) { |
|
| 2473 | - $less = new self(); |
|
| 2474 | - } |
|
| 2475 | - return $less->cachedCompile($in, $force); |
|
| 2476 | - } |
|
| 2477 | - |
|
| 2478 | - protected static $cssColors = array( |
|
| 2479 | - 'aliceblue' => '240,248,255', |
|
| 2480 | - 'antiquewhite' => '250,235,215', |
|
| 2481 | - 'aqua' => '0,255,255', |
|
| 2482 | - 'aquamarine' => '127,255,212', |
|
| 2483 | - 'azure' => '240,255,255', |
|
| 2484 | - 'beige' => '245,245,220', |
|
| 2485 | - 'bisque' => '255,228,196', |
|
| 2486 | - 'black' => '0,0,0', |
|
| 2487 | - 'blanchedalmond' => '255,235,205', |
|
| 2488 | - 'blue' => '0,0,255', |
|
| 2489 | - 'blueviolet' => '138,43,226', |
|
| 2490 | - 'brown' => '165,42,42', |
|
| 2491 | - 'burlywood' => '222,184,135', |
|
| 2492 | - 'cadetblue' => '95,158,160', |
|
| 2493 | - 'chartreuse' => '127,255,0', |
|
| 2494 | - 'chocolate' => '210,105,30', |
|
| 2495 | - 'coral' => '255,127,80', |
|
| 2496 | - 'cornflowerblue' => '100,149,237', |
|
| 2497 | - 'cornsilk' => '255,248,220', |
|
| 2498 | - 'crimson' => '220,20,60', |
|
| 2499 | - 'cyan' => '0,255,255', |
|
| 2500 | - 'darkblue' => '0,0,139', |
|
| 2501 | - 'darkcyan' => '0,139,139', |
|
| 2502 | - 'darkgoldenrod' => '184,134,11', |
|
| 2503 | - 'darkgray' => '169,169,169', |
|
| 2504 | - 'darkgreen' => '0,100,0', |
|
| 2505 | - 'darkgrey' => '169,169,169', |
|
| 2506 | - 'darkkhaki' => '189,183,107', |
|
| 2507 | - 'darkmagenta' => '139,0,139', |
|
| 2508 | - 'darkolivegreen' => '85,107,47', |
|
| 2509 | - 'darkorange' => '255,140,0', |
|
| 2510 | - 'darkorchid' => '153,50,204', |
|
| 2511 | - 'darkred' => '139,0,0', |
|
| 2512 | - 'darksalmon' => '233,150,122', |
|
| 2513 | - 'darkseagreen' => '143,188,143', |
|
| 2514 | - 'darkslateblue' => '72,61,139', |
|
| 2515 | - 'darkslategray' => '47,79,79', |
|
| 2516 | - 'darkslategrey' => '47,79,79', |
|
| 2517 | - 'darkturquoise' => '0,206,209', |
|
| 2518 | - 'darkviolet' => '148,0,211', |
|
| 2519 | - 'deeppink' => '255,20,147', |
|
| 2520 | - 'deepskyblue' => '0,191,255', |
|
| 2521 | - 'dimgray' => '105,105,105', |
|
| 2522 | - 'dimgrey' => '105,105,105', |
|
| 2523 | - 'dodgerblue' => '30,144,255', |
|
| 2524 | - 'firebrick' => '178,34,34', |
|
| 2525 | - 'floralwhite' => '255,250,240', |
|
| 2526 | - 'forestgreen' => '34,139,34', |
|
| 2527 | - 'fuchsia' => '255,0,255', |
|
| 2528 | - 'gainsboro' => '220,220,220', |
|
| 2529 | - 'ghostwhite' => '248,248,255', |
|
| 2530 | - 'gold' => '255,215,0', |
|
| 2531 | - 'goldenrod' => '218,165,32', |
|
| 2532 | - 'gray' => '128,128,128', |
|
| 2533 | - 'green' => '0,128,0', |
|
| 2534 | - 'greenyellow' => '173,255,47', |
|
| 2535 | - 'grey' => '128,128,128', |
|
| 2536 | - 'honeydew' => '240,255,240', |
|
| 2537 | - 'hotpink' => '255,105,180', |
|
| 2538 | - 'indianred' => '205,92,92', |
|
| 2539 | - 'indigo' => '75,0,130', |
|
| 2540 | - 'ivory' => '255,255,240', |
|
| 2541 | - 'khaki' => '240,230,140', |
|
| 2542 | - 'lavender' => '230,230,250', |
|
| 2543 | - 'lavenderblush' => '255,240,245', |
|
| 2544 | - 'lawngreen' => '124,252,0', |
|
| 2545 | - 'lemonchiffon' => '255,250,205', |
|
| 2546 | - 'lightblue' => '173,216,230', |
|
| 2547 | - 'lightcoral' => '240,128,128', |
|
| 2548 | - 'lightcyan' => '224,255,255', |
|
| 2549 | - 'lightgoldenrodyellow' => '250,250,210', |
|
| 2550 | - 'lightgray' => '211,211,211', |
|
| 2551 | - 'lightgreen' => '144,238,144', |
|
| 2552 | - 'lightgrey' => '211,211,211', |
|
| 2553 | - 'lightpink' => '255,182,193', |
|
| 2554 | - 'lightsalmon' => '255,160,122', |
|
| 2555 | - 'lightseagreen' => '32,178,170', |
|
| 2556 | - 'lightskyblue' => '135,206,250', |
|
| 2557 | - 'lightslategray' => '119,136,153', |
|
| 2558 | - 'lightslategrey' => '119,136,153', |
|
| 2559 | - 'lightsteelblue' => '176,196,222', |
|
| 2560 | - 'lightyellow' => '255,255,224', |
|
| 2561 | - 'lime' => '0,255,0', |
|
| 2562 | - 'limegreen' => '50,205,50', |
|
| 2563 | - 'linen' => '250,240,230', |
|
| 2564 | - 'magenta' => '255,0,255', |
|
| 2565 | - 'maroon' => '128,0,0', |
|
| 2566 | - 'mediumaquamarine' => '102,205,170', |
|
| 2567 | - 'mediumblue' => '0,0,205', |
|
| 2568 | - 'mediumorchid' => '186,85,211', |
|
| 2569 | - 'mediumpurple' => '147,112,219', |
|
| 2570 | - 'mediumseagreen' => '60,179,113', |
|
| 2571 | - 'mediumslateblue' => '123,104,238', |
|
| 2572 | - 'mediumspringgreen' => '0,250,154', |
|
| 2573 | - 'mediumturquoise' => '72,209,204', |
|
| 2574 | - 'mediumvioletred' => '199,21,133', |
|
| 2575 | - 'midnightblue' => '25,25,112', |
|
| 2576 | - 'mintcream' => '245,255,250', |
|
| 2577 | - 'mistyrose' => '255,228,225', |
|
| 2578 | - 'moccasin' => '255,228,181', |
|
| 2579 | - 'navajowhite' => '255,222,173', |
|
| 2580 | - 'navy' => '0,0,128', |
|
| 2581 | - 'oldlace' => '253,245,230', |
|
| 2582 | - 'olive' => '128,128,0', |
|
| 2583 | - 'olivedrab' => '107,142,35', |
|
| 2584 | - 'orange' => '255,165,0', |
|
| 2585 | - 'orangered' => '255,69,0', |
|
| 2586 | - 'orchid' => '218,112,214', |
|
| 2587 | - 'palegoldenrod' => '238,232,170', |
|
| 2588 | - 'palegreen' => '152,251,152', |
|
| 2589 | - 'paleturquoise' => '175,238,238', |
|
| 2590 | - 'palevioletred' => '219,112,147', |
|
| 2591 | - 'papayawhip' => '255,239,213', |
|
| 2592 | - 'peachpuff' => '255,218,185', |
|
| 2593 | - 'peru' => '205,133,63', |
|
| 2594 | - 'pink' => '255,192,203', |
|
| 2595 | - 'plum' => '221,160,221', |
|
| 2596 | - 'powderblue' => '176,224,230', |
|
| 2597 | - 'purple' => '128,0,128', |
|
| 2598 | - 'red' => '255,0,0', |
|
| 2599 | - 'rosybrown' => '188,143,143', |
|
| 2600 | - 'royalblue' => '65,105,225', |
|
| 2601 | - 'saddlebrown' => '139,69,19', |
|
| 2602 | - 'salmon' => '250,128,114', |
|
| 2603 | - 'sandybrown' => '244,164,96', |
|
| 2604 | - 'seagreen' => '46,139,87', |
|
| 2605 | - 'seashell' => '255,245,238', |
|
| 2606 | - 'sienna' => '160,82,45', |
|
| 2607 | - 'silver' => '192,192,192', |
|
| 2608 | - 'skyblue' => '135,206,235', |
|
| 2609 | - 'slateblue' => '106,90,205', |
|
| 2610 | - 'slategray' => '112,128,144', |
|
| 2611 | - 'slategrey' => '112,128,144', |
|
| 2612 | - 'snow' => '255,250,250', |
|
| 2613 | - 'springgreen' => '0,255,127', |
|
| 2614 | - 'steelblue' => '70,130,180', |
|
| 2615 | - 'tan' => '210,180,140', |
|
| 2616 | - 'teal' => '0,128,128', |
|
| 2617 | - 'thistle' => '216,191,216', |
|
| 2618 | - 'tomato' => '255,99,71', |
|
| 2619 | - 'transparent' => '0,0,0,0', |
|
| 2620 | - 'turquoise' => '64,224,208', |
|
| 2621 | - 'violet' => '238,130,238', |
|
| 2622 | - 'wheat' => '245,222,179', |
|
| 2623 | - 'white' => '255,255,255', |
|
| 2624 | - 'whitesmoke' => '245,245,245', |
|
| 2625 | - 'yellow' => '255,255,0', |
|
| 2626 | - 'yellowgreen' => '154,205,50' |
|
| 2627 | - ); |
|
| 62 | + public static $VERSION = "v0.8.0"; |
|
| 63 | + |
|
| 64 | + public static $TRUE = array("keyword", "true"); |
|
| 65 | + public static $FALSE = array("keyword", "false"); |
|
| 66 | + |
|
| 67 | + protected $libFunctions = array(); |
|
| 68 | + protected $registeredVars = array(); |
|
| 69 | + protected $preserveComments = false; |
|
| 70 | + |
|
| 71 | + public $vPrefix = '@'; // prefix of abstract properties |
|
| 72 | + public $mPrefix = '$'; // prefix of abstract blocks |
|
| 73 | + public $parentSelector = '&'; |
|
| 74 | + |
|
| 75 | + public $importDisabled = false; |
|
| 76 | + public $importDir = ''; |
|
| 77 | + |
|
| 78 | + public $scope; |
|
| 79 | + public $formatter; |
|
| 80 | + public $formatterName; |
|
| 81 | + public $parser; |
|
| 82 | + public $_parseFile; |
|
| 83 | + public $env; |
|
| 84 | + public $count; |
|
| 85 | + |
|
| 86 | + protected $numberPrecision = null; |
|
| 87 | + |
|
| 88 | + protected $allParsedFiles = array(); |
|
| 89 | + |
|
| 90 | + // set to the parser that generated the current line when compiling |
|
| 91 | + // so we know how to create error messages |
|
| 92 | + protected $sourceParser = null; |
|
| 93 | + protected $sourceLoc = null; |
|
| 94 | + |
|
| 95 | + protected static $nextImportId = 0; // uniquely identify imports |
|
| 96 | + |
|
| 97 | + // attempts to find the path of an import url, returns null for css files |
|
| 98 | + protected function findImport($url) |
|
| 99 | + { |
|
| 100 | + foreach ((array) $this->importDir as $dir) { |
|
| 101 | + $full = $dir.(substr($dir, -1) != '/' ? '/' : '').$url; |
|
| 102 | + if ($this->fileExists($file = $full.'.less') || $this->fileExists($file = $full)) { |
|
| 103 | + return $file; |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + return null; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * fileExists |
|
| 112 | + * |
|
| 113 | + * @param string $name Filename |
|
| 114 | + * @return boolean |
|
| 115 | + */ |
|
| 116 | + protected function fileExists($name) |
|
| 117 | + { |
|
| 118 | + return is_file($name); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + public static function compressList($items, $delim) |
|
| 122 | + { |
|
| 123 | + if (!isset($items[1]) && isset($items[0])) { |
|
| 124 | + return $items[0]; |
|
| 125 | + } else { |
|
| 126 | + return array('list', $delim, $items); |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + public static function preg_quote($what) |
|
| 131 | + { |
|
| 132 | + return preg_quote($what, '/'); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + protected function tryImport($importPath, $parentBlock, $out) |
|
| 136 | + { |
|
| 137 | + if ($importPath[0] == "function" && $importPath[1] == "url") { |
|
| 138 | + $importPath = $this->flattenList($importPath[2]); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + $str = $this->coerceString($importPath); |
|
| 142 | + if ($str === null) { |
|
| 143 | + return false; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + $url = $this->compileValue($this->lib_e($str)); |
|
| 147 | + |
|
| 148 | + // don't import if it ends in css |
|
| 149 | + if (substr_compare($url, '.css', -4, 4) === 0) { |
|
| 150 | + return false; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + $realPath = $this->findImport($url); |
|
| 154 | + |
|
| 155 | + if ($realPath === null) { |
|
| 156 | + return false; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + if ($this->importDisabled) { |
|
| 160 | + return array(false, "/* import disabled */"); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + if (isset($this->allParsedFiles[realpath($realPath)])) { |
|
| 164 | + return array(false, null); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + $this->addParsedFile($realPath); |
|
| 168 | + $parser = $this->makeParser($realPath); |
|
| 169 | + $root = $parser->parse(file_get_contents($realPath)); |
|
| 170 | + |
|
| 171 | + // set the parents of all the block props |
|
| 172 | + foreach ($root->props as $prop) { |
|
| 173 | + if ($prop[0] == "block") { |
|
| 174 | + $prop[1]->parent = $parentBlock; |
|
| 175 | + } |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + // copy mixins into scope, set their parents |
|
| 179 | + // bring blocks from import into current block |
|
| 180 | + // TODO: need to mark the source parser these came from this file |
|
| 181 | + foreach ($root->children as $childName => $child) { |
|
| 182 | + if (isset($parentBlock->children[$childName])) { |
|
| 183 | + $parentBlock->children[$childName] = array_merge( |
|
| 184 | + $parentBlock->children[$childName], |
|
| 185 | + $child |
|
| 186 | + ); |
|
| 187 | + } else { |
|
| 188 | + $parentBlock->children[$childName] = $child; |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + $pi = pathinfo($realPath); |
|
| 193 | + $dir = $pi["dirname"]; |
|
| 194 | + |
|
| 195 | + list($top, $bottom) = $this->sortProps($root->props, true); |
|
| 196 | + $this->compileImportedProps($top, $parentBlock, $out, $parser, $dir); |
|
| 197 | + |
|
| 198 | + return array(true, $bottom, $parser, $dir); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + protected function compileImportedProps($props, $block, $out, $sourceParser, $importDir) |
|
| 202 | + { |
|
| 203 | + $oldSourceParser = $this->sourceParser; |
|
| 204 | + |
|
| 205 | + $oldImport = $this->importDir; |
|
| 206 | + |
|
| 207 | + // TODO: this is because the importDir api is stupid |
|
| 208 | + $this->importDir = (array) $this->importDir; |
|
| 209 | + array_unshift($this->importDir, $importDir); |
|
| 210 | + |
|
| 211 | + foreach ($props as $prop) { |
|
| 212 | + $this->compileProp($prop, $block, $out); |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + $this->importDir = $oldImport; |
|
| 216 | + $this->sourceParser = $oldSourceParser; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * Recursively compiles a block. |
|
| 221 | + * |
|
| 222 | + * A block is analogous to a CSS block in most cases. A single LESS document |
|
| 223 | + * is encapsulated in a block when parsed, but it does not have parent tags |
|
| 224 | + * so all of it's children appear on the root level when compiled. |
|
| 225 | + * |
|
| 226 | + * Blocks are made up of props and children. |
|
| 227 | + * |
|
| 228 | + * Props are property instructions, array tuples which describe an action |
|
| 229 | + * to be taken, eg. write a property, set a variable, mixin a block. |
|
| 230 | + * |
|
| 231 | + * The children of a block are just all the blocks that are defined within. |
|
| 232 | + * This is used to look up mixins when performing a mixin. |
|
| 233 | + * |
|
| 234 | + * Compiling the block involves pushing a fresh environment on the stack, |
|
| 235 | + * and iterating through the props, compiling each one. |
|
| 236 | + * |
|
| 237 | + * See Lessc::compileProp() |
|
| 238 | + * |
|
| 239 | + */ |
|
| 240 | + protected function compileBlock($block) |
|
| 241 | + { |
|
| 242 | + switch ($block->type) { |
|
| 243 | + case "root": |
|
| 244 | + $this->compileRoot($block); |
|
| 245 | + break; |
|
| 246 | + case null: |
|
| 247 | + $this->compileCSSBlock($block); |
|
| 248 | + break; |
|
| 249 | + case "media": |
|
| 250 | + $this->compileMedia($block); |
|
| 251 | + break; |
|
| 252 | + case "directive": |
|
| 253 | + $name = "@".$block->name; |
|
| 254 | + if (!empty($block->value)) { |
|
| 255 | + $name .= " ".$this->compileValue($this->reduce($block->value)); |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + $this->compileNestedBlock($block, array($name)); |
|
| 259 | + break; |
|
| 260 | + default: |
|
| 261 | + $this->throwError("unknown block type: $block->type\n"); |
|
| 262 | + } |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + protected function compileCSSBlock($block) |
|
| 266 | + { |
|
| 267 | + $env = $this->pushEnv(); |
|
| 268 | + |
|
| 269 | + $selectors = $this->compileSelectors($block->tags); |
|
| 270 | + $env->selectors = $this->multiplySelectors($selectors); |
|
| 271 | + $out = $this->makeOutputBlock(null, $env->selectors); |
|
| 272 | + |
|
| 273 | + $this->scope->children[] = $out; |
|
| 274 | + $this->compileProps($block, $out); |
|
| 275 | + |
|
| 276 | + $block->scope = $env; // mixins carry scope with them! |
|
| 277 | + $this->popEnv(); |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + protected function compileMedia($media) |
|
| 281 | + { |
|
| 282 | + $env = $this->pushEnv($media); |
|
| 283 | + $parentScope = $this->mediaParent($this->scope); |
|
| 284 | + |
|
| 285 | + $query = $this->compileMediaQuery($this->multiplyMedia($env)); |
|
| 286 | + |
|
| 287 | + $this->scope = $this->makeOutputBlock($media->type, array($query)); |
|
| 288 | + $parentScope->children[] = $this->scope; |
|
| 289 | + |
|
| 290 | + $this->compileProps($media, $this->scope); |
|
| 291 | + |
|
| 292 | + if (count($this->scope->lines) > 0) { |
|
| 293 | + $orphanSelelectors = $this->findClosestSelectors(); |
|
| 294 | + if (!is_null($orphanSelelectors)) { |
|
| 295 | + $orphan = $this->makeOutputBlock(null, $orphanSelelectors); |
|
| 296 | + $orphan->lines = $this->scope->lines; |
|
| 297 | + array_unshift($this->scope->children, $orphan); |
|
| 298 | + $this->scope->lines = array(); |
|
| 299 | + } |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + $this->scope = $this->scope->parent; |
|
| 303 | + $this->popEnv(); |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + protected function mediaParent($scope) |
|
| 307 | + { |
|
| 308 | + while (!empty($scope->parent)) { |
|
| 309 | + if (!empty($scope->type) && $scope->type != "media") { |
|
| 310 | + break; |
|
| 311 | + } |
|
| 312 | + $scope = $scope->parent; |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + return $scope; |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + protected function compileNestedBlock($block, $selectors) |
|
| 319 | + { |
|
| 320 | + $this->pushEnv($block); |
|
| 321 | + $this->scope = $this->makeOutputBlock($block->type, $selectors); |
|
| 322 | + $this->scope->parent->children[] = $this->scope; |
|
| 323 | + |
|
| 324 | + $this->compileProps($block, $this->scope); |
|
| 325 | + |
|
| 326 | + $this->scope = $this->scope->parent; |
|
| 327 | + $this->popEnv(); |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + protected function compileRoot($root) |
|
| 331 | + { |
|
| 332 | + $this->pushEnv(); |
|
| 333 | + $this->scope = $this->makeOutputBlock($root->type); |
|
| 334 | + $this->compileProps($root, $this->scope); |
|
| 335 | + $this->popEnv(); |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + protected function compileProps($block, $out) |
|
| 339 | + { |
|
| 340 | + foreach ($this->sortProps($block->props) as $prop) { |
|
| 341 | + $this->compileProp($prop, $block, $out); |
|
| 342 | + } |
|
| 343 | + $out->lines = $this->deduplicate($out->lines); |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + /** |
|
| 347 | + * Deduplicate lines in a block. Comments are not deduplicated. If a |
|
| 348 | + * duplicate rule is detected, the comments immediately preceding each |
|
| 349 | + * occurence are consolidated. |
|
| 350 | + */ |
|
| 351 | + protected function deduplicate($lines) |
|
| 352 | + { |
|
| 353 | + $unique = array(); |
|
| 354 | + $comments = array(); |
|
| 355 | + |
|
| 356 | + foreach ($lines as $line) { |
|
| 357 | + if (strpos($line, '/*') === 0) { |
|
| 358 | + $comments[] = $line; |
|
| 359 | + continue; |
|
| 360 | + } |
|
| 361 | + if (!in_array($line, $unique)) { |
|
| 362 | + $unique[] = $line; |
|
| 363 | + } |
|
| 364 | + array_splice($unique, array_search($line, $unique), 0, $comments); |
|
| 365 | + $comments = array(); |
|
| 366 | + } |
|
| 367 | + return array_merge($unique, $comments); |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + protected function sortProps($props, $split = false) |
|
| 371 | + { |
|
| 372 | + $vars = array(); |
|
| 373 | + $imports = array(); |
|
| 374 | + $other = array(); |
|
| 375 | + $stack = array(); |
|
| 376 | + |
|
| 377 | + foreach ($props as $prop) { |
|
| 378 | + switch ($prop[0]) { |
|
| 379 | + case "comment": |
|
| 380 | + $stack[] = $prop; |
|
| 381 | + break; |
|
| 382 | + case "assign": |
|
| 383 | + $stack[] = $prop; |
|
| 384 | + if (isset($prop[1][0]) && $prop[1][0] == $this->vPrefix) { |
|
| 385 | + $vars = array_merge($vars, $stack); |
|
| 386 | + } else { |
|
| 387 | + $other = array_merge($other, $stack); |
|
| 388 | + } |
|
| 389 | + $stack = array(); |
|
| 390 | + break; |
|
| 391 | + case "import": |
|
| 392 | + $id = self::$nextImportId++; |
|
| 393 | + $prop[] = $id; |
|
| 394 | + $stack[] = $prop; |
|
| 395 | + $imports = array_merge($imports, $stack); |
|
| 396 | + $other[] = array("import_mixin", $id); |
|
| 397 | + $stack = array(); |
|
| 398 | + break; |
|
| 399 | + default: |
|
| 400 | + $stack[] = $prop; |
|
| 401 | + $other = array_merge($other, $stack); |
|
| 402 | + $stack = array(); |
|
| 403 | + break; |
|
| 404 | + } |
|
| 405 | + } |
|
| 406 | + $other = array_merge($other, $stack); |
|
| 407 | + |
|
| 408 | + if ($split) { |
|
| 409 | + return array(array_merge($imports, $vars), $other); |
|
| 410 | + } else { |
|
| 411 | + return array_merge($imports, $vars, $other); |
|
| 412 | + } |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + protected function compileMediaQuery($queries) |
|
| 416 | + { |
|
| 417 | + $compiledQueries = array(); |
|
| 418 | + foreach ($queries as $query) { |
|
| 419 | + $parts = array(); |
|
| 420 | + foreach ($query as $q) { |
|
| 421 | + switch ($q[0]) { |
|
| 422 | + case "mediaType": |
|
| 423 | + $parts[] = implode(" ", array_slice($q, 1)); |
|
| 424 | + break; |
|
| 425 | + case "mediaExp": |
|
| 426 | + if (isset($q[2])) { |
|
| 427 | + $parts[] = "($q[1]: ". |
|
| 428 | + $this->compileValue($this->reduce($q[2])).")"; |
|
| 429 | + } else { |
|
| 430 | + $parts[] = "($q[1])"; |
|
| 431 | + } |
|
| 432 | + break; |
|
| 433 | + case "variable": |
|
| 434 | + $parts[] = $this->compileValue($this->reduce($q)); |
|
| 435 | + break; |
|
| 436 | + } |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + if (count($parts) > 0) { |
|
| 440 | + $compiledQueries[] = implode(" and ", $parts); |
|
| 441 | + } |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + $out = "@media"; |
|
| 445 | + if (!empty($parts)) { |
|
| 446 | + $out .= " ". |
|
| 447 | + implode($this->formatter->selectorSeparator, $compiledQueries); |
|
| 448 | + } |
|
| 449 | + return $out; |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + protected function multiplyMedia($env, $childQueries = null) |
|
| 453 | + { |
|
| 454 | + if (is_null($env) || |
|
| 455 | + !empty($env->block->type) && $env->block->type != "media" |
|
| 456 | + ) { |
|
| 457 | + return $childQueries; |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + // plain old block, skip |
|
| 461 | + if (empty($env->block->type)) { |
|
| 462 | + return $this->multiplyMedia($env->parent, $childQueries); |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + $out = array(); |
|
| 466 | + $queries = $env->block->queries; |
|
| 467 | + if (is_null($childQueries)) { |
|
| 468 | + $out = $queries; |
|
| 469 | + } else { |
|
| 470 | + foreach ($queries as $parent) { |
|
| 471 | + foreach ($childQueries as $child) { |
|
| 472 | + $out[] = array_merge($parent, $child); |
|
| 473 | + } |
|
| 474 | + } |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + return $this->multiplyMedia($env->parent, $out); |
|
| 478 | + } |
|
| 479 | + |
|
| 480 | + protected function expandParentSelectors(&$tag, $replace) |
|
| 481 | + { |
|
| 482 | + $parts = explode("$&$", $tag); |
|
| 483 | + $count = 0; |
|
| 484 | + foreach ($parts as &$part) { |
|
| 485 | + $c = 0; |
|
| 486 | + $part = str_replace($this->parentSelector, $replace, $part, $c); |
|
| 487 | + $count += $c; |
|
| 488 | + } |
|
| 489 | + $tag = implode($this->parentSelector, $parts); |
|
| 490 | + return $count; |
|
| 491 | + } |
|
| 492 | + |
|
| 493 | + protected function findClosestSelectors() |
|
| 494 | + { |
|
| 495 | + $env = $this->env; |
|
| 496 | + $selectors = null; |
|
| 497 | + while ($env !== null) { |
|
| 498 | + if (isset($env->selectors)) { |
|
| 499 | + $selectors = $env->selectors; |
|
| 500 | + break; |
|
| 501 | + } |
|
| 502 | + $env = $env->parent; |
|
| 503 | + } |
|
| 504 | + |
|
| 505 | + return $selectors; |
|
| 506 | + } |
|
| 507 | + |
|
| 508 | + |
|
| 509 | + // multiply $selectors against the nearest selectors in env |
|
| 510 | + protected function multiplySelectors($selectors) |
|
| 511 | + { |
|
| 512 | + // find parent selectors |
|
| 513 | + |
|
| 514 | + $parentSelectors = $this->findClosestSelectors(); |
|
| 515 | + if (is_null($parentSelectors)) { |
|
| 516 | + // kill parent reference in top level selector |
|
| 517 | + foreach ($selectors as &$s) { |
|
| 518 | + $this->expandParentSelectors($s, ""); |
|
| 519 | + } |
|
| 520 | + |
|
| 521 | + return $selectors; |
|
| 522 | + } |
|
| 523 | + |
|
| 524 | + $out = array(); |
|
| 525 | + foreach ($parentSelectors as $parent) { |
|
| 526 | + foreach ($selectors as $child) { |
|
| 527 | + $count = $this->expandParentSelectors($child, $parent); |
|
| 528 | + |
|
| 529 | + // don't prepend the parent tag if & was used |
|
| 530 | + if ($count > 0) { |
|
| 531 | + $out[] = trim($child); |
|
| 532 | + } else { |
|
| 533 | + $out[] = trim($parent.' '.$child); |
|
| 534 | + } |
|
| 535 | + } |
|
| 536 | + } |
|
| 537 | + |
|
| 538 | + return $out; |
|
| 539 | + } |
|
| 540 | + |
|
| 541 | + // reduces selector expressions |
|
| 542 | + protected function compileSelectors($selectors) |
|
| 543 | + { |
|
| 544 | + $out = array(); |
|
| 545 | + |
|
| 546 | + foreach ($selectors as $s) { |
|
| 547 | + if (is_array($s)) { |
|
| 548 | + list(, $value) = $s; |
|
| 549 | + $out[] = trim($this->compileValue($this->reduce($value))); |
|
| 550 | + } else { |
|
| 551 | + $out[] = $s; |
|
| 552 | + } |
|
| 553 | + } |
|
| 554 | + |
|
| 555 | + return $out; |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + protected function eq($left, $right) |
|
| 559 | + { |
|
| 560 | + return $left == $right; |
|
| 561 | + } |
|
| 562 | + |
|
| 563 | + protected function patternMatch($block, $orderedArgs, $keywordArgs) |
|
| 564 | + { |
|
| 565 | + // match the guards if it has them |
|
| 566 | + // any one of the groups must have all its guards pass for a match |
|
| 567 | + if (!empty($block->guards)) { |
|
| 568 | + $groupPassed = false; |
|
| 569 | + foreach ($block->guards as $guardGroup) { |
|
| 570 | + foreach ($guardGroup as $guard) { |
|
| 571 | + $this->pushEnv(); |
|
| 572 | + $this->zipSetArgs($block->args, $orderedArgs, $keywordArgs); |
|
| 573 | + |
|
| 574 | + $negate = false; |
|
| 575 | + if ($guard[0] == "negate") { |
|
| 576 | + $guard = $guard[1]; |
|
| 577 | + $negate = true; |
|
| 578 | + } |
|
| 579 | + |
|
| 580 | + $passed = $this->reduce($guard) == self::$TRUE; |
|
| 581 | + if ($negate) { |
|
| 582 | + $passed = !$passed; |
|
| 583 | + } |
|
| 584 | + |
|
| 585 | + $this->popEnv(); |
|
| 586 | + |
|
| 587 | + if ($passed) { |
|
| 588 | + $groupPassed = true; |
|
| 589 | + } else { |
|
| 590 | + $groupPassed = false; |
|
| 591 | + break; |
|
| 592 | + } |
|
| 593 | + } |
|
| 594 | + |
|
| 595 | + if ($groupPassed) { |
|
| 596 | + break; |
|
| 597 | + } |
|
| 598 | + } |
|
| 599 | + |
|
| 600 | + if (!$groupPassed) { |
|
| 601 | + return false; |
|
| 602 | + } |
|
| 603 | + } |
|
| 604 | + |
|
| 605 | + if (empty($block->args)) { |
|
| 606 | + return $block->isVararg || empty($orderedArgs) && empty($keywordArgs); |
|
| 607 | + } |
|
| 608 | + |
|
| 609 | + $remainingArgs = $block->args; |
|
| 610 | + if ($keywordArgs) { |
|
| 611 | + $remainingArgs = array(); |
|
| 612 | + foreach ($block->args as $arg) { |
|
| 613 | + if ($arg[0] == "arg" && isset($keywordArgs[$arg[1]])) { |
|
| 614 | + continue; |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + $remainingArgs[] = $arg; |
|
| 618 | + } |
|
| 619 | + } |
|
| 620 | + |
|
| 621 | + $i = -1; // no args |
|
| 622 | + // try to match by arity or by argument literal |
|
| 623 | + foreach ($remainingArgs as $i => $arg) { |
|
| 624 | + switch ($arg[0]) { |
|
| 625 | + case "lit": |
|
| 626 | + if (empty($orderedArgs[$i]) || !$this->eq($arg[1], $orderedArgs[$i])) { |
|
| 627 | + return false; |
|
| 628 | + } |
|
| 629 | + break; |
|
| 630 | + case "arg": |
|
| 631 | + // no arg and no default value |
|
| 632 | + if (!isset($orderedArgs[$i]) && !isset($arg[2])) { |
|
| 633 | + return false; |
|
| 634 | + } |
|
| 635 | + break; |
|
| 636 | + case "rest": |
|
| 637 | + $i--; // rest can be empty |
|
| 638 | + break 2; |
|
| 639 | + } |
|
| 640 | + } |
|
| 641 | + |
|
| 642 | + if ($block->isVararg) { |
|
| 643 | + return true; // not having enough is handled above |
|
| 644 | + } else { |
|
| 645 | + $numMatched = $i + 1; |
|
| 646 | + // greater than because default values always match |
|
| 647 | + return $numMatched >= count($orderedArgs); |
|
| 648 | + } |
|
| 649 | + } |
|
| 650 | + |
|
| 651 | + protected function patternMatchAll($blocks, $orderedArgs, $keywordArgs, $skip = array()) |
|
| 652 | + { |
|
| 653 | + $matches = null; |
|
| 654 | + foreach ($blocks as $block) { |
|
| 655 | + // skip seen blocks that don't have arguments |
|
| 656 | + if (isset($skip[$block->id]) && !isset($block->args)) { |
|
| 657 | + continue; |
|
| 658 | + } |
|
| 659 | + |
|
| 660 | + if ($this->patternMatch($block, $orderedArgs, $keywordArgs)) { |
|
| 661 | + $matches[] = $block; |
|
| 662 | + } |
|
| 663 | + } |
|
| 664 | + |
|
| 665 | + return $matches; |
|
| 666 | + } |
|
| 667 | + |
|
| 668 | + // attempt to find blocks matched by path and args |
|
| 669 | + protected function findBlocks($searchIn, $path, $orderedArgs, $keywordArgs, $seen = array()) |
|
| 670 | + { |
|
| 671 | + if ($searchIn == null) { |
|
| 672 | + return null; |
|
| 673 | + } |
|
| 674 | + if (isset($seen[$searchIn->id])) { |
|
| 675 | + return null; |
|
| 676 | + } |
|
| 677 | + $seen[$searchIn->id] = true; |
|
| 678 | + |
|
| 679 | + $name = $path[0]; |
|
| 680 | + |
|
| 681 | + if (isset($searchIn->children[$name])) { |
|
| 682 | + $blocks = $searchIn->children[$name]; |
|
| 683 | + if (count($path) == 1) { |
|
| 684 | + $matches = $this->patternMatchAll($blocks, $orderedArgs, $keywordArgs, $seen); |
|
| 685 | + if (!empty($matches)) { |
|
| 686 | + // This will return all blocks that match in the closest |
|
| 687 | + // scope that has any matching block, like lessjs |
|
| 688 | + return $matches; |
|
| 689 | + } |
|
| 690 | + } else { |
|
| 691 | + $matches = array(); |
|
| 692 | + foreach ($blocks as $subBlock) { |
|
| 693 | + $subMatches = $this->findBlocks( |
|
| 694 | + $subBlock, |
|
| 695 | + array_slice($path, 1), |
|
| 696 | + $orderedArgs, |
|
| 697 | + $keywordArgs, |
|
| 698 | + $seen |
|
| 699 | + ); |
|
| 700 | + |
|
| 701 | + if (!is_null($subMatches)) { |
|
| 702 | + foreach ($subMatches as $sm) { |
|
| 703 | + $matches[] = $sm; |
|
| 704 | + } |
|
| 705 | + } |
|
| 706 | + } |
|
| 707 | + |
|
| 708 | + return count($matches) > 0 ? $matches : null; |
|
| 709 | + } |
|
| 710 | + } |
|
| 711 | + if ($searchIn->parent === $searchIn) { |
|
| 712 | + return null; |
|
| 713 | + } |
|
| 714 | + return $this->findBlocks($searchIn->parent, $path, $orderedArgs, $keywordArgs, $seen); |
|
| 715 | + } |
|
| 716 | + |
|
| 717 | + // sets all argument names in $args to either the default value |
|
| 718 | + // or the one passed in through $values |
|
| 719 | + protected function zipSetArgs($args, $orderedValues, $keywordValues) |
|
| 720 | + { |
|
| 721 | + $assignedValues = array(); |
|
| 722 | + |
|
| 723 | + $i = 0; |
|
| 724 | + foreach ($args as $a) { |
|
| 725 | + if ($a[0] == "arg") { |
|
| 726 | + if (isset($keywordValues[$a[1]])) { |
|
| 727 | + // has keyword arg |
|
| 728 | + $value = $keywordValues[$a[1]]; |
|
| 729 | + } elseif (isset($orderedValues[$i])) { |
|
| 730 | + // has ordered arg |
|
| 731 | + $value = $orderedValues[$i]; |
|
| 732 | + $i++; |
|
| 733 | + } elseif (isset($a[2])) { |
|
| 734 | + // has default value |
|
| 735 | + $value = $a[2]; |
|
| 736 | + } else { |
|
| 737 | + $value = null; // :( |
|
| 738 | + $this->throwError("Failed to assign arg ".$a[1]); // This ends function by throwing an exception |
|
| 739 | + } |
|
| 740 | + |
|
| 741 | + $value = $this->reduce($value); |
|
| 742 | + $this->set($a[1], $value); |
|
| 743 | + $assignedValues[] = $value; |
|
| 744 | + } else { |
|
| 745 | + // a lit |
|
| 746 | + $i++; |
|
| 747 | + } |
|
| 748 | + } |
|
| 749 | + |
|
| 750 | + // check for a rest |
|
| 751 | + $last = end($args); |
|
| 752 | + if ($last && $last[0] == "rest") { |
|
| 753 | + $rest = array_slice($orderedValues, count($args) - 1); |
|
| 754 | + $this->set($last[1], $this->reduce(array("list", " ", $rest))); |
|
| 755 | + } |
|
| 756 | + |
|
| 757 | + // wow is this the only true use of PHP's + operator for arrays? |
|
| 758 | + $this->env->arguments = $assignedValues + $orderedValues; |
|
| 759 | + } |
|
| 760 | + |
|
| 761 | + // compile a prop and update $lines or $blocks appropriately |
|
| 762 | + protected function compileProp($prop, $block, $out) |
|
| 763 | + { |
|
| 764 | + // set error position context |
|
| 765 | + $this->sourceLoc = isset($prop[-1]) ? $prop[-1] : -1; |
|
| 766 | + |
|
| 767 | + switch ($prop[0]) { |
|
| 768 | + case 'assign': |
|
| 769 | + list(, $name, $value) = $prop; |
|
| 770 | + if ($name[0] == $this->vPrefix) { |
|
| 771 | + $this->set($name, $value); |
|
| 772 | + } else { |
|
| 773 | + $out->lines[] = $this->formatter->property( |
|
| 774 | + $name, |
|
| 775 | + $this->compileValue($this->reduce($value)) |
|
| 776 | + ); |
|
| 777 | + } |
|
| 778 | + break; |
|
| 779 | + case 'block': |
|
| 780 | + list(, $child) = $prop; |
|
| 781 | + $this->compileBlock($child); |
|
| 782 | + break; |
|
| 783 | + case 'mixin': |
|
| 784 | + list(, $path, $args, $suffix) = $prop; |
|
| 785 | + |
|
| 786 | + $orderedArgs = array(); |
|
| 787 | + $keywordArgs = array(); |
|
| 788 | + foreach ((array) $args as $arg) { |
|
| 789 | + $argval = null; |
|
| 790 | + switch ($arg[0]) { |
|
| 791 | + case "arg": |
|
| 792 | + if (!isset($arg[2])) { |
|
| 793 | + $orderedArgs[] = $this->reduce(array("variable", $arg[1])); |
|
| 794 | + } else { |
|
| 795 | + $keywordArgs[$arg[1]] = $this->reduce($arg[2]); |
|
| 796 | + } |
|
| 797 | + break; |
|
| 798 | + |
|
| 799 | + case "lit": |
|
| 800 | + $orderedArgs[] = $this->reduce($arg[1]); |
|
| 801 | + break; |
|
| 802 | + default: |
|
| 803 | + $this->throwError("Unknown arg type: ".$arg[0]); |
|
| 804 | + } |
|
| 805 | + } |
|
| 806 | + |
|
| 807 | + $mixins = $this->findBlocks($block, $path, $orderedArgs, $keywordArgs); |
|
| 808 | + |
|
| 809 | + if ($mixins === null) { |
|
| 810 | + $this->throwError("{$prop[1][0]} is undefined"); |
|
| 811 | + } |
|
| 812 | + |
|
| 813 | + foreach ($mixins as $mixin) { |
|
| 814 | + if ($mixin === $block && !$orderedArgs) { |
|
| 815 | + continue; |
|
| 816 | + } |
|
| 817 | + |
|
| 818 | + $haveScope = false; |
|
| 819 | + if (isset($mixin->parent->scope)) { |
|
| 820 | + $haveScope = true; |
|
| 821 | + $mixinParentEnv = $this->pushEnv(); |
|
| 822 | + $mixinParentEnv->storeParent = $mixin->parent->scope; |
|
| 823 | + } |
|
| 824 | + |
|
| 825 | + $haveArgs = false; |
|
| 826 | + if (isset($mixin->args)) { |
|
| 827 | + $haveArgs = true; |
|
| 828 | + $this->pushEnv(); |
|
| 829 | + $this->zipSetArgs($mixin->args, $orderedArgs, $keywordArgs); |
|
| 830 | + } |
|
| 831 | + |
|
| 832 | + $oldParent = $mixin->parent; |
|
| 833 | + if ($mixin != $block) { |
|
| 834 | + $mixin->parent = $block; |
|
| 835 | + } |
|
| 836 | + |
|
| 837 | + foreach ($this->sortProps($mixin->props) as $subProp) { |
|
| 838 | + if ($suffix !== null && |
|
| 839 | + $subProp[0] == "assign" && |
|
| 840 | + is_string($subProp[1]) && |
|
| 841 | + $subProp[1][0] != $this->vPrefix |
|
| 842 | + ) { |
|
| 843 | + $subProp[2] = array( |
|
| 844 | + 'list', ' ', |
|
| 845 | + array($subProp[2], array('keyword', $suffix)) |
|
| 846 | + ); |
|
| 847 | + } |
|
| 848 | + |
|
| 849 | + $this->compileProp($subProp, $mixin, $out); |
|
| 850 | + } |
|
| 851 | + |
|
| 852 | + $mixin->parent = $oldParent; |
|
| 853 | + |
|
| 854 | + if ($haveArgs) { |
|
| 855 | + $this->popEnv(); |
|
| 856 | + } |
|
| 857 | + if ($haveScope) { |
|
| 858 | + $this->popEnv(); |
|
| 859 | + } |
|
| 860 | + } |
|
| 861 | + |
|
| 862 | + break; |
|
| 863 | + case 'raw': |
|
| 864 | + $out->lines[] = $prop[1]; |
|
| 865 | + break; |
|
| 866 | + case "directive": |
|
| 867 | + list(, $name, $value) = $prop; |
|
| 868 | + $out->lines[] = "@$name ".$this->compileValue($this->reduce($value)).';'; |
|
| 869 | + break; |
|
| 870 | + case "comment": |
|
| 871 | + $out->lines[] = $prop[1]; |
|
| 872 | + break; |
|
| 873 | + case "import": |
|
| 874 | + list(, $importPath, $importId) = $prop; |
|
| 875 | + $importPath = $this->reduce($importPath); |
|
| 876 | + |
|
| 877 | + if (!isset($this->env->imports)) { |
|
| 878 | + $this->env->imports = array(); |
|
| 879 | + } |
|
| 880 | + |
|
| 881 | + $result = $this->tryImport($importPath, $block, $out); |
|
| 882 | + |
|
| 883 | + $this->env->imports[$importId] = $result === false ? |
|
| 884 | + array(false, "@import ".$this->compileValue($importPath).";") : $result; |
|
| 885 | + |
|
| 886 | + break; |
|
| 887 | + case "import_mixin": |
|
| 888 | + list(, $importId) = $prop; |
|
| 889 | + $import = $this->env->imports[$importId]; |
|
| 890 | + if ($import[0] === false) { |
|
| 891 | + if (isset($import[1])) { |
|
| 892 | + $out->lines[] = $import[1]; |
|
| 893 | + } |
|
| 894 | + } else { |
|
| 895 | + list(, $bottom, $parser, $importDir) = $import; |
|
| 896 | + $this->compileImportedProps($bottom, $block, $out, $parser, $importDir); |
|
| 897 | + } |
|
| 898 | + |
|
| 899 | + break; |
|
| 900 | + default: |
|
| 901 | + $this->throwError("unknown op: {$prop[0]}\n"); |
|
| 902 | + } |
|
| 903 | + } |
|
| 904 | + |
|
| 905 | + |
|
| 906 | + /** |
|
| 907 | + * Compiles a primitive value into a CSS property value. |
|
| 908 | + * |
|
| 909 | + * Values in lessphp are typed by being wrapped in arrays, their format is |
|
| 910 | + * typically: |
|
| 911 | + * |
|
| 912 | + * array(type, contents [, additional_contents]*) |
|
| 913 | + * |
|
| 914 | + * The input is expected to be reduced. This function will not work on |
|
| 915 | + * things like expressions and variables. |
|
| 916 | + */ |
|
| 917 | + public function compileValue($value) |
|
| 918 | + { |
|
| 919 | + switch ($value[0]) { |
|
| 920 | + case 'list': |
|
| 921 | + // [1] - delimiter |
|
| 922 | + // [2] - array of values |
|
| 923 | + return implode($value[1], array_map(array($this, 'compileValue'), $value[2])); |
|
| 924 | + case 'raw_color': |
|
| 925 | + if (!empty($this->formatter->compressColors)) { |
|
| 926 | + return $this->compileValue($this->coerceColor($value)); |
|
| 927 | + } |
|
| 928 | + return $value[1]; |
|
| 929 | + case 'keyword': |
|
| 930 | + // [1] - the keyword |
|
| 931 | + return $value[1]; |
|
| 932 | + case 'number': |
|
| 933 | + list(, $num, $unit) = $value; |
|
| 934 | + // [1] - the number |
|
| 935 | + // [2] - the unit |
|
| 936 | + if ($this->numberPrecision !== null) { |
|
| 937 | + $num = round($num, $this->numberPrecision); |
|
| 938 | + } |
|
| 939 | + return $num.$unit; |
|
| 940 | + case 'string': |
|
| 941 | + // [1] - contents of string (includes quotes) |
|
| 942 | + list(, $delim, $content) = $value; |
|
| 943 | + foreach ($content as &$part) { |
|
| 944 | + if (is_array($part)) { |
|
| 945 | + $part = $this->compileValue($part); |
|
| 946 | + } |
|
| 947 | + } |
|
| 948 | + return $delim.implode($content).$delim; |
|
| 949 | + case 'color': |
|
| 950 | + // [1] - red component (either number or a %) |
|
| 951 | + // [2] - green component |
|
| 952 | + // [3] - blue component |
|
| 953 | + // [4] - optional alpha component |
|
| 954 | + list(, $r, $g, $b) = $value; |
|
| 955 | + $r = round($r); |
|
| 956 | + $g = round($g); |
|
| 957 | + $b = round($b); |
|
| 958 | + |
|
| 959 | + if (count($value) == 5 && $value[4] != 1) { // rgba |
|
| 960 | + return 'rgba('.$r.','.$g.','.$b.','.$value[4].')'; |
|
| 961 | + } |
|
| 962 | + |
|
| 963 | + $h = sprintf("#%02x%02x%02x", $r, $g, $b); |
|
| 964 | + |
|
| 965 | + if (!empty($this->formatter->compressColors)) { |
|
| 966 | + // Converting hex color to short notation (e.g. #003399 to #039) |
|
| 967 | + if ($h[1] === $h[2] && $h[3] === $h[4] && $h[5] === $h[6]) { |
|
| 968 | + $h = '#'.$h[1].$h[3].$h[5]; |
|
| 969 | + } |
|
| 970 | + } |
|
| 971 | + |
|
| 972 | + return $h; |
|
| 973 | + |
|
| 974 | + case 'function': |
|
| 975 | + list(, $name, $args) = $value; |
|
| 976 | + return $name.'('.$this->compileValue($args).')'; |
|
| 977 | + default: // assumed to be unit |
|
| 978 | + $this->throwError("unknown value type: $value[0]"); |
|
| 979 | + } |
|
| 980 | + } |
|
| 981 | + |
|
| 982 | + protected function lib_pow($args) |
|
| 983 | + { |
|
| 984 | + list($base, $exp) = $this->assertArgs($args, 2, "pow"); |
|
| 985 | + return pow($this->assertNumber($base), $this->assertNumber($exp)); |
|
| 986 | + } |
|
| 987 | + |
|
| 988 | + protected function lib_pi() |
|
| 989 | + { |
|
| 990 | + return pi(); |
|
| 991 | + } |
|
| 992 | + |
|
| 993 | + protected function lib_mod($args) |
|
| 994 | + { |
|
| 995 | + list($a, $b) = $this->assertArgs($args, 2, "mod"); |
|
| 996 | + return $this->assertNumber($a) % $this->assertNumber($b); |
|
| 997 | + } |
|
| 998 | + |
|
| 999 | + protected function lib_tan($num) |
|
| 1000 | + { |
|
| 1001 | + return tan($this->assertNumber($num)); |
|
| 1002 | + } |
|
| 1003 | + |
|
| 1004 | + protected function lib_sin($num) |
|
| 1005 | + { |
|
| 1006 | + return sin($this->assertNumber($num)); |
|
| 1007 | + } |
|
| 1008 | + |
|
| 1009 | + protected function lib_cos($num) |
|
| 1010 | + { |
|
| 1011 | + return cos($this->assertNumber($num)); |
|
| 1012 | + } |
|
| 1013 | + |
|
| 1014 | + protected function lib_atan($num) |
|
| 1015 | + { |
|
| 1016 | + $num = atan($this->assertNumber($num)); |
|
| 1017 | + return array("number", $num, "rad"); |
|
| 1018 | + } |
|
| 1019 | + |
|
| 1020 | + protected function lib_asin($num) |
|
| 1021 | + { |
|
| 1022 | + $num = asin($this->assertNumber($num)); |
|
| 1023 | + return array("number", $num, "rad"); |
|
| 1024 | + } |
|
| 1025 | + |
|
| 1026 | + protected function lib_acos($num) |
|
| 1027 | + { |
|
| 1028 | + $num = acos($this->assertNumber($num)); |
|
| 1029 | + return array("number", $num, "rad"); |
|
| 1030 | + } |
|
| 1031 | + |
|
| 1032 | + protected function lib_sqrt($num) |
|
| 1033 | + { |
|
| 1034 | + return sqrt($this->assertNumber($num)); |
|
| 1035 | + } |
|
| 1036 | + |
|
| 1037 | + protected function lib_extract($value) |
|
| 1038 | + { |
|
| 1039 | + list($list, $idx) = $this->assertArgs($value, 2, "extract"); |
|
| 1040 | + $idx = $this->assertNumber($idx); |
|
| 1041 | + // 1 indexed |
|
| 1042 | + if ($list[0] == "list" && isset($list[2][$idx - 1])) { |
|
| 1043 | + return $list[2][$idx - 1]; |
|
| 1044 | + } |
|
| 1045 | + return ''; |
|
| 1046 | + } |
|
| 1047 | + |
|
| 1048 | + protected function lib_isnumber($value) |
|
| 1049 | + { |
|
| 1050 | + return $this->toBool($value[0] == "number"); |
|
| 1051 | + } |
|
| 1052 | + |
|
| 1053 | + protected function lib_isstring($value) |
|
| 1054 | + { |
|
| 1055 | + return $this->toBool($value[0] == "string"); |
|
| 1056 | + } |
|
| 1057 | + |
|
| 1058 | + protected function lib_iscolor($value) |
|
| 1059 | + { |
|
| 1060 | + return $this->toBool($this->coerceColor($value)); |
|
| 1061 | + } |
|
| 1062 | + |
|
| 1063 | + protected function lib_iskeyword($value) |
|
| 1064 | + { |
|
| 1065 | + return $this->toBool($value[0] == "keyword"); |
|
| 1066 | + } |
|
| 1067 | + |
|
| 1068 | + protected function lib_ispixel($value) |
|
| 1069 | + { |
|
| 1070 | + return $this->toBool($value[0] == "number" && $value[2] == "px"); |
|
| 1071 | + } |
|
| 1072 | + |
|
| 1073 | + protected function lib_ispercentage($value) |
|
| 1074 | + { |
|
| 1075 | + return $this->toBool($value[0] == "number" && $value[2] == "%"); |
|
| 1076 | + } |
|
| 1077 | + |
|
| 1078 | + protected function lib_isem($value) |
|
| 1079 | + { |
|
| 1080 | + return $this->toBool($value[0] == "number" && $value[2] == "em"); |
|
| 1081 | + } |
|
| 1082 | + |
|
| 1083 | + protected function lib_isrem($value) |
|
| 1084 | + { |
|
| 1085 | + return $this->toBool($value[0] == "number" && $value[2] == "rem"); |
|
| 1086 | + } |
|
| 1087 | + |
|
| 1088 | + protected function lib_rgbahex($color) |
|
| 1089 | + { |
|
| 1090 | + $color = $this->coerceColor($color); |
|
| 1091 | + if (is_null($color)) { |
|
| 1092 | + $this->throwError("color expected for rgbahex"); |
|
| 1093 | + } |
|
| 1094 | + |
|
| 1095 | + return sprintf( |
|
| 1096 | + "#%02x%02x%02x%02x", |
|
| 1097 | + isset($color[4]) ? $color[4] * 255 : 255, |
|
| 1098 | + $color[1], |
|
| 1099 | + $color[2], |
|
| 1100 | + $color[3] |
|
| 1101 | + ); |
|
| 1102 | + } |
|
| 1103 | + |
|
| 1104 | + protected function lib_argb($color) |
|
| 1105 | + { |
|
| 1106 | + return $this->lib_rgbahex($color); |
|
| 1107 | + } |
|
| 1108 | + |
|
| 1109 | + /** |
|
| 1110 | + * Given an url, decide whether to output a regular link or the base64-encoded contents of the file |
|
| 1111 | + * |
|
| 1112 | + * @param array $value either an argument list (two strings) or a single string |
|
| 1113 | + * @return string formatted url(), either as a link or base64-encoded |
|
| 1114 | + */ |
|
| 1115 | + protected function lib_data_uri($value) |
|
| 1116 | + { |
|
| 1117 | + $mime = ($value[0] === 'list') ? $value[2][0][2] : null; |
|
| 1118 | + $url = ($value[0] === 'list') ? $value[2][1][2][0] : $value[2][0]; |
|
| 1119 | + |
|
| 1120 | + $fullpath = $this->findImport($url); |
|
| 1121 | + |
|
| 1122 | + if ($fullpath && ($fsize = filesize($fullpath)) !== false) { |
|
| 1123 | + // IE8 can't handle data uris larger than 32KB |
|
| 1124 | + if ($fsize / 1024 < 32) { |
|
| 1125 | + if (is_null($mime)) { |
|
| 1126 | + if (class_exists('finfo')) { // php 5.3+ |
|
| 1127 | + $finfo = new finfo(FILEINFO_MIME); |
|
| 1128 | + $mime = explode('; ', $finfo->file($fullpath)); |
|
| 1129 | + $mime = $mime[0]; |
|
| 1130 | + } elseif (function_exists('mime_content_type')) { // PHP 5.2 |
|
| 1131 | + $mime = mime_content_type($fullpath); |
|
| 1132 | + } |
|
| 1133 | + } |
|
| 1134 | + |
|
| 1135 | + if (!is_null($mime)) { // fallback if the mime type is still unknown |
|
| 1136 | + $url = sprintf('data:%s;base64,%s', $mime, base64_encode(file_get_contents($fullpath))); |
|
| 1137 | + } |
|
| 1138 | + } |
|
| 1139 | + } |
|
| 1140 | + |
|
| 1141 | + return 'url("'.$url.'")'; |
|
| 1142 | + } |
|
| 1143 | + |
|
| 1144 | + // utility func to unquote a string |
|
| 1145 | + protected function lib_e($arg) |
|
| 1146 | + { |
|
| 1147 | + switch ($arg[0]) { |
|
| 1148 | + case "list": |
|
| 1149 | + $items = $arg[2]; |
|
| 1150 | + if (isset($items[0])) { |
|
| 1151 | + return $this->lib_e($items[0]); |
|
| 1152 | + } |
|
| 1153 | + $this->throwError("unrecognised input"); // This ends function by throwing an exception |
|
| 1154 | + // no break |
|
| 1155 | + case "string": |
|
| 1156 | + $arg[1] = ""; |
|
| 1157 | + return $arg; |
|
| 1158 | + case "keyword": |
|
| 1159 | + return $arg; |
|
| 1160 | + default: |
|
| 1161 | + return array("keyword", $this->compileValue($arg)); |
|
| 1162 | + } |
|
| 1163 | + } |
|
| 1164 | + |
|
| 1165 | + protected function lib__sprintf($args) |
|
| 1166 | + { |
|
| 1167 | + if ($args[0] != "list") { |
|
| 1168 | + return $args; |
|
| 1169 | + } |
|
| 1170 | + $values = $args[2]; |
|
| 1171 | + $string = array_shift($values); |
|
| 1172 | + $template = $this->compileValue($this->lib_e($string)); |
|
| 1173 | + |
|
| 1174 | + $i = 0; |
|
| 1175 | + $m = array(); |
|
| 1176 | + if (preg_match_all('/%[dsa]/', $template, $m)) { |
|
| 1177 | + foreach ($m[0] as $match) { |
|
| 1178 | + $val = isset($values[$i]) ? |
|
| 1179 | + $this->reduce($values[$i]) : array('keyword', ''); |
|
| 1180 | + |
|
| 1181 | + // lessjs compat, renders fully expanded color, not raw color |
|
| 1182 | + if ($color = $this->coerceColor($val)) { |
|
| 1183 | + $val = $color; |
|
| 1184 | + } |
|
| 1185 | + |
|
| 1186 | + $i++; |
|
| 1187 | + $rep = $this->compileValue($this->lib_e($val)); |
|
| 1188 | + $template = preg_replace( |
|
| 1189 | + '/'.self::preg_quote($match).'/', |
|
| 1190 | + $rep, |
|
| 1191 | + $template, |
|
| 1192 | + 1 |
|
| 1193 | + ); |
|
| 1194 | + } |
|
| 1195 | + } |
|
| 1196 | + |
|
| 1197 | + $d = $string[0] == "string" ? $string[1] : '"'; |
|
| 1198 | + return array("string", $d, array($template)); |
|
| 1199 | + } |
|
| 1200 | + |
|
| 1201 | + protected function lib_floor($arg) |
|
| 1202 | + { |
|
| 1203 | + $value = $this->assertNumber($arg); |
|
| 1204 | + return array("number", floor($value), $arg[2]); |
|
| 1205 | + } |
|
| 1206 | + |
|
| 1207 | + protected function lib_ceil($arg) |
|
| 1208 | + { |
|
| 1209 | + $value = $this->assertNumber($arg); |
|
| 1210 | + return array("number", ceil($value), $arg[2]); |
|
| 1211 | + } |
|
| 1212 | + |
|
| 1213 | + protected function lib_round($arg) |
|
| 1214 | + { |
|
| 1215 | + if ($arg[0] != "list") { |
|
| 1216 | + $value = $this->assertNumber($arg); |
|
| 1217 | + return array("number", round($value), $arg[2]); |
|
| 1218 | + } else { |
|
| 1219 | + $value = $this->assertNumber($arg[2][0]); |
|
| 1220 | + $precision = $this->assertNumber($arg[2][1]); |
|
| 1221 | + return array("number", round($value, $precision), $arg[2][0][2]); |
|
| 1222 | + } |
|
| 1223 | + } |
|
| 1224 | + |
|
| 1225 | + protected function lib_unit($arg) |
|
| 1226 | + { |
|
| 1227 | + if ($arg[0] == "list") { |
|
| 1228 | + list($number, $newUnit) = $arg[2]; |
|
| 1229 | + return array("number", $this->assertNumber($number), |
|
| 1230 | + $this->compileValue($this->lib_e($newUnit))); |
|
| 1231 | + } else { |
|
| 1232 | + return array("number", $this->assertNumber($arg), ""); |
|
| 1233 | + } |
|
| 1234 | + } |
|
| 1235 | + |
|
| 1236 | + /** |
|
| 1237 | + * Helper function to get arguments for color manipulation functions. |
|
| 1238 | + * takes a list that contains a color like thing and a percentage |
|
| 1239 | + */ |
|
| 1240 | + public function colorArgs($args) |
|
| 1241 | + { |
|
| 1242 | + if ($args[0] != 'list' || count($args[2]) < 2) { |
|
| 1243 | + return array(array('color', 0, 0, 0), 0); |
|
| 1244 | + } |
|
| 1245 | + list($color, $delta) = $args[2]; |
|
| 1246 | + $color = $this->assertColor($color); |
|
| 1247 | + $delta = (float) $delta[1]; |
|
| 1248 | + |
|
| 1249 | + return array($color, $delta); |
|
| 1250 | + } |
|
| 1251 | + |
|
| 1252 | + protected function lib_darken($args) |
|
| 1253 | + { |
|
| 1254 | + list($color, $delta) = $this->colorArgs($args); |
|
| 1255 | + |
|
| 1256 | + $hsl = $this->toHSL($color); |
|
| 1257 | + $hsl[3] = $this->clamp($hsl[3] - $delta, 100); |
|
| 1258 | + return $this->toRGB($hsl); |
|
| 1259 | + } |
|
| 1260 | + |
|
| 1261 | + protected function lib_lighten($args) |
|
| 1262 | + { |
|
| 1263 | + list($color, $delta) = $this->colorArgs($args); |
|
| 1264 | + |
|
| 1265 | + $hsl = $this->toHSL($color); |
|
| 1266 | + $hsl[3] = $this->clamp($hsl[3] + $delta, 100); |
|
| 1267 | + return $this->toRGB($hsl); |
|
| 1268 | + } |
|
| 1269 | + |
|
| 1270 | + protected function lib_saturate($args) |
|
| 1271 | + { |
|
| 1272 | + list($color, $delta) = $this->colorArgs($args); |
|
| 1273 | + |
|
| 1274 | + $hsl = $this->toHSL($color); |
|
| 1275 | + $hsl[2] = $this->clamp($hsl[2] + $delta, 100); |
|
| 1276 | + return $this->toRGB($hsl); |
|
| 1277 | + } |
|
| 1278 | + |
|
| 1279 | + protected function lib_desaturate($args) |
|
| 1280 | + { |
|
| 1281 | + list($color, $delta) = $this->colorArgs($args); |
|
| 1282 | + |
|
| 1283 | + $hsl = $this->toHSL($color); |
|
| 1284 | + $hsl[2] = $this->clamp($hsl[2] - $delta, 100); |
|
| 1285 | + return $this->toRGB($hsl); |
|
| 1286 | + } |
|
| 1287 | + |
|
| 1288 | + protected function lib_spin($args) |
|
| 1289 | + { |
|
| 1290 | + list($color, $delta) = $this->colorArgs($args); |
|
| 1291 | + |
|
| 1292 | + $hsl = $this->toHSL($color); |
|
| 1293 | + |
|
| 1294 | + $hsl[1] = $hsl[1] + $delta % 360; |
|
| 1295 | + if ($hsl[1] < 0) { |
|
| 1296 | + $hsl[1] += 360; |
|
| 1297 | + } |
|
| 1298 | + |
|
| 1299 | + return $this->toRGB($hsl); |
|
| 1300 | + } |
|
| 1301 | + |
|
| 1302 | + protected function lib_fadeout($args) |
|
| 1303 | + { |
|
| 1304 | + list($color, $delta) = $this->colorArgs($args); |
|
| 1305 | + $color[4] = $this->clamp((isset($color[4]) ? $color[4] : 1) - $delta / 100); |
|
| 1306 | + return $color; |
|
| 1307 | + } |
|
| 1308 | + |
|
| 1309 | + protected function lib_fadein($args) |
|
| 1310 | + { |
|
| 1311 | + list($color, $delta) = $this->colorArgs($args); |
|
| 1312 | + $color[4] = $this->clamp((isset($color[4]) ? $color[4] : 1) + $delta / 100); |
|
| 1313 | + return $color; |
|
| 1314 | + } |
|
| 1315 | + |
|
| 1316 | + protected function lib_hue($color) |
|
| 1317 | + { |
|
| 1318 | + $hsl = $this->toHSL($this->assertColor($color)); |
|
| 1319 | + return round($hsl[1]); |
|
| 1320 | + } |
|
| 1321 | + |
|
| 1322 | + protected function lib_saturation($color) |
|
| 1323 | + { |
|
| 1324 | + $hsl = $this->toHSL($this->assertColor($color)); |
|
| 1325 | + return round($hsl[2]); |
|
| 1326 | + } |
|
| 1327 | + |
|
| 1328 | + protected function lib_lightness($color) |
|
| 1329 | + { |
|
| 1330 | + $hsl = $this->toHSL($this->assertColor($color)); |
|
| 1331 | + return round($hsl[3]); |
|
| 1332 | + } |
|
| 1333 | + |
|
| 1334 | + // get the alpha of a color |
|
| 1335 | + // defaults to 1 for non-colors or colors without an alpha |
|
| 1336 | + protected function lib_alpha($value) |
|
| 1337 | + { |
|
| 1338 | + if (!is_null($color = $this->coerceColor($value))) { |
|
| 1339 | + return isset($color[4]) ? $color[4] : 1; |
|
| 1340 | + } |
|
| 1341 | + return ''; |
|
| 1342 | + } |
|
| 1343 | + |
|
| 1344 | + // set the alpha of the color |
|
| 1345 | + protected function lib_fade($args) |
|
| 1346 | + { |
|
| 1347 | + list($color, $alpha) = $this->colorArgs($args); |
|
| 1348 | + $color[4] = $this->clamp($alpha / 100.0); |
|
| 1349 | + return $color; |
|
| 1350 | + } |
|
| 1351 | + |
|
| 1352 | + protected function lib_percentage($arg) |
|
| 1353 | + { |
|
| 1354 | + $num = $this->assertNumber($arg); |
|
| 1355 | + return array("number", $num * 100, "%"); |
|
| 1356 | + } |
|
| 1357 | + |
|
| 1358 | + /** |
|
| 1359 | + * Mix color with white in variable proportion. |
|
| 1360 | + * |
|
| 1361 | + * It is the same as calling `mix(#ffffff, @color, @weight)`. |
|
| 1362 | + * |
|
| 1363 | + * tint(@color, [@weight: 50%]); |
|
| 1364 | + * |
|
| 1365 | + * http://lesscss.org/functions/#color-operations-tint |
|
| 1366 | + * |
|
| 1367 | + * @return array Color |
|
| 1368 | + */ |
|
| 1369 | + protected function lib_tint($args) |
|
| 1370 | + { |
|
| 1371 | + $white = ['color', 255, 255, 255]; |
|
| 1372 | + if ($args[0] == 'color') { |
|
| 1373 | + return $this->lib_mix(['list', ',', [$white, $args]]); |
|
| 1374 | + } elseif ($args[0] == "list" && count($args[2]) == 2) { |
|
| 1375 | + return $this->lib_mix([$args[0], $args[1], [$white, $args[2][0], $args[2][1]]]); |
|
| 1376 | + } else { |
|
| 1377 | + $this->throwError("tint expects (color, weight)"); |
|
| 1378 | + } |
|
| 1379 | + return array(); |
|
| 1380 | + } |
|
| 1381 | + |
|
| 1382 | + /** |
|
| 1383 | + * Mix color with black in variable proportion. |
|
| 1384 | + * |
|
| 1385 | + * It is the same as calling `mix(#000000, @color, @weight)` |
|
| 1386 | + * |
|
| 1387 | + * shade(@color, [@weight: 50%]); |
|
| 1388 | + * |
|
| 1389 | + * http://lesscss.org/functions/#color-operations-shade |
|
| 1390 | + * |
|
| 1391 | + * @return array Color |
|
| 1392 | + */ |
|
| 1393 | + protected function lib_shade($args) |
|
| 1394 | + { |
|
| 1395 | + $black = ['color', 0, 0, 0]; |
|
| 1396 | + if ($args[0] == 'color') { |
|
| 1397 | + return $this->lib_mix(['list', ',', [$black, $args]]); |
|
| 1398 | + } elseif ($args[0] == "list" && count($args[2]) == 2) { |
|
| 1399 | + return $this->lib_mix([$args[0], $args[1], [$black, $args[2][0], $args[2][1]]]); |
|
| 1400 | + } else { |
|
| 1401 | + $this->throwError("shade expects (color, weight)"); |
|
| 1402 | + } |
|
| 1403 | + return array(); |
|
| 1404 | + } |
|
| 1405 | + |
|
| 1406 | + /** |
|
| 1407 | + * lib_mix |
|
| 1408 | + * mixes two colors by weight |
|
| 1409 | + * mix(@color1, @color2, [@weight: 50%]); |
|
| 1410 | + * http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html#mix-instance_method |
|
| 1411 | + * |
|
| 1412 | + * @param array $args Args |
|
| 1413 | + * @return array |
|
| 1414 | + */ |
|
| 1415 | + protected function lib_mix($args) |
|
| 1416 | + { |
|
| 1417 | + if ($args[0] != "list" || count($args[2]) < 2) { |
|
| 1418 | + $this->throwError("mix expects (color1, color2, weight)"); |
|
| 1419 | + } |
|
| 1420 | + |
|
| 1421 | + list($first, $second) = $args[2]; |
|
| 1422 | + $first = $this->assertColor($first); |
|
| 1423 | + $second = $this->assertColor($second); |
|
| 1424 | + |
|
| 1425 | + $first_a = $this->lib_alpha($first); |
|
| 1426 | + $second_a = $this->lib_alpha($second); |
|
| 1427 | + |
|
| 1428 | + if (isset($args[2][2])) { |
|
| 1429 | + $weight = $args[2][2][1] / 100.0; |
|
| 1430 | + } else { |
|
| 1431 | + $weight = 0.5; |
|
| 1432 | + } |
|
| 1433 | + |
|
| 1434 | + $w = $weight * 2 - 1; |
|
| 1435 | + $a = $first_a - $second_a; |
|
| 1436 | + |
|
| 1437 | + $w1 = (($w * $a == -1 ? $w : ($w + $a) / (1 + $w * $a)) + 1) / 2.0; |
|
| 1438 | + $w2 = 1.0 - $w1; |
|
| 1439 | + |
|
| 1440 | + $new = array('color', |
|
| 1441 | + $w1 * $first[1] + $w2 * $second[1], |
|
| 1442 | + $w1 * $first[2] + $w2 * $second[2], |
|
| 1443 | + $w1 * $first[3] + $w2 * $second[3], |
|
| 1444 | + ); |
|
| 1445 | + |
|
| 1446 | + if ($first_a != 1.0 || $second_a != 1.0) { |
|
| 1447 | + $new[] = $first_a * $weight + $second_a * ($weight - 1); |
|
| 1448 | + } |
|
| 1449 | + |
|
| 1450 | + return $this->fixColor($new); |
|
| 1451 | + } |
|
| 1452 | + |
|
| 1453 | + /** |
|
| 1454 | + * lib_contrast |
|
| 1455 | + * |
|
| 1456 | + * @param array $args Args |
|
| 1457 | + * @return array |
|
| 1458 | + */ |
|
| 1459 | + protected function lib_contrast($args) |
|
| 1460 | + { |
|
| 1461 | + $darkColor = array('color', 0, 0, 0); |
|
| 1462 | + $lightColor = array('color', 255, 255, 255); |
|
| 1463 | + $threshold = 0.43; |
|
| 1464 | + |
|
| 1465 | + if ($args[0] == 'list') { |
|
| 1466 | + $inputColor = (isset($args[2][0])) ? $this->assertColor($args[2][0]) : $lightColor; |
|
| 1467 | + $darkColor = (isset($args[2][1])) ? $this->assertColor($args[2][1]) : $darkColor; |
|
| 1468 | + $lightColor = (isset($args[2][2])) ? $this->assertColor($args[2][2]) : $lightColor; |
|
| 1469 | + $threshold = (isset($args[2][3])) ? $this->assertNumber($args[2][3]) : $threshold; |
|
| 1470 | + } else { |
|
| 1471 | + $inputColor = $this->assertColor($args); |
|
| 1472 | + } |
|
| 1473 | + |
|
| 1474 | + $inputColor = $this->coerceColor($inputColor); |
|
| 1475 | + $darkColor = $this->coerceColor($darkColor); |
|
| 1476 | + $lightColor = $this->coerceColor($lightColor); |
|
| 1477 | + |
|
| 1478 | + //Figure out which is actually light and dark! |
|
| 1479 | + if ($this->toLuma($darkColor) > $this->toLuma($lightColor)) { |
|
| 1480 | + $t = $lightColor; |
|
| 1481 | + $lightColor = $darkColor; |
|
| 1482 | + $darkColor = $t; |
|
| 1483 | + } |
|
| 1484 | + |
|
| 1485 | + $inputColor_alpha = $this->lib_alpha($inputColor); |
|
| 1486 | + if (($this->toLuma($inputColor) * $inputColor_alpha) < $threshold) { |
|
| 1487 | + return $lightColor; |
|
| 1488 | + } |
|
| 1489 | + return $darkColor; |
|
| 1490 | + } |
|
| 1491 | + |
|
| 1492 | + private function toLuma($color) |
|
| 1493 | + { |
|
| 1494 | + list(, $r, $g, $b) = $this->coerceColor($color); |
|
| 1495 | + |
|
| 1496 | + $r = $r / 255; |
|
| 1497 | + $g = $g / 255; |
|
| 1498 | + $b = $b / 255; |
|
| 1499 | + |
|
| 1500 | + $r = ($r <= 0.03928) ? $r / 12.92 : pow((($r + 0.055) / 1.055), 2.4); |
|
| 1501 | + $g = ($g <= 0.03928) ? $g / 12.92 : pow((($g + 0.055) / 1.055), 2.4); |
|
| 1502 | + $b = ($b <= 0.03928) ? $b / 12.92 : pow((($b + 0.055) / 1.055), 2.4); |
|
| 1503 | + |
|
| 1504 | + return (0.2126 * $r) + (0.7152 * $g) + (0.0722 * $b); |
|
| 1505 | + } |
|
| 1506 | + |
|
| 1507 | + protected function lib_luma($color) |
|
| 1508 | + { |
|
| 1509 | + return array("number", round($this->toLuma($color) * 100, 8), "%"); |
|
| 1510 | + } |
|
| 1511 | + |
|
| 1512 | + |
|
| 1513 | + public function assertColor($value, $error = "expected color value") |
|
| 1514 | + { |
|
| 1515 | + $color = $this->coerceColor($value); |
|
| 1516 | + if (is_null($color)) { |
|
| 1517 | + $this->throwError($error); |
|
| 1518 | + } |
|
| 1519 | + return $color; |
|
| 1520 | + } |
|
| 1521 | + |
|
| 1522 | + public function assertNumber($value, $error = "expecting number") |
|
| 1523 | + { |
|
| 1524 | + if ($value[0] == "number") { |
|
| 1525 | + return $value[1]; |
|
| 1526 | + } |
|
| 1527 | + $this->throwError($error); |
|
| 1528 | + } |
|
| 1529 | + |
|
| 1530 | + public function assertArgs($value, $expectedArgs, $name = "") |
|
| 1531 | + { |
|
| 1532 | + if ($expectedArgs == 1) { |
|
| 1533 | + return $value; |
|
| 1534 | + } else { |
|
| 1535 | + if ($value[0] !== "list" || $value[1] != ",") { |
|
| 1536 | + $this->throwError("expecting list"); |
|
| 1537 | + } |
|
| 1538 | + $values = $value[2]; |
|
| 1539 | + $numValues = count($values); |
|
| 1540 | + if ($expectedArgs != $numValues) { |
|
| 1541 | + if ($name) { |
|
| 1542 | + $name = $name.": "; |
|
| 1543 | + } |
|
| 1544 | + |
|
| 1545 | + $this->throwError("{$name}expecting $expectedArgs arguments, got $numValues"); |
|
| 1546 | + } |
|
| 1547 | + |
|
| 1548 | + return $values; |
|
| 1549 | + } |
|
| 1550 | + } |
|
| 1551 | + |
|
| 1552 | + protected function toHSL($color) |
|
| 1553 | + { |
|
| 1554 | + if ($color[0] === 'hsl') { |
|
| 1555 | + return $color; |
|
| 1556 | + } |
|
| 1557 | + |
|
| 1558 | + $r = $color[1] / 255; |
|
| 1559 | + $g = $color[2] / 255; |
|
| 1560 | + $b = $color[3] / 255; |
|
| 1561 | + |
|
| 1562 | + $min = min($r, $g, $b); |
|
| 1563 | + $max = max($r, $g, $b); |
|
| 1564 | + |
|
| 1565 | + $L = ($min + $max) / 2; |
|
| 1566 | + if ($min == $max) { |
|
| 1567 | + $S = $H = 0; |
|
| 1568 | + } else { |
|
| 1569 | + if ($L < 0.5) { |
|
| 1570 | + $S = ($max - $min) / ($max + $min); |
|
| 1571 | + } else { |
|
| 1572 | + $S = ($max - $min) / (2.0 - $max - $min); |
|
| 1573 | + } |
|
| 1574 | + if ($r == $max) { |
|
| 1575 | + $H = ($g - $b) / ($max - $min); |
|
| 1576 | + } elseif ($g == $max) { |
|
| 1577 | + $H = 2.0 + ($b - $r) / ($max - $min); |
|
| 1578 | + } elseif ($b == $max) { |
|
| 1579 | + $H = 4.0 + ($r - $g) / ($max - $min); |
|
| 1580 | + } |
|
| 1581 | + } |
|
| 1582 | + |
|
| 1583 | + $out = array('hsl', |
|
| 1584 | + ($H < 0 ? $H + 6 : $H) * 60, |
|
| 1585 | + $S * 100, |
|
| 1586 | + $L * 100, |
|
| 1587 | + ); |
|
| 1588 | + |
|
| 1589 | + if (count($color) > 4) { |
|
| 1590 | + // copy alpha |
|
| 1591 | + $out[] = $color[4]; |
|
| 1592 | + } |
|
| 1593 | + return $out; |
|
| 1594 | + } |
|
| 1595 | + |
|
| 1596 | + protected function toRGB_helper($comp, $temp1, $temp2) |
|
| 1597 | + { |
|
| 1598 | + if ($comp < 0) { |
|
| 1599 | + $comp += 1.0; |
|
| 1600 | + } elseif ($comp > 1) { |
|
| 1601 | + $comp -= 1.0; |
|
| 1602 | + } |
|
| 1603 | + |
|
| 1604 | + if (6 * $comp < 1) { |
|
| 1605 | + return $temp1 + ($temp2 - $temp1) * 6 * $comp; |
|
| 1606 | + } |
|
| 1607 | + if (2 * $comp < 1) { |
|
| 1608 | + return $temp2; |
|
| 1609 | + } |
|
| 1610 | + if (3 * $comp < 2) { |
|
| 1611 | + return $temp1 + ($temp2 - $temp1) * ((2 / 3) - $comp) * 6; |
|
| 1612 | + } |
|
| 1613 | + |
|
| 1614 | + return $temp1; |
|
| 1615 | + } |
|
| 1616 | + |
|
| 1617 | + /** |
|
| 1618 | + * Converts a hsl array into a color value in rgb. |
|
| 1619 | + * Expects H to be in range of 0 to 360, S and L in 0 to 100 |
|
| 1620 | + */ |
|
| 1621 | + protected function toRGB($color) |
|
| 1622 | + { |
|
| 1623 | + if ($color[0] === 'color') { |
|
| 1624 | + return $color; |
|
| 1625 | + } |
|
| 1626 | + |
|
| 1627 | + $H = $color[1] / 360; |
|
| 1628 | + $S = $color[2] / 100; |
|
| 1629 | + $L = $color[3] / 100; |
|
| 1630 | + |
|
| 1631 | + if ($S == 0) { |
|
| 1632 | + $r = $g = $b = $L; |
|
| 1633 | + } else { |
|
| 1634 | + $temp2 = $L < 0.5 ? |
|
| 1635 | + $L * (1.0 + $S) : $L + $S - $L * $S; |
|
| 1636 | + |
|
| 1637 | + $temp1 = 2.0 * $L - $temp2; |
|
| 1638 | + |
|
| 1639 | + $r = $this->toRGB_helper($H + 1 / 3, $temp1, $temp2); |
|
| 1640 | + $g = $this->toRGB_helper($H, $temp1, $temp2); |
|
| 1641 | + $b = $this->toRGB_helper($H - 1 / 3, $temp1, $temp2); |
|
| 1642 | + } |
|
| 1643 | + |
|
| 1644 | + // $out = array('color', round($r*255), round($g*255), round($b*255)); |
|
| 1645 | + $out = array('color', $r * 255, $g * 255, $b * 255); |
|
| 1646 | + if (count($color) > 4) { |
|
| 1647 | + // copy alpha |
|
| 1648 | + $out[] = $color[4]; |
|
| 1649 | + } |
|
| 1650 | + return $out; |
|
| 1651 | + } |
|
| 1652 | + |
|
| 1653 | + protected function clamp($v, $max = 1, $min = 0) |
|
| 1654 | + { |
|
| 1655 | + return min($max, max($min, $v)); |
|
| 1656 | + } |
|
| 1657 | + |
|
| 1658 | + /** |
|
| 1659 | + * Convert the rgb, rgba, hsl color literals of function type |
|
| 1660 | + * as returned by the parser into values of color type. |
|
| 1661 | + */ |
|
| 1662 | + protected function funcToColor($func) |
|
| 1663 | + { |
|
| 1664 | + $fname = $func[1]; |
|
| 1665 | + if ($func[2][0] != 'list') { |
|
| 1666 | + // need a list of arguments |
|
| 1667 | + return false; |
|
| 1668 | + } |
|
| 1669 | + $rawComponents = $func[2][2]; |
|
| 1670 | + |
|
| 1671 | + if ($fname == 'hsl' || $fname == 'hsla') { |
|
| 1672 | + $hsl = array('hsl'); |
|
| 1673 | + $i = 0; |
|
| 1674 | + foreach ($rawComponents as $c) { |
|
| 1675 | + $val = $this->reduce($c); |
|
| 1676 | + $val = isset($val[1]) ? (float) $val[1] : 0; |
|
| 1677 | + |
|
| 1678 | + if ($i == 0) { |
|
| 1679 | + $clamp = 360; |
|
| 1680 | + } elseif ($i < 3) { |
|
| 1681 | + $clamp = 100; |
|
| 1682 | + } else { |
|
| 1683 | + $clamp = 1; |
|
| 1684 | + } |
|
| 1685 | + |
|
| 1686 | + $hsl[] = $this->clamp($val, $clamp); |
|
| 1687 | + $i++; |
|
| 1688 | + } |
|
| 1689 | + |
|
| 1690 | + while (count($hsl) < 4) { |
|
| 1691 | + $hsl[] = 0; |
|
| 1692 | + } |
|
| 1693 | + return $this->toRGB($hsl); |
|
| 1694 | + |
|
| 1695 | + } elseif ($fname == 'rgb' || $fname == 'rgba') { |
|
| 1696 | + $components = array(); |
|
| 1697 | + $i = 1; |
|
| 1698 | + foreach ($rawComponents as $c) { |
|
| 1699 | + $c = $this->reduce($c); |
|
| 1700 | + if ($i < 4) { |
|
| 1701 | + if ($c[0] == "number" && $c[2] == "%") { |
|
| 1702 | + $components[] = 255 * ($c[1] / 100); |
|
| 1703 | + } else { |
|
| 1704 | + $components[] = (float) $c[1]; |
|
| 1705 | + } |
|
| 1706 | + } elseif ($i == 4) { |
|
| 1707 | + if ($c[0] == "number" && $c[2] == "%") { |
|
| 1708 | + $components[] = 1.0 * ($c[1] / 100); |
|
| 1709 | + } else { |
|
| 1710 | + $components[] = (float) $c[1]; |
|
| 1711 | + } |
|
| 1712 | + } else { |
|
| 1713 | + break; |
|
| 1714 | + } |
|
| 1715 | + |
|
| 1716 | + $i++; |
|
| 1717 | + } |
|
| 1718 | + while (count($components) < 3) { |
|
| 1719 | + $components[] = 0; |
|
| 1720 | + } |
|
| 1721 | + array_unshift($components, 'color'); |
|
| 1722 | + return $this->fixColor($components); |
|
| 1723 | + } |
|
| 1724 | + |
|
| 1725 | + return false; |
|
| 1726 | + } |
|
| 1727 | + |
|
| 1728 | + protected function reduce($value, $forExpression = false) |
|
| 1729 | + { |
|
| 1730 | + switch ($value[0]) { |
|
| 1731 | + case "interpolate": |
|
| 1732 | + $reduced = $this->reduce($value[1]); |
|
| 1733 | + $var = $this->compileValue($reduced); |
|
| 1734 | + $res = $this->reduce(array("variable", $this->vPrefix.$var)); |
|
| 1735 | + |
|
| 1736 | + if ($res[0] == "raw_color") { |
|
| 1737 | + $res = $this->coerceColor($res); |
|
| 1738 | + } |
|
| 1739 | + |
|
| 1740 | + if (empty($value[2])) { |
|
| 1741 | + $res = $this->lib_e($res); |
|
| 1742 | + } |
|
| 1743 | + |
|
| 1744 | + return $res; |
|
| 1745 | + case "variable": |
|
| 1746 | + $key = $value[1]; |
|
| 1747 | + if (is_array($key)) { |
|
| 1748 | + $key = $this->reduce($key); |
|
| 1749 | + $key = $this->vPrefix.$this->compileValue($this->lib_e($key)); |
|
| 1750 | + } |
|
| 1751 | + |
|
| 1752 | + $seen = & $this->env->seenNames; |
|
| 1753 | + |
|
| 1754 | + if (!empty($seen[$key])) { |
|
| 1755 | + $this->throwError("infinite loop detected: $key"); |
|
| 1756 | + } |
|
| 1757 | + |
|
| 1758 | + $seen[$key] = true; |
|
| 1759 | + $out = $this->reduce($this->get($key)); |
|
| 1760 | + $seen[$key] = false; |
|
| 1761 | + return $out; |
|
| 1762 | + case "list": |
|
| 1763 | + foreach ($value[2] as &$item) { |
|
| 1764 | + $item = $this->reduce($item, $forExpression); |
|
| 1765 | + } |
|
| 1766 | + return $value; |
|
| 1767 | + case "expression": |
|
| 1768 | + return $this->evaluate($value); |
|
| 1769 | + case "string": |
|
| 1770 | + foreach ($value[2] as &$part) { |
|
| 1771 | + if (is_array($part)) { |
|
| 1772 | + $strip = $part[0] == "variable"; |
|
| 1773 | + $part = $this->reduce($part); |
|
| 1774 | + if ($strip) { |
|
| 1775 | + $part = $this->lib_e($part); |
|
| 1776 | + } |
|
| 1777 | + } |
|
| 1778 | + } |
|
| 1779 | + return $value; |
|
| 1780 | + case "escape": |
|
| 1781 | + list(, $inner) = $value; |
|
| 1782 | + return $this->lib_e($this->reduce($inner)); |
|
| 1783 | + case "function": |
|
| 1784 | + $color = $this->funcToColor($value); |
|
| 1785 | + if ($color) { |
|
| 1786 | + return $color; |
|
| 1787 | + } |
|
| 1788 | + |
|
| 1789 | + list(, $name, $args) = $value; |
|
| 1790 | + if ($name == "%") { |
|
| 1791 | + $name = "_sprintf"; |
|
| 1792 | + } |
|
| 1793 | + |
|
| 1794 | + $f = isset($this->libFunctions[$name]) ? |
|
| 1795 | + $this->libFunctions[$name] : array($this, 'lib_'.str_replace('-', '_', $name)); |
|
| 1796 | + |
|
| 1797 | + if (is_callable($f)) { |
|
| 1798 | + if ($args[0] == 'list') { |
|
| 1799 | + $args = self::compressList($args[2], $args[1]); |
|
| 1800 | + } |
|
| 1801 | + |
|
| 1802 | + $ret = call_user_func($f, $this->reduce($args, true), $this); |
|
| 1803 | + |
|
| 1804 | + if (is_null($ret)) { |
|
| 1805 | + return array("string", "", array( |
|
| 1806 | + $name, "(", $args, ")" |
|
| 1807 | + )); |
|
| 1808 | + } |
|
| 1809 | + |
|
| 1810 | + // convert to a typed value if the result is a php primitive |
|
| 1811 | + if (is_numeric($ret)) { |
|
| 1812 | + $ret = array('number', $ret, ""); |
|
| 1813 | + } elseif (!is_array($ret)) { |
|
| 1814 | + $ret = array('keyword', $ret); |
|
| 1815 | + } |
|
| 1816 | + |
|
| 1817 | + return $ret; |
|
| 1818 | + } |
|
| 1819 | + |
|
| 1820 | + // plain function, reduce args |
|
| 1821 | + $value[2] = $this->reduce($value[2]); |
|
| 1822 | + return $value; |
|
| 1823 | + case "unary": |
|
| 1824 | + list(, $op, $exp) = $value; |
|
| 1825 | + $exp = $this->reduce($exp); |
|
| 1826 | + |
|
| 1827 | + if ($exp[0] == "number") { |
|
| 1828 | + switch ($op) { |
|
| 1829 | + case "+": |
|
| 1830 | + return $exp; |
|
| 1831 | + case "-": |
|
| 1832 | + $exp[1] *= -1; |
|
| 1833 | + return $exp; |
|
| 1834 | + } |
|
| 1835 | + } |
|
| 1836 | + return array("string", "", array($op, $exp)); |
|
| 1837 | + } |
|
| 1838 | + |
|
| 1839 | + if ($forExpression) { |
|
| 1840 | + switch ($value[0]) { |
|
| 1841 | + case "keyword": |
|
| 1842 | + if ($color = $this->coerceColor($value)) { |
|
| 1843 | + return $color; |
|
| 1844 | + } |
|
| 1845 | + break; |
|
| 1846 | + case "raw_color": |
|
| 1847 | + return $this->coerceColor($value); |
|
| 1848 | + } |
|
| 1849 | + } |
|
| 1850 | + |
|
| 1851 | + return $value; |
|
| 1852 | + } |
|
| 1853 | + |
|
| 1854 | + |
|
| 1855 | + // coerce a value for use in color operation |
|
| 1856 | + protected function coerceColor($value) |
|
| 1857 | + { |
|
| 1858 | + switch ($value[0]) { |
|
| 1859 | + case 'color': |
|
| 1860 | + return $value; |
|
| 1861 | + case 'raw_color': |
|
| 1862 | + $c = array("color", 0, 0, 0); |
|
| 1863 | + $colorStr = substr($value[1], 1); |
|
| 1864 | + $num = hexdec($colorStr); |
|
| 1865 | + $width = strlen($colorStr) == 3 ? 16 : 256; |
|
| 1866 | + |
|
| 1867 | + for ($i = 3; $i > 0; $i--) { // 3 2 1 |
|
| 1868 | + $t = intval($num) % $width; |
|
| 1869 | + $num /= $width; |
|
| 1870 | + |
|
| 1871 | + $c[$i] = $t * (256 / $width) + $t * floor(16/$width); |
|
| 1872 | + } |
|
| 1873 | + |
|
| 1874 | + return $c; |
|
| 1875 | + case 'keyword': |
|
| 1876 | + $name = $value[1]; |
|
| 1877 | + if (isset(self::$cssColors[$name])) { |
|
| 1878 | + $rgba = explode(',', self::$cssColors[$name]); |
|
| 1879 | + |
|
| 1880 | + if (isset($rgba[3])) { |
|
| 1881 | + return array('color', $rgba[0], $rgba[1], $rgba[2], $rgba[3]); |
|
| 1882 | + } |
|
| 1883 | + return array('color', $rgba[0], $rgba[1], $rgba[2]); |
|
| 1884 | + } |
|
| 1885 | + return null; |
|
| 1886 | + } |
|
| 1887 | + return null; |
|
| 1888 | + } |
|
| 1889 | + |
|
| 1890 | + // make something string like into a string |
|
| 1891 | + protected function coerceString($value) |
|
| 1892 | + { |
|
| 1893 | + switch ($value[0]) { |
|
| 1894 | + case "string": |
|
| 1895 | + return $value; |
|
| 1896 | + case "keyword": |
|
| 1897 | + return array("string", "", array($value[1])); |
|
| 1898 | + } |
|
| 1899 | + return null; |
|
| 1900 | + } |
|
| 1901 | + |
|
| 1902 | + // turn list of length 1 into value type |
|
| 1903 | + protected function flattenList($value) |
|
| 1904 | + { |
|
| 1905 | + if ($value[0] == "list" && count($value[2]) == 1) { |
|
| 1906 | + return $this->flattenList($value[2][0]); |
|
| 1907 | + } |
|
| 1908 | + return $value; |
|
| 1909 | + } |
|
| 1910 | + |
|
| 1911 | + public function toBool($a) |
|
| 1912 | + { |
|
| 1913 | + return $a ? self::$TRUE : self::$FALSE; |
|
| 1914 | + } |
|
| 1915 | + |
|
| 1916 | + // evaluate an expression |
|
| 1917 | + protected function evaluate($exp) |
|
| 1918 | + { |
|
| 1919 | + list(, $op, $left, $right, $whiteBefore, $whiteAfter) = $exp; |
|
| 1920 | + |
|
| 1921 | + $left = $this->reduce($left, true); |
|
| 1922 | + $right = $this->reduce($right, true); |
|
| 1923 | + |
|
| 1924 | + if ($leftColor = $this->coerceColor($left)) { |
|
| 1925 | + $left = $leftColor; |
|
| 1926 | + } |
|
| 1927 | + |
|
| 1928 | + if ($rightColor = $this->coerceColor($right)) { |
|
| 1929 | + $right = $rightColor; |
|
| 1930 | + } |
|
| 1931 | + |
|
| 1932 | + $ltype = $left[0]; |
|
| 1933 | + $rtype = $right[0]; |
|
| 1934 | + |
|
| 1935 | + // operators that work on all types |
|
| 1936 | + if ($op == "and") { |
|
| 1937 | + return $this->toBool($left == self::$TRUE && $right == self::$TRUE); |
|
| 1938 | + } |
|
| 1939 | + |
|
| 1940 | + if ($op == "=") { |
|
| 1941 | + return $this->toBool($this->eq($left, $right)); |
|
| 1942 | + } |
|
| 1943 | + |
|
| 1944 | + if ($op == "+" && !is_null($str = $this->stringConcatenate($left, $right))) { |
|
| 1945 | + return $str; |
|
| 1946 | + } |
|
| 1947 | + |
|
| 1948 | + // type based operators |
|
| 1949 | + $fname = "op_{$ltype}_{$rtype}"; |
|
| 1950 | + if (is_callable(array($this, $fname))) { |
|
| 1951 | + $out = $this->$fname($op, $left, $right); |
|
| 1952 | + if (!is_null($out)) { |
|
| 1953 | + return $out; |
|
| 1954 | + } |
|
| 1955 | + } |
|
| 1956 | + |
|
| 1957 | + // make the expression look it did before being parsed |
|
| 1958 | + $paddedOp = $op; |
|
| 1959 | + if ($whiteBefore) { |
|
| 1960 | + $paddedOp = " ".$paddedOp; |
|
| 1961 | + } |
|
| 1962 | + if ($whiteAfter) { |
|
| 1963 | + $paddedOp .= " "; |
|
| 1964 | + } |
|
| 1965 | + |
|
| 1966 | + return array("string", "", array($left, $paddedOp, $right)); |
|
| 1967 | + } |
|
| 1968 | + |
|
| 1969 | + protected function stringConcatenate($left, $right) |
|
| 1970 | + { |
|
| 1971 | + if ($strLeft = $this->coerceString($left)) { |
|
| 1972 | + if ($right[0] == "string") { |
|
| 1973 | + $right[1] = ""; |
|
| 1974 | + } |
|
| 1975 | + $strLeft[2][] = $right; |
|
| 1976 | + return $strLeft; |
|
| 1977 | + } |
|
| 1978 | + |
|
| 1979 | + if ($strRight = $this->coerceString($right)) { |
|
| 1980 | + array_unshift($strRight[2], $left); |
|
| 1981 | + return $strRight; |
|
| 1982 | + } |
|
| 1983 | + return ''; |
|
| 1984 | + } |
|
| 1985 | + |
|
| 1986 | + |
|
| 1987 | + // make sure a color's components don't go out of bounds |
|
| 1988 | + protected function fixColor($c) |
|
| 1989 | + { |
|
| 1990 | + foreach (range(1, 3) as $i) { |
|
| 1991 | + if ($c[$i] < 0) { |
|
| 1992 | + $c[$i] = 0; |
|
| 1993 | + } |
|
| 1994 | + if ($c[$i] > 255) { |
|
| 1995 | + $c[$i] = 255; |
|
| 1996 | + } |
|
| 1997 | + } |
|
| 1998 | + |
|
| 1999 | + return $c; |
|
| 2000 | + } |
|
| 2001 | + |
|
| 2002 | + protected function op_number_color($op, $lft, $rgt) |
|
| 2003 | + { |
|
| 2004 | + if ($op == '+' || $op == '*') { |
|
| 2005 | + return $this->op_color_number($op, $rgt, $lft); |
|
| 2006 | + } |
|
| 2007 | + return array(); |
|
| 2008 | + } |
|
| 2009 | + |
|
| 2010 | + protected function op_color_number($op, $lft, $rgt) |
|
| 2011 | + { |
|
| 2012 | + if ($rgt[0] == '%') { |
|
| 2013 | + $rgt[1] /= 100; |
|
| 2014 | + } |
|
| 2015 | + |
|
| 2016 | + return $this->op_color_color( |
|
| 2017 | + $op, |
|
| 2018 | + $lft, |
|
| 2019 | + array_fill(1, count($lft) - 1, $rgt[1]) |
|
| 2020 | + ); |
|
| 2021 | + } |
|
| 2022 | + |
|
| 2023 | + protected function op_color_color($op, $left, $right) |
|
| 2024 | + { |
|
| 2025 | + $out = array('color'); |
|
| 2026 | + $max = count($left) > count($right) ? count($left) : count($right); |
|
| 2027 | + foreach (range(1, $max - 1) as $i) { |
|
| 2028 | + $lval = isset($left[$i]) ? $left[$i] : 0; |
|
| 2029 | + $rval = isset($right[$i]) ? $right[$i] : 0; |
|
| 2030 | + switch ($op) { |
|
| 2031 | + case '+': |
|
| 2032 | + $out[] = $lval + $rval; |
|
| 2033 | + break; |
|
| 2034 | + case '-': |
|
| 2035 | + $out[] = $lval - $rval; |
|
| 2036 | + break; |
|
| 2037 | + case '*': |
|
| 2038 | + $out[] = $lval * $rval; |
|
| 2039 | + break; |
|
| 2040 | + case '%': |
|
| 2041 | + $out[] = $lval % $rval; |
|
| 2042 | + break; |
|
| 2043 | + case '/': |
|
| 2044 | + if ($rval == 0) { |
|
| 2045 | + $this->throwError("evaluate error: can't divide by zero"); |
|
| 2046 | + } |
|
| 2047 | + $out[] = $lval / $rval; |
|
| 2048 | + break; |
|
| 2049 | + default: |
|
| 2050 | + $this->throwError('evaluate error: color op number failed on op '.$op); |
|
| 2051 | + } |
|
| 2052 | + } |
|
| 2053 | + return $this->fixColor($out); |
|
| 2054 | + } |
|
| 2055 | + |
|
| 2056 | + public function lib_red($color) |
|
| 2057 | + { |
|
| 2058 | + $color = $this->coerceColor($color); |
|
| 2059 | + if (is_null($color)) { |
|
| 2060 | + $this->throwError('color expected for red()'); |
|
| 2061 | + } |
|
| 2062 | + |
|
| 2063 | + return $color[1]; |
|
| 2064 | + } |
|
| 2065 | + |
|
| 2066 | + public function lib_green($color) |
|
| 2067 | + { |
|
| 2068 | + $color = $this->coerceColor($color); |
|
| 2069 | + if (is_null($color)) { |
|
| 2070 | + $this->throwError('color expected for green()'); |
|
| 2071 | + } |
|
| 2072 | + |
|
| 2073 | + return $color[2]; |
|
| 2074 | + } |
|
| 2075 | + |
|
| 2076 | + public function lib_blue($color) |
|
| 2077 | + { |
|
| 2078 | + $color = $this->coerceColor($color); |
|
| 2079 | + if (is_null($color)) { |
|
| 2080 | + $this->throwError('color expected for blue()'); |
|
| 2081 | + } |
|
| 2082 | + |
|
| 2083 | + return $color[3]; |
|
| 2084 | + } |
|
| 2085 | + |
|
| 2086 | + |
|
| 2087 | + // operator on two numbers |
|
| 2088 | + protected function op_number_number($op, $left, $right) |
|
| 2089 | + { |
|
| 2090 | + $unit = empty($left[2]) ? $right[2] : $left[2]; |
|
| 2091 | + |
|
| 2092 | + $value = 0; |
|
| 2093 | + switch ($op) { |
|
| 2094 | + case '+': |
|
| 2095 | + $value = $left[1] + $right[1]; |
|
| 2096 | + break; |
|
| 2097 | + case '*': |
|
| 2098 | + $value = $left[1] * $right[1]; |
|
| 2099 | + break; |
|
| 2100 | + case '-': |
|
| 2101 | + $value = $left[1] - $right[1]; |
|
| 2102 | + break; |
|
| 2103 | + case '%': |
|
| 2104 | + $value = $left[1] % $right[1]; |
|
| 2105 | + break; |
|
| 2106 | + case '/': |
|
| 2107 | + if ($right[1] == 0) { |
|
| 2108 | + $this->throwError('parse error: divide by zero'); |
|
| 2109 | + } |
|
| 2110 | + $value = $left[1] / $right[1]; |
|
| 2111 | + break; |
|
| 2112 | + case '<': |
|
| 2113 | + return $this->toBool($left[1] < $right[1]); |
|
| 2114 | + case '>': |
|
| 2115 | + return $this->toBool($left[1] > $right[1]); |
|
| 2116 | + case '>=': |
|
| 2117 | + return $this->toBool($left[1] >= $right[1]); |
|
| 2118 | + case '=<': |
|
| 2119 | + return $this->toBool($left[1] <= $right[1]); |
|
| 2120 | + default: |
|
| 2121 | + $this->throwError('parse error: unknown number operator: '.$op); |
|
| 2122 | + } |
|
| 2123 | + |
|
| 2124 | + return array("number", $value, $unit); |
|
| 2125 | + } |
|
| 2126 | + |
|
| 2127 | + |
|
| 2128 | + /* environment functions */ |
|
| 2129 | + |
|
| 2130 | + protected function makeOutputBlock($type, $selectors = null) |
|
| 2131 | + { |
|
| 2132 | + $b = new stdclass(); |
|
| 2133 | + $b->lines = array(); |
|
| 2134 | + $b->children = array(); |
|
| 2135 | + $b->selectors = $selectors; |
|
| 2136 | + $b->type = $type; |
|
| 2137 | + $b->parent = $this->scope; |
|
| 2138 | + return $b; |
|
| 2139 | + } |
|
| 2140 | + |
|
| 2141 | + // the state of execution |
|
| 2142 | + protected function pushEnv($block = null) |
|
| 2143 | + { |
|
| 2144 | + $e = new stdclass(); |
|
| 2145 | + $e->parent = $this->env; |
|
| 2146 | + $e->store = array(); |
|
| 2147 | + $e->block = $block; |
|
| 2148 | + |
|
| 2149 | + $this->env = $e; |
|
| 2150 | + return $e; |
|
| 2151 | + } |
|
| 2152 | + |
|
| 2153 | + // pop something off the stack |
|
| 2154 | + protected function popEnv() |
|
| 2155 | + { |
|
| 2156 | + $old = $this->env; |
|
| 2157 | + $this->env = $this->env->parent; |
|
| 2158 | + return $old; |
|
| 2159 | + } |
|
| 2160 | + |
|
| 2161 | + // set something in the current env |
|
| 2162 | + protected function set($name, $value) |
|
| 2163 | + { |
|
| 2164 | + $this->env->store[$name] = $value; |
|
| 2165 | + } |
|
| 2166 | + |
|
| 2167 | + |
|
| 2168 | + // get the highest occurrence entry for a name |
|
| 2169 | + protected function get($name) |
|
| 2170 | + { |
|
| 2171 | + $current = $this->env; |
|
| 2172 | + |
|
| 2173 | + $isArguments = $name == $this->vPrefix.'arguments'; |
|
| 2174 | + while ($current) { |
|
| 2175 | + if ($isArguments && isset($current->arguments)) { |
|
| 2176 | + return array('list', ' ', $current->arguments); |
|
| 2177 | + } |
|
| 2178 | + |
|
| 2179 | + if (isset($current->store[$name])) { |
|
| 2180 | + return $current->store[$name]; |
|
| 2181 | + } |
|
| 2182 | + |
|
| 2183 | + $current = isset($current->storeParent) ? |
|
| 2184 | + $current->storeParent : $current->parent; |
|
| 2185 | + } |
|
| 2186 | + |
|
| 2187 | + $this->throwError("variable $name is undefined"); |
|
| 2188 | + } |
|
| 2189 | + |
|
| 2190 | + // inject array of unparsed strings into environment as variables |
|
| 2191 | + protected function injectVariables($args) |
|
| 2192 | + { |
|
| 2193 | + $this->pushEnv(); |
|
| 2194 | + $parser = new lessc_parser($this, __METHOD__); |
|
| 2195 | + $value = null; |
|
| 2196 | + foreach ($args as $name => $strValue) { |
|
| 2197 | + if ($name[0] !== '@') { |
|
| 2198 | + $name = '@'.$name; |
|
| 2199 | + } |
|
| 2200 | + $parser->count = 0; |
|
| 2201 | + $parser->buffer = (string) $strValue; |
|
| 2202 | + if (!$parser->propertyValue($value)) { |
|
| 2203 | + throw new Exception("failed to parse passed in variable $name: $strValue"); |
|
| 2204 | + } |
|
| 2205 | + |
|
| 2206 | + $this->set($name, $value); |
|
| 2207 | + } |
|
| 2208 | + } |
|
| 2209 | + |
|
| 2210 | + /** |
|
| 2211 | + * Initialize any static state, can initialize parser for a file |
|
| 2212 | + * $opts isn't used yet |
|
| 2213 | + */ |
|
| 2214 | + public function __construct($fname = null) |
|
| 2215 | + { |
|
| 2216 | + if ($fname !== null) { |
|
| 2217 | + // used for deprecated parse method |
|
| 2218 | + $this->_parseFile = $fname; |
|
| 2219 | + } |
|
| 2220 | + } |
|
| 2221 | + |
|
| 2222 | + public function compile($string, $name = null) |
|
| 2223 | + { |
|
| 2224 | + $locale = setlocale(LC_NUMERIC, 0); |
|
| 2225 | + setlocale(LC_NUMERIC, "C"); |
|
| 2226 | + |
|
| 2227 | + $this->parser = $this->makeParser($name); |
|
| 2228 | + $root = $this->parser->parse($string); |
|
| 2229 | + |
|
| 2230 | + $this->env = null; |
|
| 2231 | + $this->scope = null; |
|
| 2232 | + |
|
| 2233 | + $this->formatter = $this->newFormatter(); |
|
| 2234 | + |
|
| 2235 | + if (!empty($this->registeredVars)) { |
|
| 2236 | + $this->injectVariables($this->registeredVars); |
|
| 2237 | + } |
|
| 2238 | + |
|
| 2239 | + $this->sourceParser = $this->parser; // used for error messages |
|
| 2240 | + $this->compileBlock($root); |
|
| 2241 | + |
|
| 2242 | + ob_start(); |
|
| 2243 | + $this->formatter->block($this->scope); |
|
| 2244 | + $out = ob_get_clean(); |
|
| 2245 | + setlocale(LC_NUMERIC, $locale); |
|
| 2246 | + return $out; |
|
| 2247 | + } |
|
| 2248 | + |
|
| 2249 | + public function compileFile($fname, $outFname = null) |
|
| 2250 | + { |
|
| 2251 | + if (!is_readable($fname)) { |
|
| 2252 | + throw new Exception('load error: failed to find '.$fname); |
|
| 2253 | + } |
|
| 2254 | + |
|
| 2255 | + $pi = pathinfo($fname); |
|
| 2256 | + |
|
| 2257 | + $oldImport = $this->importDir; |
|
| 2258 | + |
|
| 2259 | + $this->importDir = (array) $this->importDir; |
|
| 2260 | + $this->importDir[] = $pi['dirname'].'/'; |
|
| 2261 | + |
|
| 2262 | + $this->addParsedFile($fname); |
|
| 2263 | + |
|
| 2264 | + $out = $this->compile(file_get_contents($fname), $fname); |
|
| 2265 | + |
|
| 2266 | + $this->importDir = $oldImport; |
|
| 2267 | + |
|
| 2268 | + if ($outFname !== null) { |
|
| 2269 | + return file_put_contents($outFname, $out); |
|
| 2270 | + } |
|
| 2271 | + |
|
| 2272 | + return $out; |
|
| 2273 | + } |
|
| 2274 | + |
|
| 2275 | + // compile only if changed input has changed or output doesn't exist |
|
| 2276 | + public function checkedCompile($in, $out) |
|
| 2277 | + { |
|
| 2278 | + if (!is_file($out) || filemtime($in) > filemtime($out)) { |
|
| 2279 | + $this->compileFile($in, $out); |
|
| 2280 | + return true; |
|
| 2281 | + } |
|
| 2282 | + return false; |
|
| 2283 | + } |
|
| 2284 | + |
|
| 2285 | + /** |
|
| 2286 | + * Execute lessphp on a .less file or a lessphp cache structure |
|
| 2287 | + * |
|
| 2288 | + * The lessphp cache structure contains information about a specific |
|
| 2289 | + * less file having been parsed. It can be used as a hint for future |
|
| 2290 | + * calls to determine whether or not a rebuild is required. |
|
| 2291 | + * |
|
| 2292 | + * The cache structure contains two important keys that may be used |
|
| 2293 | + * externally: |
|
| 2294 | + * |
|
| 2295 | + * compiled: The final compiled CSS |
|
| 2296 | + * updated: The time (in seconds) the CSS was last compiled |
|
| 2297 | + * |
|
| 2298 | + * The cache structure is a plain-ol' PHP associative array and can |
|
| 2299 | + * be serialized and unserialized without a hitch. |
|
| 2300 | + * |
|
| 2301 | + * @param mixed $in Input |
|
| 2302 | + * @param bool $force Force rebuild? |
|
| 2303 | + * @return array|null lessphp cache structure |
|
| 2304 | + */ |
|
| 2305 | + public function cachedCompile($in, $force = false) |
|
| 2306 | + { |
|
| 2307 | + // assume no root |
|
| 2308 | + $root = null; |
|
| 2309 | + |
|
| 2310 | + if (is_string($in)) { |
|
| 2311 | + $root = $in; |
|
| 2312 | + } elseif (is_array($in) && isset($in['root'])) { |
|
| 2313 | + if ($force || !isset($in['files'])) { |
|
| 2314 | + // If we are forcing a recompile or if for some reason the |
|
| 2315 | + // structure does not contain any file information we should |
|
| 2316 | + // specify the root to trigger a rebuild. |
|
| 2317 | + $root = $in['root']; |
|
| 2318 | + } elseif (isset($in['files']) && is_array($in['files'])) { |
|
| 2319 | + foreach ($in['files'] as $fname => $ftime) { |
|
| 2320 | + if (!file_exists($fname) || filemtime($fname) > $ftime) { |
|
| 2321 | + // One of the files we knew about previously has changed |
|
| 2322 | + // so we should look at our incoming root again. |
|
| 2323 | + $root = $in['root']; |
|
| 2324 | + break; |
|
| 2325 | + } |
|
| 2326 | + } |
|
| 2327 | + } |
|
| 2328 | + } else { |
|
| 2329 | + // TODO: Throw an exception? We got neither a string nor something |
|
| 2330 | + // that looks like a compatible lessphp cache structure. |
|
| 2331 | + return null; |
|
| 2332 | + } |
|
| 2333 | + |
|
| 2334 | + if ($root !== null) { |
|
| 2335 | + // If we have a root value which means we should rebuild. |
|
| 2336 | + $out = array(); |
|
| 2337 | + $out['root'] = $root; |
|
| 2338 | + $out['compiled'] = $this->compileFile($root); |
|
| 2339 | + $out['files'] = $this->allParsedFiles(); |
|
| 2340 | + $out['updated'] = time(); |
|
| 2341 | + return $out; |
|
| 2342 | + } else { |
|
| 2343 | + // No changes, pass back the structure |
|
| 2344 | + // we were given initially. |
|
| 2345 | + return $in; |
|
| 2346 | + } |
|
| 2347 | + } |
|
| 2348 | + |
|
| 2349 | + // parse and compile buffer |
|
| 2350 | + // This is deprecated |
|
| 2351 | + public function parse($str = null, $initialVariables = null) |
|
| 2352 | + { |
|
| 2353 | + if (is_array($str)) { |
|
| 2354 | + $initialVariables = $str; |
|
| 2355 | + $str = null; |
|
| 2356 | + } |
|
| 2357 | + |
|
| 2358 | + $oldVars = $this->registeredVars; |
|
| 2359 | + if ($initialVariables !== null) { |
|
| 2360 | + $this->setVariables($initialVariables); |
|
| 2361 | + } |
|
| 2362 | + |
|
| 2363 | + if ($str == null) { |
|
| 2364 | + if (empty($this->_parseFile)) { |
|
| 2365 | + throw new exception("nothing to parse"); |
|
| 2366 | + } |
|
| 2367 | + |
|
| 2368 | + $out = $this->compileFile($this->_parseFile); |
|
| 2369 | + } else { |
|
| 2370 | + $out = $this->compile($str); |
|
| 2371 | + } |
|
| 2372 | + |
|
| 2373 | + $this->registeredVars = $oldVars; |
|
| 2374 | + return $out; |
|
| 2375 | + } |
|
| 2376 | + |
|
| 2377 | + protected function makeParser($name) |
|
| 2378 | + { |
|
| 2379 | + $parser = new lessc_parser($this, $name); |
|
| 2380 | + $parser->writeComments = $this->preserveComments; |
|
| 2381 | + |
|
| 2382 | + return $parser; |
|
| 2383 | + } |
|
| 2384 | + |
|
| 2385 | + public function setFormatter($name) |
|
| 2386 | + { |
|
| 2387 | + $this->formatterName = $name; |
|
| 2388 | + } |
|
| 2389 | + |
|
| 2390 | + protected function newFormatter() |
|
| 2391 | + { |
|
| 2392 | + $className = "lessc_formatter_lessjs"; |
|
| 2393 | + if (!empty($this->formatterName)) { |
|
| 2394 | + if (!is_string($this->formatterName)) { |
|
| 2395 | + return $this->formatterName; |
|
| 2396 | + } |
|
| 2397 | + $className = "lessc_formatter_$this->formatterName"; |
|
| 2398 | + } |
|
| 2399 | + |
|
| 2400 | + return new $className(); |
|
| 2401 | + } |
|
| 2402 | + |
|
| 2403 | + public function setPreserveComments($preserve) |
|
| 2404 | + { |
|
| 2405 | + $this->preserveComments = $preserve; |
|
| 2406 | + } |
|
| 2407 | + |
|
| 2408 | + public function registerFunction($name, $func) |
|
| 2409 | + { |
|
| 2410 | + $this->libFunctions[$name] = $func; |
|
| 2411 | + } |
|
| 2412 | + |
|
| 2413 | + public function unregisterFunction($name) |
|
| 2414 | + { |
|
| 2415 | + unset($this->libFunctions[$name]); |
|
| 2416 | + } |
|
| 2417 | + |
|
| 2418 | + public function setVariables($variables) |
|
| 2419 | + { |
|
| 2420 | + $this->registeredVars = array_merge($this->registeredVars, $variables); |
|
| 2421 | + } |
|
| 2422 | + |
|
| 2423 | + public function unsetVariable($name) |
|
| 2424 | + { |
|
| 2425 | + unset($this->registeredVars[$name]); |
|
| 2426 | + } |
|
| 2427 | + |
|
| 2428 | + public function setImportDir($dirs) |
|
| 2429 | + { |
|
| 2430 | + $this->importDir = (array) $dirs; |
|
| 2431 | + } |
|
| 2432 | + |
|
| 2433 | + public function addImportDir($dir) |
|
| 2434 | + { |
|
| 2435 | + $this->importDir = (array) $this->importDir; |
|
| 2436 | + $this->importDir[] = $dir; |
|
| 2437 | + } |
|
| 2438 | + |
|
| 2439 | + public function allParsedFiles() |
|
| 2440 | + { |
|
| 2441 | + return $this->allParsedFiles; |
|
| 2442 | + } |
|
| 2443 | + |
|
| 2444 | + public function addParsedFile($file) |
|
| 2445 | + { |
|
| 2446 | + $this->allParsedFiles[realpath($file)] = filemtime($file); |
|
| 2447 | + } |
|
| 2448 | + |
|
| 2449 | + /** |
|
| 2450 | + * Uses the current value of $this->count to show line and line number |
|
| 2451 | + */ |
|
| 2452 | + public function throwError($msg = null) |
|
| 2453 | + { |
|
| 2454 | + if ($this->sourceLoc >= 0) { |
|
| 2455 | + $this->sourceParser->throwError($msg, $this->sourceLoc); |
|
| 2456 | + } |
|
| 2457 | + throw new exception($msg); |
|
| 2458 | + } |
|
| 2459 | + |
|
| 2460 | + // compile file $in to file $out if $in is newer than $out |
|
| 2461 | + // returns true when it compiles, false otherwise |
|
| 2462 | + public static function ccompile($in, $out, $less = null) |
|
| 2463 | + { |
|
| 2464 | + if ($less === null) { |
|
| 2465 | + $less = new self(); |
|
| 2466 | + } |
|
| 2467 | + return $less->checkedCompile($in, $out); |
|
| 2468 | + } |
|
| 2469 | + |
|
| 2470 | + public static function cexecute($in, $force = false, $less = null) |
|
| 2471 | + { |
|
| 2472 | + if ($less === null) { |
|
| 2473 | + $less = new self(); |
|
| 2474 | + } |
|
| 2475 | + return $less->cachedCompile($in, $force); |
|
| 2476 | + } |
|
| 2477 | + |
|
| 2478 | + protected static $cssColors = array( |
|
| 2479 | + 'aliceblue' => '240,248,255', |
|
| 2480 | + 'antiquewhite' => '250,235,215', |
|
| 2481 | + 'aqua' => '0,255,255', |
|
| 2482 | + 'aquamarine' => '127,255,212', |
|
| 2483 | + 'azure' => '240,255,255', |
|
| 2484 | + 'beige' => '245,245,220', |
|
| 2485 | + 'bisque' => '255,228,196', |
|
| 2486 | + 'black' => '0,0,0', |
|
| 2487 | + 'blanchedalmond' => '255,235,205', |
|
| 2488 | + 'blue' => '0,0,255', |
|
| 2489 | + 'blueviolet' => '138,43,226', |
|
| 2490 | + 'brown' => '165,42,42', |
|
| 2491 | + 'burlywood' => '222,184,135', |
|
| 2492 | + 'cadetblue' => '95,158,160', |
|
| 2493 | + 'chartreuse' => '127,255,0', |
|
| 2494 | + 'chocolate' => '210,105,30', |
|
| 2495 | + 'coral' => '255,127,80', |
|
| 2496 | + 'cornflowerblue' => '100,149,237', |
|
| 2497 | + 'cornsilk' => '255,248,220', |
|
| 2498 | + 'crimson' => '220,20,60', |
|
| 2499 | + 'cyan' => '0,255,255', |
|
| 2500 | + 'darkblue' => '0,0,139', |
|
| 2501 | + 'darkcyan' => '0,139,139', |
|
| 2502 | + 'darkgoldenrod' => '184,134,11', |
|
| 2503 | + 'darkgray' => '169,169,169', |
|
| 2504 | + 'darkgreen' => '0,100,0', |
|
| 2505 | + 'darkgrey' => '169,169,169', |
|
| 2506 | + 'darkkhaki' => '189,183,107', |
|
| 2507 | + 'darkmagenta' => '139,0,139', |
|
| 2508 | + 'darkolivegreen' => '85,107,47', |
|
| 2509 | + 'darkorange' => '255,140,0', |
|
| 2510 | + 'darkorchid' => '153,50,204', |
|
| 2511 | + 'darkred' => '139,0,0', |
|
| 2512 | + 'darksalmon' => '233,150,122', |
|
| 2513 | + 'darkseagreen' => '143,188,143', |
|
| 2514 | + 'darkslateblue' => '72,61,139', |
|
| 2515 | + 'darkslategray' => '47,79,79', |
|
| 2516 | + 'darkslategrey' => '47,79,79', |
|
| 2517 | + 'darkturquoise' => '0,206,209', |
|
| 2518 | + 'darkviolet' => '148,0,211', |
|
| 2519 | + 'deeppink' => '255,20,147', |
|
| 2520 | + 'deepskyblue' => '0,191,255', |
|
| 2521 | + 'dimgray' => '105,105,105', |
|
| 2522 | + 'dimgrey' => '105,105,105', |
|
| 2523 | + 'dodgerblue' => '30,144,255', |
|
| 2524 | + 'firebrick' => '178,34,34', |
|
| 2525 | + 'floralwhite' => '255,250,240', |
|
| 2526 | + 'forestgreen' => '34,139,34', |
|
| 2527 | + 'fuchsia' => '255,0,255', |
|
| 2528 | + 'gainsboro' => '220,220,220', |
|
| 2529 | + 'ghostwhite' => '248,248,255', |
|
| 2530 | + 'gold' => '255,215,0', |
|
| 2531 | + 'goldenrod' => '218,165,32', |
|
| 2532 | + 'gray' => '128,128,128', |
|
| 2533 | + 'green' => '0,128,0', |
|
| 2534 | + 'greenyellow' => '173,255,47', |
|
| 2535 | + 'grey' => '128,128,128', |
|
| 2536 | + 'honeydew' => '240,255,240', |
|
| 2537 | + 'hotpink' => '255,105,180', |
|
| 2538 | + 'indianred' => '205,92,92', |
|
| 2539 | + 'indigo' => '75,0,130', |
|
| 2540 | + 'ivory' => '255,255,240', |
|
| 2541 | + 'khaki' => '240,230,140', |
|
| 2542 | + 'lavender' => '230,230,250', |
|
| 2543 | + 'lavenderblush' => '255,240,245', |
|
| 2544 | + 'lawngreen' => '124,252,0', |
|
| 2545 | + 'lemonchiffon' => '255,250,205', |
|
| 2546 | + 'lightblue' => '173,216,230', |
|
| 2547 | + 'lightcoral' => '240,128,128', |
|
| 2548 | + 'lightcyan' => '224,255,255', |
|
| 2549 | + 'lightgoldenrodyellow' => '250,250,210', |
|
| 2550 | + 'lightgray' => '211,211,211', |
|
| 2551 | + 'lightgreen' => '144,238,144', |
|
| 2552 | + 'lightgrey' => '211,211,211', |
|
| 2553 | + 'lightpink' => '255,182,193', |
|
| 2554 | + 'lightsalmon' => '255,160,122', |
|
| 2555 | + 'lightseagreen' => '32,178,170', |
|
| 2556 | + 'lightskyblue' => '135,206,250', |
|
| 2557 | + 'lightslategray' => '119,136,153', |
|
| 2558 | + 'lightslategrey' => '119,136,153', |
|
| 2559 | + 'lightsteelblue' => '176,196,222', |
|
| 2560 | + 'lightyellow' => '255,255,224', |
|
| 2561 | + 'lime' => '0,255,0', |
|
| 2562 | + 'limegreen' => '50,205,50', |
|
| 2563 | + 'linen' => '250,240,230', |
|
| 2564 | + 'magenta' => '255,0,255', |
|
| 2565 | + 'maroon' => '128,0,0', |
|
| 2566 | + 'mediumaquamarine' => '102,205,170', |
|
| 2567 | + 'mediumblue' => '0,0,205', |
|
| 2568 | + 'mediumorchid' => '186,85,211', |
|
| 2569 | + 'mediumpurple' => '147,112,219', |
|
| 2570 | + 'mediumseagreen' => '60,179,113', |
|
| 2571 | + 'mediumslateblue' => '123,104,238', |
|
| 2572 | + 'mediumspringgreen' => '0,250,154', |
|
| 2573 | + 'mediumturquoise' => '72,209,204', |
|
| 2574 | + 'mediumvioletred' => '199,21,133', |
|
| 2575 | + 'midnightblue' => '25,25,112', |
|
| 2576 | + 'mintcream' => '245,255,250', |
|
| 2577 | + 'mistyrose' => '255,228,225', |
|
| 2578 | + 'moccasin' => '255,228,181', |
|
| 2579 | + 'navajowhite' => '255,222,173', |
|
| 2580 | + 'navy' => '0,0,128', |
|
| 2581 | + 'oldlace' => '253,245,230', |
|
| 2582 | + 'olive' => '128,128,0', |
|
| 2583 | + 'olivedrab' => '107,142,35', |
|
| 2584 | + 'orange' => '255,165,0', |
|
| 2585 | + 'orangered' => '255,69,0', |
|
| 2586 | + 'orchid' => '218,112,214', |
|
| 2587 | + 'palegoldenrod' => '238,232,170', |
|
| 2588 | + 'palegreen' => '152,251,152', |
|
| 2589 | + 'paleturquoise' => '175,238,238', |
|
| 2590 | + 'palevioletred' => '219,112,147', |
|
| 2591 | + 'papayawhip' => '255,239,213', |
|
| 2592 | + 'peachpuff' => '255,218,185', |
|
| 2593 | + 'peru' => '205,133,63', |
|
| 2594 | + 'pink' => '255,192,203', |
|
| 2595 | + 'plum' => '221,160,221', |
|
| 2596 | + 'powderblue' => '176,224,230', |
|
| 2597 | + 'purple' => '128,0,128', |
|
| 2598 | + 'red' => '255,0,0', |
|
| 2599 | + 'rosybrown' => '188,143,143', |
|
| 2600 | + 'royalblue' => '65,105,225', |
|
| 2601 | + 'saddlebrown' => '139,69,19', |
|
| 2602 | + 'salmon' => '250,128,114', |
|
| 2603 | + 'sandybrown' => '244,164,96', |
|
| 2604 | + 'seagreen' => '46,139,87', |
|
| 2605 | + 'seashell' => '255,245,238', |
|
| 2606 | + 'sienna' => '160,82,45', |
|
| 2607 | + 'silver' => '192,192,192', |
|
| 2608 | + 'skyblue' => '135,206,235', |
|
| 2609 | + 'slateblue' => '106,90,205', |
|
| 2610 | + 'slategray' => '112,128,144', |
|
| 2611 | + 'slategrey' => '112,128,144', |
|
| 2612 | + 'snow' => '255,250,250', |
|
| 2613 | + 'springgreen' => '0,255,127', |
|
| 2614 | + 'steelblue' => '70,130,180', |
|
| 2615 | + 'tan' => '210,180,140', |
|
| 2616 | + 'teal' => '0,128,128', |
|
| 2617 | + 'thistle' => '216,191,216', |
|
| 2618 | + 'tomato' => '255,99,71', |
|
| 2619 | + 'transparent' => '0,0,0,0', |
|
| 2620 | + 'turquoise' => '64,224,208', |
|
| 2621 | + 'violet' => '238,130,238', |
|
| 2622 | + 'wheat' => '245,222,179', |
|
| 2623 | + 'white' => '255,255,255', |
|
| 2624 | + 'whitesmoke' => '245,245,245', |
|
| 2625 | + 'yellow' => '255,255,0', |
|
| 2626 | + 'yellowgreen' => '154,205,50' |
|
| 2627 | + ); |
|
| 2628 | 2628 | } |
@@ -50,7 +50,7 @@ discard block |
||
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | |
| 53 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
| 53 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
| 54 | 54 | /** |
| 55 | 55 | * Show a combo list with contracts qualified for a third party |
| 56 | 56 | * |
@@ -66,7 +66,7 @@ discard block |
||
| 66 | 66 | */ |
| 67 | 67 | public function select_contract($socid = -1, $selected = 0, $htmlname = 'contrattid', $maxlength = 16, $showempty = 1, $showRef = 0, $noouput = 0, $morecss = 'minwidth150') |
| 68 | 68 | { |
| 69 | - // phpcs:enable |
|
| 69 | + // phpcs:enable |
|
| 70 | 70 | global $user, $conf, $langs; |
| 71 | 71 | |
| 72 | 72 | $hideunselectables = false; |
@@ -60,7 +60,7 @@ discard block |
||
| 60 | 60 | $this->db = $db; |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
| 63 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
| 64 | 64 | /** |
| 65 | 65 | * Scan a file with antivirus. |
| 66 | 66 | * This function runs the command defined in setup. This antivirus command must return 0 if OK. |
@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | */ |
| 72 | 72 | public function dol_avscan_file($file) |
| 73 | 73 | { |
| 74 | - // phpcs:enable |
|
| 74 | + // phpcs:enable |
|
| 75 | 75 | global $conf; |
| 76 | 76 | |
| 77 | 77 | if (preg_match('/\.virus$/i', $file)) { |
@@ -314,7 +314,7 @@ |
||
| 314 | 314 | return $this->LibStatut($this->active, $mode); |
| 315 | 315 | } |
| 316 | 316 | |
| 317 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
| 317 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
| 318 | 318 | /** |
| 319 | 319 | * Return the label of a given status |
| 320 | 320 | * |