/** * Get the current css buffer * * @return string */ public function getCss(){ $precision = ini_get('precision'); @ini_set('precision',16); $root = new Less_Tree_Ruleset(null, $this->rules ); $root->root = true; //$importVisitor = new Less_importVisitor(); //$importVisitor->run($root); $evaldRoot = $root->compile($this->env); $joinSelector = new Less_joinSelectorVisitor(); $joinSelector->run($evaldRoot); $extendsVisitor = new Less_processExtendsVisitor(); $extendsVisitor->run($evaldRoot); $css = $evaldRoot->toCSS($this->env); if( $this->env->compress ){ $css = preg_replace('/(\s)+/',"$1", $css); } @ini_set('precision',$precision); return $css; }
/** * Generates the CSS * * @return string */ public function generateCSS() { $output = new Less_Output_Mapped($this->contentsMap, $this); // catch the output $this->root->genCSS($output); $sourceMapUrl = $this->getOption('sourceMapURL'); $sourceMapFilename = $this->getOption('sourceMapFilename'); $sourceMapContent = $this->generateJson(); $sourceMapWriteTo = $this->getOption('sourceMapWriteTo'); if (!$sourceMapUrl && $sourceMapFilename) { $sourceMapUrl = $this->normalizeFilename($sourceMapFilename); } // write map to a file if ($sourceMapWriteTo) { $this->saveMap($sourceMapWriteTo, $sourceMapContent); } // inline the map if (!$sourceMapUrl) { $sourceMapUrl = sprintf('data:application/json,%s', Less_Functions::encodeURIComponent($sourceMapContent)); } if ($sourceMapUrl) { $output->add(sprintf('/*# sourceMappingURL=%s */', $sourceMapUrl)); } return $output->toString(); }
/** * Compile Less_Tree_Mixin_Call objects * * @param Less_Tree_Ruleset $ruleset * @param integer $rsRuleCnt */ private function EvalMixinCalls($ruleset, $env, &$rsRuleCnt) { for ($i = 0; $i < $rsRuleCnt; $i++) { $rule = $ruleset->rules[$i]; if ($rule instanceof Less_Tree_Mixin_Call) { $rule = $rule->compile($env); $temp = array(); foreach ($rule as $r) { if ($r instanceof Less_Tree_Rule && $r->variable) { // do not pollute the scope if the variable is // already there. consider returning false here // but we need a way to "return" variable from mixins if (!$ruleset->variable($r->name)) { $temp[] = $r; } } else { $temp[] = $r; } } $temp_count = count($temp) - 1; array_splice($ruleset->rules, $i, 1, $temp); $rsRuleCnt += $temp_count; $i += $temp_count; $ruleset->resetCache(); } elseif ($rule instanceof Less_Tree_RulesetCall) { $rule = $rule->compile($env); $rules = array(); foreach ($rule->rules as $r) { if ($r instanceof Less_Tree_Rule && $r->variable) { continue; } $rules[] = $r; } array_splice($ruleset->rules, $i, 1, $rules); $temp_count = count($rules); $rsRuleCnt += $temp_count - 1; $i += $temp_count - 1; $ruleset->resetCache(); } } }
public function compileParams($env, $mixinFrames, $args = array(), &$evaldArguments = array()) { $frame = new Less_Tree_Ruleset(null, array()); $params = $this->params; $mixinEnv = null; $argsLength = 0; if ($args) { $argsLength = count($args); for ($i = 0; $i < $argsLength; $i++) { $arg = $args[$i]; if ($arg && $arg['name']) { $isNamedFound = false; foreach ($params as $j => $param) { if (!isset($evaldArguments[$j]) && $arg['name'] === $params[$j]['name']) { $evaldArguments[$j] = $arg['value']->compile($env); array_unshift($frame->rules, new Less_Tree_Rule($arg['name'], $arg['value']->compile($env))); $isNamedFound = true; break; } } if ($isNamedFound) { array_splice($args, $i, 1); $i--; $argsLength--; continue; } else { throw new Less_Exception_Compiler("Named argument for " . $this->name . ' ' . $args[$i]['name'] . ' not found'); } } } } $argIndex = 0; foreach ($params as $i => $param) { if (isset($evaldArguments[$i])) { continue; } $arg = null; if (isset($args[$argIndex])) { $arg = $args[$argIndex]; } if (isset($param['name']) && $param['name']) { if (isset($param['variadic'])) { $varargs = array(); for ($j = $argIndex; $j < $argsLength; $j++) { $varargs[] = $args[$j]['value']->compile($env); } $expression = new Less_Tree_Expression($varargs); array_unshift($frame->rules, new Less_Tree_Rule($param['name'], $expression->compile($env))); } else { $val = $arg && $arg['value'] ? $arg['value'] : false; if ($val) { $val = $val->compile($env); } else { if (isset($param['value'])) { if (!$mixinEnv) { $mixinEnv = new Less_Environment(); $mixinEnv->frames = array_merge(array($frame), $mixinFrames); } $val = $param['value']->compile($mixinEnv); $frame->resetCache(); } else { throw new Less_Exception_Compiler("Wrong number of arguments for " . $this->name . " (" . $argsLength . ' for ' . $this->arity . ")"); } } array_unshift($frame->rules, new Less_Tree_Rule($param['name'], $val)); $evaldArguments[$i] = $val; } } if (isset($param['variadic']) && $args) { for ($j = $argIndex; $j < $argsLength; $j++) { $evaldArguments[$j] = $args[$j]['value']->compile($env); } } $argIndex++; } ksort($evaldArguments); $evaldArguments = array_values($evaldArguments); return $frame; }
/** * Parse the import url and return the rules * * @return Less_Tree_Media|array */ public function ParseImport($full_path, $uri, $env) { $import_env = clone $env; if (isset($this->options['reference']) && $this->options['reference'] || isset($this->currentFileInfo['reference'])) { $import_env->currentFileInfo['reference'] = true; } if (isset($this->options['multiple']) && $this->options['multiple']) { $import_env->importMultiple = true; } $parser = new Less_Parser($import_env); $root = $parser->parseFile($full_path, $uri, true); $ruleset = new Less_Tree_Ruleset(array(), $root->rules); $ruleset->evalImports($import_env); return $this->features ? new Less_Tree_Media($ruleset->rules, $this->features->value) : $ruleset->rules; }
function compile($env) { $evald = $this->compileForImport($env); $uri = $full_path = false; //get path & uri $evald_path = $evald->getPath(); if ($evald_path && $env->isPathRelative($evald_path)) { foreach (Less_Parser::$import_dirs as $rootpath => $rooturi) { $temp = $rootpath . $evald_path; if (file_exists($temp)) { $full_path = Less_Environment::NormPath($temp); $uri = Less_Environment::NormPath(dirname($rooturi . $evald_path)); break; } } } if (!$full_path) { $uri = $evald_path; $full_path = $evald_path; } //import once $realpath = realpath($full_path); if (!isset($evald->options['multiple']) && $realpath && Less_Parser::FileParsed($realpath)) { $evald->skip = true; } $features = $evald->features ? $evald->features->compile($env) : null; if ($evald->skip) { return array(); } if ($evald->css) { $temp = $this->compilePath($env); return new Less_Tree_Import($this->compilePath($env), $features, $this->options, $this->index); } $parser = new Less_Parser($env); $evald->root = $parser->parseFile($full_path, $uri, true); $ruleset = new Less_Tree_Ruleset(array(), $evald->root->rules); $ruleset->evalImports($env); return $this->features ? new Less_Tree_Media($ruleset->rules, $this->features->value) : $ruleset->rules; }
public function compile($env, $args = NULL, $important = NULL) { $_arguments = array(); $mixinFrames = array_merge($this->frames, $env->frames); $mixinEnv = new Less_Environment(); $mixinEnv->addFrames($mixinFrames); $frame = $this->compileParams($env, $mixinEnv, $args, $_arguments); $ex = new Less_Tree_Expression($_arguments); array_unshift($frame->rules, new Less_Tree_Rule('@arguments', $ex->compile($env))); $rules = $important ? Less_Tree_Ruleset::makeImportant($this->selectors, $this->rules)->rules : array_slice($this->rules, 0); $ruleset = new Less_Tree_Ruleset(null, $rules); // duplicate the environment, adding new frames. $ruleSetEnv = new Less_Environment(); $ruleSetEnv->addFrame($this); $ruleSetEnv->addFrame($frame); $ruleSetEnv->addFrames($mixinFrames); $ruleSetEnv->compress = $env->compress; $ruleset = $ruleset->compile($ruleSetEnv); $ruleset->originalRuleset = $this; return $ruleset; }